Browse Source

reorganize some things again

master
Brett Langdon 10 years ago
parent
commit
a6b94b0994
9 changed files with 270 additions and 271 deletions
  1. +1
    -1
      error/error.go
  2. +64
    -64
      grammar/expressions.go
  3. +82
    -0
      grammar/nodes.go
  4. +77
    -79
      grammar/parser.go
  5. +0
    -82
      grammar/rules.go
  6. +5
    -5
      grammar/start.go
  7. +20
    -20
      grammar/statements.go
  8. +16
    -16
      grammar/tests.go
  9. +5
    -4
      main.go

parser/error.go → error/error.go View File


+ 64
- 64
grammar/expressions.go View File

@ -3,251 +3,251 @@ package grammar
import "github.com/brettlangdon/gython/symbol"
type TestlistStarExpressionChild interface {
Rule
Node
testlistStarExpressionChild()
}
type TestlistStarExpression struct {
ParentRule
ParentNode
}
func NewTestListStarExpression() *TestlistStarExpression {
rule := &TestlistStarExpression{}
rule.initBaseRule(symbol.TESTLIST_STAR_EXPR)
rule.initBaseNode(symbol.TESTLIST_STAR_EXPR)
return rule
}
func (rule *TestlistStarExpression) expressionStatementChild() {}
func (rule *TestlistStarExpression) SetChild(n TestlistStarExpressionChild) {
rule.ParentRule.SetChild(n)
rule.ParentNode.SetChild(n)
}
type ComparisonChild interface {
Rule
Node
comparisonChild()
}
type Comparison struct {
ListRule
ListNode
}
func NewComparison() *Comparison {
rule := &Comparison{}
rule.initBaseRule(symbol.COMPARISON)
rule.initListRule()
rule.initBaseNode(symbol.COMPARISON)
rule.initListNode()
return rule
}
func (rule *Comparison) notTestChild() {}
func (rule *Comparison) Append(n ComparisonChild) { rule.ListRule.Append(n) }
func (rule *Comparison) Append(n ComparisonChild) { rule.ListNode.Append(n) }
type ExpressionChild interface {
Rule
Node
expressionChild()
}
type Expression struct {
ListRule
ListNode
}
func NewExpression() *Expression {
rule := &Expression{}
rule.initBaseRule(symbol.EXPR)
rule.initListRule()
rule.initBaseNode(symbol.EXPR)
rule.initListNode()
return rule
}
func (rule *Expression) comparisonChild() {}
func (rule *Expression) Append(n ExpressionChild) { rule.ListRule.Append(n) }
func (rule *Expression) Append(n ExpressionChild) { rule.ListNode.Append(n) }
type XorExpressionChild interface {
Rule
Node
xorExpressionChild()
}
type XorExpression struct {
ListRule
ListNode
}
func NewXorExpression() *XorExpression {
rule := &XorExpression{}
rule.initBaseRule(symbol.XOR_EXPR)
rule.initListRule()
rule.initBaseNode(symbol.XOR_EXPR)
rule.initListNode()
return rule
}
func (rule *XorExpression) expressionChild() {}
func (rule *XorExpression) Append(n XorExpressionChild) { rule.ListRule.Append(n) }
func (rule *XorExpression) Append(n XorExpressionChild) { rule.ListNode.Append(n) }
type AndExpressionChild interface {
Rule
Node
andExpressionChild()
}
type AndExpression struct {
ListRule
ListNode
}
func NewAndExpression() *AndExpression {
rule := &AndExpression{}
rule.initBaseRule(symbol.AND_EXPR)
rule.initListRule()
rule.initBaseNode(symbol.AND_EXPR)
rule.initListNode()
return rule
}
func (rule *AndExpression) xorExpressionChild() {}
func (rule *AndExpression) Append(n AndExpressionChild) { rule.ListRule.Append(n) }
func (rule *AndExpression) Append(n AndExpressionChild) { rule.ListNode.Append(n) }
type ShiftExpressionChild interface {
Rule
Node
shiftExpressionChild()
}
type ShiftExpression struct {
ListRule
ListNode
}
func NewShiftExpression() *ShiftExpression {
rule := &ShiftExpression{}
rule.initBaseRule(symbol.SHIFT_EXPR)
rule.initListRule()
rule.initBaseNode(symbol.SHIFT_EXPR)
rule.initListNode()
return rule
}
func (rule *ShiftExpression) andExpressionChild() {}
func (rule *ShiftExpression) Append(n ShiftExpressionChild) { rule.ListRule.Append(n) }
func (rule *ShiftExpression) Append(n ShiftExpressionChild) { rule.ListNode.Append(n) }
type ArithmeticExpressionChild interface {
Rule
Node
arithmeticExpressionChild()
}
type ArithmeticExpression struct {
ListRule
ListNode
}
func NewArithmeticExpression() *ArithmeticExpression {
rule := &ArithmeticExpression{}
rule.initBaseRule(symbol.ARITH_EXPR)
rule.initListRule()
rule.initBaseNode(symbol.ARITH_EXPR)
rule.initListNode()
return rule
}
func (rule *ArithmeticExpression) shiftExpressionChild() {}
func (rule *ArithmeticExpression) Append(n ArithmeticExpressionChild) { rule.ListRule.Append(n) }
func (rule *ArithmeticExpression) Append(n ArithmeticExpressionChild) { rule.ListNode.Append(n) }
type TermChild interface {
Rule
Node
termChild()
}
type Term struct {
ListRule
ListNode
}
func NewTerm() *Term {
rule := &Term{}
rule.initBaseRule(symbol.TERM)
rule.initListRule()
rule.initBaseNode(symbol.TERM)
rule.initListNode()
return rule
}
func (rule *Term) arithmeticExpressionChild() {}
func (rule *Term) Append(n TermChild) { rule.ListRule.Append(n) }
func (rule *Term) Append(n TermChild) { rule.ListNode.Append(n) }
type FactorChild interface {
Rule
Node
factorChild()
}
type Factor struct {
ListRule
ListNode
}
func NewFactor() *Factor {
rule := &Factor{}
rule.initBaseRule(symbol.FACTOR)
rule.initListRule()
rule.initBaseNode(symbol.FACTOR)
rule.initListNode()
return rule
}
func (rule *Factor) factorChild() {}
func (rule *Factor) powerChild() {}
func (rule *Factor) termChild() {}
func (rule *Factor) Append(n FactorChild) { rule.ListRule.Append(n) }
func (rule *Factor) Append(n FactorChild) { rule.ListNode.Append(n) }
type PowerChild interface {
Rule
Node
powerChild()
}
type Power struct {
ListRule
ListNode
}
func NewPower() *Power {
rule := &Power{}
rule.initBaseRule(symbol.POWER)
rule.initListRule()
rule.initBaseNode(symbol.POWER)
rule.initListNode()
return rule
}
func (rule *Power) factorChild() {}
func (rule *Power) Append(n PowerChild) { rule.ListRule.Append(n) }
func (rule *Power) Append(n PowerChild) { rule.ListNode.Append(n) }
type AtomExpressionChild interface {
Rule
Node
atomExpressionChild()
}
type AtomExpression struct {
ListRule
ListNode
}
func NewAtomExpression() *AtomExpression {
rule := &AtomExpression{}
rule.initBaseRule(symbol.ATOM_EXPR)
rule.initListRule()
rule.initBaseNode(symbol.ATOM_EXPR)
rule.initListNode()
return rule
}
func (rule *AtomExpression) powerChild() {}
func (rule *AtomExpression) Append(n AtomExpressionChild) { rule.ListRule.Append(n) }
func (rule *AtomExpression) Append(n AtomExpressionChild) { rule.ListNode.Append(n) }
type AtomChild interface {
Rule
Node
atomChild()
}
type Atom struct {
ListRule
ListNode
}
func NewAtom() *Atom {
rule := &Atom{}
rule.initBaseRule(symbol.ATOM)
rule.initListRule()
rule.initBaseNode(symbol.ATOM)
rule.initListNode()
return rule
}
func (rule *Atom) atomExpressionChild() {}
func (rule *Atom) Append(n AtomChild) { rule.ListRule.Append(n) }
func (rule *Atom) Append(n AtomChild) { rule.ListNode.Append(n) }
type TrailerChild interface {
Rule
Node
trailerChild()
}
type Trailer struct {
ListRule
ListNode
}
func NewTrailer() *Trailer {
rule := &Trailer{}
rule.initBaseRule(symbol.TRAILER)
rule.initListRule()
rule.initBaseNode(symbol.TRAILER)
rule.initListNode()
return rule
}
func (rule *Trailer) atomExpressionChild() {}
func (rule *Trailer) Append(n TrailerChild) { rule.ListRule.Append(n) }
func (rule *Trailer) Append(n TrailerChild) { rule.ListNode.Append(n) }

