ni_nitdoc: added fast copy past utility to signatures.
[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 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 end
86
87 redef class ANode
88 # Return an array of tokens that match a given text
89 fun collect_tokens_by_text(text: String): Array[Token]
90 do
91 var v = new CollectTokensByTextVisitor(text)
92 v.enter_visit(self)
93 return v.result
94 end
95
96 # Return an array of node that are annotated
97 # The attached node can be retrieved by two invocation of parent
98 fun collect_annotations_by_name(name: String): Array[AAnnotation]
99 do
100 var v = new CollectAnnotationsByNameVisitor(name)
101 v.enter_visit(self)
102 return v.result
103 end
104 end
105
106 private class CollectTokensByTextVisitor
107 super Visitor
108 var text: String
109 init(text: String) do self.text = text
110 var result = new Array[Token]
111 redef fun visit(node)
112 do
113 if node == null then return
114 node.visit_all(self)
115 if node isa Token and node.text == text then result.add(node)
116 end
117 end
118
119 private class CollectAnnotationsByNameVisitor
120 super Visitor
121 var name: String
122 init(name: String) do self.name = name
123 var result = new Array[AAnnotation]
124 redef fun visit(node)
125 do
126 if node == null then return
127 node.visit_all(self)
128 if node isa AAnnotation and node.n_atid.n_id.text == name then result.add(node)
129 end
130 end