1 $ // This file is part of NIT ( http://www.nitlanguage.org ).
3 $ // Copyright 2008 Jean Privat <jean@pryen.org>
4 $ // Based on algorithms developped for ( http://www.sablecc.org/ ).
6 $ // Licensed under the Apache License, Version 2.0 (the "License");
7 $ // you may not use this file except in compliance with the License.
8 $ // You may obtain a copy of the License at
10 $ // http://www.apache.org/licenses/LICENSE-2.0
12 $ // Unless required by applicable law or agreed to in writing, software
13 $ // distributed under the License is distributed on an "AS IS" BASIS,
14 $ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 $ // See the License for the specific language governing permissions and
16 $ // limitations under the License.
18 $ template make_lexer()
20 # The lexer extract NIT tokens from an input stream.
21 # It is better user with the Parser
25 var _token: nullable Token
31 readable var _file: SourceFile
33 # Current character in the stream
34 var _stream_pos: Int = 0
36 # Current line number in the input stream
39 # Current column in the input stream
42 # Was the last character a cariage-return?
45 $ foreach {lexer_data/state}
46 # Constante state values
47 private fun state_${translate(@name,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}: Int do return @id end
50 # Create a new lexer for a stream (and a name)
51 init(file: SourceFile)
56 # Give the next token (but do not consume it)
59 while _token == null do
62 return _token.as(not null)
65 # Give and consume the next token
69 while result == null do
76 # Get a token, or null if it is discarded
77 private fun get_token: nullable Token
82 var start_stream_pos = sp
84 var start_line = _line
85 var string = _file.string
86 var string_len = string.length
90 var accept_length = -1
95 if sp >= string_len then
98 var c = string[sp].ascii
107 _file.line_starts[line] = sp
111 _file.line_starts[line] = sp
117 _file.line_starts[line] = sp
124 var old_state = dfa_state
125 if dfa_state < -1 then
126 old_state = -2 - dfa_state
132 var high = lexer_goto(old_state, 0) - 1
136 var middle = (low + high) / 2
137 var offset = middle * 3 + 1 # +1 because length is at 0
139 if c < lexer_goto(old_state, offset) then
141 else if c > lexer_goto(old_state, offset+1) then
144 dfa_state = lexer_goto(old_state, offset+2)
149 if dfa_state > -2 then break
157 if dfa_state >= 0 then
158 var tok = lexer_accept(dfa_state)
160 accept_state = dfa_state
162 accept_length = sp - start_stream_pos
167 if accept_state != -1 then
168 var location = new Location(_file, start_line + 1, accept_line + 1, start_pos + 1, accept_pos)
171 _stream_pos = start_stream_pos + accept_length
173 if accept_token == ${position()-1} then
174 $ if {count(transition[@from!=@to])!=0}
175 var state_id = _state
176 $ foreach transition in {transition[@from!=@to]}
177 if state_id == ${/parser/lexer_data/state[@name=$transition/@from]/@id} then
178 _state = state_${translate(@to,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
184 var token_text = string.substring(start_stream_pos, accept_length)
185 return new @ename.init_tk(token_text, location)
187 return new @ename.init_tk(location)
196 var location = new Location(_file, start_line + 1, start_line + 1, start_pos + 1, start_pos + 1)
197 if sp > start_stream_pos then
198 var text = string.substring(start_stream_pos, sp-start_stream_pos)
199 var token = new PError.init_error("Syntax error: unknown token {text}.", location)
202 var token = new EOF(location)
215 $ template make_lexer_table()
216 $ foreach {lexer_data/goto_table/state}
218 $ if {count(goto)!=0}
219 static const int lexer_goto_row${position()}[] = {
222 @low, @high, @state[-sep ','-]
227 static const int lexer_goto_row_null[] = {0};
228 const int* const lexer_goto_table[] = {
230 $ if {count(goto)!=0}
231 lexer_goto_row${position()}[-sep ','-]
233 lexer_goto_row_null[-sep ','-]
239 $ foreach {lexer_data/accept_table/state}
240 const int lexer_accept_table[] = {
241 [-foreach {i}-]${.}[-sep ','-][-end foreach-]