Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / parser / xss / lexer.xss
1 $ // This file is part of NIT ( http://www.nitlanguage.org ).
2 $ //
3 $ // Copyright 2008 Jean Privat <jean@pryen.org>
4 $ // Based on algorithms developped for ( http://www.sablecc.org/ ).
5 $ //
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
9 $ //
10 $ //     http://www.apache.org/licenses/LICENSE-2.0
11 $ //
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.
17
18 $ template make_lexer()
19 redef class Lexer
20         redef fun make_token(accept_token, location)
21         do
22 $ foreach {//token}
23 $    if {@parser_index}
24                 if accept_token == ${position()-1} then
25 $        if {count(transition[@from!=@to])!=0}
26                         var state_id = _state
27 $            foreach transition in {transition[@from!=@to]}
28                         if state_id == ${/parser/lexer_data/state[@name=$transition/@from]/@id} then
29                                 _state = state_${translate(@to,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
30                         end
31 $            end
32 $        end if
33                         return new @ename.init_tk(location)
34                 end
35 $    end
36 $ end foreach
37                 abort # unknown token index `accept_token`
38         end
39 end
40
41 $ end template
42
43
44
45 $ template make_lexer_table()
46 $ foreach {lexer_data/goto_table/state}
47 $     foreach {row}
48 $         if {count(goto)!=0}
49 static const int lexer_goto_row${position()}[] = {
50         ${count(goto)},
51 $             foreach {goto}
52         @low, @high, @state[-sep ','-]
53 $             end foreach
54 };
55 $         end
56 $     end foreach
57 static const int lexer_goto_row_null[] = {0};
58 const int* const lexer_goto_table[] = {
59 $     foreach {row}
60 $         if {count(goto)!=0}
61         lexer_goto_row${position()}[-sep ','-]
62 $         else
63         lexer_goto_row_null[-sep ','-]
64 $         end
65 $     end foreach
66 };
67 $ end foreach
68
69 $ foreach {lexer_data/accept_table/state}
70 const int lexer_accept_table[] = {
71         [-foreach {i}-]${.}[-sep ','-][-end foreach-]
72 };
73 $ end foreach
74
75 $ end template