-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.go
224 lines (182 loc) · 5.28 KB
/
compile.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (C) 2016 Kohei YOSHIDA. All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of The BSD 3-Clause License
// that can be found in the LICENSE file.
package uritemplate
import (
"fmt"
"unicode/utf8"
)
type compiler struct {
prog *prog
}
func (c *compiler) init() {
c.prog = &prog{}
}
func (c *compiler) op(opcode progOpcode) uint32 {
i := len(c.prog.op)
c.prog.op = append(c.prog.op, progOp{code: opcode})
return uint32(i)
}
func (c *compiler) opWithRune(opcode progOpcode, r rune) uint32 {
addr := c.op(opcode)
(&c.prog.op[addr]).r = r
return addr
}
func (c *compiler) opWithRuneClass(opcode progOpcode, rc runeClass) uint32 {
addr := c.op(opcode)
(&c.prog.op[addr]).rc = rc
return addr
}
func (c *compiler) opWithAddr(opcode progOpcode, absaddr uint32) uint32 {
addr := c.op(opcode)
(&c.prog.op[addr]).i = absaddr
return addr
}
func (c *compiler) opWithAddrDelta(opcode progOpcode, delta uint32) uint32 {
return c.opWithAddr(opcode, uint32(len(c.prog.op))+delta)
}
func (c *compiler) opWithName(opcode progOpcode, name string) uint32 {
addr := c.op(opcode)
(&c.prog.op[addr]).name = name
return addr
}
func (c *compiler) compileString(str string) {
for i := 0; i < len(str); {
// NOTE(yosida95): It is confirmed at parse time that literals
// consist of only valid-UTF8 runes.
r, size := utf8.DecodeRuneInString(str[i:])
c.opWithRune(opRune, r)
i += size
}
}
func (c *compiler) compileRuneClass(rc runeClass, maxlen int) {
for i := 0; i < maxlen; i++ {
if i > 0 {
c.opWithAddrDelta(opSplit, 7)
}
c.opWithAddrDelta(opSplit, 3) // raw rune or pct-encoded
c.opWithRuneClass(opRuneClass, rc) // raw rune
c.opWithAddrDelta(opJmp, 4) //
c.opWithRune(opRune, '%') // pct-encoded
c.opWithRuneClass(opRuneClass, runeClassPctE) //
c.opWithRuneClass(opRuneClass, runeClassPctE) //
}
}
func (c *compiler) compileRuneClassInfinite(rc runeClass) {
start := c.opWithAddrDelta(opSplit, 3) // raw rune or pct-encoded
c.opWithRuneClass(opRuneClass, rc) // raw rune
c.opWithAddrDelta(opJmp, 4) //
c.opWithRune(opRune, '%') // pct-encoded
c.opWithRuneClass(opRuneClass, runeClassPctE) //
c.opWithRuneClass(opRuneClass, runeClassPctE) //
c.opWithAddrDelta(opSplit, 2) // loop
c.opWithAddr(opJmp, start) //
}
func (c *compiler) compileVarspecValue(spec varspec, expr *expression) {
var specname string
if spec.maxlen > 0 {
specname = fmt.Sprintf("%s:%d", spec.name, spec.maxlen)
} else {
specname = spec.name
}
c.prog.numCap++
c.opWithName(opCapStart, specname)
split := c.op(opSplit)
if spec.maxlen > 0 {
c.compileRuneClass(expr.allow, spec.maxlen)
} else {
c.compileRuneClassInfinite(expr.allow)
}
capEnd := c.opWithName(opCapEnd, specname)
c.prog.op[split].i = capEnd
}
func (c *compiler) compileVarspec(spec varspec, expr *expression) {
switch {
case expr.named && spec.explode:
split1 := c.op(opSplit)
noop := c.op(opNoop)
c.compileString(spec.name)
split2 := c.op(opSplit)
c.opWithRune(opRune, '=')
c.compileVarspecValue(spec, expr)
split3 := c.op(opSplit)
c.compileString(expr.sep)
c.opWithAddr(opJmp, noop)
c.prog.op[split2].i = uint32(len(c.prog.op))
c.compileString(expr.ifemp)
c.opWithAddr(opJmp, split3)
c.prog.op[split1].i = uint32(len(c.prog.op))
c.prog.op[split3].i = uint32(len(c.prog.op))
case expr.named && !spec.explode:
c.compileString(spec.name)
split2 := c.op(opSplit)
c.opWithRune(opRune, '=')
split3 := c.op(opSplit)
split4 := c.op(opSplit)
c.compileVarspecValue(spec, expr)
split5 := c.op(opSplit)
c.prog.op[split4].i = split5
c.compileString(",")
c.opWithAddr(opJmp, split4)
c.prog.op[split3].i = uint32(len(c.prog.op))
c.compileString(",")
jmp1 := c.op(opJmp)
c.prog.op[split2].i = uint32(len(c.prog.op))
c.compileString(expr.ifemp)
c.prog.op[split5].i = uint32(len(c.prog.op))
c.prog.op[jmp1].i = uint32(len(c.prog.op))
case !expr.named:
start := uint32(len(c.prog.op))
c.compileVarspecValue(spec, expr)
split1 := c.op(opSplit)
jmp := c.op(opJmp)
c.prog.op[split1].i = uint32(len(c.prog.op))
if spec.explode {
c.compileString(expr.sep)
} else {
c.opWithRune(opRune, ',')
}
c.opWithAddr(opJmp, start)
c.prog.op[jmp].i = uint32(len(c.prog.op))
}
}
func (c *compiler) compileExpression(expr *expression) {
if len(expr.vars) < 1 {
return
}
split1 := c.op(opSplit)
c.compileString(expr.first)
for i, size := 0, len(expr.vars); i < size; i++ {
spec := expr.vars[i]
split2 := c.op(opSplit)
if i > 0 {
split3 := c.op(opSplit)
c.compileString(expr.sep)
c.prog.op[split3].i = uint32(len(c.prog.op))
}
c.compileVarspec(spec, expr)
c.prog.op[split2].i = uint32(len(c.prog.op))
}
c.prog.op[split1].i = uint32(len(c.prog.op))
}
func (c *compiler) compileLiterals(lt literals) {
c.compileString(string(lt))
}
func (c *compiler) compile(tmpl *Template) {
c.op(opLineBegin)
for i := range tmpl.exprs {
expr := tmpl.exprs[i]
switch expr := expr.(type) {
default:
panic("unhandled expression")
case *expression:
c.compileExpression(expr)
case literals:
c.compileLiterals(expr)
}
}
c.op(opLineEnd)
c.op(opEnd)
}