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