forked from cznic/golex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (120 loc) · 2.8 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// blame: jnml, labs.nic.cz
package main
import (
"bufio"
"flag"
"fmt"
"github.com/cznic/lex"
"io"
"os"
)
const (
OFILE = "lex.yy.go"
)
var (
stdin = bufio.NewReader(os.Stdin)
stdout = bufio.NewWriter(os.Stdout)
stderr = bufio.NewWriter(os.Stderr)
)
type renderer interface {
render(srcname string, l *lex.L)
}
type writer interface {
io.Writer
wprintf(s string, args ...interface{}) (n int, err error)
}
type noRender struct {
w io.Writer
}
func (r *noRender) Write(p []byte) (n int, err error) {
return r.w.Write(p)
}
func (r *noRender) wprintf(s string, args ...interface{}) (n int, err error) {
n, err = io.WriteString(r.w, fmt.Sprintf(s, args...))
if err != nil {
panic(err)
}
return
}
func q(c uint32) string {
switch c {
default:
s := fmt.Sprintf("%q", string(c))
return "'" + s[1:len(s)-1] + "'"
case '\'':
return "'\\''"
case '"':
return "'\"'"
}
panic("unreachable")
}
func main() {
defer func() {
if e := recover(); e != nil {
fmt.Fprintf(stderr, "%s: %s\n", os.Args[0], e.(error))
stderr.Flush()
os.Exit(1)
}
}()
oflag := ""
var dfaflag, hflag, tflag, vflag, nodfaopt, bits32 bool
flag.BoolVar(&dfaflag, "DFA", false, "write DFA on stdout and quit")
flag.BoolVar(&hflag, "h", false, "show help and exit")
flag.StringVar(&oflag, "o", OFILE, "lexer output")
flag.BoolVar(&tflag, "t", false, "write scanner on stdout instead of "+OFILE)
flag.BoolVar(&vflag, "v", false, "write summary of scanner statistics to stderr")
flag.BoolVar(&nodfaopt, "nodfaopt", false, "disable DFA optimization - don't use this for production code")
//flag.BoolVar(&bits32, "32bit", false, "assume unicode rune lexer (partially implemented)")
flag.Parse()
if hflag || flag.NArg() > 1 {
flag.Usage()
fmt.Fprintf(stderr, "\n%s [-o out_name] [other_options] [in_name]\n", os.Args[0])
fmt.Fprintln(stderr, " If no in_name is given then read from stdin.")
stderr.Flush()
os.Exit(1)
}
var (
lfile *bufio.Reader // source .l
gofile *bufio.Writer // dest .go
)
lname := flag.Arg(0)
if lname == "" {
lfile = stdin
} else {
l, err := os.Open(lname)
if err != nil {
panic(err)
}
defer l.Close()
lfile = bufio.NewReader(l)
}
l, err := lex.NewL(lname, lfile, nodfaopt, bits32)
if err != nil {
panic(err)
}
if dfaflag {
fmt.Println(l.DfaString())
panic(nil)
}
if tflag {
gofile = stdout
} else {
if oflag == "" {
oflag = OFILE
}
g, err := os.Create(oflag)
if err != nil {
panic(err)
}
defer g.Close()
gofile = bufio.NewWriter(g)
}
defer gofile.Flush()
renderGo{noRender{gofile}, map[int]bool{}}.render(lname, l)
if vflag {
fmt.Fprintln(os.Stderr, l.String())
}
}