contrib: bring in the pep8 analysis framework
[nit.git] / contrib / pep8analysis / src / pep8analysis.nit
1 module pep8analysis
2
3 import backbone
4 import ast
5 import model
6 import cfg
7 import flow_analysis
8
9 redef class AnalysisManager
10 var opt_help = new OptionBool("Display this help message", "--help","-h")
11 var opt_quiet = new OptionBool("Do not show notes", "--quiet","-q")
12 fun quiet: Bool do return opt_quiet.value
13 fun verbose: Bool do return not opt_quiet.value
14
15 var opt_output = new OptionString("Output directory", "--output", "-o")
16
17 redef init
18 do
19 super
20
21 opts.add_option(opt_help)
22 opts.add_option(opt_quiet)
23 opts.add_option(opt_output)
24 end
25
26 fun run
27 do
28 opts.parse(args)
29 var files = opts.rest
30
31 if files.is_empty or opt_help.value then
32 print "Usage: {sys.program_name} [options] file.pep [other_file.pep [...]]"
33 print "Options:"
34 opts.usage
35 return
36 end
37
38 var dir = opt_output.value
39 if dir == null then dir = "out"
40 if not dir.file_exists then dir.mkdir
41
42 # Parsing
43 for filename in files do
44 reset # noter
45
46 if verbose then print "Analyzing {filename}"
47 if not filename.file_exists then
48 print "Target file \"{filename}\" does not exist."
49 exit 1
50 end
51 var ast = build_ast( filename )
52 assert ast != null
53
54 if failed then continue
55
56 # Build program model
57 var model = build_model(ast)
58
59 if failed then continue
60
61 if model.lines.is_empty then
62 fatal_error( ast, "This programs appears empty" )
63 continue
64 end
65
66 # Create CFG
67 var cfg = build_cfg(model)
68
69 if failed then continue
70
71 # Run analyses
72
73 ## Reaching defs
74 do_reaching_defs_analysis(cfg)
75
76 ## Range
77 do_range_analysis(ast, cfg)
78
79 ## Types
80 do_types_analysis(ast, cfg)
81
82 # Print results
83 var of = new OFStream.open("{dir}/{filename.replace("/","-").replace(".pep",".dot")}")
84 cfg.print_dot(of, true)
85 of.close
86 end
87 if not opt_quiet.value then
88 print_notes
89 end
90 end
91 end
92
93 redef class Object
94 redef fun manager do return once new AnalysisManager
95 end
96
97 manager.run