examples: annotate examples
[nit.git] / contrib / nitcc / examples / blob.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 # Example of the hijack of a lexer to inject custom behavior.
16 # see `blob.sablecc` for the grammar
17 import blob_test_parser
18
19 redef class Lexer_blob
20
21 # Two context, *in blob* (custom), and *not in blob* (normal).
22 # The initial state is *in blob*.
23 var in_blob = true
24
25 # Refine the `next_token` to hijack the lexer.
26 redef fun next_token
27 do
28 if not in_blob then
29 # Normal lexer
30 var res = super
31 # Watch for tokens that trigger a context change
32 if res isa Nendmark then in_blob = true
33 return res
34 end
35
36 # Custom lexer
37 # Manage pos, line and col manually
38 # TODO: improve the lexer API
39
40 var pos = pos_start
41 var line = line_start
42 var col = col_start
43 var text = stream
44 var len = text.length
45
46 # Need to count three '{' or the end of text
47 var cpt = 0
48 while pos < len do
49 var c = text[pos]
50 if c == '{' then
51 cpt += 1
52 if cpt == 3 then
53 # Got them, backtrack them.
54 pos -= 3
55 col -= 3
56 break
57 end
58 else
59 cpt = 0
60 end
61
62 # Next char, count lines.
63 pos += 1
64 col += 1
65 if c == '\n' then
66 line += 1
67 col = 1
68 end
69 end
70
71 # Create manually the `blob token`
72 var token = new Nblob
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)
76
77 # Prepare for the next token
78 pos_start = pos + 1
79 line_start = line
80 col_start = col + 1
81 in_blob = false
82
83 return token
84 end
85 end