Skip to content

Commit 2b23986

Browse files
committed
Merge pull request google#7 from jbeda/parser
Finish up blocking out the parser.
2 parents 3b5fc97 + 142b77c commit 2b23986

13 files changed

+1249
-20
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@
1212
This is a port of [jsonnet](http://jsonnet.org/) to go. It is very much a work in progress.
1313

1414
This implementation is largely based on the the [jsonnet C++ implementation](https://github.com/google/jsonnet).
15+
16+
## Implementation Notes
17+
18+
We are generating some helper classes on types by using http://clipperhouse.github.io/gen/. Do the following to regenerate these if necessary:
19+
20+
```
21+
go get github.com/clipperhouse/gen
22+
go get github.com/clipperhouse/set
23+
go generate
24+
```

_gen.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import (
4+
_ "github.com/clipperhouse/set"
5+
_ "github.com/clipperhouse/slice"
6+
_ "github.com/clipperhouse/stringer"
7+
)

ast.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import (
5353
// )
5454

5555
// identifier represents a variable / parameter / field name.
56+
57+
//+gen set
5658
type identifier string
5759
type identifiers []identifier
5860

@@ -78,17 +80,18 @@ func (n *astNodeBase) Loc() *LocationRange {
7880

7981
// ---------------------------------------------------------------------------
8082

83+
// +gen stringer
8184
type astCompKind int
8285

8386
const (
84-
astCompFor = iota
87+
astCompFor astCompKind = iota
8588
astCompIf
8689
)
8790

8891
type astCompSpec struct {
8992
kind astCompKind
90-
varName identifier // nil when kind != compSpecFor
91-
expr *astNode
93+
varName *identifier // nil when kind != compSpecFor
94+
expr astNode
9295
}
9396
type astCompSpecs []astCompSpec
9497

@@ -203,9 +206,36 @@ var bopStrings = []string{
203206
bopOr: "||",
204207
}
205208

209+
var bopMap = map[string]binaryOp{
210+
"*": bopMult,
211+
"/": bopDiv,
212+
"%": bopPercent,
213+
214+
"+": bopPlus,
215+
"-": bopMinus,
216+
217+
"<<": bopShiftL,
218+
">>": bopShiftR,
219+
220+
">": bopGreater,
221+
">=": bopGreaterEq,
222+
"<": bopLess,
223+
"<=": bopLessEq,
224+
225+
"==": bopManifestEqual,
226+
"!=": bopManifestUnequal,
227+
228+
"&": bopBitwiseAnd,
229+
"^": bopBitwiseXor,
230+
"|": bopBitwiseOr,
231+
232+
"&&": bopAnd,
233+
"||": bopOr,
234+
}
235+
206236
func (b binaryOp) String() string {
207237
if b < 0 || int(b) >= len(bopStrings) {
208-
panic(fmt.Sprintf("INTERNAL ERROR: Unrecognised binary operator: %v", b))
238+
panic(fmt.Sprintf("INTERNAL ERROR: Unrecognised binary operator: %d", b))
209239
}
210240
return bopStrings[b]
211241
}
@@ -299,11 +329,11 @@ type astIndex struct {
299329

300330
// astLocalBind is a helper struct for astLocal
301331
type astLocalBind struct {
302-
variable identifier
303-
body astNode
304-
functionSugar bool
305-
params identifiers // if functionSugar is true
306-
trailingComman bool
332+
variable identifier
333+
body astNode
334+
functionSugar bool
335+
params identifiers // if functionSugar is true
336+
trailingComma bool
307337
}
308338
type astLocalBinds []astLocalBind
309339

@@ -332,11 +362,13 @@ type astLiteralNull struct{ astNodeBase }
332362
// astLiteralNumber represents a JSON number
333363
type astLiteralNumber struct {
334364
astNodeBase
335-
value float64
365+
value float64
366+
originalString string
336367
}
337368

338369
// ---------------------------------------------------------------------------
339370

371+
// +gen stringer
340372
type astLiteralStringKind int
341373

342374
const (
@@ -355,6 +387,7 @@ type astLiteralString struct {
355387

356388
// ---------------------------------------------------------------------------
357389

390+
// +gen stringer
358391
type astObjectFieldKind int
359392

360393
const (
@@ -365,6 +398,7 @@ const (
365398
astObjectLocal // local id = expr2
366399
)
367400

401+
// +gen stringer
368402
type astObjectFieldHide int
369403

370404
const (
@@ -379,7 +413,7 @@ type astObjectField struct {
379413
superSugar bool // +: (ignore if kind != astObjectField*)
380414
methodSugar bool // f(x, y, z): ... (ignore if kind == astObjectAssert)
381415
expr1 astNode // Not in scope of the object
382-
id identifier
416+
id *identifier
383417
ids identifiers // If methodSugar == true then holds the params.
384418
trailingComma bool // If methodSugar == true then remembers the trailing comma
385419
expr2, expr3 astNode // In scope of the object (can see self).
@@ -453,6 +487,7 @@ type astSelf struct{ astNodeBase }
453487
// Either index or identifier will be set before desugaring. After desugaring, id will be
454488
// nil.
455489
type astSuperIndex struct {
490+
astNodeBase
456491
index astNode
457492
id *identifier
458493
}
@@ -475,9 +510,16 @@ var uopStrings = []string{
475510
uopMinus: "-",
476511
}
477512

513+
var uopMap = map[string]unaryOp{
514+
"!": uopNot,
515+
"~": uopBitwiseNot,
516+
"+": uopPlus,
517+
"-": uopMinus,
518+
}
519+
478520
func (u unaryOp) String() string {
479521
if u < 0 || int(u) >= len(uopStrings) {
480-
panic(fmt.Sprintf("INTERNAL ERROR: Unrecognised unary operator: %v", u))
522+
panic(fmt.Sprintf("INTERNAL ERROR: Unrecognised unary operator: %d", u))
481523
}
482524
return uopStrings[u]
483525
}

astcompkind_stringer.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Generated by: main
2+
// TypeWriter: stringer
3+
// Directive: +gen on astCompKind
4+
5+
package jsonnet
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
const _astCompKind_name = "astCompForastCompIf"
12+
13+
var _astCompKind_index = [...]uint8{0, 10, 19}
14+
15+
func (i astCompKind) String() string {
16+
if i < 0 || i+1 >= astCompKind(len(_astCompKind_index)) {
17+
return fmt.Sprintf("astCompKind(%d)", i)
18+
}
19+
return _astCompKind_name[_astCompKind_index[i]:_astCompKind_index[i+1]]
20+
}

astliteralstringkind_stringer.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Generated by: main
2+
// TypeWriter: stringer
3+
// Directive: +gen on astLiteralStringKind
4+
5+
package jsonnet
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
const _astLiteralStringKind_name = "astStringSingleastStringDoubleastStringBlock"
12+
13+
var _astLiteralStringKind_index = [...]uint8{0, 15, 30, 44}
14+
15+
func (i astLiteralStringKind) String() string {
16+
if i < 0 || i+1 >= astLiteralStringKind(len(_astLiteralStringKind_index)) {
17+
return fmt.Sprintf("astLiteralStringKind(%d)", i)
18+
}
19+
return _astLiteralStringKind_name[_astLiteralStringKind_index[i]:_astLiteralStringKind_index[i+1]]
20+
}

astobjectfieldhide_stringer.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Generated by: main
2+
// TypeWriter: stringer
3+
// Directive: +gen on astObjectFieldHide
4+
5+
package jsonnet
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
const _astObjectFieldHide_name = "astObjectFieldHiddenastObjectFieldInheritastObjectFieldVisible"
12+
13+
var _astObjectFieldHide_index = [...]uint8{0, 20, 41, 62}
14+
15+
func (i astObjectFieldHide) String() string {
16+
if i < 0 || i+1 >= astObjectFieldHide(len(_astObjectFieldHide_index)) {
17+
return fmt.Sprintf("astObjectFieldHide(%d)", i)
18+
}
19+
return _astObjectFieldHide_name[_astObjectFieldHide_index[i]:_astObjectFieldHide_index[i+1]]
20+
}

astobjectfieldkind_stringer.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Generated by: main
2+
// TypeWriter: stringer
3+
// Directive: +gen on astObjectFieldKind
4+
5+
package jsonnet
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
const _astObjectFieldKind_name = "astObjectAssertastObjectFieldIDastObjectFieldExprastObjectFieldStrastObjectLocal"
12+
13+
var _astObjectFieldKind_index = [...]uint8{0, 15, 31, 49, 66, 80}
14+
15+
func (i astObjectFieldKind) String() string {
16+
if i < 0 || i+1 >= astObjectFieldKind(len(_astObjectFieldKind_index)) {
17+
return fmt.Sprintf("astObjectFieldKind(%d)", i)
18+
}
19+
return _astObjectFieldKind_name[_astObjectFieldKind_index[i]:_astObjectFieldKind_index[i+1]]
20+
}

doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ text.
2525
See http://jsonnet.org/ for a full language description and tutorial.
2626
*/
2727
package jsonnet
28+
29+
//go:generate gen

0 commit comments

Comments
 (0)