X-Git-Url: http://nitlanguage.org diff --git a/src/parser/parser_work.nit b/src/parser/parser_work.nit index 03d82b12..7daee0f 100644 --- a/src/parser/parser_work.nit +++ b/src/parser/parser_work.nit @@ -20,10 +20,10 @@ intrude import parser_prod # State of the parser automata as stored in the parser stack. private class State # The internal state number - readable writable var _state: Int + var state: Int # The node stored with the state in the stack - readable writable var _nodes: nullable Object + var nodes: nullable Object init(state: Int, nodes: nullable Object) do @@ -32,16 +32,17 @@ private class State end end +# The parser of the Nit language. class Parser super TablesCapable # Associated lexer - var _lexer: Lexer + var lexer: Lexer # Stack of pushed states and productions - var _stack: Array[State] + private var stack: Array[State] # Position in the stack - var _stack_pos: Int + private var stack_pos: Int # Create a new parser based on a given lexer init(lexer: Lexer) @@ -83,8 +84,8 @@ class Parser _stack_pos = pos if pos < _stack.length then var state = _stack[pos] - state.state = numstate - state.nodes = list_node + state._state = numstate + state._nodes = list_node else _stack.push(new State(numstate, list_node)) end @@ -93,13 +94,13 @@ class Parser # The current state private fun state: Int do - return _stack[_stack_pos].state + return _stack[_stack_pos]._state end # Pop something from the stack state private fun pop: nullable Object do - var res = _stack[_stack_pos].nodes + var res = _stack[_stack_pos]._nodes _stack_pos = _stack_pos -1 return res end @@ -153,6 +154,8 @@ class Parser (new ComputeProdLocationVisitor).enter_visit(node) return node else if action_type == 3 then # ERROR + # skip injected tokens + while not isset token._location do token = lexer.next var node2 = new AParserError.init_parser_error("Syntax error: unexpected {token}.", token.location, token) var node = new Start(null, node2) return node @@ -160,33 +163,34 @@ class Parser end end - var _reduce_table: Array[ReduceAction] + private var reduce_table: Array[ReduceAction] private fun build_reduce_table is abstract end redef class Prod # Location on the first token after the start of a production - # So outside the production for epilon production - var _first_location: nullable Location + # So outside the production for epsilon production + var first_location: nullable Location end # Find location of production nodes # Uses existing token locations to infer location of productions. private class ComputeProdLocationVisitor super Visitor - # Currenlty visited productions that need a first token - var _need_first_prods: Array[Prod] = new Array[Prod] + # Currently visited productions that need a first token + var need_first_prods = new Array[Prod] # Already visited epsilon productions that waits something after them - var _need_after_epsilons: Array[Prod] = new Array[Prod] + var need_after_epsilons = new Array[Prod] # Location of the last visited token in the current production - var _last_location: nullable Location = null + var last_location: nullable Location = null redef fun visit(n: ANode) do if n isa Token then - var loc = n.location + if not isset n._location then return + var loc = n._location _last_location = loc # Add a first token to productions that need one @@ -237,7 +241,7 @@ private class ComputeProdLocationVisitor init do end end -# Each reduca action has its own class, this one is the root of the hierarchy. +# Each reduce action has its own class, this one is the root of the hierarchy. private abstract class ReduceAction fun action(p: Parser) is abstract fun concat(l1, l2 : Array[Object]): Array[Object] @@ -246,6 +250,6 @@ private abstract class ReduceAction l1.append(l2) return l1 end - var _goto: Int + var goto: Int init(g: Int) do _goto = g end