-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
90 lines (76 loc) · 2.05 KB
/
global.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
// Copyright 2016 The CC 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 cc
import (
"reflect"
"strconv"
"strings"
"github.com/cznic/golex/lex"
"github.com/cznic/strutil"
"github.com/cznic/xc"
)
const (
commentNotClosed = "comment not closed"
)
var (
debugIncludes bool
debugMacros bool
dict = xc.Dict
fset = xc.FileSet
isGenerating bool // go generate hook.
isTesting bool // Test hook.
printHooks = strutil.PrettyPrintHooks{}
)
func init() {
for k, v := range xc.PrintHooks {
printHooks[k] = v
}
lcRT := reflect.TypeOf(lex.Char{})
lcH := func(f strutil.Formatter, v interface{}, prefix, suffix string) {
c := v.(lex.Char)
r := c.Rune
s := yySymName(int(r))
if x := s[0]; x >= '0' && x <= '9' {
s = strconv.QuoteRune(r)
}
f.Format("%s%v: %s"+suffix, prefix, fset.Position(c.Pos()), s)
}
printHooks[lcRT] = lcH
printHooks[reflect.TypeOf(xc.Token{})] = func(f strutil.Formatter, v interface{}, prefix, suffix string) {
t := v.(xc.Token)
if (t == xc.Token{}) {
return
}
lcH(f, t.Char, prefix, "")
if s := t.S(); len(s) != 0 {
f.Format(" %q", s)
}
f.Format(suffix)
}
printHooks[reflect.TypeOf(PPTokenList(0))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) {
x := v.(PPTokenList)
if x == 0 {
return
}
a := strings.Split(prefix+PrettyString(decodeTokens(x, nil, true))+",", "\n")
for _, v := range a {
f.Format("%s\n", v)
}
}
printHooks[reflect.TypeOf((*ctype)(nil))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) {
f.Format(prefix)
f.Format("%v", v.(Type).String())
f.Format(suffix)
}
printHooks[reflect.TypeOf(Kind(0))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) {
f.Format(prefix)
f.Format("%v", v.(Kind))
f.Format(suffix)
}
printHooks[reflect.TypeOf(Linkage(0))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) {
f.Format(prefix)
f.Format("%v", v.(Linkage))
f.Format(suffix)
}
}