-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautoblocks-parser.go
102 lines (89 loc) · 2.2 KB
/
autoblocks-parser.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2019 Autoblocks <[email protected]>
// All rights reserved. Use of this source code is governed by a LGPL v3
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"flag"
"os"
"encoding/json"
"fmt"
"ioutil"
"github.com/Autoblocks/go-dsl"
"github.com/Autoblocks/autoblocks-parser/ab"
"github.com/Autoblocks/autoblocks-parser/il"
"github.com/Autoblocks/autoblocks-parser/st"
"github.com/Autoblocks/autoblocks-parser/sfc"
"github.com/Autoblocks/autoblocks-parser/ld"
"github.com/Autoblocks/autoblocks-parser/fbd"
)
func main() {
flag.Parse()
switch flag.NArg() {
case 0:
scanFn := ab.Scan
parseFn := ab.Parse
ts := ab.NewTokenSet()
ns := ab.NewNodeSet()
break
case 1:
arg, _ = ioutil.ReadFile(flag.Arg(0))
if arg == "AB"{
scanFn := ab.Scan
parseFn := ab.Parse
ts := ab.NewTokenSet()
ns := ab.NewNodeSet()
}else if arg == "IL"{
scanFn := il.Scan
parseFn := il.Parse
ts := il.NewTokenSet()
ns := il.NewNodeSet()
}else if arg == "ST"{
scanFn := st.Scan
parseFn := st.Parse
ts := st.NewTokenSet()
ns := st.NewNodeSet()
}else if arg == "SFC"{
scanFn := sfc.Scan
parseFn := sfc.Parse
ts := sfc.NewTokenSet()
ns := sfc.NewNodeSet()
}else if arg == "LD"{
scanFn := ld.Scan
parseFn := ld.Parse
ts := ld.NewTokenSet()
ns := ld.NewNodeSet()
}else if arg == "FBD"{
scanFn := fbd.Scan
parseFn := fbd.Parse
ts := fbd.NewTokenSet()
ns := fbd.NewNodeSet()
}else{
fmt.Printf("Language type muast be one of: AB, IL, ST, SFC, LD, FBD\n")
os.Exit(1)
}
break
default:
fmt.Printf("Input must be from stdin or file\n")
os.Exit(1)
}
reader := bufio.NewReader(os.Stdin)
bufreader := bufio.NewReader(reader)
logfilename := "log.txt"
logfile, err := os.Create(logfilename)
if err != nil {
fmt.Println("Error: Could not create log file " + logfilename + ": " + err.Error())
os.Exit(1)
}
ast, errs := dsl.ParseAndLog(parseFn, scanFn, ts, ns, bufreader, logfile)
logfile.Close()
outJson, _ := json.Marshal(ast.RootNode)
outString := string(outJson)
if errs != nil {
for _, err := range errs {
fmt.Print(err.String())
}
}else{
fmt.Println(outString)
}
}