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 private var src
: String
19 # Current position in `src`
22 # Position at which current line started
23 private var line_start
= 0
25 # Current line in `src`
28 # Offset in the current line
29 private fun line_offset
: Int do return pos
- line_start
+ 1
31 # Gives the current location in the `src`
32 fun current_location
: Location do return new Location(line
, line_offset
)
34 # Advances in `src` until a non-whitespace character is encountered
35 private fun ignore_whitespaces
do
36 var srclen
= src
.length
37 if pos
>= srclen
then return
39 while c
.is_whitespace
do
41 if pos
>= srclen
then break
50 # Reads characters until pattern `s` is found
51 private fun ignore_until
(s
: String): Int do
52 if s
.length
== 0 then return pos
53 var srclen
= src
.length
54 if pos
>= srclen
then return -1
60 if pos
>= srclen
then return -1
70 if relpos
>= srclen
then
74 if src
[relpos
] != i
then
81 if fnd
then return pos
86 # Information about the location of an entity in a source document
88 # Line in which the element is described
90 # Offset in the line at which the element is positioned
93 redef fun to_s
do return "line {line}, position {offset}"