forked from pointlander/peg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (112 loc) · 2.79 KB
/
main.go
File metadata and controls
131 lines (112 loc) · 2.79 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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"
"io"
"log"
"os"
"github.com/pointlander/peg/tree"
)
//go:generate ./bootstrap.bash
//go:generate ./generate-grammars.bash
var (
Version = "dev"
inline = flag.Bool("inline", false, "parse rule inlining")
switchFlag = flag.Bool("switch", false, "replace if-else if-else like blocks with switch blocks")
printFlag = 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")
outputFile = flag.String("output", "", "output to `FILE` (\"-\" for stdout)")
showVersion = flag.Bool("version", false, "print the version and exit")
)
// main is the entry point for the PEG compiler.
func main() {
flag.Parse()
if *showVersion {
fmt.Println("version:", Version)
return
}
err := parse(
func(p *Peg[uint32], out io.Writer) error {
if *printFlag {
p.Print()
}
if *syntax {
p.PrintSyntaxTree()
}
p.Strict = *strict
if err := p.Compile(*outputFile, os.Args, out); err != nil {
return err
}
return nil
},
)
if err != nil {
if *strict {
log.Fatal(err)
}
fmt.Fprintln(os.Stderr, "warning:", err)
}
}
// getIO returns input and output streams based on command-line flags.
func getIO() (in io.ReadCloser, out io.WriteCloser, err error) {
in, out = os.Stdin, os.Stdout
if flag.NArg() > 0 && flag.Arg(0) != "-" {
in, err = os.Open(flag.Arg(0))
if err != nil {
return nil, nil, err
}
if *outputFile == "" {
*outputFile = flag.Arg(0) + ".go"
}
}
if *outputFile != "" && *outputFile != "-" {
out, err = os.OpenFile(*outputFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
if in != nil && in != os.Stdin {
err := in.Close()
if err != nil {
panic(err)
}
}
return nil, nil, err
}
}
return in, out, nil
}
// parse reads input, parses, executes, and compiles the PEG grammar.
func parse(compile func(*Peg[uint32], io.Writer) error) error {
in, out, err := getIO()
if err != nil {
return err
}
defer func() {
if in != nil && in != os.Stdin {
err := in.Close()
if err != nil {
panic(err)
}
}
if out != nil && out != os.Stdout {
err := out.Close()
if err != nil {
panic(err)
}
}
}()
buffer, err := io.ReadAll(in)
if err != nil {
return err
}
p := &Peg[uint32]{Tree: tree.New(*inline, *switchFlag, *noast), Buffer: string(buffer)}
_ = p.Init(Pretty[uint32](true), Size[uint32](1<<15))
if err = p.Parse(); err != nil {
return err
}
p.Execute()
return compile(p, out)
}