-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
76 lines (66 loc) · 1.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/uchijo/plmfa-based-regex/eval"
"github.com/uchijo/plmfa-based-regex/model"
"github.com/uchijo/plmfa-based-regex/parser"
)
type Output struct {
Match bool `json:"match"`
Error bool `json:"error"`
Regex string `json:"regex"`
Input string `json:"input"`
}
type Args struct {
input string
regex string
eSem bool
noRecover bool
showLog bool
}
var args Args
func init() {
var epsilonSemantics = flag.Bool("e", false, "use epsilon semantics")
var noRecover = flag.Bool("noRecover", false, "doesnt recover from panic when true")
var showLog = flag.Bool("showLog", false, "show log on matching")
flag.Parse()
rawArgs := flag.Args()
args = Args{
input: rawArgs[0],
regex: rawArgs[1],
eSem: *epsilonSemantics,
noRecover: *noRecover,
showLog: *showLog,
}
}
func main() {
// エラーで死ぬか、エラーがあったことをjsonで通知するかの分岐
defer func() {
if args.noRecover {
return
}
if r := recover(); r != nil {
result := Output{
Error: true,
}
byteText, _ := json.Marshal(result)
fmt.Println(string(byteText))
}
}()
regex := parser.GenAst(args.regex)
states, start, _ := model.CreateCompleteStates(regex)
input := eval.InputBuffer{
Input: args.input,
}
matched := eval.Search(states, input, start, args.eSem, args.showLog)
result := Output{
Match: matched,
Error: false,
Input: args.input,
Regex: args.regex,
}
byteText, _ := json.Marshal(result)
fmt.Println(string(byteText))
}