Browse Source

shuffle a few things around

master
Brett Langdon 10 years ago
parent
commit
cb215f52d0
5 changed files with 67 additions and 46 deletions
  1. +2
    -0
      ast/nodes.go
  2. +0
    -38
      ast/smallstatements.go
  3. +6
    -1
      ast/start.go
  4. +46
    -2
      ast/statements.go
  5. +13
    -5
      parser/parser.go

+ 2
- 0
ast/nodes.go View File

@ -21,7 +21,9 @@ func (node *TokenNode) atomExpressionChild() {}
func (node *TokenNode) comparisonChild() {}
func (node *TokenNode) expressionStatementChild() {}
func (node *TokenNode) factorChild() {}
func (node *TokenNode) fileInputChild() {}
func (node *TokenNode) shiftExpressionChild() {}
func (node *TokenNode) simpleStatementChild() {}
func (node *TokenNode) trailerChild() {}
func (node *TokenNode) Name() string { return token.TokenNames[node.Token.ID] }
func (node *TokenNode) Repr() []interface{} {


+ 0
- 38
ast/smallstatements.go View File

@ -1,38 +0,0 @@
package ast
type SmallStatementChildNode interface {
Node
smallStmtChild()
}
type SmallStatement struct {
ParentNode
}
func NewSmallStatement() *SmallStatement {
node := &SmallStatement{}
node.initBaseNode(SMALL_STMT)
return node
}
func (node *SmallStatement) SetChild(n SmallStatementChildNode) { node.ParentNode.SetChild(n) }
type ExpressionStatementChildNode interface {
Node
expressionStatementChild()
}
type ExpressionStatement struct {
ListNode
Expression *TestlistStarExpression
}
func NewExpressionStatement() *ExpressionStatement {
node := &ExpressionStatement{}
node.initBaseNode(EXPR_STMT)
node.initListNode()
return node
}
func (node *ExpressionStatement) smallStmtChild() {}
func (node *ExpressionStatement) Append(n ExpressionStatementChildNode) { node.ListNode.Append(n) }

+ 6
- 1
ast/start.go View File

@ -1,5 +1,10 @@
package ast
type FileInputChildNode interface {
Node
fileInputChild()
}
type FileInput struct {
ListNode
}
@ -11,4 +16,4 @@ func NewFileInput() *FileInput {
return node
}
func (node *FileInput) Append(n *Statement) { node.ListNode.Append(n) }
func (node *FileInput) Append(n FileInputChildNode) { node.ListNode.Append(n) }

+ 46
- 2
ast/statements.go View File

@ -15,8 +15,14 @@ func NewStatement() *Statement {
return node
}
func (node *Statement) fileInputChild() {}
func (node *Statement) SetChild(n StatementChildNode) { node.ParentNode.SetChild(n) }
type SimpleStatementChildNode interface {
Node
simpleStatementChild()
}
type SimpleStatement struct {
ListNode
}
@ -28,8 +34,8 @@ func NewSimpleStatement() *SimpleStatement {
return node
}
func (node *SimpleStatement) stmtChildNode() {}
func (node *SimpleStatement) Append(n *SmallStatement) { node.ListNode.Append(n) }
func (node *SimpleStatement) stmtChildNode() {}
func (node *SimpleStatement) Append(n SimpleStatementChildNode) { node.ListNode.Append(n) }
type CompoundStatement struct {
BaseNode
@ -41,3 +47,41 @@ func NewCompoundStatement() *CompoundStatement {
return node
}
func (node *CompoundStatement) stmtChildNode() {}
type SmallStatementChildNode interface {
Node
smallStmtChild()
}
type SmallStatement struct {
ParentNode
}
func NewSmallStatement() *SmallStatement {
node := &SmallStatement{}
node.initBaseNode(SMALL_STMT)
return node
}
func (node *SmallStatement) simpleStatementChild() {}
func (node *SmallStatement) SetChild(n SmallStatementChildNode) { node.ParentNode.SetChild(n) }
type ExpressionStatementChildNode interface {
Node
expressionStatementChild()
}
type ExpressionStatement struct {
ListNode
Expression *TestlistStarExpression
}
func NewExpressionStatement() *ExpressionStatement {
node := &ExpressionStatement{}
node.initBaseNode(EXPR_STMT)
node.initListNode()
return node
}
func (node *ExpressionStatement) smallStmtChild() {}
func (node *ExpressionStatement) Append(n ExpressionStatementChildNode) { node.ListNode.Append(n) }

+ 13
- 5
parser/parser.go View File

@ -557,7 +557,12 @@ func (parser *Parser) parseSimpleStatement() *ast.SimpleStatement {
break
}
}
parser.expect(token.NEWLINE)
next := parser.nextToken()
if next.ID != token.NEWLINE {
parser.addError("Expected \"NEWLINE\" instead found \"" + next.ID.String() + "\"")
return nil
}
simpleStmt.Append(ast.NewTokenNode(next))
// no small statements found
if simpleStmt.Length() == 0 {
@ -589,8 +594,7 @@ func (parser *Parser) parseFileInput() *ast.FileInput {
for parser.tokenizer.State() == errorcode.E_OK {
next := parser.nextToken()
if next.ID == token.NEWLINE {
// root.Append(ast.NewTokenNode(next))
continue
root.Append(ast.NewTokenNode(next))
} else if next.ID == token.ENDMARKER {
// Unread, so we can read in the expected value later
parser.unreadToken(next)
@ -602,11 +606,15 @@ func (parser *Parser) parseFileInput() *ast.FileInput {
break
}
root.Append(stmt)
break
}
}
parser.expect(token.ENDMARKER)
next := parser.nextToken()
if next.ID != token.ENDMARKER {
parser.addError("Expected \"ENDMARKER\" instead received \"" + next.ID.String() + "\"")
return nil
}
root.Append(ast.NewTokenNode(next))
return root
}


Loading…
Cancel
Save