7fa876fc15717bb416400e4938cf929e4378dbf8
[nit.git] / contrib / nitcc / src / nitcc_lexer0.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Ad-hoc hand-writen lexer for nitcc
16 # This avoid to commit (and relyon ) a generated lexer
17 #
18 module nitcc_lexer0
19
20 # Required for the tokens definitions
21 import nitcc_parser
22
23 # Hand-writen lexer of nitcc
24 # Used only for the boostrap of the tool.
25 class Lexer_nitcc
26 var text: String
27
28 var iter: Iterator[Char] = "".chars.iterator
29 var pos = 0
30
31 var tokens = new Array[NToken]
32
33 fun lex: Array[NToken]
34 do
35 iter = text.chars.iterator
36 while iter.is_ok do
37 trim
38 if not iter.is_ok then break
39 var c = iter.item
40 iter.next
41 pos += 1
42 if c == '*' then
43 tokens.add new Nstar
44 else if c == '?' then
45 tokens.add new Nques
46 else if c == '+' then
47 tokens.add new Nplus
48 else if c == '-' then
49 if iter.item == '>' then
50 iter.next
51 tokens.add new Narrow
52 else
53 tokens.add new Nminus
54 end
55 else if c == '(' then
56 tokens.add new Nopar
57 else if c == ')' then
58 tokens.add new Ncpar
59 else if c == '{' then
60 tokens.add new Nocur
61 else if c == '}' then
62 tokens.add new Nccur
63 else if c == '|' then
64 tokens.add new Npipe
65 else if c == ',' then
66 tokens.add new Ncomma
67 else if c == ':' then
68 tokens.add new Ncolo
69 else if c == ';' then
70 tokens.add new Nsemi
71 else if c == '.' then
72 tokens.add new Ndot
73 else if c == '=' then
74 tokens.add new Neq
75 else if c == '\'' then
76 str
77 else if c >= 'a' and c <= 'z' then
78 id(c)
79 else if c >= 'A' and c <= 'Z' then
80 kw(c)
81 else if c == '/' and iter.is_ok and iter.item == '/' then
82 while iter.is_ok and iter.item != '\n' do iter.next
83 else
84 error(c)
85 end
86 end
87 tokens.add new NEof
88 return tokens
89 end
90
91 fun error(c: Char)
92 do
93 print "pos {pos}: Lexer error on '{c}'."
94 abort
95 end
96
97 fun str
98 do
99 var b = new FlatBuffer
100 b.add('\'')
101 while iter.is_ok do
102 var c = iter.item
103 iter.next
104 if c == '\\' then
105 if not iter.is_ok then
106 error(c)
107 end
108 b.add(c)
109 c = iter.item
110 iter.next
111 else if c == '\'' then
112 b.add(c)
113 var token = new Nstr
114 token.text = b.to_s
115 tokens.add token
116 return
117 end
118 b.add c
119 end
120 error('\n')
121 abort
122 end
123
124 fun id(c: Char)
125 do
126 var b = new FlatBuffer
127 b.add c
128 while iter.is_ok do
129 c = iter.item
130 if c != '_' and (c<'a' or c >'z') and (c<'0' or c>'9') then
131 break
132 end
133 b.add c
134 iter.next
135 end
136 var token = new Nid
137 token.text = b.to_s
138 tokens.add token
139 end
140
141 fun kw(c: Char)
142 do
143 var b = new FlatBuffer
144 b.add c
145 while iter.is_ok do
146 c = iter.item
147 if c != '_' and (c<'a' or c >'z') and (c<'0' or c>'9') then
148 break
149 end
150 b.add c
151 iter.next
152 end
153 var token = new Nkw
154 token.text = b.to_s
155 tokens.add token
156 end
157
158 fun trim
159 do
160 while iter.is_ok and iter.item <= ' ' do
161 iter.next
162 pos += 1
163 end
164 end
165 end