Find location of production nodes

Uses existing token locations to infer location of productions.

Introduced properties

private var _last_token: nullable Token

nitc :: ComputeProdLocationVisitor :: _last_token

The last visited token in the current production
private var _need_after_epsilons: Array[Prod]

nitc :: ComputeProdLocationVisitor :: _need_after_epsilons

Already visited epsilon productions that waits something after them
private var _need_first_prods: Array[Prod]

nitc :: ComputeProdLocationVisitor :: _need_first_prods

Currently visited productions that need a first token
private var _token: nullable Token

nitc :: ComputeProdLocationVisitor :: _token

The current (or starting) cursor on the token sequence used to collect loose tokens
private fun last_token: nullable Token

nitc :: ComputeProdLocationVisitor :: last_token

The last visited token in the current production
private fun last_token=(last_token: nullable Token)

nitc :: ComputeProdLocationVisitor :: last_token=

The last visited token in the current production
private fun need_after_epsilons: Array[Prod]

nitc :: ComputeProdLocationVisitor :: need_after_epsilons

Already visited epsilon productions that waits something after them
private fun need_after_epsilons=(need_after_epsilons: Array[Prod])

nitc :: ComputeProdLocationVisitor :: need_after_epsilons=

Already visited epsilon productions that waits something after them
private fun need_first_prods: Array[Prod]

nitc :: ComputeProdLocationVisitor :: need_first_prods

Currently visited productions that need a first token
private fun need_first_prods=(need_first_prods: Array[Prod])

nitc :: ComputeProdLocationVisitor :: need_first_prods=

Currently visited productions that need a first token
private fun token: nullable Token

nitc :: ComputeProdLocationVisitor :: token

The current (or starting) cursor on the token sequence used to collect loose tokens
private fun token=(token: nullable Token)

nitc :: ComputeProdLocationVisitor :: token=

The current (or starting) cursor on the token sequence used to collect loose tokens

Redefined properties

redef type SELF: ComputeProdLocationVisitor

nitc $ ComputeProdLocationVisitor :: SELF

Type of this instance, automatically specialized in every class
redef fun visit(n: ANode)

nitc $ ComputeProdLocationVisitor :: visit

What the visitor do when a node is visited

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _current_node: nullable ANode

nitc :: Visitor :: _current_node

The current visited node
private var _last_token: nullable Token

nitc :: ComputeProdLocationVisitor :: _last_token

The last visited token in the current production
private var _need_after_epsilons: Array[Prod]

nitc :: ComputeProdLocationVisitor :: _need_after_epsilons

Already visited epsilon productions that waits something after them
private var _need_first_prods: Array[Prod]

nitc :: ComputeProdLocationVisitor :: _need_first_prods

Currently visited productions that need a first token
private var _token: nullable Token

nitc :: ComputeProdLocationVisitor :: _token

The current (or starting) cursor on the token sequence used to collect loose tokens
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun current_node: nullable ANode

nitc :: Visitor :: current_node

The current visited node
fun current_node=(current_node: nullable ANode)

nitc :: Visitor :: current_node=

The current visited node
fun enter_visit(e: nullable ANode)

nitc :: Visitor :: enter_visit

Ask the visitor to visit a given node.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private fun last_token: nullable Token

nitc :: ComputeProdLocationVisitor :: last_token

The last visited token in the current production
private fun last_token=(last_token: nullable Token)

nitc :: ComputeProdLocationVisitor :: last_token=

The last visited token in the current production
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
private fun need_after_epsilons: Array[Prod]

nitc :: ComputeProdLocationVisitor :: need_after_epsilons

Already visited epsilon productions that waits something after them
private fun need_after_epsilons=(need_after_epsilons: Array[Prod])

nitc :: ComputeProdLocationVisitor :: need_after_epsilons=

Already visited epsilon productions that waits something after them
private fun need_first_prods: Array[Prod]

