dacc9ff34bed8440f77c4e61bc8d7669fcb13866
[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 import cpp
36
37 in "C++" `{
38 #include <bind.h>
39
40 using namespace emscripten;
41
42 EMSCRIPTEN_BINDINGS(my_module) {
43 function("run_analysis", &NativeString_run_analysis, allow_raw_pointers());
44 }
45 `}
46
47 redef class AnalysisManager
48
49 fun run_web(src: String)
50 do
51 sys.suggest_garbage_collection
52
53 var stream = new StringIStream(src)
54 var ast = build_ast("web", stream)
55 if ast == null then return
56
57 sys.suggest_garbage_collection
58
59 if failed then exit 1
60
61 # Build program model
62 var model = build_model(ast)
63 if failed then exit 1
64
65 if model.lines.is_empty then
66 fatal_error( ast, "This programs appears empty" )
67 return
68 end
69
70 sys.suggest_garbage_collection
71
72 # Create CFG
73 var cfg = build_cfg(model)
74 if failed then exit 1
75
76 # Run analyses
77
78 sys.suggest_garbage_collection
79
80 ## Reaching defs
81 do_reaching_defs_analysis(cfg)
82
83 sys.suggest_garbage_collection
84
85 ## Range
86 do_range_analysis(ast, cfg)
87
88 sys.suggest_garbage_collection
89
90 ## Types
91 do_types_analysis(ast, cfg)
92
93 sys.suggest_garbage_collection
94
95 print_notes
96 if notes.is_empty then print "Success: Nothing wrong detected"
97
98 var of = new StringOStream
99 cfg.print_dot(of, false)
100 of.close
101 show_graph(of.to_s)
102
103 # Ready next
104 reset
105 clear
106 end
107
108 fun show_graph(content: String) do "show_graph('{content.escape_to_c}');".run_js
109 end
110
111 redef class NativeString
112 fun run_analysis do manager.run_web to_s
113 end
114
115 fun dummy_set_callbacks import NativeString.run_analysis in "C++" `{
116 `}
117
118 dummy_set_callbacks