-
Notifications
You must be signed in to change notification settings - Fork 120
/
main.go
89 lines (75 loc) · 2.04 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
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"github.com/pointlander/peg/tree"
)
//go:generate -command build go run build.go
//go:generate build buildinfo
//go:generate build peg
var (
inline = flag.Bool("inline", false, "parse rule inlining")
_switch = flag.Bool("switch", false, "replace if-else if-else like blocks with switch blocks")
print = flag.Bool("print", false, "directly dump the syntax tree")
syntax = flag.Bool("syntax", false, "print out the syntax tree")
noast = flag.Bool("noast", false, "disable AST")
strict = flag.Bool("strict", false, "treat compiler warnings as errors")
filename = flag.String("output", "", "specify name of output file")
showVersion = flag.Bool("version", false, "print the version and exit")
showBuildTime = flag.Bool("time", false, "show the last time `build.go buildinfo` was ran")
)
func main() {
runtime.GOMAXPROCS(2)
flag.Parse()
if *showVersion {
if IS_TAGGED {
fmt.Println("version:", VERSION)
} else {
fmt.Printf("version: %s-%s\n", VERSION, COMMIT)
}
if *showBuildTime {
fmt.Println("time:", BUILDTIME)
}
return
}
if flag.NArg() != 1 {
flag.Usage()
log.Fatalf("FILE: the peg file to compile")
}
file := flag.Arg(0)
buffer, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
p := &Peg{Tree: tree.New(*inline, *_switch, *noast), Buffer: string(buffer)}
p.Init(Pretty(true), Size(1<<15))
if err := p.Parse(); err != nil {
log.Fatal(err)
}
p.Execute()
if *print {
p.Print()
}
if *syntax {
p.PrintSyntaxTree()
}
if *filename == "" {
*filename = file + ".go"
}
out, err := os.OpenFile(*filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Printf("%v: %v\n", *filename, err)
return
}
defer out.Close()
p.Strict = *strict
if err = p.Compile(*filename, os.Args, out); err != nil {
log.Fatal(err)
}
}