nitcc: introduce nitcc
[nit.git] / contrib / nitcc / re2nfa.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 # Transformation of regular expression to NFA
16 module re2nfa
17
18 import nitcc_lexer
19 import autom
20
21 redef class Node
22 # Build the NFA of the regular expression
23 fun make_rfa: Automaton do
24 print inspect
25 abort
26 end
27 end
28
29 redef class Nstr
30 # The real value of the string
31 fun value: String do return text.substring(1, text.length-2).unescape_nit
32 redef fun make_rfa: Automaton
33 do
34 var a = new Automaton.epsilon
35 var val
36 for c in self.value do
37 var b = new Automaton.atom(c.ascii)
38 a.concat(b)
39 end
40 return a
41 end
42 end
43
44 redef class Nch_dec
45 # The real value of the char
46 fun value: String do return text.substring_from(1).to_i.ascii.to_s
47 redef fun make_rfa: Automaton
48 do
49 var a = new Automaton.atom(self.value.first.ascii)
50 return a
51 end
52 end
53
54 redef class NProd
55 redef fun make_rfa: Automaton
56 do
57 assert children.length == 1 else print "no make_rfa for {self}"
58 return children.first.make_rfa
59 end
60 end
61
62 redef class Nre_alter
63 redef fun make_rfa
64 do
65 var a = children[0].make_rfa
66 var b = children[2].make_rfa
67 a.alternate(b)
68 return a
69 end
70 end
71
72 redef class Nre_minus
73 redef fun make_rfa
74 do
75 var a = children[0].make_rfa
76 var b = children[2].make_rfa.to_dfa
77 for t in b.start.outs do
78 if not t.to.outs.is_empty then
79 print "Not Yet Implemented Error: '-' only works on single char"
80 exit(1)
81 end
82 a.minus_sym(t.symbol.as(not null))
83 end
84 return a
85 end
86 end
87
88 redef class Nre_conc
89 redef fun make_rfa
90 do
91 var a = children[0].make_rfa
92 var b = children[1].make_rfa
93 a.concat(b)
94 return a
95 end
96 end
97
98 redef class Nre_star
99 redef fun make_rfa
100 do
101 var a = children[0].make_rfa
102 a.close
103 return a
104 end
105 end
106
107 redef class Nre_ques
108 redef fun make_rfa
109 do
110 var a = children[0].make_rfa
111 a.optionnal
112 return a
113 end
114 end
115
116 redef class Nre_plus
117 redef fun make_rfa
118 do
119 var a = children[0].make_rfa
120 a.plus
121 return a
122 end
123 end
124
125 redef class Nre_par
126 redef fun make_rfa
127 do
128 return children[1].make_rfa
129 end
130 end
131
132 redef class Nre_class
133 redef fun make_rfa: Automaton
134 do
135 var c1 = children[0].as(Nstr).value
136 var c2 = children[3].as(Nstr).value
137 if c1.length != 1 or c2.length != 1 then
138 print "Classes only works on single char"
139 exit(1)
140 abort
141 end
142 var a = new Automaton.cla(c1.first.ascii, c2.first.ascii)
143 return a
144 end
145 end
146
147 redef class Nre_any
148 redef fun make_rfa: Automaton
149 do
150 var a = new Automaton.cla(0, 127)
151 return a
152 end
153 end