From 2c7fd79cfc5b1279d6bf0f8265d9741b3ae4d6e7 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Mon, 23 Dec 2019 19:39:26 +0200 Subject: [PATCH] Be less strict about identifier names. This PR closes #58 by allowing digits in identifiers. This might cause surprises. --- lexer/lexer.go | 43 +++++++------------------------------------ lexer/lexer_test.go | 8 +++----- 2 files changed, 10 insertions(+), 41 deletions(-) diff --git a/lexer/lexer.go b/lexer/lexer.go index b2be721..201c513 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -3,6 +3,7 @@ package lexer import ( "fmt" "strings" + "unicode" "github.com/skx/monkey/token" ) @@ -604,7 +605,12 @@ func (l *Lexer) peekChar() rune { // determinate ch is identifier or not func isIdentifier(ch rune) bool { - return !isDigit(ch) && !isWhitespace(ch) && !isBrace(ch) && !isOperator(ch) && !isComparison(ch) && !isCompound(ch) && !isBrace(ch) && !isParen(ch) && !isBracket(ch) && !isEmpty(ch) + + if unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '.' || ch == '?' || ch == '_' { + return true + } + + return false } // is white space @@ -612,41 +618,6 @@ func isWhitespace(ch rune) bool { return ch == rune(' ') || ch == rune('\t') || ch == rune('\n') || ch == rune('\r') } -// is operators -func isOperator(ch rune) bool { - return ch == rune('+') || ch == rune('%') || ch == rune('-') || ch == rune('/') || ch == rune('*') -} - -// is comparison -func isComparison(ch rune) bool { - return ch == rune('=') || ch == rune('!') || ch == rune('>') || ch == rune('<') -} - -// is compound -func isCompound(ch rune) bool { - return ch == rune(',') || ch == rune(':') || ch == rune('"') || ch == rune(';') -} - -// is brace -func isBrace(ch rune) bool { - return ch == rune('{') || ch == rune('}') -} - -// is bracket -func isBracket(ch rune) bool { - return ch == rune('[') || ch == rune(']') -} - -// is parenthesis -func isParen(ch rune) bool { - return ch == rune('(') || ch == rune(')') -} - -// is empty -func isEmpty(ch rune) bool { - return rune(0) == ch -} - // is Digit func isDigit(ch rune) bool { return rune('0') <= ch && ch <= rune('9') diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index a50fbdc..185b747 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -359,15 +359,13 @@ let a = "steve\t"; let a = "steve\r"; let a = "steve\\"; let a = "steve\""; -let c = 3.113£; -.; -` - input += "`/bin/ls`" - input += "/*\n" +let c = 3.113; +.;` l := New(input) tok := l.NextToken() for tok.Type != token.EOF { + tok = l.NextToken() } }