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.
19 import popcorn
::pop_config
24 redef fun get
(req
, res
) do
25 res
.html
new RETemplate
28 redef fun post
(req
, res
) do
29 # TODO Retrieve re from post body
30 var re
= req
.string_arg
("re")
32 var tpl
= new RETemplate
34 if re
== null or re
.is_empty
then
35 tpl
.error
= "Error: empty regexp"
43 var re_parser
= new REParser
44 var node
= re_parser
.parse_re
(re
)
47 tpl
.error
= re_parser
.last_error
.as(not null)
53 var nfa
= re_parser
.make_nfa
(node
)
54 tpl
.nfa
= automaton_to_svg
(nfa
)
55 tpl
.dfa
= automaton_to_svg
(nfa
.to_dfa
)
60 private fun automaton_to_svg
(automaton
: Automaton): String do
61 automaton
.to_dot
(false).write_to_file
"dot.tmp"
62 sys
.system
"dot -Tsvg -osvg.tmp dot.tmp"
63 var svg
= "svg.tmp".to_path
.read_all
64 sys
.system
"rm -f dot.tmp svg.tmp"
73 var nfa
: nullable String = null
74 var dfa
: nullable String = null
75 var error
: nullable String = null
77 redef fun rendering
do
82 <meta charset="utf-8" />
83 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
84 <meta name="viewport" content="width=device-width, initial-scale=1" />
85 <title>REParser</title>
86 <link rel="stylesheet"
87 href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
90 <div class="container">
91 <h1>Regular Expressions to NFA and DFA</h1>
93 <label for="re">Enter a regular expression:</label>
94 <form action="/" method="POST" class="input-group">
95 <input id="re" name="re" type="text" class="form-control" value="{{{re}}}">
96 <span class="input-group-btn">
97 <input type="submit" class="btn btn-default" value="Convert" />
102 var error
= self.error
103 if error
!= null then
104 add
"""<p class="text-danger">{{{error}}}</p>"""
111 <div>{{{nfa}}}</div><br><br>"""
118 <div>{{{dfa}}}</div><br><br>"""
122 <div class="text-center text-muted">
123 <p>Powered by <a href="http://nitlanguage.org">nit</a>!</p>
130 var config
= new AppConfig
131 config
.parse_options
(args
)
134 app
.use
("/", new REHandler)
135 app
.listen
(config
.app_host
, config
.app_port
)