-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eskip: improve lexer performance 2 #2870
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ opentracingplugin/build | |
build/ | ||
skptesting/lorem.html | ||
.vscode/* | ||
*.test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"errors" | ||
"fmt" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
type token struct { | ||
|
@@ -14,14 +13,6 @@ type token struct { | |
|
||
type charPredicate func(byte) bool | ||
|
||
type scanner interface { | ||
scan(string) (token, string, error) | ||
} | ||
|
||
type scannerFunc func(string) (token, string, error) | ||
|
||
func (sf scannerFunc) scan(code string) (token, string, error) { return sf(code) } | ||
|
||
type eskipLex struct { | ||
code string | ||
lastToken string | ||
|
@@ -78,11 +69,11 @@ func (l *eskipLex) init(code string) { | |
|
||
func isNewline(c byte) bool { return c == newlineChar } | ||
func isUnderscore(c byte) bool { return c == underscore } | ||
func isAlpha(c byte) bool { return unicode.IsLetter(rune(c)) } | ||
func isDigit(c byte) bool { return unicode.IsDigit(rune(c)) } | ||
func isSymbolChar(c byte) bool { return isUnderscore(c) || isAlpha(c) || isDigit(c) } | ||
func isAlpha(c byte) bool { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') } | ||
func isDigit(c byte) bool { return c >= '0' && c <= '9' } | ||
func isSymbolChar(c byte) bool { return isAlpha(c) || isDigit(c) || isUnderscore(c) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check more frequent class first |
||
func isDecimalChar(c byte) bool { return c == decimalChar } | ||
func isNumberChar(c byte) bool { return isDecimalChar(c) || isDigit(c) } | ||
func isNumberChar(c byte) bool { return isDigit(c) || isDecimalChar(c) } | ||
|
||
func scanWhile(code string, p charPredicate) (string, string) { | ||
for i := 0; i < len(code); i++ { | ||
|
@@ -277,74 +268,78 @@ func scanNumber(code string) (t token, rest string, err error) { | |
|
||
func scanSymbol(code string) (t token, rest string, err error) { | ||
t.id = symbol | ||
t.val, rest = scanWhile(code, isSymbolChar) | ||
for i := 0; i < len(code); i++ { | ||
if !isSymbolChar(code[i]) { | ||
t.val, rest = code[0:i], code[i:] | ||
return | ||
} | ||
} | ||
t.val, rest = code, "" | ||
return | ||
} | ||
Comment on lines
269
to
279
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
func selectScanner(code string) scanner { | ||
func scan(code string) (token, string, error) { | ||
switch code[0] { | ||
case ',': | ||
return commaToken | ||
return commaToken.scan(code) | ||
case ')': | ||
return closeparenToken | ||
return closeparenToken.scan(code) | ||
case '(': | ||
return openparenToken | ||
return openparenToken.scan(code) | ||
case ':': | ||
return colonToken | ||
return colonToken.scan(code) | ||
case ';': | ||
return semicolonToken | ||
return semicolonToken.scan(code) | ||
case '>': | ||
return closearrowToken | ||
return closearrowToken.scan(code) | ||
case '*': | ||
return anyToken | ||
return anyToken.scan(code) | ||
case '&': | ||
if len(code) >= 2 && code[1] == '&' { | ||
return andToken | ||
return andToken.scan(code) | ||
} | ||
case '-': | ||
if len(code) >= 2 && code[1] == '>' { | ||
return arrowToken | ||
return arrowToken.scan(code) | ||
} | ||
case '/': | ||
return scannerFunc(scanRegexpOrComment) | ||
return scanRegexpOrComment(code) | ||
case '"': | ||
return scannerFunc(scanDoubleQuote) | ||
return scanDoubleQuote(code) | ||
case '`': | ||
return scannerFunc(scanBacktick) | ||
return scanBacktick(code) | ||
case '<': | ||
for _, tok := range openarrowPrefixedTokens { | ||
if strings.HasPrefix(code, tok.val) { | ||
return tok | ||
return tok.scan(code) | ||
} | ||
} | ||
return openarrowToken | ||
return openarrowToken.scan(code) | ||
} | ||
|
||
if isNumberChar(code[0]) { | ||
return scannerFunc(scanNumber) | ||
return scanNumber(code) | ||
} | ||
|
||
if isAlpha(code[0]) || isUnderscore(code[0]) { | ||
return scannerFunc(scanSymbol) | ||
return scanSymbol(code) | ||
} | ||
|
||
return nil | ||
return token{}, "", unexpectedToken | ||
} | ||
|
||
func (l *eskipLex) next() (t token, err error) { | ||
func (l *eskipLex) next() (token, error) { | ||
l.code = scanWhitespace(l.code) | ||
if len(l.code) == 0 { | ||
err = eof | ||
return | ||
return token{}, eof | ||
} | ||
|
||
s := selectScanner(l.code) | ||
if s == nil { | ||
err = unexpectedToken | ||
return | ||
t, rest, err := scan(l.code) | ||
if err == unexpectedToken { | ||
return token{}, err | ||
} | ||
l.code = rest | ||
|
||
t, l.code, err = s.scan(l.code) | ||
if err == void { | ||
return l.next() | ||
} | ||
|
@@ -353,7 +348,7 @@ func (l *eskipLex) next() (t token, err error) { | |
l.lastToken = t.val | ||
} | ||
|
||
return | ||
return t, err | ||
} | ||
|
||
func (l *eskipLex) Lex(lval *eskipSymType) int { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is equivalent but inlines better since c is byte and unicode.* does internal check for ascii