d731e41e9907c1ee9011476c46bb52d0f66d1aad
[nit.git] / contrib / pep8analysis / src / backbone.nit
1 import opts
2
3 import parser
4
5 class AnalysisManager
6 super Noter
7 var opts = new OptionContext
8 end
9
10 abstract class Noter
11 var notes = new Array[Note]
12
13 var failed = false
14
15 fun print_notes
16 do
17 if not notes.is_empty then
18 print "# Notes:"
19 for n in notes do print n
20 end
21 end
22
23 fun fatal_error(n: ANode, msg: String)
24 do
25 notes.add( new Fatal(n.location, msg) )
26 failed = true
27 end
28
29 # Reset failure status
30 fun reset do failed = false
31
32 # Clear list of notes
33 fun clear do notes = new Array[Note]
34 end
35
36 abstract class Note
37 var line: Location
38 var to: nullable Location = null
39 var msg: String
40
41 init (line: Location, msg: String)
42 do
43 self.line = line
44 self.msg = msg
45 end
46 init range(from, to: Location, msg: String)
47 do
48 self.line = from
49 self.to = to
50 self.msg = msg
51 end
52
53 fun prefix: String is abstract
54 redef fun to_s do
55 var s = ""
56 var to = to
57 if to != null then
58 s += " from {line} to {to}"
59 s = "{line.to_file_s}:{line.to_line_s}--{to.to_line_s}; "
60 else
61 s = "{line.to_file_s}:{line.to_line_s}; "
62 end
63 return "{prefix}{s}{msg}"
64 end
65 end
66
67 class Warn
68 super Note
69 init (line: Location, msg: String) do super
70 init range(from, to: Location, msg: String) do super
71 redef fun prefix do return "Warning: "
72 end
73
74 class P8Error
75 super Note
76 init (line: Location, msg: String) do super
77 init range(from, to: Location, msg: String) do super
78 redef fun prefix do return "Error: "
79 end
80
81 class Fatal
82 super Note
83 init (line: Location, msg: String) do super
84 init range(from, to: Location, msg: String) do super
85 redef fun prefix do return "Fatal: "
86 end
87
88 redef class Location
89 # "line 5"
90 fun to_line_s: String
91 do
92 return line_start.to_s
93 end
94
95 fun to_file_s: String
96 do
97 return file.filename
98 end
99 end
100
101 protected fun manager: AnalysisManager do return once new AnalysisManager