-# State of the parser automata as stored in the parser stack.
-private class State
- # The internal state number
- readable writable attr _state: Int
-
- # The node stored with the state in the stack
- readable writable attr _nodes: nullable Object
-
- init(state: Int, nodes: nullable Object)
- do
- _state = state
- _nodes = nodes
- end
-end
-
-class Parser
-special ParserTable
- # Associated lexer
- attr _lexer: Lexer
-
- # Stack of pushed states and productions
- attr _stack: Array[State]
-
- # Position in the stack
- attr _stack_pos: Int
-
- # Create a new parser based on a given lexer
- init(lexer: Lexer)
- do
- _lexer = lexer
- _stack = new Array[State]
- _stack_pos = -1
- build_goto_table
- build_action_table
- build_reduce_table
- end
-
- # Do a transition in the automata
- private meth go_to(index: Int): Int
- do
- var state = state
- var table = _goto_table[index]
- var low = 1
- var high = table.length/2 - 1
-
- while low <= high do
- var middle = (low + high) / 2
- var subindex = middle * 2
-
- if state < table[subindex] then
- high = middle - 1
- else if state > table[subindex] then
- low = middle + 1
- else
- return table[subindex + 1]
- end
- end
-
- return table[1] # Default value
- end
-
- # Push someting in the state stack
- private meth push(numstate: Int, list_node: nullable Object)
- do
- var pos = _stack_pos + 1
- _stack_pos = pos
- if pos < _stack.length then
- var state = _stack[pos]
- state.state = numstate
- state.nodes = list_node
- else
- _stack.push(new State(numstate, list_node))
- end
- end
-
- # The current state
- private meth state: Int
- do
- return _stack[_stack_pos].state
- end
-
- # Pop something from the stack state
- private meth pop: nullable Object
- do
- var res = _stack[_stack_pos].nodes
- _stack_pos = _stack_pos -1
- return res
- end
-
- # Build and return a full AST.
- meth parse: Start
- do
- push(0, null)
-
- var lexer = _lexer
- while true do
- var token = lexer.peek
- var last_pos = token.pos
- var last_line = token.line
-
- if token isa PError then
- return new Start(null, token)
- end
-
- var index = token.parser_index
- var table = _action_table[state]
- var action_type = table[1]
- var action_value = table[2]
-
- var low = 1
- var high = table.length/3 - 1
-
- while low <= high do
- var middle = (low + high) / 2
- var subindex = middle * 3
-
- if index < table[subindex] then
- high = middle - 1
- else if index > table[subindex] then
- low = middle + 1
- else
- action_type = table[subindex + 1]
- action_value = table[subindex + 2]
- high = low -1 # break
- end
- end
-
- if action_type == 0 then # SHIFT
- push(action_value, lexer.next)
- else if action_type == 1 then # REDUCE
- _reduce_table[action_value].action(self)
- else if action_type == 2 then # ACCEPT
- var node2 = lexer.next
- assert node2 isa EOF
- var node1 = pop
- assert node1 isa ${/parser/prods/prod/@ename}
- var node = new Start(node1, node2)
- (new SearchTokensVisitor).visit(node)
- return node
- else if action_type == 3 then # ERROR
- var node2 = new PError.init_error(lexer.filename, last_line, last_pos, error_messages[errors[action_value]])
- var node = new Start(null, node2)
- return node
- end
- end
- abort
- end
-
- attr _reduce_table: Array[ReduceAction]
- private meth build_reduce_table