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
.chars
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
.chars
.first
.ascii
)
59 redef fun value
: String do return text
.substring_from
(2).to_hex
.ascii
.to_s
60 redef fun make_rfa
: Automaton
62 var a
= new Automaton.atom
(self.value
.chars
.first
.ascii
)
68 redef fun make_rfa
: Automaton
70 assert children
.length
== 1 else print
"no make_rfa for {self}"
71 return children
.first
.make_rfa
78 var a
= children
[0].make_rfa
79 var b
= children
[2].make_rfa
88 var a
= children
[0].make_rfa
89 var b
= children
[2].make_rfa
.to_dfa
90 for t
in b
.start
.outs
do
91 if not t
.to
.outs
.is_empty
then
92 # `b` is not a single char, so just use except
93 # "a - b == a Except (Any* b Any*)"
94 var any1
= new Automaton.cla
(0, null)
96 var any2
= new Automaton.cla
(0, null)
104 a
.minus_sym
(t
.symbol
.as(not null))
113 print
"{children.first.position.to_s}: NOT YET IMPLEMENTED: token `End`; replaced with an empty string"
114 return new Automaton.epsilon
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 c
.tags
[s
].has
(tb
) then
144 redef class Nre_except
147 var a
= children
[0].make_rfa
148 var b
= children
[2].make_rfa
153 redef class Nre_shortest
156 var a
= children
[2].make_rfa
159 for t
in s
.outs
.to_a
do t
.delete
165 redef class Nre_longest
168 var a
= children
[2].make_rfa
170 for s
in a
.accept
.to_a
do
171 if not s
.outs
.is_empty
then a
.accept
.remove
(s
)
180 var a
= children
[0].make_rfa
181 var b
= children
[1].make_rfa
190 var a
= children
[0].make_rfa
199 var a
= children
[0].make_rfa
208 var a
= children
[0].make_rfa
217 return children
[1].make_rfa
221 redef class Nre_class
222 redef fun make_rfa
: Automaton
224 var c1
= children
[0].children
[0].value
225 var c2
= children
[3].children
[0].value
226 if c1
.length
!= 1 or c2
.length
!= 1 then
227 print
"Classes only works on single char"
231 var a
= new Automaton.cla
(c1
.chars
.first
.ascii
, c2
.chars
.first
.ascii
)
237 redef fun make_rfa
: Automaton
239 var a
= new Automaton.cla
(0, null)