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