pep8analysis: add copyright and doc to the pep8analysis_web module
[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
23
24 import emscripten
25
26 import backbone
27 import ast
28 import model
29 import cfg
30 import flow_analysis
31
32 redef class AnalysisManager
33
34 fun run(src: String)
35 do
36 var stream = new StringIStream(src)
37 var ast = build_ast("web", stream)
38 assert ast != null
39
40 if failed then exit 1
41
42 # Build program model
43 var model = build_model(ast)
44 if failed then exit 1
45
46 if model.lines.is_empty then
47 fatal_error( ast, "This programs appears empty" )
48 exit 1
49 end
50
51 # Create CFG
52 var cfg = build_cfg(model)
53 if failed then exit 1
54
55 # Run analyses
56
57 ## Reaching defs
58 do_reaching_defs_analysis(cfg)
59
60 ## Range
61 do_range_analysis(ast, cfg)
62
63 ## Types
64 do_types_analysis(ast, cfg)
65
66 print_notes
67 if notes.is_empty then print "Success: Nothing wrong detected"
68
69 var of = new StringOStream
70 cfg.print_dot(of, true)
71 of.close
72 show_graph(of.to_s)
73
74 # Ready next
75 reset
76 clear
77 end
78
79 fun show_graph(content: String) do "show_graph('{content.escape_to_c}');".run_js
80 end
81
82 class StringIStream
83 super BufferedIStream
84
85 init(str: String) do _buffer = new FlatBuffer.from(str)
86
87 redef fun fill_buffer do end_reached = true
88 redef var end_reached: Bool = false
89 end
90
91 redef class Object
92 redef fun manager do return once new AnalysisManager
93 end
94
95 manager.run args.first