parser_base :: StringProcessor :: current_location
Gives the current location in thesrc
parser_base :: StringProcessor :: hot_location
Returns the current location as aLocation
object
parser_base :: StringProcessor :: ignore_until
Reads characters until patterns
is found
parser_base :: StringProcessor :: ignore_until_whitespace
Ignores any printable character until a whitespace is encounteredparser_base :: StringProcessor :: ignore_until_whitespace_or_comment
Advancepos
until a whitespace or #
is encountered
parser_base :: StringProcessor :: ignore_whitespaces
Advances insrc
until a non-whitespace character is encountered
parser_base :: StringProcessor :: line_offset
Offset in the current lineparser_base :: StringProcessor :: line_start
Position at which current line startedparser_base :: StringProcessor :: line_start=
Position at which current line startedparser_base :: StringProcessor :: read_number
Read a token and parse it as aFloat
parser_base :: StringProcessor :: read_token
Read a single token after skipping preceding whitespacesparser_base :: StringProcessor :: read_until_eol_or_comment
Advancepos
until the next end of line or a #
parser_base :: StringProcessor :: read_vec3
Read 2 or 3 numbers and return them as aVec3
parser_base :: StringProcessor :: read_vec4
Read 3 or 4 numbers and return them as aVec4
parser_base :: StringProcessor :: skip_eol
Advancepos
to skip the next end of line
parser_base $ StringProcessor :: SELF
Type of this instance, automatically specialized in every classcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
parser_base :: StringProcessor :: current_location
Gives the current location in thesrc
core :: Object :: defaultinit
parser_base :: StringProcessor :: hot_location
Returns the current location as aLocation
object
parser_base :: StringProcessor :: ignore_until
Reads characters until patterns
is found
parser_base :: StringProcessor :: ignore_until_whitespace
Ignores any printable character until a whitespace is encounteredparser_base :: StringProcessor :: ignore_until_whitespace_or_comment
Advancepos
until a whitespace or #
is encountered
parser_base :: StringProcessor :: ignore_whitespaces
Advances insrc
until a non-whitespace character is encountered
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
parser_base :: StringProcessor :: line_offset
Offset in the current lineparser_base :: StringProcessor :: line_start
Position at which current line startedparser_base :: StringProcessor :: line_start=
Position at which current line startedcore :: Object :: output_class_name
Display class name on stdout (debug only).parser_base :: StringProcessor :: read_number
Read a token and parse it as aFloat
parser_base :: StringProcessor :: read_token
Read a single token after skipping preceding whitespacesparser_base :: StringProcessor :: read_until_eol_or_comment
Advancepos
until the next end of line or a #
parser_base :: StringProcessor :: read_vec3
Read 2 or 3 numbers and return them as aVec3
parser_base :: StringProcessor :: read_vec4
Read 3 or 4 numbers and return them as aVec4
parser_base :: StringProcessor :: skip_eol
Advancepos
to skip the next end of line
# 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
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