1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Utils and tools related to parsers and AST
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
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
32 if eof
isa AError then
33 self.fatal_error
(null, "Fatal Error: {eof.message}")
36 return tree
.n_base
.as(not null)
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
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")
49 return nclassdefs
.first
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
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")
63 return npropdefs
.first
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
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(AMethPropdef).n_block
.as(ABlockExpr).n_expr
.first
.as(ADoExpr).n_block
.as(not null)
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
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(AMethPropdef).n_block
.as(ABlockExpr).n_expr
.first
.as(AVardeclExpr).n_expr
.as(not null)
86 # Try to parse the `string` as something
88 # Returns the first possible syntacticaly correct type among:
92 # - an expression `AExpr`
93 # - a block of statements `ABlockExpr`
94 # - a full module `AModule`
95 # - a `AError` if nothing else matches
97 # var tc = new ToolContext
98 # assert tc.parse_something("foo") isa TId
99 # assert tc.parse_something("foo[bar]") isa AExpr
100 # assert tc.parse_something("Foo[Bar]") isa AType
101 # assert tc.parse_something("foo\nbar") isa ABlockExpr
102 # assert tc.parse_something("fun foo do bar\nfoo") isa AModule
103 # assert tc.parse_something("fun fun") isa AParserError
104 # assert tc.parse_something("?%^&") isa ALexerError
105 fun parse_something
(string
: String): ANode
107 var source
= new SourceFile.from_string
("", string
)
113 lexer
= new InjectedLexer(source
)
114 lexer
.injected_before
.add
new TKwvar
115 lexer
.injected_before
.add
new TId
116 lexer
.injected_before
.add
new TColumn
117 lexer
.injected_before
.add
new TClassid
118 lexer
.injected_before
.add
new TObra
119 lexer
.injected_after
.add
new TCbra
120 tree
= (new Parser(lexer
)).parse
122 if not eof
isa AError then
123 var ntype
= tree
.n_base
.n_classdefs
.first
.n_propdefs
.first
.as(AMethPropdef).n_block
.as(ABlockExpr).n_expr
.first
.as(AVardeclExpr).n_type
.n_types
.first
129 lexer
= new Lexer(source
)
130 var first
= lexer
.next
131 if first
isa EOF then return first
132 var second
= lexer
.next
133 if second
isa EOF and not second
isa AError then
138 lexer
= new InjectedLexer(source
)
139 lexer
.injected_before
.add
new TKwvar
140 lexer
.injected_before
.add
new TId
141 lexer
.injected_before
.add
new TAssign
142 lexer
.injected_before
.add
new TOpar
143 lexer
.injected_after
.add
new TCpar
144 tree
= (new Parser(lexer
)).parse
146 if not eof
isa AError then
147 var nexpr
= tree
.n_base
.n_classdefs
.first
.n_propdefs
.first
.as(AMethPropdef).n_block
.as(ABlockExpr).n_expr
.first
.as(AVardeclExpr).n_expr
.as(AParExpr).n_expr
151 if eof
.location
> error
.location
then error
= eof
153 lexer
= new InjectedLexer(source
)
154 lexer
.injected_before
.add
new TKwdo
155 lexer
.injected_before
.add
new TEol
156 lexer
.injected_after
.add
new TEol
157 lexer
.injected_after
.add
new TKwend
158 tree
= (new Parser(lexer
)).parse
160 if not eof
isa AError then
161 var nblock
= tree
.n_base
.n_classdefs
.first
.n_propdefs
.first
.as(AMethPropdef).n_block
.as(ABlockExpr).n_expr
.first
.as(ADoExpr).n_block
.as(ABlockExpr)
162 nblock
.n_kwend
= null # drop injected token
166 if eof
.location
> error
.location
then error
= eof
168 lexer
= new Lexer(source
)
169 tree
= (new Parser(lexer
)).parse
171 if not eof
isa AError then
172 return tree
.n_base
.as(not null)
174 if eof
.location
> error
.location
then error
= eof
179 # Parse the input of the user as something
180 fun interactive_parse
(prompt
: String): ANode
187 var s
= sys
.stdin
.read_line
188 if s
== "" then continue
189 if s
.chars
.first
== ':' then
190 var res
= new TString
195 var text
= oldtext
+ s
+ "\n"
197 var n
= parse_something
(text
)
199 if n
isa AParserError and n
.token
isa EOF then
200 # Unexpected end of file, thus continuing
201 if oldtext
== "" then prompt
= "." * prompt
.length
214 var injected_before
= new List[Token]
215 var injected_after
= new List[Token]
216 private var is_finished
= false
220 if not injected_before
.is_empty
then
221 var tok
= injected_before
.shift
224 if not is_finished
then
226 if not next
isa EOF then return next
227 injected_after
.push
(next
)
231 var tok
= injected_after
.shift
237 # Return an array of tokens that match a given text
238 fun collect_tokens_by_text
(text
: String): Array[Token]
240 var v
= new CollectTokensByTextVisitor(text
)
245 # Return an array of node that are annotated
246 # The attached node can be retrieved by two invocation of parent
247 fun collect_annotations_by_name
(name
: String): Array[AAnnotation]
249 var v
= new CollectAnnotationsByNameVisitor(name
)
255 private class CollectTokensByTextVisitor
258 init(text
: String) do self.text
= text
259 var result
= new Array[Token]
260 redef fun visit
(node
)
263 if node
isa Token and node
.text
== text
then result
.add
(node
)
267 private class CollectAnnotationsByNameVisitor
270 init(name
: String) do self.name
= name
271 var result
= new Array[AAnnotation]
272 redef fun visit
(node
)
275 if node
isa AAnnotation and node
.n_atid
.n_id
.text
== name
then result
.add
(node
)