1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Example of the hijack of a lexer to inject custom behavior.
16 # see `blob.sablecc` for the grammar
17 import blob_test_parser
19 redef class Lexer_blob
21 # Two context, *in blob* (custom), and *not in blob* (normal).
22 # The initial state is *in blob*.
25 # Refine the `next_token` to hijack the lexer.
31 # Watch for tokens that trigger a context change
32 if res
isa Nendmark then in_blob
= true
37 # Manage pos, line and col manually
38 # TODO: improve the lexer API
46 # Need to count three '{' or the end of text
53 # Got them, backtrack them.
62 # Next char, count lines.
71 # Create manually the `blob token`
73 var position
= new Position(pos_start
, pos
, line_start
, line
, col_start
, col
)
74 token
.position
= position
75 token
.text
= text
.substring
(pos_start
, pos-pos_start
+1)
77 # Prepare for the next token