nitc :: ComputeProdLocationVisitor :: need_first_prods

Currently visited productions that need a first token
private fun need_first_prods=(need_first_prods: Array[Prod])

nitc :: ComputeProdLocationVisitor :: need_first_prods=

Currently visited productions that need a first token
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
private fun token: nullable Token

nitc :: ComputeProdLocationVisitor :: token

The current (or starting) cursor on the token sequence used to collect loose tokens
private fun token=(token: nullable Token)

nitc :: ComputeProdLocationVisitor :: token=

The current (or starting) cursor on the token sequence used to collect loose tokens
protected abstract fun visit(e: ANode)

nitc :: Visitor :: visit

What the visitor do when a node is visited
package_diagram nitc::parser_work::ComputeProdLocationVisitor ComputeProdLocationVisitor nitc::Visitor Visitor nitc::parser_work::ComputeProdLocationVisitor->nitc::Visitor core::Object Object nitc::Visitor->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Visitor

nitc :: Visitor

Abstract standard visitor on the AST

Class definitions

nitc $ ComputeProdLocationVisitor
# Find location of production nodes
# Uses existing token locations to infer location of productions.
private class ComputeProdLocationVisitor
	super Visitor

	# The current (or starting) cursor on the token sequence used to collect loose tokens
	var token: nullable Token

	# 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 = new Array[Prod]

	# The last visited token in the current production
	var last_token: nullable Token = null

	redef fun visit(n: ANode)
	do
		if n isa Token then
			# Skip injected tokens
			if not isset n._location then return

			# Collect loose tokens (not in the AST) and attach them to token in the AST
			var cursor = token
			if n != cursor then
				var lt = last_token
				# In order, we have the tokens:
				# * `lt` the previous visited token in the AST (if any)
				# * then `cursor` the loose tokens to attach
				# * then `n` the current visited token in the AST

				# In the following, we advance `cursor` to add them to `lt.next_looses` or to `n.prev_looses`.
				if lt != null then
					var ltl = lt.location.line_end
					# floating tokens on the same line of a AST-token follows it
					while cursor != null and cursor != n and ltl == cursor.location.line_start do
						cursor.is_loose = true
						lt.next_looses.add cursor
						cursor = cursor.next_token
					end
				end
				# other loose tokens precede the next AST-token
				while cursor != null and cursor != n do
					cursor.is_loose = true
					n.prev_looses.add cursor
					cursor = cursor.next_token
				end
			end
			token = n.next_token

			var loc = n._location
			_last_token = n

			# Add a first token to productions that need one
			if not _need_first_prods.is_empty then
				for no in _need_first_prods do
					no._first_location = loc
				end
				_need_first_prods.clear
			end

			# Find location for already visited epsilon production that need one
			if not _need_after_epsilons.is_empty then
				var loco = new Location(loc.file, loc.line_start, loc.line_start, loc.column_start, loc.column_start) 
				for no in _need_after_epsilons do
					no.location = loco
				end
				_need_after_epsilons.clear
			end
		else
			assert n isa Prod
			_need_first_prods.add(n)

			n.visit_all(self)

			var startl = n._first_location
			if startl != null then
				# Non-epsilon production
				var endl = _last_token.location

				if startl == endl then
					n.location = startl
				else
					n.location = new Location(startl.file, startl.line_start, endl.line_end, startl.column_start, endl.column_end)
				end

				if not _need_after_epsilons.is_empty then
					var loc = new Location(endl.file, endl.line_end, endl.line_end, endl.column_end, endl.column_end)
					for no in _need_after_epsilons do
						# Epsilon production that finishes the current non-epsilon production
						no.location = loc
					end
					_need_after_epsilons.clear
				end
			else
				# Epsilon production in the middle or that finishes a parent non-epsilon production
				_need_after_epsilons.add(n)
			end
		end
	end
end
src/parser/parser_work.nit:176,1--277,3