Merge branch 'master' into polymorphic_extern_classes
[nit.git] / contrib / pep8analysis / src / pep8analysis_web.nit
1 # This file is part of the pep8analysis project (http://xymus.net/pep8/)
2 #
3 # Copyright 2013-2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Web version of the pep8analysis tool
18 #
19 # Takes the entire Pep/8 source code as argument and prints out the
20 # analysis results. The result graph will be sent to the JavaScript function
21 # `show_graph` with the source of the graph in Graphviz's dot.
22 module pep8analysis_web is
23 cpp_compiler_option("--std=c++11 --bind")
24 c_linker_option("--bind")
25 end
26
27 import emscripten
28
29 import backbone
30 import ast
31 import model
32 import cfg
33 import flow_analysis
34 intrude import standard::stream
35
36 in "C++" `{
37 #include <bind.h>
38
39 using namespace emscripten;
40
41 EMSCRIPTEN_BINDINGS(my_module) {
42 function("run_analysis", &NativeString_run_analysis, allow_raw_pointers());
43 }
44 `}
45
46 redef class AnalysisManager
47
48 fun run(src: String)
49 do
50 sys.suggest_garbage_collection
51
52 var stream = new StringIStream(src)
53 var ast = build_ast("web", stream)
54 if ast == null then return
55
56 sys.suggest_garbage_collection
57
58 if failed then exit 1
59
60 # Build program model
61 var model = build_model(ast)
62 if failed then exit 1
63
64 if model.lines.is_empty then
65 fatal_error( ast, "This programs appears empty" )
66 return
67 end
68
69 sys.suggest_garbage_collection
70
71 # Create CFG
72 var cfg = build_cfg(model)
73 if failed then exit 1
74
75 # Run analyses
76
77 sys.suggest_garbage_collection
78
79 ## Reaching defs
80 do_reaching_defs_analysis(cfg)
81
82 sys.suggest_garbage_collection
83
84 ## Range
85 do_range_analysis(ast, cfg)
86
87 sys.suggest_garbage_collection
88
89 ## Types
90 do_types_analysis(ast, cfg)
91
92 sys.suggest_garbage_collection
93
94 print_notes
95 if notes.is_empty then print "Success: Nothing wrong detected"
96
97 var of = new StringOStream
98 cfg.print_dot(of, false)
99 of.close
100 show_graph(of.to_s)
101
102 # Ready next
103 reset
104 clear
105 end
106
107 fun show_graph(content: String) do "show_graph('{content.escape_to_c}');".run_js
108 end
109
110 class StringIStream
111 super BufferedIStream
112
113 init(str: String) do _buffer = new FlatBuffer.from(str)
114
115 redef fun fill_buffer do end_reached = true
116 redef var end_reached: Bool = false
117 end
118
119 redef class NativeString
120 fun run_analysis do manager.run to_s
121 end
122
123 fun dummy_set_callbacks import NativeString.run_analysis in "C++" `{
124 `}
125
126 dummy_set_callbacks