raz / tools / Scan.raz

173 lines · 5.2 KiB · raw · history · last change e0d008cd

1(* Scan: the symbols of raz source, for the tools. Unlike the
2 compiler's scanner it keeps comments, as symbols of their own, and
3 says where each symbol is in the text and whether a line break comes
4 before it. It doesn't check what it reads: a malformed number is
5 read as far as it goes, and a string or character left open ends
6 with its line, a raw string or comment with the text. *)
7module Scan;
8
9type
10 Kind* = (word, number, literal, symbol, comment, eof);
11
12 (* word is an identifier or a reserved word; literal a string or a
13 character; symbol an operator or delimiter. *)
14 Scanner* = record
15 src*: []const U8;
16 pos*: INT; (* where the next symbol is looked for *)
17 kind*: Kind; (* the current symbol *)
18 start*, stop*: INT; (* its text: src[start..stop] *)
19 lineBreak*: BOOL; (* a line break comes before it, or it is the first *)
20 end;
21
22const
23 words = "and begin break by code const continue defer div do else elsif end extern for forward if import in link match mod module not of or packed proc record repeat return set shl shr then to try type until var variant VOLATILE while xor";
24
25proc isSpace*(c: U8): BOOL;
26begin
27 return (c = ' ') or (c = '\t') or (c = '\n') or (c = '\r');
28end;
29
30proc isWord*(c: U8): BOOL;
31begin
32 return (c >= 'a') and (c <= 'z') or (c >= 'A') and (c <= 'Z')
33 or (c >= '0') and (c <= '9') or (c = '_');
34end;
35
36proc isDigit(c: U8): BOOL;
37begin
38 return (c >= '0') and (c <= '9');
39end;
40
41(* Whether w is one of the words of list, which are separated by
42 single spaces. *)
43proc among*(w, list: []const U8): BOOL;
44var i, j: INT;
45begin
46 i := 0;
47 while i < LEN(list) do
48 j := i;
49 while (j < LEN(list)) and (list[j] # ' ') do INC(j); end;
50 if list[i..j] = w then return true; end;
51 i := j + 1;
52 end;
53 return false;
54end;
55
56(* Whether w is one of the report's reserved words. *)
57proc reserved*(w: []const U8): BOOL;
58begin
59 return among(w, words);
60end;
61
62(* The scanner at the start of src; next gives the first symbol. *)
63proc init*(var s: Scanner; src: []const U8);
64begin
65 s := {src: src, kind: eof};
66end;
67
68(* Past the comment that starts at p, its nested comments with it. *)
69proc pastComment(src: []const U8; p: INT): INT;
70var depth: INT;
71begin
72 depth := 0;
73 repeat
74 if (src[p] = '(') and (p + 1 < LEN(src)) and (src[p + 1] = '*') then
75 INC(depth); INC(p, 2);
76 elsif (src[p] = '*') and (p + 1 < LEN(src)) and (src[p + 1] = ')') then
77 DEC(depth); INC(p, 2);
78 else
79 INC(p);
80 end;
81 until (depth = 0) or (p >= LEN(src));
82 return p;
83end;
84
85(* Past the number that starts at p: digits and letters, then a real's
86 point, digits and exponent, as in 0FFH and 2.0E-3. *)
87proc pastNumber(src: []const U8; p: INT): INT;
88begin
89 while (p < LEN(src)) and isWord(src[p]) do INC(p); end;
90 if (p + 1 < LEN(src)) and (src[p] = '.') and isDigit(src[p + 1]) then
91 INC(p);
92 while (p < LEN(src)) and isWord(src[p]) do INC(p); end;
93 if (src[p - 1] = 'E') and (p < LEN(src)) and ((src[p] = '-') or (src[p] = '+')) then
94 INC(p);
95 while (p < LEN(src)) and isWord(src[p]) do INC(p); end;
96 end;
97 end;
98 return p;
99end;
100
101(* Past the string or character that starts at p, or to its line's end;
102 a raw string, between backquotes, has no escapes and may run over
103 lines. *)
104proc pastLiteral(src: []const U8; p: INT): INT;
105var q: U8;
106begin
107 q := src[p];
108 INC(p);
109 while (p < LEN(src)) and (src[p] # q) and ((src[p] # '\n') or (q = '`')) do
110 if (src[p] = '\\') and (q # '`') and (p + 1 < LEN(src)) then INC(p); end;
111 INC(p);
112 end;
113 if (p < LEN(src)) and (src[p] = q) then INC(p); end;
114 return p;
115end;
116
117(* Whether src[p..] starts with the symbol t. *)
118proc at(src: []const U8; p: INT; t: []const U8): BOOL;
119begin
120 return (p + LEN(t) <= LEN(src)) and (src[p..p + LEN(t)] = t);
121end;
122
123(* On to the next symbol. *)
124proc next*(var s: Scanner);
125var src: []const U8; p: INT; c: U8;
126begin
127 src := s.src;
128 p := s.pos;
129 s.lineBreak := p = 0;
130 while (p < LEN(src)) and isSpace(src[p]) do
131 if src[p] = '\n' then s.lineBreak := true; end;
132 INC(p);
133 end;
134 s.start := p;
135 if p >= LEN(src) then
136 s.kind := eof;
137 else
138 c := src[p];
139 if at(src, p, "(*") then
140 s.kind := comment; p := pastComment(src, p);
141 elsif isDigit(c) then
142 s.kind := number; p := pastNumber(src, p);
143 elsif isWord(c) then
144 s.kind := word;
145 while (p < LEN(src)) and isWord(src[p]) do INC(p); end;
146 elsif (c = '"') or (c = '\'') or (c = '`') then
147 s.kind := literal; p := pastLiteral(src, p);
148 else
149 s.kind := symbol;
150 if at(src, p, "...") then INC(p, 3);
151 elsif at(src, p, ":=") or at(src, p, "<=") or at(src, p, ">=") or at(src, p, "..") then
152 INC(p, 2);
153 else
154 INC(p);
155 end;
156 end;
157 end;
158 s.stop := p;
159 s.pos := p;
160end;
161
162proc text*(const s: Scanner): []const U8;
163begin
164 return s.src[s.start..s.stop];
165end;
166
167(* Whether the current symbol is t. *)
168proc is*(const s: Scanner; t: []const U8): BOOL;
169begin
170 return text(s) = t;
171end;
172
173end Scan.