Skip to content

Commit

Permalink
Move AST to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
hausdorff authored and sparkprime committed Aug 25, 2017
1 parent c610dec commit ad56a07
Show file tree
Hide file tree
Showing 24 changed files with 609 additions and 561 deletions.
84 changes: 49 additions & 35 deletions ast.go → ast/ast.go
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package jsonnet
package ast

import (
"fmt"
Expand All @@ -33,26 +33,40 @@ type Identifiers []Identifier
type Node interface {
Loc() *LocationRange
FreeVariables() Identifiers
setFreeVariables(Identifiers)
SetFreeVariables(Identifiers)
}
type Nodes []Node

// ---------------------------------------------------------------------------

type nodeBase struct {
type NodeBase struct {
loc LocationRange
freeVariables Identifiers
}

func (n *nodeBase) Loc() *LocationRange {
func NewNodeBase(loc LocationRange, freeVariables Identifiers) NodeBase {
return NodeBase{
loc: loc,
freeVariables: freeVariables,
}
}

func NewNodeBaseLoc(loc LocationRange) NodeBase {
return NodeBase{
loc: loc,
freeVariables: []Identifier{},
}
}

func (n *NodeBase) Loc() *LocationRange {
return &n.loc
}

func (n *nodeBase) FreeVariables() Identifiers {
func (n *NodeBase) FreeVariables() Identifiers {
return n.freeVariables
}

func (n *nodeBase) setFreeVariables(idents Identifiers) {
func (n *NodeBase) SetFreeVariables(idents Identifiers) {
n.freeVariables = idents
}

Expand All @@ -79,7 +93,7 @@ type CompSpecs []CompSpec

// Apply represents a function call
type Apply struct {
nodeBase
NodeBase
Target Node
Arguments Nodes
TrailingComma bool
Expand All @@ -91,7 +105,7 @@ type Apply struct {

// ApplyBrace represents e { }. Desugared to e + { }.
type ApplyBrace struct {
nodeBase
NodeBase
Left Node
Right Node
}
Expand All @@ -100,7 +114,7 @@ type ApplyBrace struct {

// Array represents array constructors [1, 2, 3].
type Array struct {
nodeBase
NodeBase
Elements Nodes
TrailingComma bool
}
Expand All @@ -110,7 +124,7 @@ type Array struct {
// ArrayComp represents array comprehensions (which are like Python list
// comprehensions)
type ArrayComp struct {
nodeBase
NodeBase
Body Node
TrailingComma bool
Specs CompSpecs
Expand All @@ -123,7 +137,7 @@ type ArrayComp struct {
// After parsing, message can be nil indicating that no message was
// specified. This AST is elimiated by desugaring.
type Assert struct {
nodeBase
NodeBase
Cond Node
Message Node
Rest Node
Expand Down Expand Up @@ -187,7 +201,7 @@ var bopStrings = []string{
BopOr: "||",
}

var bopMap = map[string]BinaryOp{
var BopMap = map[string]BinaryOp{
"*": BopMult,
"/": BopDiv,
"%": BopPercent,
Expand Down Expand Up @@ -223,7 +237,7 @@ func (b BinaryOp) String() string {

// Binary represents binary operators.
type Binary struct {
nodeBase
NodeBase
Left Node
Op BinaryOp
Right Node
Expand All @@ -236,7 +250,7 @@ type Binary struct {
// After parsing, branchFalse can be nil indicating that no else branch
// was specified. The desugarer fills this in with a LiteralNull
type Conditional struct {
nodeBase
NodeBase
Cond Node
BranchTrue Node
BranchFalse Node
Expand All @@ -245,21 +259,21 @@ type Conditional struct {
// ---------------------------------------------------------------------------

// Dollar represents the $ keyword
type Dollar struct{ nodeBase }
type Dollar struct{ NodeBase }

// ---------------------------------------------------------------------------

// Error represents the error e.
type Error struct {
nodeBase
NodeBase
Expr Node
}

// ---------------------------------------------------------------------------

// Function represents a function definition
type Function struct {
nodeBase
NodeBase
Parameters Identifiers // TODO(sbarzowski) support default arguments
TrailingComma bool
Body Node
Expand All @@ -269,15 +283,15 @@ type Function struct {

// Import represents import "file".
type Import struct {
nodeBase
NodeBase
File string
}

// ---------------------------------------------------------------------------

// ImportStr represents importstr "file".
type ImportStr struct {
nodeBase
NodeBase
File string
}

Expand All @@ -288,14 +302,14 @@ type ImportStr struct {
// One of index and id will be nil before desugaring. After desugaring id
// will be nil.
type Index struct {
nodeBase
NodeBase
Target Node
Index Node
Id *Identifier
}

type Slice struct {
nodeBase
NodeBase
Target Node

// Each of these can be nil
Expand All @@ -318,7 +332,7 @@ type LocalBinds []LocalBind

// Local represents local x = e; e. After desugaring, functionSugar is false.
type Local struct {
nodeBase
NodeBase
Binds LocalBinds
Body Node
}
Expand All @@ -327,20 +341,20 @@ type Local struct {

// LiteralBoolean represents true and false
type LiteralBoolean struct {
nodeBase
NodeBase
Value bool
}

// ---------------------------------------------------------------------------

// LiteralNull represents the null keyword
type LiteralNull struct{ nodeBase }
type LiteralNull struct{ NodeBase }

// ---------------------------------------------------------------------------

// LiteralNumber represents a JSON number
type LiteralNumber struct {
nodeBase
NodeBase
Value float64
OriginalString string
}
Expand All @@ -360,7 +374,7 @@ const (

// LiteralString represents a JSON string
type LiteralString struct {
nodeBase
NodeBase
Value string
Kind LiteralStringKind
BlockIndent string
Expand Down Expand Up @@ -418,7 +432,7 @@ type ObjectFields []ObjectField
// The trailing comma is only allowed if len(fields) > 0. Converted to
// DesugaredObject during desugaring.
type Object struct {
nodeBase
NodeBase
Fields ObjectFields
TrailingComma bool
}
Expand All @@ -437,7 +451,7 @@ type DesugaredObjectFields []DesugaredObjectField
//
// The assertions either return true or raise an error.
type DesugaredObject struct {
nodeBase
NodeBase
Asserts Nodes
Fields DesugaredObjectFields
}
Expand All @@ -447,7 +461,7 @@ type DesugaredObject struct {
// ObjectComp represents object comprehension
// { [e]: e for x in e for.. if... }.
type ObjectComp struct {
nodeBase
NodeBase
Fields ObjectFields
TrailingComma bool
Specs CompSpecs
Expand All @@ -458,7 +472,7 @@ type ObjectComp struct {
// ObjectComprehensionSimple represents post-desugaring object
// comprehension { [e]: e for x in e }.
type ObjectComprehensionSimple struct {
nodeBase
NodeBase
Field Node
Value Node
Id Identifier
Expand All @@ -468,7 +482,7 @@ type ObjectComprehensionSimple struct {
// ---------------------------------------------------------------------------

// Self represents the self keyword.
type Self struct{ nodeBase }
type Self struct{ NodeBase }

// ---------------------------------------------------------------------------

Expand All @@ -477,7 +491,7 @@ type Self struct{ nodeBase }
// Either index or identifier will be set before desugaring. After desugaring, id will be
// nil.
type SuperIndex struct {
nodeBase
NodeBase
Index Node
Id *Identifier
}
Expand All @@ -500,7 +514,7 @@ var uopStrings = []string{
UopMinus: "-",
}

var uopMap = map[string]UnaryOp{
var UopMap = map[string]UnaryOp{
"!": UopNot,
"~": UopBitwiseNot,
"+": UopPlus,
Expand All @@ -516,7 +530,7 @@ func (u UnaryOp) String() string {

// Unary represents unary operators.
type Unary struct {
nodeBase
NodeBase
Op UnaryOp
Expr Node
}
Expand All @@ -525,7 +539,7 @@ type Unary struct {

// Var represents variables.
type Var struct {
nodeBase
NodeBase
Id Identifier
}

Expand Down
2 changes: 1 addition & 1 deletion astcompkind_stringer.go → ast/compkind_stringer.go
Expand Up @@ -2,7 +2,7 @@
// TypeWriter: stringer
// Directive: +gen on astCompKind

package jsonnet
package ast

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion identifier_set.go → ast/identifier_set.go
Expand Up @@ -2,7 +2,7 @@
// TypeWriter: set
// Directive: +gen on identifier

package jsonnet
package ast

// Set is a modification of https://github.com/deckarep/golang-set
// The MIT License (MIT)
Expand Down
Expand Up @@ -2,7 +2,7 @@
// TypeWriter: stringer
// Directive: +gen on astLiteralStringKind

package jsonnet
package ast

import (
"fmt"
Expand Down
6 changes: 3 additions & 3 deletions location.go → ast/location.go
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package jsonnet
package ast

import "fmt"

Expand Down Expand Up @@ -71,10 +71,10 @@ func (lr *LocationRange) String() string {
}

// This is useful for special locations, e.g. manifestation entry point.
func makeLocationRangeMessage(msg string) LocationRange {
func MakeLocationRangeMessage(msg string) LocationRange {
return LocationRange{FileName: msg}
}

func makeLocationRange(fn string, begin Location, end Location) LocationRange {
func MakeLocationRange(fn string, begin Location, end Location) LocationRange {
return LocationRange{FileName: fn, Begin: begin, End: end}
}
Expand Up @@ -2,7 +2,7 @@
// TypeWriter: stringer
// Directive: +gen on astObjectFieldHide

package jsonnet
package ast

import (
"fmt"
Expand Down
Expand Up @@ -2,7 +2,7 @@
// TypeWriter: stringer
// Directive: +gen on astObjectFieldKind

package jsonnet
package ast

import (
"fmt"
Expand Down
7 changes: 7 additions & 0 deletions ast/util.go
@@ -0,0 +1,7 @@
package ast

func (i *IdentifierSet) Append(idents Identifiers) {
for _, ident := range idents {
i.Add(ident)
}
}

0 comments on commit ad56a07

Please sign in to comment.