Skip to content

Commit

Permalink
Improves parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamillosantos committed Dec 6, 2017
1 parent 09cf82d commit a5ef363
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
44 changes: 20 additions & 24 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type ExpressionError error

func newExpression(expression antlr.Tree) Expression {
func NewExpression(expression antlr.Tree) Expression {
switch e := expression.(type) {
case *parser.VariableContext:
return &ExpressionField{
Expand All @@ -19,61 +19,57 @@ func newExpression(expression antlr.Tree) Expression {
return NewExpressionValue(v)
case *parser.AtomContext:
if e.GetChildCount() == 1 {
return newExpression(e.GetChild(0))
return NewExpression(e.GetChild(0))
} else {
return newExpression(e.GetChild(1))
return NewExpression(e.GetChild(1))
}
case *parser.SignedAtomContext:
if e.GetChildCount() == 1 {
return newExpression(e.GetChild(0))
} else if e.GetChildCount() == 2 {
expr := NewExpressionMultiple()
expr.Add("", NewExpressionValue(0))
expr.Add(e.GetOperator().GetText(), newExpression(e.GetChild(1)))
return expr
} else {
// TODO
panic("TODO! " + e.GetText())
return NewExpression(e.GetChild(0))
}
expr := NewExpressionMultiple()
expr.Add("", NewExpressionValue(0))
expr.Add(e.GetOperator().GetText(), NewExpression(e.GetChild(1)))
return expr
case *parser.ExpressionContext:
if e.GetChildCount() == 1 {
return newExpression(e.GetChild(0))
return NewExpression(e.GetChild(0))
} else {
r := NewExpressionMultiple()
for i, me := range e.AllMultiplyingExpression() {
if i == 0 {
r.Add("", newExpression(me))
r.Add("", NewExpression(me))
} else {
r.Add(e.GetChild(i*2 - 1).(*antlr.TerminalNodeImpl).GetText(), newExpression(me))
r.Add(e.GetChild(i*2 - 1).(*antlr.TerminalNodeImpl).GetText(), NewExpression(me))
}
}
return r
}
case *parser.MultiplyingExpressionContext:
childCount := e.GetChildCount()
if childCount == 1 {
return newExpression(e.GetChild(0))
return NewExpression(e.GetChild(0))
} else {
r := NewExpressionMultiple()
for i, me := range e.AllPowExpression() {
if i == 0 {
r.Add("", newExpression(me))
r.Add("", NewExpression(me))
} else {
r.Add(e.GetChild(i*2 - 1).(*antlr.TerminalNodeImpl).GetText(), newExpression(me))
r.Add(e.GetChild(i*2 - 1).(*antlr.TerminalNodeImpl).GetText(), NewExpression(me))
}
}
return r
}
case *parser.PowExpressionContext:
if e.GetChildCount() == 1 {
return newExpression(e.GetChild(0))
return NewExpression(e.GetChild(0))
} else {
r := NewExpressionMultiple()
for i, me := range e.AllSignedAtom() {
if i == 0 {
r.Add("", newExpression(me))
r.Add("", NewExpression(me))
} else {
r.Add(e.GetChild(i*2 - 1).(*antlr.TerminalNodeImpl).GetText(), newExpression(me))
r.Add(e.GetChild(i*2 - 1).(*antlr.TerminalNodeImpl).GetText(), NewExpression(me))
}
}
return r
Expand All @@ -82,12 +78,12 @@ func newExpression(expression antlr.Tree) Expression {
eParams := e.AllExpression()
params := make([]Expression, 0, len(eParams))
for _, p := range eParams {
params = append(params, newExpression(p))
params = append(params, NewExpression(p))
}
return NewExpressionFunction(e.GetFname().GetText(), params...)
case *parser.BinaryOpContext:
atoms := e.AllAtom()
return NewExpressionBinary(newExpression(atoms[0]), e.Relop().GetText(), newExpression(atoms[1]))
return NewExpressionBinary(NewExpression(atoms[0]), e.Relop().GetText(), NewExpression(atoms[1]))
case *parser.StrContext:
str := e.GetText()
return NewExpressionValue(str[1:len(str)-1])
Expand All @@ -102,5 +98,5 @@ func Compile(expression string) (Expression, error) {
p := parser.NewExpressionParser(stream)
p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
p.BuildParseTrees = true
return newExpression(p.Expression()), nil
return NewExpression(p.Expression()), nil
}
4 changes: 4 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func TestCompile(t *testing.T) {
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })

g.Describe("Compile", func() {
g.It("should return a nil expression", func() {
Expect(expressions.NewExpression(nil)).To(BeNil())
})

g.It("should resolve a simple equation with additions and subtractions", func() {
expr, err := expressions.Compile("1 + 2 - 3 + 4 - 5 + 6")
Expect(err).To(BeNil())
Expand Down

0 comments on commit a5ef363

Please sign in to comment.