This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
150 lines (128 loc) · 2.97 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
146
147
148
149
150
// Copyright (c) 2014 The golex 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"
"bytes"
"flag"
"fmt"
"go/format"
"io"
"log"
"os"
"github.com/cznic/lex"
)
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 {
log.Fatal(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 "'\"'"
}
}
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
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 {
log.Fatal(err)
}
defer l.Close()
lfile = bufio.NewReader(l)
}
l, err := lex.NewL(lname, lfile, nodfaopt, bits32)
if err != nil {
log.Fatal(err)
}
if dfaflag {
fmt.Println(l.DfaString())
os.Exit(1)
}
if tflag {
gofile = stdout
} else {
if oflag == "" {
oflag = oFile
}
g, err := os.Create(oflag)
if err != nil {
log.Fatal(err)
}
defer g.Close()
gofile = bufio.NewWriter(g)
}
defer gofile.Flush()
var buf bytes.Buffer
renderGo{noRender{&buf}, map[int]bool{}}.render(lname, l)
dst, err := format.Source(buf.Bytes())
switch {
case err != nil:
fmt.Fprintf(os.Stderr, "%v\n", err)
if _, err := gofile.Write(buf.Bytes()); err != nil {
log.Fatal(err)
}
default:
if _, err := gofile.Write(dst); err != nil {
log.Fatal(err)
}
}
if vflag {
fmt.Fprintln(os.Stderr, l.String())
}
}