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 # Runtime library required by parsers and lexers generated by nitcc
18 # A abstract parser engine generated by nitcc
21 # FIXME: provide something better, like a lexer?
22 var tokens
= new List[NToken]
24 # Look at the next token
25 # Used by generated parsers
26 fun peek_token
: NToken do return tokens
.first
28 # Consume the next token
29 # Used by generated parsers
30 fun get_token
: NToken do return tokens
.shift
32 # Consume the next token and shift to the state `dest`.
33 # Used by generated parsers
34 fun shift
(dest
: LRState)
37 #print "shift {t} -> {dest}"
39 state_stack
.push state
43 # After a reduction on `goto` go to the next state
44 # Used by generated parsers
45 fun goto
(goto
: LRGoto)
47 #print "reduce from {state} -> {prod}"
48 state
.goto
(self, goto
)
51 # push a new state on the stack of states (
52 # Used by generated parsers
53 fun push
(dest
: LRState)
55 #print "push prod {prod} -> {dest}"
56 state_stack
.push state
60 # Pop and return the last node
61 # Also pop (and discard) the associated state
62 # Used by generated parsers
65 var res
= node_stack
.pop
66 state
= state_stack
.pop
70 # Produce a parse error and stop the parsing
71 # Used by generated parsers
74 var token
= peek_token
75 #print "* parse error in state {state} on token {token}"
76 #print " expected: {state.error_msg}"
77 #print " node_stack={node_stack.join(", ")}"
78 #print " state_stack={state_stack.join(", ")}"
81 if token
isa NLexerError then
84 error
= new NParserError
85 error
.position
= token
.position
86 error
.text
= token
.text
89 error
.error_tree
.children
.add_all
(node_stack
)
90 error
.expected
= state
.error_msg
96 # The stating state for parsing
97 # Used by generated parsers
98 protected fun start_state
: LRState is abstract
101 # Used by generated parsers
102 var state
: LRState is noinit
110 # Used by generated parsers
111 var node_stack
= new Array[Node]
113 # The stack of states
114 # Used by generated parsers
115 var state_stack
= new Array[LRState]
117 # Should the parser stop
118 # Used by generated parsers
119 var stop
= true is writable
121 # Parse a full sequence of tokens and return a complete syntactic tree
129 #print "* current state {state}"
130 #print " tokens={tokens.join(" ")}"
131 #print " node_stack={node_stack.join(" ")}"
132 #print " state_stack={state_stack.join(" ")}"
136 return node_stack
.first
140 # A state in a parser LR automaton generated by nitcc
141 # Used by generated parsers
142 abstract class LRState
143 fun action
(parser
: Parser) is abstract
144 fun goto
(parser
: Parser, goto
: LRGoto) is abstract
145 fun error_msg
: String do return "FIXME"
148 # A concrete production in a parser LR automation generated by nitcc
149 # Used by generated parsers
150 abstract class LRGoto
155 # A abstract lexer engine generated by nitcc
157 # The input stream of characters
160 # The stating state for lexing
161 # Used by generated parsers
162 protected fun start_state
: DFAState is abstract
164 # Lexize a stream of characters and return a sequence of tokens
165 fun lex
: List[NToken]
167 var res
= new List[NToken]
168 var state
= start_state
178 var last_state
: nullable DFAState = null
180 var length
= text
.length
182 if state
.is_accept
then
190 if pos
>= length
then
195 next
= state
.trans
(c
)
198 if pos_start
< length
then
199 if last_state
== null then
200 var token
= new NLexerError
201 var position
= new Position(pos_start
, pos
, line_start
, line
, col_start
, col
)
202 token
.position
= position
203 token
.text
= text
.substring
(pos_start
, pos-pos_start
+1)
207 var position
= new Position(pos_start
, pos_end
, line_start
, line_end
, col_start
, col_end
)
208 var token
= last_state
.make_token
(position
, text
.substring
(pos_start
, pos_end-pos_start
+1))
209 if token
!= null then res
.add
(token
)
211 if pos
>= length
then
213 var position
= new Position(pos
, pos
, line
, line
, col
, col
)
214 token
.position
= position
220 pos_start
= pos_end
+ 1
222 line_start
= line_end
242 # A state in a lexer automaton generated by nitcc
243 # Used by generated lexers
245 fun is_accept
: Bool do return false
246 fun trans
(c
: Char): nullable DFAState do return null
247 fun make_token
(position
: Position, text
: String): nullable NToken is abstract
252 # A abstract visitor on syntactic trees generated by nitcc
253 abstract class Visitor
254 # The main entry point to visit a node `n`
255 # Should not be redefined
256 fun enter_visit
(n
: Node)
261 # The specific implementation of a visit
263 # Should be redefined by concrete visitors
265 # Should not be called directly (use `enter_visit` instead)
267 # By default, the visitor just rescursively visit the children of `n`
268 protected fun visit
(n
: Node)
270 n
.visit_children
(self)
274 # Print a node (using to_s) on a line and recustively each children indented (with two spaces)
275 class TreePrinterVisitor
278 private var indent
= 0
281 for i
in [0..indent
[ do writer
.write
(" ")
290 # A position into a input stream
291 # Used to give position to tokens
300 redef fun to_s
do return "{line_start}:{col_start}-{line_end}:{col_end}"
302 # Get the lines covered by `self` and underline the target columns.
304 # This is useful for pretty printing errors or debug the output
307 # var src = "var Foo = new Array[Int]"
308 # var pos = new Position(0,0, 1, 1, 5, 8)
310 # assert pos.underline(src) == """
311 # var Foo = new Array[Int]
314 fun underline
(source
: Text): String
316 var res
= new FlatBuffer
318 # All the concerned lines
319 var lines
= source
.split
("\n")
320 for line
in [line_start
..line_end
] do
321 res
.append lines
[line-1
]
325 # Cover all columns, no matter their lines
326 var col_start
= col_start
.min
(col_end
)
327 var col_end
= self.col_start
.max
(col_end
)
330 var ptr
= " "*(col_start-1
).max
(0) + "^"*(col_end-col_start
)
337 # A node of a syntactic tree
339 # The name of the node (as used in the grammar file)
340 fun node_name
: String do return class_name
342 # A point of view on the direct children of the node
343 fun children
: SequenceRead[nullable Node] is abstract
345 # A point of view of a depth-first visit of all non-null children
346 var depth
: Collection[Node] = new DephCollection(self)
348 # Visit all the children of the node with the visitor `v`
349 protected fun visit_children
(v
: Visitor)
351 for c
in children
do if c
!= null then v
.enter_visit
(c
)
354 # The position of the node in the input stream
355 var position
: nullable Position = null is writable
357 # Produce a graphiz file for the syntaxtic tree rooted at `self`.
358 fun to_dot
(filepath
: String)
360 var f
= new FileWriter.open
(filepath
)
361 f
.write
("digraph g \{\n")
362 f
.write
("rankdir=BT;\n")
364 var a
= new Array[NToken]
367 f
.write
("\{ rank=same\n")
375 f
.write
("n{n.object_id}")
377 f
.write
("[style=invis];\n")
384 private fun to_dot_visitor
(f
: Writer, a
: Array[NToken])
386 f
.write
("n{object_id} [label=\"{node_name}\
"];\n")
388 if x
== null then continue
389 f
.write
("n{x.object_id} -> n{object_id};\n")
390 x
.to_dot_visitor
(f
,a
)
399 return "{node_name}@({pos})"
404 private class DephCollection
405 super Collection[Node]
407 redef fun iterator
do return new DephIterator([node
].iterator
)
410 private class DephIterator
413 var stack
= new List[Iterator[nullable Node]]
415 init(i
: Iterator[nullable Node]) is old_style_init
do
419 redef fun is_ok
do return not stack
.is_empty
420 redef fun item
do return stack
.last
.item
.as(not null)
424 stack
.push i
.item
.children
.iterator
427 if not stack
.last
.is_ok
then
431 if stack
.last
.item
== null then
440 # A token produced by the lexer and used in a syntactic tree
441 abstract class NToken
444 redef fun children
do return once
new Array[Node]
446 redef fun to_dot_visitor
(f
, a
)
448 var labe
= "{node_name}"
450 if pos
!= null then labe
+= "\\n{pos}"
452 if node_name
!= "'{text}'" then
453 labe
+= "\\n'{text.escape_to_c}'"
455 f
.write
("n{object_id} [label=\"{labe}\
",shape=box];\n")
459 # The text associated with the token
460 var text
: String = "" is writable
465 if node_name
!= "'{text}'" then
466 res
+= "='{text.escape_to_c}'"
472 # The special token for the end of stream
477 # A special token used to represent a parser or lexer error
478 abstract class NError
481 # All the partially built tree during parsing (aka the node_stack)
482 var error_tree
= new Nodes[Node]
484 # The things unexpected
485 fun unexpected
: String is abstract
487 # The things expected (if any)
488 var expected
: nullable String = null
490 # The error message,using `expected` and `unexpected`
494 var res
= "Unexpected {unexpected}"
495 if exp
!= null then res
+= "; is acceptable instead: {exp}"
500 # A lexer error as a token for the unexpected characted
504 redef fun unexpected
do return "character '{text.chars.first}'"
507 # A parser error linked to a unexpected token
511 # The unexpected token
512 var token
: nullable NToken = null
516 var res
= token
.node_name
517 var text
= token
.text
518 if not text
.is_empty
and res
!= "'{text}'" then
519 res
+= " '{text.escape_to_c}'"
525 # A hogeneous sequence of node, used to represent unbounded lists (and + modifier)
528 redef var children
: Array[T
] = new Array[T
]
531 # A production with a specific, named and statically typed children
534 redef var children
: SequenceRead[nullable Node] = new NProdChildren(self)
536 # The exact number of direct children
537 # Used to implement `children` by generated parsers
538 fun number_of_children
: Int is abstract
540 # The specific children got by its index
541 # Used to implement `children` by generated parsers
542 fun child
(x
: Int): nullable Node is abstract
546 private class NProdChildren
547 super SequenceRead[nullable Node]
549 redef fun iterator
do return new NProdIterator(prod
)
550 redef fun length
do return prod
.number_of_children
551 redef fun is_empty
do return prod
.number_of_children
== 0
552 redef fun [](i
) do return prod
.child
(i
)
555 private class NProdIterator
556 super IndexedIterator[nullable Node]
559 redef fun is_ok
do return index
< prod
.number_of_children
560 redef fun next
do index
+= 1
561 redef fun item
do return prod
.child
(index
)
564 # All-in-one abstract class to test generated parsers on a given
565 abstract class TestParser
566 # How to get a new lexer on a given stream of character
567 fun new_lexer
(text
: String): Lexer is abstract
569 # How to get a new parser
570 fun new_parser
: Parser is abstract
572 # The name of the language (used for generated files)
573 fun name
: String is abstract
575 # Use the class as the main enrty point of the program
576 # - parse arguments and options of the command
577 # - test the parser (see `work`)
580 if args
.is_empty
then
581 print
"usage {name}_test <filepath> | - | -e <text>"
585 var filepath
= args
.shift
587 if filepath
== "-" then
588 text
= sys
.stdin
.read_all
589 else if filepath
== "-e" then
590 if args
.is_empty
then
591 print
"Error: -e need a text"
596 var f
= new FileReader.open
(filepath
)
601 if not args
.is_empty
then
602 print
"Error: superfluous arguments."
609 # Produce a full syntactic tree for a given stream of character
610 # Produce also statistics and output files
611 fun work
(text
: String): Node
613 print
"INPUT: {text.length} chars"
614 var l
= new_lexer
(text
)
617 var tokout
= "{name}.tokens.out"
618 print
"TOKEN: {tokens.length} tokens (see {tokout})"
620 var f
= new FileWriter.open
(tokout
)
627 p
.tokens
.add_all
(tokens
)
631 var astout
= "{name}.ast.out"
632 f
= new FileWriter.open
(astout
)
633 var tpv
= new TreePrinterVisitor(f
)
634 var astdotout
= "{name}.ast.dot"
636 print
"Syntax error: {n.message}"
637 print
"ERROR: {n} (see {astout} and {astdotout})"
641 print
"ROOT: {n}; {n.depth.length} nodes (see {astout} and {astdotout})"