README: document nit_env.sh
[nit.git] / lib / parser_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
9 # another product.
10
11 # Simple base for hand-made parsers of all kinds
12 module parser_base
13
14 # Basic facilities for common parser operations on String sources
15 class StringProcessor
16 # Source document to parse
17 private var src: String
18
19 # Current position in `src`
20 private var pos = 0
21
22 # Position at which current line started
23 private var line_start = 0
24
25 # Current line in `src`
26 private var line = 1
27
28 # Offset in the current line
29 private fun line_offset: Int do return pos - line_start + 1
30
31 # Gives the current location in the `src`
32 fun current_location: Location do return new Location(line, line_offset)
33
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
38 var c = src[pos]
39 while c.is_whitespace do
40 pos += 1
41 if pos >= srclen then break
42 if c == '\n' then
43 line += 1
44 line_start = pos
45 end
46 c = src[pos]
47 end
48 end
49
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
55 loop
56 var c = s[0]
57 var src_c = src[pos]
58 while src_c != c do
59 pos += 1
60 if pos >= srclen then return -1
61 if src_c == '\n' then
62 line += 1
63 line_start= pos
64 end
65 src_c = src[pos]
66 end
67 var relpos = pos
68 var fnd = true
69 for i in s do
70 if relpos >= srclen then
71 fnd = false
72 break
73 end
74 if src[relpos] != i then
75 pos += 1
76 fnd = false
77 break
78 end
79 relpos += 1
80 end
81 if fnd then return pos
82 end
83 end
84 end
85
86 # Information about the location of an entity in a source document
87 class Location
88 # Line in which the element is described
89 var line: Int
90 # Offset in the line at which the element is positioned
91 var offset: Int
92
93 redef fun to_s do return "line {line}, position {offset}"
94 end