parser: add ToolContext#parse_something
[nit.git] / src / parser_util.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Utils and tools related to parsers and AST
16 module parser_util
17
18 intrude import parser
19 import toolcontext
20
21 redef class ToolContext
22 # Parse a full module given as a string
23 # Fatal error if the `string` is not a syntactically correct module
24 fun parse_module(string: String): AModule
25 do
26 var source = new SourceFile.from_string("", string)
27 var lexer = new Lexer(source)
28 var parser = new Parser(lexer)
29 var tree = parser.parse
30
31 var eof = tree.n_eof
32 if eof isa AError then
33 self.fatal_error(null, "Fatal Error: {eof.message}")
34 abort
35 end
36 return tree.n_base.as(not null)
37 end
38
39 # Parse a full classdef given as a string
40 # Fatal error if the `string` is not a syntactically correct class definition
41 fun parse_classdef(string: String): AClassdef
42 do
43 var nmodule = parse_module(string)
44 var nclassdefs = nmodule.n_classdefs
45 if nclassdefs.length != 1 then
46 self.fatal_error(null, "Fatal Error: not a classdef")
47 abort
48 end
49 return nclassdefs.first
50 end
51
52 # Parse a full propdef given as a string
53 # Fatal error if the `string` is not a syntactically correct property definition
54 fun parse_propdef(string: String): APropdef
55 do
56 var mod_string = "class Dummy\n{string}\nend"
57 var nclassdef = parse_classdef(mod_string)
58 var npropdefs = nclassdef.n_propdefs
59 if npropdefs.length != 1 then
60 self.fatal_error(null, "Fatal Error: not a propdef")
61 abort
62 end
63 return npropdefs.first
64 end
65
66 # Parse a full statement block given as a string
67 # Fatal error if the `string` is not a syntactically correct statement block
68 fun parse_stmts(string: String): AExpr
69 do
70 var mod_string = "do\n{string}\nend"
71 var nmodule = parse_module(mod_string)
72 var nblock = nmodule.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(not null)
73 return nblock
74 end
75
76 # Parse a full expression given as a string
77 # Fatal error if the `string` is not a syntactically correct expression
78 fun parse_expr(string: String): AExpr
79 do
80 var mod_string = "var dummy = \n{string}"
81 var nmodule = parse_module(mod_string)
82 var nexpr = nmodule.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_expr.as(not null)
83 return nexpr
84 end
85
86 # Try to parse the `string` as something
87 #
88 # Returns the first possible syntacticaly correct type among:
89 #
90 # - a type `AType`
91 # - a single `Token`
92 # - an expression `AExpr`
93 # - a block of statements `ABlockExpr`
94 # - a full module `AModule`
95 # - a `AError` if nothing else matches
96 fun parse_something(string: String): ANode
97 do
98 var source = new SourceFile.from_string("", string)
99 var error
100 var tree
101 var eof
102 var lexer
103
104 lexer = new InjectedLexer(source)
105 lexer.injected_before.add new TKwvar
106 lexer.injected_before.add new TId
107 lexer.injected_before.add new TColumn
108 lexer.injected_before.add new TClassid
109 lexer.injected_before.add new TObra
110 lexer.injected_after.add new TCbra
111 tree = (new Parser(lexer)).parse
112 eof = tree.n_eof
113 if not eof isa AError then
114 var ntype = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_type.n_types.first
115 return ntype
116 end
117 error = eof
118
119 lexer = new Lexer(source)
120 var first = lexer.next
121 if not first isa EOF then
122 var second = lexer.next
123 if second isa EOF and not second isa AError then
124 return first
125 end
126 end
127
128 lexer = new InjectedLexer(source)
129 lexer.injected_before.add new TKwvar
130 lexer.injected_before.add new TId
131 lexer.injected_before.add new TAssign
132 lexer.injected_before.add new TOpar
133 lexer.injected_after.add new TCpar
134 tree = (new Parser(lexer)).parse
135 eof = tree.n_eof
136 if not eof isa AError then
137 var nexpr = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_expr.as(AParExpr).n_expr
138 return nexpr
139 end
140 if eof.location > error.location then error = eof
141
142 lexer = new InjectedLexer(source)
143 lexer.injected_before.add new TKwdo
144 lexer.injected_after.add new TKwend
145 tree = (new Parser(lexer)).parse
146 eof = tree.n_eof
147 if not eof isa AError then
148 var nblock = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(not null)
149 return nblock
150 end
151 if eof.location > error.location then error = eof
152
153 lexer = new Lexer(source)
154 tree = (new Parser(lexer)).parse
155 eof = tree.n_eof
156 if not eof isa AError then
157 return tree.n_base.as(not null)
158 end
159 if eof.location > error.location then error = eof
160
161 return error
162 end
163 end
164
165 class InjectedLexer
166 super Lexer
167
168 var injected_before = new List[Token]
169 var injected_after = new List[Token]
170 private var is_finished = false
171
172 redef fun get_token
173 do
174 if not injected_before.is_empty then
175 var tok = injected_before.shift
176 if tok._location == null then tok._location = new Location(file, 1, 1, 1, 0)
177 return tok
178 end
179 if not is_finished then
180 var next = super
181 if not next isa EOF then return next
182 injected_after.push(next)
183 is_finished = true
184 end
185
186 var tok = injected_after.shift
187 if tok._location == null then tok._location = new Location(file, 1, 1, 1, 0)
188 return tok
189 end
190 end
191
192 redef class ANode
193 # Return an array of tokens that match a given text
194 fun collect_tokens_by_text(text: String): Array[Token]
195 do
196 var v = new CollectTokensByTextVisitor(text)
197 v.enter_visit(self)
198 return v.result
199 end
200
201 # Return an array of node that are annotated
202 # The attached node can be retrieved by two invocation of parent
203 fun collect_annotations_by_name(name: String): Array[AAnnotation]
204 do
205 var v = new CollectAnnotationsByNameVisitor(name)
206 v.enter_visit(self)
207 return v.result
208 end
209 end
210
211 private class CollectTokensByTextVisitor
212 super Visitor
213 var text: String
214 init(text: String) do self.text = text
215 var result = new Array[Token]
216 redef fun visit(node)
217 do
218 node.visit_all(self)
219 if node isa Token and node.text == text then result.add(node)
220 end
221 end
222
223 private class CollectAnnotationsByNameVisitor
224 super Visitor
225 var name: String
226 init(name: String) do self.name = name
227 var result = new Array[AAnnotation]
228 redef fun visit(node)
229 do
230 node.visit_all(self)
231 if node isa AAnnotation and node.n_atid.n_id.text == name then result.add(node)
232 end
233 end