+ 82
- 0
grammar/nodes.go View File

@ -0,0 +1,82 @@
package grammar
import (
"fmt"
"github.com/brettlangdon/gython/symbol"
"github.com/brettlangdon/gython/token"
)
type Node interface {
Name() string
Repr() []interface{}
}
type TokenNode struct {
Token *token.Token
}
func NewTokenNode(tok *token.Token) *TokenNode {
return &TokenNode{
Token: tok,
}
}
func (rule *TokenNode) atomChild() {}
func (rule *TokenNode) atomExpressionChild() {}
func (rule *TokenNode) comparisonChild() {}
func (rule *TokenNode) expressionStatementChild() {}
func (rule *TokenNode) factorChild() {}
func (rule *TokenNode) fileInputChild() {}
func (rule *TokenNode) shiftExpressionChild() {}
func (rule *TokenNode) simpleStatementChild() {}
func (rule *TokenNode) trailerChild() {}
func (rule *TokenNode) Name() string { return token.TokenNames[rule.Token.ID] }
func (rule *TokenNode) Repr() []interface{} {
parts := make([]interface{}, 0)
parts = append(parts, rule.Name())
literal := fmt.Sprintf("%#v", rule.Token.Literal)
return append(parts, literal)
}
type BaseNode struct {
ID symbol.SymbolID
child Node
}
func (rule *BaseNode) initBaseNode(id symbol.SymbolID) { rule.ID = id }
func (rule *BaseNode) Name() string { return symbol.SymbolNames[rule.ID] }
func (rule *BaseNode) Repr() (parts []interface{}) { return append(parts, rule.Name()) }
type ParentNode struct {
BaseNode
child Node
}
func (rule *ParentNode) SetChild(n Node) { rule.child = n }
func (rule *ParentNode) Child() Node { return rule.child }
func (rule *ParentNode) Repr() (parts []interface{}) {
parts = rule.BaseNode.Repr()
child := rule.Child()
if child != nil {
parts = append(parts, child.Repr())
}
return parts
}
type ListNode struct {
BaseNode
children []Node
}
func (rule *ListNode) initListNode() { rule.children = make([]Node, 0) }
func (rule *ListNode) Length() int { return len(rule.children) }
func (rule *ListNode) Children() []Node { return rule.children }
func (rule *ListNode) Append(n Node) { rule.children = append(rule.children, n) }
func (rule *ListNode) Repr() (parts []interface{}) {
parts = rule.BaseNode.Repr()
children := rule.Children()
for _, child := range children {
parts = append(parts, child.Repr())
}
return parts
}

