Property definitions

nitcc_runtime $ Parser :: defaultinit
# A abstract parser engine generated by nitcc
abstract class Parser
	# The list of tokens
	# FIXME: provide something better, like a lexer?
	var tokens = new CircularArray[NToken]

	# Look at the next token
	# Used by generated parsers
	fun peek_token: NToken do return tokens.first

	# Consume the next token
	# Used by generated parsers
	fun get_token: NToken do return tokens.shift

	# Consume the next token and shift to the state `dest`.
	# Used by generated parsers
	fun shift(dest: LRState)
	do
		var t = get_token
		#print "shift {t} -> {dest}"
		node_stack.push t
		state_stack.push state
		state = dest
	end

	# After a reduction on `goto` go to the next state
	# Used by generated parsers
	fun goto(goto: LRGoto)
	do
		#print "reduce from {state} -> {prod}"
		state.goto(self, goto)
	end

	# push a new state on the stack of states (
	# Used by generated parsers
	fun push(dest: LRState)
	do
		#print "push prod {prod} -> {dest}"
		state_stack.push state
		state = dest
	end

	# Pop and return the last node
	# Also pop (and discard) the associated state
	# Used by generated parsers
	fun pop: Node
	do
		var res = node_stack.pop
		state = state_stack.pop
		return res
	end

	# Produce a parse error and stop the parsing
	# Used by generated parsers
	fun parse_error
	do
		var token = peek_token
		#print "* parse error in state {state} on token {token}"
		#print "  expected: {state.error_msg}"
		#print "  node_stack={node_stack.join(", ")}"
		#print "  state_stack={state_stack.join(", ")}"
		node_stack.push(token)
		var error: NError
		if token isa NLexerError then
			error = token
		else
			error = new NParserError
			error.position = token.position
			error.text = token.text
			error.token = token
		end
		error.error_tree.children.add_all(node_stack)
		error.expected = state.error_msg
		node_stack.clear
		node_stack.add error
		stop = true
	end

	# The stating state for parsing
	# Used by generated parsers
	protected fun start_state: LRState is abstract

	# The current state
	# Used by generated parsers
	var state: LRState is noinit

	init
	do
		state = start_state
	end

	# The stack of nodes
	# Used by generated parsers
	var node_stack = new Array[Node]

	# The stack of states
	# Used by generated parsers
	var state_stack = new Array[LRState]

	# Should the parser stop
	# Used by generated parsers
	var stop = true is writable

	# Parse a full sequence of tokens and return a complete syntactic tree
	fun parse: Node
	do
		state = start_state
		state_stack.clear
		node_stack.clear
		stop = false
		while not stop do
			#print "* current state {state}"
			#print "  tokens={tokens.join(" ")}"
			#print "  node_stack={node_stack.join(" ")}"
			#print "  state_stack={state_stack.join(" ")}"
			state.action(self)
		end
		#print "* over"
		return node_stack.first
	end
end
lib/nitcc_runtime/nitcc_runtime.nit:20,1--140,3