Skip to content

Commit

Permalink
Merge pull request #59 from skx/58-lexer-names
Browse files Browse the repository at this point in the history
Be less strict about identifier names.
  • Loading branch information
skx committed Dec 23, 2019
2 parents 967f1d3 + 2c7fd79 commit e266094
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
43 changes: 7 additions & 36 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lexer
import (
"fmt"
"strings"
"unicode"

"github.com/skx/monkey/token"
)
Expand Down Expand Up @@ -604,49 +605,19 @@ 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
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')
Expand Down
8 changes: 3 additions & 5 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit e266094

Please sign in to comment.