1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Simple base for hand-made parsers of all kinds
14 # Basic facilities for common parser operations on String sources
16 # Source document to parse
17 protected var src
: String
19 # Length of the source document
20 protected var len
: Int is noinit
22 # Current position in `src`
25 # Position at which current line started
26 protected var line_start
= 0
28 # Current line in `src`
29 protected var line
= 1
31 # Offset in the current line
32 protected fun line_offset
: Int do return pos
- line_start
+ 1
38 # Gives the current location in the `src`
39 fun current_location
: Location do return new Location(line
, line_offset
)
41 # Advances in `src` until a non-whitespace character is encountered
42 protected fun ignore_whitespaces
do
45 if p
>= srclen
then return
47 while c
.is_whitespace
do
49 if p
>= srclen
then break
60 # Reads characters until pattern `s` is found
61 protected fun ignore_until
(s
: String): Int do
62 if s
.length
== 0 then return _pos
65 if p
>= srclen
then return -1
84 if relpos
>= srclen
then
88 if src
[relpos
] != i
then
102 # Ignores any printable character until a whitespace is encountered
103 protected fun ignore_until_whitespace
: Int do
104 while not src
[pos
].is_whitespace
do pos
+= 1
108 # Returns the current location as a `Location` object
109 protected fun hot_location
: Location do return new Location(line
, line_offset
)
112 # Information about the location of an entity in a source document
114 # Line in which the element is described
116 # Offset in the line at which the element is positioned
119 redef fun to_s
do return "line {line}, position {offset}"