3 // ASTKind represents different states in the parse table
4 // and the type of AST that is being constructed
7 // ASTKind* is used in the parse table to transition between
8 // the different states
10 ASTKindNone = ASTKind(iota)
17 ASTKindSectionStatement
18 ASTKindNestedSectionStatement
19 ASTKindCompletedNestedSectionStatement
20 ASTKindCommentStatement
21 ASTKindCompletedSectionStatement
24 func (k ASTKind) String() string {
32 case ASTKindStatement:
34 case ASTKindSectionStatement:
36 case ASTKindExprStatement:
38 case ASTKindCommentStatement:
40 case ASTKindNestedSectionStatement:
41 return "nested_section_stmt"
42 case ASTKindCompletedSectionStatement:
43 return "completed_stmt"
44 case ASTKindSkipStatement:
51 // AST interface allows us to determine what kind of node we
52 // are on and casting may not need to be necessary.
54 // The root is always the first node in Children
62 func newAST(kind ASTKind, root AST, children ...AST) AST {
65 Children: append([]AST{root}, children...),
69 func newASTWithRootToken(kind ASTKind, root Token, children ...AST) AST {
78 // AppendChild will append to the list of children an AST has.
79 func (a *AST) AppendChild(child AST) {
80 a.Children = append(a.Children, child)
83 // GetRoot will return the root AST which can be the first entry
84 // in the children list or a token.
85 func (a *AST) GetRoot() AST {
90 if len(a.Children) == 0 {
97 // GetChildren will return the current AST's list of children
98 func (a *AST) GetChildren() []AST {
99 if len(a.Children) == 0 {
107 return a.Children[1:]
110 // SetChildren will set and override all children of the AST.
111 func (a *AST) SetChildren(children []AST) {
113 a.Children = children
115 a.Children = append(a.Children[:1], children...)
119 // Start is used to indicate the starting state of the parse table.
120 var Start = newAST(ASTKindStart, AST{})