da66d9f3fbeb6f053c8aca983dfba58241ebf343
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 # Transformation of regular expression to NFA
22 # Build the NFA of the regular expression
23 fun make_rfa
: Automaton do
28 # The real value of the string
36 redef fun value
: String do return text
.substring
(1, text
.length-2
).unescape_nit
37 redef fun make_rfa
: Automaton
39 var a
= new Automaton.epsilon
41 for c
in self.value
do
42 var b
= new Automaton.atom
(c
.ascii
)
50 redef fun value
: String do return text
.substring_from
(1).to_i
.ascii
.to_s
51 redef fun make_rfa
: Automaton
53 var a
= new Automaton.atom
(self.value
.first
.ascii
)
59 redef fun make_rfa
: Automaton
61 assert children
.length
== 1 else print
"no make_rfa for {self}"
62 return children
.first
.make_rfa
69 var a
= children
[0].make_rfa
70 var b
= children
[2].make_rfa
79 var a
= children
[0].make_rfa
80 var b
= children
[2].make_rfa
.to_dfa
81 for t
in b
.start
.outs
do
82 if not t
.to
.outs
.is_empty
then
83 print
"Not Yet Implemented Error: '-' only works on single char"
86 a
.minus_sym
(t
.symbol
.as(not null))
95 var a
= children
[0].make_rfa
96 var ta
= new Token("1")
98 var b
= children
[2].make_rfa
99 var tb
= new Token("2")
102 var c
= new Automaton.empty
107 for s
in c
.retrotags
[ta
] do
108 if c
.tags
[s
].has
(tb
) then
118 redef class Nre_except
121 var a
= children
[0].make_rfa
122 var ta
= new Token("1")
124 var b
= children
[2].make_rfa
125 var tb
= new Token("2")
128 var c
= new Automaton.empty
133 for s
in c
.retrotags
[ta
] do
134 if not c
.tags
[s
].has
(tb
) then
144 redef class Nre_shortest
147 var a
= children
[2].make_rfa
150 for t
in s
.outs
.to_a
do t
.delete
156 redef class Nre_longest
159 var a
= children
[2].make_rfa
161 for s
in a
.accept
.to_a
do
162 if not s
.outs
.is_empty
then a
.accept
.remove
(s
)
171 var a
= children
[0].make_rfa
172 var b
= children
[1].make_rfa
181 var a
= children
[0].make_rfa
190 var a
= children
[0].make_rfa
199 var a
= children
[0].make_rfa
208 return children
[1].make_rfa
212 redef class Nre_class
213 redef fun make_rfa
: Automaton
215 var c1
= children
[0].children
[0].value
216 var c2
= children
[3].children
[0].value
217 if c1
.length
!= 1 or c2
.length
!= 1 then
218 print
"Classes only works on single char"
222 var a
= new Automaton.cla
(c1
.first
.ascii
, c2
.first
.ascii
)
228 redef fun make_rfa
: Automaton
230 var a
= new Automaton.cla
(0, null)