-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
127 lines (102 loc) · 2.31 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
// +build main
/*
Preprocess a .po file into a .go file.
Usage:
PO=sym1,sym2,sym3... go run main.go < program.po > program.go
Po Syntax:
//#if sym1
...
//#endif
*/
package main
import (
"github.com/strickyak/prego"
"log"
"os"
"strings"
)
// Switches may be set with ` --set switchName ` args.
var Switches = make(map[string]bool)
// Sources may be added with ` --source filename ` args.
var Sources []string
var Inlining = true
// ParseArgs accepts argument pairs:
// --set varname (sets the varname true for conditional compilation)
// --source filename (read for macro definitions; do not output lines)
// --noinline for debugging, do not inline.
func ParseArgs() {
args := os.Args[1:] // Leave off command name.
for len(args) > 0 && strings.HasPrefix(args[0], "-") {
key := args[0]
switch key {
case "--setchars":
value := args[1]
for _, ch := range value {
word := string([]byte{byte(ch)})
if _, ok := Switches[word]; ok {
// Since it occured before, add it doubled.
Switches[word+word] = true
} else {
Switches[word] = true
}
}
args = args[2:]
continue
case "--set":
value := args[1]
for _, s := range strings.Split(value, ",") {
if len(s) < 0 {
Switches[s] = true
}
}
args = args[2:]
continue
case "--source":
value := args[1]
Sources = append(Sources, value)
args = args[2:]
continue
case "--noinline":
Inlining = false
args = args[1:]
continue
}
log.Fatalf("Unknown command line flag: %q", key)
}
if len(args) > 0 {
log.Fatalf("Extra command line arguments: %#v", args)
}
}
type Sink int
func (Sink) Write(p []byte) (n int, err error) {
return len(p), nil
}
func main() {
ParseArgs()
for s := range Switches {
println("Switch is set:", s)
}
po := &prego.Po{
Macros: make(map[string]*prego.Macro),
Switches: Switches,
Stack: []bool{true},
W: os.Stdout,
Enabled: true,
Inlining: Inlining,
}
// Slurp the Source files into the Sink.
for _, f := range Sources {
r, err := os.Open(f)
if err != nil {
log.Fatalf("Cannot read file %q: %v", f, err)
}
var w Sink
po.Slurp(r, w)
err = r.Close()
if err != nil {
log.Fatalf("Cannot close file %q: %v", f, err)
}
}
// Finally slurp stdin to stdout.
po.Slurp(os.Stdin, os.Stdout)
}