Simplify expression parsing
This commit is contained in:
parent
508ed37040
commit
193bd5c2d5
105
parser.go
105
parser.go
@ -487,8 +487,8 @@ func (p *Parser) tryUnaryExpression() (*Expression, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) tryMultiplicativeExpression() (*Expression, error) {
|
func (p *Parser) tryBinaryExpression0(opFunc func() (*Expression, error), operators ...Operator) (*Expression, error) {
|
||||||
left, err := p.tryUnaryExpression()
|
left, err := opFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -498,7 +498,7 @@ func (p *Parser) tryMultiplicativeExpression() (*Expression, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
op, err := p.tryOperator(Operator_Multiply, Operator_Divide, Operator_Modulo)
|
op, err := p.tryOperator(operators...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -507,7 +507,7 @@ func (p *Parser) tryMultiplicativeExpression() (*Expression, error) {
|
|||||||
return left, nil
|
return left, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
right, err := p.tryUnaryExpression()
|
right, err := opFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -520,103 +520,20 @@ func (p *Parser) tryMultiplicativeExpression() (*Expression, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Parser) tryMultiplicativeExpression() (*Expression, error) {
|
||||||
|
return p.tryBinaryExpression0(p.tryUnaryExpression, Operator_Multiply, Operator_Divide, Operator_Modulo)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Parser) tryAdditiveExpression() (*Expression, error) {
|
func (p *Parser) tryAdditiveExpression() (*Expression, error) {
|
||||||
left, err := p.tryMultiplicativeExpression()
|
return p.tryBinaryExpression0(p.tryMultiplicativeExpression, Operator_Plus, Operator_Minus)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if left == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
op, err := p.tryOperator(Operator_Plus, Operator_Minus)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if op == nil {
|
|
||||||
return left, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
right, err := p.tryMultiplicativeExpression()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if right == nil {
|
|
||||||
return nil, p.error("expected expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
left = &Expression{Type: Expression_Binary, Value: BinaryExpression{Operation: getArithmeticOperation(*op), Left: *left, Right: *right}, Position: left.Position}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) tryRelationalExpression() (*Expression, error) {
|
func (p *Parser) tryRelationalExpression() (*Expression, error) {
|
||||||
left, err := p.tryAdditiveExpression()
|
return p.tryBinaryExpression0(p.tryAdditiveExpression, Operator_Less, Operator_Greater, Operator_LessEquals, Operator_GreaterEquals)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if left == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
op, err := p.tryOperator(Operator_Less, Operator_Greater, Operator_LessEquals, Operator_GreaterEquals)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if op == nil {
|
|
||||||
return left, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
right, err := p.tryAdditiveExpression()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if right == nil {
|
|
||||||
return nil, p.error("expected expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
left = &Expression{Type: Expression_Binary, Value: BinaryExpression{Operation: getArithmeticOperation(*op), Left: *left, Right: *right}, Position: left.Position}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) tryEqualityExpression() (*Expression, error) {
|
func (p *Parser) tryEqualityExpression() (*Expression, error) {
|
||||||
left, err := p.tryRelationalExpression()
|
return p.tryBinaryExpression0(p.tryRelationalExpression, Operator_EqualsEquals, Operator_NotEquals)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if left == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
op, err := p.tryOperator(Operator_EqualsEquals, Operator_NotEquals)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if op == nil {
|
|
||||||
return left, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
right, err := p.tryRelationalExpression()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if right == nil {
|
|
||||||
return nil, p.error("expected expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
left = &Expression{Type: Expression_Binary, Value: BinaryExpression{Operation: getArithmeticOperation(*op), Left: *left, Right: *right}, Position: left.Position}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) tryBinaryExpression() (*Expression, error) {
|
func (p *Parser) tryBinaryExpression() (*Expression, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user