$ // This file is part of NIT ( http://www.nitlanguage.org ). $ // $ // Copyright 2008 Jean Privat $ // Based on algorithms developped for ( http://www.sablecc.org/ ). $ // $ // Licensed under the Apache License, Version 2.0 (the "License"); $ // you may not use this file except in compliance with the License. $ // You may obtain a copy of the License at $ // $ // http://www.apache.org/licenses/LICENSE-2.0 $ // $ // Unless required by applicable law or agreed to in writing, software $ // distributed under the License is distributed on an "AS IS" BASIS, $ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. $ // See the License for the specific language governing permissions and $ // limitations under the License. $ template make_lexer() redef class Lexer redef fun make_token(accept_token, location) do $ foreach {//token} $ if {@parser_index} if accept_token == ${position()-1} then $ if {count(transition[@from!=@to])!=0} var state_id = _state $ foreach transition in {transition[@from!=@to]} if state_id == ${/parser/lexer_data/state[@name=$transition/@from]/@id} then _state = state_${translate(@to,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} end $ end $ end if return new @ename.init_tk(location) end $ end $ end foreach abort # unknown token index `accept_token` end end $ end template $ template make_lexer_table() $ foreach {lexer_data/goto_table/state} $ foreach {row} $ if {count(goto)!=0} static const int lexer_goto_row${position()}[] = { ${count(goto)}, $ foreach {goto} @low, @high, @state[-sep ','-] $ end foreach }; $ end $ end foreach static const int lexer_goto_row_null[] = {0}; const int* const lexer_goto_table[] = { $ foreach {row} $ if {count(goto)!=0} lexer_goto_row${position()}[-sep ','-] $ else lexer_goto_row_null[-sep ','-] $ end $ end foreach }; $ end foreach $ foreach {lexer_data/accept_table/state} const int lexer_accept_table[] = { [-foreach {i}-]${.}[-sep ','-][-end foreach-] }; $ end foreach $ end template