Basic facilities for common parser operations on String sources

Introduced properties

fun current_location: Location

parser_base :: StringProcessor :: current_location

Gives the current location in the src
protected fun eof: Bool

parser_base :: StringProcessor :: eof

Is pos at the end of the source?
protected fun hot_location: Location

parser_base :: StringProcessor :: hot_location

Returns the current location as a Location object
protected fun ignore_until(s: String): Int

parser_base :: StringProcessor :: ignore_until

Reads characters until pattern s is found
protected fun ignore_until_whitespace: Int

parser_base :: StringProcessor :: ignore_until_whitespace

Ignores any printable character until a whitespace is encountered
protected fun ignore_until_whitespace_or_comment: Int

parser_base :: StringProcessor :: ignore_until_whitespace_or_comment

Advance pos until a whitespace or # is encountered
protected fun ignore_whitespaces

parser_base :: StringProcessor :: ignore_whitespaces

Advances in src until a non-whitespace character is encountered
protected fun len: Int

parser_base :: StringProcessor :: len

Length of the source document
protected fun len=(len: Int)

parser_base :: StringProcessor :: len=

Length of the source document
protected fun line: Int

parser_base :: StringProcessor :: line

Current line in src
protected fun line=(line: Int)

parser_base :: StringProcessor :: line=

Current line in src
protected fun line_offset: Int

parser_base :: StringProcessor :: line_offset

Offset in the current line
protected fun line_start: Int

parser_base :: StringProcessor :: line_start

Position at which current line started
protected fun line_start=(line_start: Int)

parser_base :: StringProcessor :: line_start=

Position at which current line started
protected fun pos: Int

parser_base :: StringProcessor :: pos

Current position in src
protected fun pos=(pos: Int)

parser_base :: StringProcessor :: pos=

Current position in src
protected fun read_number: Float

parser_base :: StringProcessor :: read_number

Read a token and parse it as a Float
protected fun read_token: String

parser_base :: StringProcessor :: read_token

Read a single token after skipping preceding whitespaces
protected fun read_until_eol_or_comment: String

parser_base :: StringProcessor :: read_until_eol_or_comment

Advance pos until the next end of line or a #
protected fun read_vec3: Vec3

parser_base :: StringProcessor :: read_vec3

Read 2 or 3 numbers and return them as a Vec3
protected fun read_vec4: Vec4

parser_base :: StringProcessor :: read_vec4

Read 3 or 4 numbers and return them as a Vec4
protected fun skip_eol

parser_base :: StringProcessor :: skip_eol

Advance pos to skip the next end of line
protected fun src: String

parser_base :: StringProcessor :: src

Source document to parse
protected fun src=(src: String)

parser_base :: StringProcessor :: src=

Source document to parse

Redefined properties

redef type SELF: StringProcessor

parser_base $ StringProcessor :: SELF

Type of this instance, automatically specialized in every class
redef init init

parser_base $ StringProcessor :: init

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
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_location: Location

parser_base :: StringProcessor :: current_location

Gives the current location in the src
protected fun eof: Bool

parser_base :: StringProcessor :: eof

Is pos at the end of the source?
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.
protected fun hot_location: Location

parser_base :: StringProcessor :: hot_location

Returns the current location as a Location object
protected fun ignore_until(s: String): Int

parser_base :: StringProcessor :: ignore_until

Reads characters until pattern s is found
protected fun ignore_until_whitespace: Int

parser_base :: StringProcessor :: ignore_until_whitespace

Ignores any printable character until a whitespace is encountered
protected fun ignore_until_whitespace_or_comment: Int

parser_base :: StringProcessor :: ignore_until_whitespace_or_comment

Advance pos until a whitespace or # is encountered
protected fun ignore_whitespaces

parser_base :: StringProcessor :: ignore_whitespaces

Advances in src until a non-whitespace character is encountered
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.
protected fun len: Int

parser_base :: StringProcessor :: len

Length of the source document
protected fun len=(len: Int)

parser_base :: StringProcessor :: len=

Length of the source document
protected fun line: Int

parser_base :: StringProcessor :: line

Current line in src
protected fun line=(line: Int)

parser_base :: StringProcessor :: line=

Current line in src
protected fun line_offset: Int

parser_base :: StringProcessor :: line_offset

Offset in the current line
protected fun line_start: Int

parser_base :: StringProcessor :: line_start

Position at which current line started
protected fun line_start=(line_start: Int)

parser_base :: StringProcessor :: line_start=

Position at which current line started
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).
protected fun pos: Int

parser_base :: StringProcessor :: pos

Current position in src
protected fun pos=(pos: Int)

parser_base :: StringProcessor :: pos=

Current position in src
protected fun read_number: Float

parser_base :: StringProcessor :: read_number

Read a token and parse it as a Float
protected fun read_token: String

parser_base :: StringProcessor :: read_token

Read a single token after skipping preceding whitespaces
protected fun read_until_eol_or_comment: String

parser_base :: StringProcessor :: read_until_eol_or_comment

Advance pos until the next end of line or a #
protected fun read_vec3: Vec3

parser_base :: StringProcessor :: read_vec3

Read 2 or 3 numbers and return them as a Vec3
protected fun read_vec4: Vec4

parser_base :: StringProcessor :: read_vec4

Read 3 or 4 numbers and return them as a Vec4
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
protected fun skip_eol

parser_base :: StringProcessor :: skip_eol

Advance pos to skip the next end of line
protected fun src: String

parser_base :: StringProcessor :: src

Source document to parse
protected fun src=(src: String)

parser_base :: StringProcessor :: src=