parser/grammar.go → grammar/parser.go View File


+ 0
- 82
grammar/rules.go View File

@ -1,82 +0,0 @@
package grammar
import (
"fmt"
"github.com/brettlangdon/gython/symbol"
"github.com/brettlangdon/gython/token"
)
type Rule interface {
Name() string
Repr() []interface{}
}
type TokenRule struct {
Token *token.Token
}
func NewTokenRule(tok *token.Token) *TokenRule {
return &TokenRule{
Token: tok,
}
}
func (rule *TokenRule) atomChild() {}
func (rule *TokenRule) atomExpressionChild() {}
func (rule *TokenRule) comparisonChild() {}
func (rule *TokenRule) expressionStatementChild() {}
func (rule *TokenRule) factorChild() {}
func (rule *TokenRule) fileInputChild() {}
func (rule *TokenRule) shiftExpressionChild() {}
func (rule *TokenRule) simpleStatementChild() {}
func (rule *TokenRule) trailerChild() {}
func (rule *TokenRule) Name() string { return token.TokenNames[rule.Token.ID] }
func (rule *TokenRule) Repr() []interface{} {
parts := make([]interface{}, 0)
parts = append(parts, rule.Name())
literal := fmt.Sprintf("%#v", rule.Token.Literal)
return append(parts, literal)
}
type BaseRule struct {
ID symbol.SymbolID
child Rule
}
func (rule *BaseRule) initBaseRule(id symbol.SymbolID) { rule.ID = id }
func (rule *BaseRule) Name() string { return symbol.SymbolNames[rule.ID] }
func (rule *BaseRule) Repr() (parts []interface{}) { return append(parts, rule.Name()) }
type ParentRule struct {
BaseRule
child Rule
}
func (rule *ParentRule) SetChild(n Rule) { rule.child = n }
func (rule *ParentRule) Child() Rule { return rule.child }
func (rule *ParentRule) Repr() (parts []interface{}) {
parts = rule.BaseRule.Repr()
child := rule.Child()
if child != nil {
parts = append(parts, child.Repr())
}
return parts
}
type ListRule struct {
BaseRule
children []Rule
}
func (rule *ListRule) initListRule() { rule.children = make([]Rule, 0) }
func (rule *ListRule) Length() int { return len(rule.children) }
func (rule *ListRule) Children() []Rule { return rule.children }
func (rule *ListRule) Append(n Rule) { rule.children = append(rule.children, n) }
func (rule *ListRule) Repr() (parts []interface{}) {
parts = rule.BaseRule.Repr()
children := rule.Children()
for _, child := range children {
parts = append(parts, child.Repr())
}
return parts
}

