forked from knieriem/peg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (43 loc) · 1.07 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
// 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 (
"bufio"
"flag"
"fmt"
"github.com/knieriem/peg"
"io/ioutil"
"log"
"os"
"runtime"
)
var (
inline = flag.Bool("inline", false, "parse rule inlining")
_switch = flag.Bool("switch", false, "replace if-else if-else like blocks with switch blocks")
optiFlags = flag.String("O", "", "turn on various optimizations")
)
func main() {
runtime.GOMAXPROCS(2)
flag.BoolVar(&peg.Verbose, "verbose", false, "enable additional output, like statistics")
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
fmt.Fprintf(os.Stderr, " FILE: the leg file to compile\n")
os.Exit(1)
}
file := flag.Arg(0)
buffer, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
p := &Leg{Tree: peg.New(*inline, *_switch), Buffer: string(buffer)}
p.Init()
if err = p.Parse(0); err == nil {
w := bufio.NewWriter(os.Stdout)
p.Compile(w, *optiFlags)
w.Flush()
} else {
log.Print(file, ":", err)
}
}