Skip to content

Commit 13d13fd

Browse files
committed
Add support for $ symbol in identifiers
1 parent 0cf3b1f commit 13d13fd

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

parser/lexer/lexer_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ var lexTests = []lexTest{
8888
{Kind: EOF},
8989
},
9090
},
91+
{
92+
`$i _0 früh`,
93+
[]Token{
94+
{Kind: Identifier, Value: "$i"},
95+
{Kind: Identifier, Value: "_0"},
96+
{Kind: Identifier, Value: "früh"},
97+
{Kind: EOF},
98+
},
99+
},
91100
}
92101

93102
func compareTokens(i1, i2 []Token) bool {

parser/lexer/state.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func root(l *lexer) stateFn {
1111
case r == eof:
1212
l.emitEOF()
1313
return nil
14-
case isSpace(r):
14+
case IsSpace(r):
1515
l.ignore()
1616
return root
1717
case r == '\'' || r == '"':
@@ -36,7 +36,7 @@ func root(l *lexer) stateFn {
3636
case r == '.':
3737
l.backup()
3838
return dot
39-
case isAlphaNumeric(r):
39+
case IsAlphaNumeric(r):
4040
l.backup()
4141
return identifier
4242
default:
@@ -67,18 +67,14 @@ func (l *lexer) scanNumber() bool {
6767
}
6868
}
6969
l.acceptRun(digits)
70-
end := l.end
71-
loc := l.loc
72-
prev := l.prev
70+
loc, prev, end := l.loc, l.prev, l.end
7371
if l.accept(".") {
7472
// Lookup for .. operator: if after dot there is another dot (1..2), it maybe a range operator.
7573
if l.peek() == '.' {
7674
// We can't backup() here, as it would require two backups,
7775
// and backup() func supports only one for now. So, save and
7876
// restore it here.
79-
l.end = end
80-
l.loc = loc
81-
l.prev = prev
77+
l.loc, l.prev, l.end = loc, prev, end
8278
return true
8379
}
8480
l.acceptRun(digits)
@@ -88,7 +84,7 @@ func (l *lexer) scanNumber() bool {
8884
l.acceptRun(digits)
8985
}
9086
// Next thing mustn't be alphanumeric.
91-
if isAlphaNumeric(l.peek()) {
87+
if IsAlphaNumeric(l.peek()) {
9288
l.next()
9389
return false
9490
}
@@ -110,7 +106,7 @@ func identifier(l *lexer) stateFn {
110106
loop:
111107
for {
112108
switch r := l.next(); {
113-
case isAlphaNumeric(r):
109+
case IsAlphaNumeric(r):
114110
// absorb
115111
default:
116112
l.backup()

parser/lexer/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"unicode/utf8"
88
)
99

10-
func isSpace(r rune) bool {
10+
func IsSpace(r rune) bool {
1111
return unicode.IsSpace(r)
1212
}
1313

14-
func isAlphaNumeric(r rune) bool {
15-
return isAlphabetic(r) || unicode.IsDigit(r)
14+
func IsAlphaNumeric(r rune) bool {
15+
return IsAlphabetic(r) || unicode.IsDigit(r)
1616
}
1717

18-
func isAlphabetic(r rune) bool {
19-
return r == '_' || unicode.IsLetter(r)
18+
func IsAlphabetic(r rune) bool {
19+
return r == '_' || r == '$' || unicode.IsLetter(r)
2020
}
2121

2222
var (

parser/parser.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"regexp"
66
"strconv"
77
"strings"
8-
"unicode"
98
"unicode/utf8"
109

1110
. "github.com/antonmedv/expr/ast"
@@ -528,25 +527,17 @@ func isValidIdentifier(str string) bool {
528527
return false
529528
}
530529
h, w := utf8.DecodeRuneInString(str)
531-
if !isAlphabetic(h) {
530+
if !IsAlphabetic(h) {
532531
return false
533532
}
534533
for _, r := range str[w:] {
535-
if !isAlphaNumeric(r) {
534+
if !IsAlphaNumeric(r) {
536535
return false
537536
}
538537
}
539538
return true
540539
}
541540

542-
func isAlphaNumeric(r rune) bool {
543-
return isAlphabetic(r) || unicode.IsDigit(r)
544-
}
545-
546-
func isAlphabetic(r rune) bool {
547-
return r == '_' || unicode.IsLetter(r)
548-
}
549-
550541
func (p *parser) parseArguments() []Node {
551542
p.expect(Bracket, "(")
552543
nodes := make([]Node, 0)

0 commit comments

Comments
 (0)