+ 5
- 5
grammar/start.go View File

@ -3,19 +3,19 @@ package grammar
import "github.com/brettlangdon/gython/symbol"
type FileInputChild interface {
Rule
Node
fileInputChild()
}
type FileInput struct {
ListRule
ListNode
}
func NewFileInput() *FileInput {
rule := &FileInput{}
rule.initBaseRule(symbol.FILE_INPUT)
rule.initListRule()
rule.initBaseNode(symbol.FILE_INPUT)
rule.initListNode()
return rule
}
func (rule *FileInput) Append(n FileInputChild) { rule.ListRule.Append(n) }
func (rule *FileInput) Append(n FileInputChild) { rule.ListNode.Append(n) }

+ 20
- 20
grammar/statements.go View File

@ -3,87 +3,87 @@ package grammar
import "github.com/brettlangdon/gython/symbol"
type StatementChild interface {
Rule
Node
stmtChild()
}
type Statement struct {
ParentRule
ParentNode
}
func NewStatement() *Statement {
rule := &Statement{}
rule.initBaseRule(symbol.STMT)
rule.initBaseNode(symbol.STMT)
return rule
}
func (rule *Statement) fileInputChild() {}
func (rule *Statement) SetChild(n StatementChild) { rule.ParentRule.SetChild(n) }
func (rule *Statement) SetChild(n StatementChild) { rule.ParentNode.SetChild(n) }
type SimpleStatementChild interface {
Rule
Node
simpleStatementChild()
}
type SimpleStatement struct {
ListRule
ListNode
}
func NewSimpleStatement() *SimpleStatement {
rule := &SimpleStatement{}
rule.initBaseRule(symbol.SIMPLE_STMT)
rule.initListRule()
rule.initBaseNode(symbol.SIMPLE_STMT)
rule.initListNode()
return rule
}
func (rule *SimpleStatement) stmtChild() {}
func (rule *SimpleStatement) Append(n SimpleStatementChild) { rule.ListRule.Append(n) }
func (rule *SimpleStatement) Append(n SimpleStatementChild) { rule.ListNode.Append(n) }
type CompoundStatement struct {
BaseRule
BaseNode
}
func NewCompoundStatement() *CompoundStatement {
rule := &CompoundStatement{}
rule.initBaseRule(symbol.COMPOUND_STMT)
rule.initBaseNode(symbol.COMPOUND_STMT)
return rule
}
func (rule *CompoundStatement) stmtChild() {}
type SmallStatementChild interface {
Rule
Node
smallStmtChild()
}
type SmallStatement struct {
ParentRule
ParentNode
}
func NewSmallStatement() *SmallStatement {
rule := &SmallStatement{}
rule.initBaseRule(symbol.SMALL_STMT)
rule.initBaseNode(symbol.SMALL_STMT)
return rule
}
func (rule *SmallStatement) simpleStatementChild() {}
func (rule *SmallStatement) SetChild(n SmallStatementChild) { rule.ParentRule.SetChild(n) }
func (rule *SmallStatement) SetChild(n SmallStatementChild) { rule.ParentNode.SetChild(n) }
type ExpressionStatementChild interface {
Rule
Node
expressionStatementChild()
}
type ExpressionStatement struct {
ListRule
ListNode
Expression *TestlistStarExpression
}
func NewExpressionStatement() *ExpressionStatement {
rule := &ExpressionStatement{}
rule.initBaseRule(symbol.EXPR_STMT)
rule.initListRule()
rule.initBaseNode(symbol.EXPR_STMT)
rule.initListNode()
return rule
}
func (rule *ExpressionStatement) smallStmtChild() {}
func (rule *ExpressionStatement) Append(n ExpressionStatementChild) { rule.ListRule.Append(n) }
func (rule *ExpressionStatement) Append(n ExpressionStatementChild) { rule.ListNode.Append(n) }

