Full expression and validation support
This commit is contained in:
@@ -153,3 +153,12 @@ type BinaryExpression struct {
|
||||
|
||||
func (b *BinaryExpression) Pos() Position { return b.Position }
|
||||
func (b *BinaryExpression) isValue() {}
|
||||
|
||||
type UnaryExpression struct {
|
||||
Position Position
|
||||
Operator Token
|
||||
Right Value
|
||||
}
|
||||
|
||||
func (u *UnaryExpression) Pos() Position { return u.Position }
|
||||
func (u *UnaryExpression) isValue() {}
|
||||
|
||||
@@ -147,18 +147,12 @@ func (l *Lexer) NextToken() Token {
|
||||
case ']':
|
||||
return l.emit(TokenRBracket)
|
||||
case '+':
|
||||
if unicode.IsSpace(l.peek()) {
|
||||
if unicode.IsSpace(l.peek()) || unicode.IsDigit(l.peek()) {
|
||||
return l.emit(TokenPlus)
|
||||
}
|
||||
return l.lexObjectIdentifier()
|
||||
case '-':
|
||||
if unicode.IsDigit(l.peek()) {
|
||||
return l.lexNumber()
|
||||
}
|
||||
if unicode.IsSpace(l.peek()) {
|
||||
return l.emit(TokenMinus)
|
||||
}
|
||||
return l.lexIdentifier()
|
||||
return l.emit(TokenMinus)
|
||||
case '*':
|
||||
return l.emit(TokenStar)
|
||||
case '/':
|
||||
@@ -242,13 +236,28 @@ func (l *Lexer) lexString() Token {
|
||||
}
|
||||
|
||||
func (l *Lexer) lexNumber() Token {
|
||||
for {
|
||||
r := l.next()
|
||||
if unicode.IsDigit(r) || unicode.IsLetter(r) || r == '.' || r == '-' || r == '+' {
|
||||
continue
|
||||
// Consume initial digits (already started)
|
||||
l.lexDigits()
|
||||
|
||||
if l.peek() == '.' {
|
||||
l.next()
|
||||
l.lexDigits()
|
||||
}
|
||||
|
||||
if r := l.peek(); r == 'e' || r == 'E' {
|
||||
l.next()
|
||||
if p := l.peek(); p == '+' || p == '-' {
|
||||
l.next()
|
||||
}
|
||||
l.backup()
|
||||
return l.emit(TokenNumber)
|
||||
l.lexDigits()
|
||||
}
|
||||
|
||||
return l.emit(TokenNumber)
|
||||
}
|
||||
|
||||
func (l *Lexer) lexDigits() {
|
||||
for unicode.IsDigit(l.peek()) {
|
||||
l.next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,7 +327,7 @@ func (l *Lexer) lexHashIdentifier() Token {
|
||||
func (l *Lexer) lexVariableReference() Token {
|
||||
for {
|
||||
r := l.next()
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '-' {
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
|
||||
continue
|
||||
}
|
||||
l.backup()
|
||||
|
||||
@@ -299,8 +299,27 @@ func (p *Parser) parseAtom() (Value, bool) {
|
||||
return &ReferenceValue{Position: tok.Position, Value: tok.Value}, true
|
||||
case TokenVariableReference:
|
||||
return &VariableReferenceValue{Position: tok.Position, Name: tok.Value}, true
|
||||
case TokenMinus:
|
||||
val, ok := p.parseAtom()
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return &UnaryExpression{Position: tok.Position, Operator: tok, Right: val}, true
|
||||
case TokenObjectIdentifier:
|
||||
return &VariableReferenceValue{Position: tok.Position, Name: tok.Value}, true
|
||||
case TokenSymbol:
|
||||
if tok.Value == "(" {
|
||||
val, ok := p.parseExpression(0)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if next := p.next(); next.Type != TokenSymbol || next.Value != ")" {
|
||||
p.addError(next.Position, "expected )")
|
||||
return nil, false
|
||||
}
|
||||
return val, true
|
||||
}
|
||||
fallthrough
|
||||
case TokenLBrace:
|
||||
arr := &ArrayValue{Position: tok.Position}
|
||||
for {
|
||||
|
||||
Reference in New Issue
Block a user