Source document to parse
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.
package_diagram parser_base::StringProcessor StringProcessor core::Object Object parser_base::StringProcessor->core::Object json::JSONStringParser JSONStringParser json::JSONStringParser->parser_base::StringProcessor dom::XMLProcessor XMLProcessor dom::XMLProcessor->parser_base::StringProcessor gamnit::ObjFileParser ObjFileParser gamnit::ObjFileParser->parser_base::StringProcessor gamnit::MtlFileParser MtlFileParser gamnit::MtlFileParser->parser_base::StringProcessor sexp::SExpProcessor SExpProcessor sexp::SExpProcessor->parser_base::StringProcessor

Parents

interface Object

core :: Object

The root of the class hierarchy.

Children

class JSONStringParser

json :: JSONStringParser

A simple ad-hoc JSON parser
class MtlFileParser

gamnit :: MtlFileParser

Parser of .mtl files
class ObjFileParser

gamnit :: ObjFileParser

Parser of .obj files in ASCII format
class SExpProcessor

sexp :: SExpProcessor

S-Expression processor
class XMLProcessor

dom :: XMLProcessor

Provides XML parsing facilities

Class definitions

parser_base $ StringProcessor
# Basic facilities for common parser operations on String sources
class StringProcessor
	# Source document to parse
	protected var src: String

	# Length of the source document
	protected var len: Int is noinit

	# Current position in `src`
	protected var pos = 0

	# Position at which current line started
	protected var line_start = 0

	# Current line in `src`
	protected var line = 1

	# Offset in the current line
	protected fun line_offset: Int do return pos - line_start + 1

	init do
		_len = src.length
	end

	# Gives the current location in the `src`
	fun current_location: Location do return new Location(line, line_offset)

	# Advances in `src` until a non-whitespace character is encountered
	protected fun ignore_whitespaces do
		var srclen = _len
		var p = _pos
		if p >= srclen then return
		var c = src[p]
		while c.is_whitespace do
			p += 1
			if p >= srclen then break
			if c == '\n' then
				_line += 1
				_line_start = p
			end
			c = src[p]
		end
		_pos = p
		return
	end

	# Reads characters until pattern `s` is found
	protected fun ignore_until(s: String): Int do
		if s.length == 0 then return _pos
		var srclen = _len
		var p = _pos
		if p >= srclen then return -1
		loop
			var c = s[0]
			var src_c = src[p]
			while src_c != c do
				p += 1
				if p >= srclen then
					_pos = p
					return -1
				end
				if src_c == '\n' then
					line += 1
					line_start= pos
				end
				src_c = src[p]
			end
			var relpos = p
			var fnd = true
			for i in s do
				if relpos >= srclen then
					fnd = false
					break
				end
				if src[relpos] != i then
					p += 1
					fnd = false
					break
				end
				relpos += 1
			end
			if fnd then
				_pos = p
				return p
			end
		end
	end

	# Ignores any printable character until a whitespace is encountered
	protected fun ignore_until_whitespace: Int do
		while src.length > pos and not src[pos].is_whitespace do pos += 1
		return pos
	end

	# Returns the current location as a `Location` object
	protected fun hot_location: Location do return new Location(line, line_offset)

	# Is `pos` at the end of the source?
	protected fun eof: Bool do return pos >= src.length
end
lib/parser_base/parser_base.nit:16,1--115,3

gamnit :: model_parser_base $ StringProcessor
redef class StringProcessor
	# Read a single token after skipping preceding whitespaces
	#
	# Returns an empty string when at `eof`
	protected fun read_token: String
	do
		while not eof and src[pos].is_whitespace and src[pos] != '\n' do
			pos += 1
		end

		var start = pos
		ignore_until_whitespace_or_comment
		var ending = pos
		var str = src.substring(start, ending-start)
		return str
	end

	# Read 3 or 4 numbers and return them as a `Vec4`
	#
	# If there is no fourth value, `Vec4::w` is set to 1.0.
	protected fun read_vec4: Vec4
	do
		var vec = new Vec4
		vec.x = read_number
		vec.y = read_number
		vec.z = read_number

		var wstr = read_token
		if wstr.length > 0 then
			vec.w = if wstr.is_numeric then wstr.to_f else 0.0
		else
			vec.w = 1.0
		end

		return vec
	end

	# Read 2 or 3 numbers and return them as a `Vec3`
	#
	# If there is no third value, `Vec3::z` is set to 0.0.
	protected fun read_vec3: Vec3
	do
		var vec = new Vec3
		vec.x = read_number
		vec.y = read_number # Make optional

		var wstr = read_token
		if wstr.length > 0 then
			vec.z = if wstr.is_numeric then wstr.to_f else 0.0
		else
			vec.z = 0.0
		end

		return vec
	end

	# Advance `pos` until a whitespace or `#` is encountered
	protected fun ignore_until_whitespace_or_comment: Int
	do
		while src.length > pos and not src[pos].is_whitespace and src[pos] != '#' do
			pos += 1
		end
		return pos
	end

	# Read a token and parse it as a `Float`
	protected fun read_number: Float
	do
		var str = read_token
		return if str.is_numeric then str.to_f else 0.0
	end

	# Advance `pos` until the next end of line or a `#`
	protected fun read_until_eol_or_comment: String
	do
		ignore_whitespaces
		var start = pos
		while not eof and src[pos] != '#' and src[pos] != '\n' do
			pos += 1
		end
		var ending = pos
		var str = src.substring(start, ending-start)
		return str.trim
	end

	# Advance `pos` to skip the next end of line
	protected fun skip_eol
	do
		while not eof do
			var c = src.chars[pos]
			pos += 1
			if c == '\n' then break
		end
	end
end
lib/gamnit/model_parsers/model_parser_base.nit:87,1--181,3