+ 16
- 16
grammar/tests.go View File

@ -3,75 +3,75 @@ package grammar
import "github.com/brettlangdon/gython/symbol"
type TestChild interface {
Rule
Node
testChild()
}
type Test struct {
ListRule
ListNode
}
func NewTest() *Test {
rule := &Test{}
rule.initBaseRule(symbol.TEST)
rule.initBaseNode(symbol.TEST)
return rule
}
func (rule *Test) testlistStarExpressionChild() {}
func (rule *Test) testChild() {}
func (rule *Test) Append(n TestChild) { rule.ListRule.Append(n) }
func (rule *Test) Append(n TestChild) { rule.ListNode.Append(n) }
type OrTestChild interface {
Rule
Node
orTestChild()
}
type OrTest struct {
ListRule
ListNode
}
func NewOrTest() *OrTest {
rule := &OrTest{}
rule.initBaseRule(symbol.OR_TEST)
rule.initBaseNode(symbol.OR_TEST)
return rule
}
func (rule *OrTest) testChild() {}
func (rule *OrTest) Append(n OrTestChild) { rule.ListRule.Append(n) }
func (rule *OrTest) Append(n OrTestChild) { rule.ListNode.Append(n) }
type AndTestChild interface {
Rule
Node
andTestChild()
}
type AndTest struct {
ListRule
ListNode
}
func NewAndTest() *AndTest {
rule := &AndTest{}
rule.initBaseRule(symbol.AND_TEST)
rule.initBaseNode(symbol.AND_TEST)
return rule
}
func (rule *AndTest) orTestChild() {}
func (rule *AndTest) Append(n AndTestChild) { rule.ListRule.Append(n) }
func (rule *AndTest) Append(n AndTestChild) { rule.ListNode.Append(n) }
type NotTestChild interface {
Rule
Node
notTestChild()
}
type NotTest struct {
ParentRule
ParentNode
}
func NewNotTest() *NotTest {
rule := &NotTest{}
rule.initBaseRule(symbol.NOT_TEST)
rule.initBaseNode(symbol.NOT_TEST)
return rule
}
func (rule *NotTest) notTestChild() {}
func (rule *NotTest) andTestChild() {}
func (rule *NotTest) SetChild(n NotTestChild) { rule.ParentRule.SetChild(n) }
func (rule *NotTest) SetChild(n NotTestChild) { rule.ParentNode.SetChild(n) }

+ 5
- 4
main.go View File

@ -4,7 +4,7 @@ import (
"fmt"
"os"
"github.com/brettlangdon/gython/parser"
"github.com/brettlangdon/gython/grammar"
"github.com/brettlangdon/gython/scanner"
"github.com/brettlangdon/gython/token"
)
@ -22,13 +22,14 @@ func tokenize() {
}
}
func parse() {
gp := parser.NewGrammarParser(os.Stdin)
func parseGrammar() {
tokenizer := scanner.NewScanner(os.Stdin)
gp := grammar.NewGrammarParser(tokenizer)
root := gp.Parse()
fmt.Println(gp)
fmt.Println(root.Repr())
}
func main() {
parse()
parseGrammar()
}

Loading…
Cancel
Save