-
Notifications
You must be signed in to change notification settings - Fork 327
/
macroExpander.go
82 lines (69 loc) · 2.4 KB
/
macroExpander.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
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package preprocessor
import "github.com/google/gapid/gapis/api/gles/glsl/ast"
// Token hide sets. Used for preventing infinite recursion during macro expansion, as per the spec.
type hideSet map[string]struct{}
func (h hideSet) Clone() hideSet {
ret := make(hideSet, len(h)+1)
for k, v := range h {
ret[k] = v
}
return ret
}
func (a hideSet) AddAll(b hideSet) {
for k := range b {
a[k] = struct{}{}
}
}
func intersect(a hideSet, b hideSet) hideSet {
ret := make(hideSet)
for k := range a {
if _, present := b[k]; present {
ret[k] = struct{}{}
}
}
return ret
}
// tokenExpansion represents an token produced as a result of macro expansion.
// It stores the tokens hide set. Used internally in the preprocessor.
type tokenExpansion struct {
Info TokenInfo // The token.
HideSet hideSet // Which tokens should not be expanded when this token is processed.
}
func newTokenExpansion(info TokenInfo) tokenExpansion {
return tokenExpansion{Info: info, HideSet: make(hideSet, 0)}
}
// The function used for expanding tokens in macro definitions. The args argument is a list of
// macro arguments.
type macroExpander func(args [][]tokenExpansion) []tokenExpansion
// argumentExpander expands to the n-th macro argument.
type argumentExpander int
func (e argumentExpander) expand(args [][]tokenExpansion) []tokenExpansion {
ret := make([]tokenExpansion, len(args[e]))
for i, arg := range args[e] {
ret[i].Info = arg.Info
ret[i].HideSet = arg.HideSet.Clone()
}
return ret
}
// TokenInfo expands to itself.
func (e TokenInfo) expand([][]tokenExpansion) []tokenExpansion {
return []tokenExpansion{{e, make(hideSet, 1)}}
}
// __LINE__ expands to the current line number
func (p *preprocessorImpl) expandLine(args [][]tokenExpansion) []tokenExpansion {
ti := TokenInfo{Token: ast.IntValue(p.line)}
return ti.expand(args)
}