chore(expr): remove the helpers of the replaced JavaScript parser, fix lint
- commit
- 34c7f7071bc2916f810d1d512d813a09b0312b83
- author
- brice · · 2026-10-12 18:20 UTC
- parent
- 5f9df8b
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
5 files · +5 -45
M expr/casing.go +0 -5
| @@ -27,11 +27,6 @@ func isJSSpace(r rune) bool { | |||
| 27 | 27 | return r >= 0x2000 && r <= 0x200A | |
| 28 | 28 | } | |
| 29 | 29 | ||
| 30 | - | // jsTrim is String.prototype.trim. | |
| 31 | - | func jsTrim(s string) string { | |
| 32 | - | return strings.TrimFunc(s, isJSSpace) | |
| 33 | - | } | |
| 34 | - | ||
| 35 | 30 | func isASCII(s string) bool { | |
| 36 | 31 | for i := 0; i < len(s); i++ { | |
| 37 | 32 | if s[i] >= utf8.RuneSelf { | |
| @@ -27,11 +27,6 @@ func isJSSpace(r rune) bool { | |||||
| 27 | return r >= 0x2000 && r <= 0x200A | 27 | return r >= 0x2000 && r <= 0x200A | ||
| 28 | } | 28 | } | ||
| 29 | 29 | ||||
| 30 | - | // jsTrim is String.prototype.trim. | |||
| 31 | - | func jsTrim(s string) string { | |||
| 32 | - | return strings.TrimFunc(s, isJSSpace) | |||
| 33 | - | } | |||
| 34 | - | ||||
| 35 | func isASCII(s string) bool { | 30 | func isASCII(s string) bool { | ||
| 36 | for i := 0; i < len(s); i++ { | 31 | for i := 0; i < len(s); i++ { | ||
| 37 | if s[i] >= utf8.RuneSelf { | 32 | if s[i] >= utf8.RuneSelf { | ||
M expr/errors.go +0 -9
| @@ -82,15 +82,6 @@ type Error struct { | |||
| 82 | 82 | ||
| 83 | 83 | func (e *Error) Error() string { return e.Message } | |
| 84 | 84 | ||
| 85 | - | func newTokenError(t ErrorType, tok Token) *Error { | |
| 86 | - | return &Error{ | |
| 87 | - | Type: t, | |
| 88 | - | Message: t.description() + ": '" + tokenString(tok) + "'", | |
| 89 | - | Pos: tok.Range.Start, | |
| 90 | - | Token: tokenString(tok), | |
| 91 | - | } | |
| 92 | - | } | |
| 93 | - | ||
| 94 | 85 | // EvalError is an error raised while evaluating an expression: a failing | |
| 95 | 86 | // function, such as fromJSON on invalid input, or an invalid format string. | |
| 96 | 87 | type EvalError struct { | |
| @@ -82,15 +82,6 @@ type Error struct { | |||||
| 82 | 82 | ||||
| 83 | func (e *Error) Error() string { return e.Message } | 83 | func (e *Error) Error() string { return e.Message } | ||
| 84 | 84 | ||||
| 85 | - | func newTokenError(t ErrorType, tok Token) *Error { | |||
| 86 | - | return &Error{ | |||
| 87 | - | Type: t, | |||
| 88 | - | Message: t.description() + ": '" + tokenString(tok) + "'", | |||
| 89 | - | Pos: tok.Range.Start, | |||
| 90 | - | Token: tokenString(tok), | |||
| 91 | - | } | |||
| 92 | - | } | |||
| 93 | - | ||||
| 94 | // EvalError is an error raised while evaluating an expression: a failing | 85 | // EvalError is an error raised while evaluating an expression: a failing | ||
| 95 | // function, such as fromJSON on invalid input, or an invalid format string. | 86 | // function, such as fromJSON on invalid input, or an invalid format string. | ||
| 96 | type EvalError struct { | 87 | type EvalError struct { | ||
M expr/lexer.go +0 -11
| @@ -70,17 +70,6 @@ type Token struct { | |||
| 70 | 70 | Value Value | |
| 71 | 71 | } | |
| 72 | 72 | ||
| 73 | - | // tokenString is the text an error message shows for a token. | |
| 74 | - | func tokenString(t Token) string { | |
| 75 | - | switch t.Type { | |
| 76 | - | case TokenEOF: | |
| 77 | - | return "EOF" | |
| 78 | - | case TokenString: | |
| 79 | - | return string(t.Value.(String)) | |
| 80 | - | } | |
| 81 | - | return t.Lexeme | |
| 82 | - | } | |
| 83 | - | ||
| 84 | 73 | // Lex splits an expression into tokens the way the JavaScript reference's | |
| 85 | 74 | // lexer does, ending with a TokenEOF token. [Parse] does not use it: it | |
| 86 | 75 | // follows the C# runner, whose tokenizer is context sensitive and lazy (see | |
| @@ -70,17 +70,6 @@ type Token struct { | |||||
| 70 | Value Value | 70 | Value Value | ||
| 71 | } | 71 | } | ||
| 72 | 72 | ||||
| 73 | - | // tokenString is the text an error message shows for a token. | |||
| 74 | - | func tokenString(t Token) string { | |||
| 75 | - | switch t.Type { | |||
| 76 | - | case TokenEOF: | |||
| 77 | - | return "EOF" | |||
| 78 | - | case TokenString: | |||
| 79 | - | return string(t.Value.(String)) | |||
| 80 | - | } | |||
| 81 | - | return t.Lexeme | |||
| 82 | - | } | |||
| 83 | - | ||||
| 84 | // Lex splits an expression into tokens the way the JavaScript reference's | 73 | // Lex splits an expression into tokens the way the JavaScript reference's | ||
| 85 | // lexer does, ending with a TokenEOF token. [Parse] does not use it: it | 74 | // lexer does, ending with a TokenEOF token. [Parse] does not use it: it | ||
| 86 | // follows the C# runner, whose tokenizer is context sensitive and lazy (see | 75 | // follows the C# runner, whose tokenizer is context sensitive and lazy (see | ||
M expr/parse_runner.go +3 -18
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | package expr | |
| 4 | 4 | ||
| 5 | 5 | import ( | |
| 6 | + | "strconv" | |
| 6 | 7 | "strings" | |
| 7 | 8 | "unicode/utf16" | |
| 8 | 9 | ) | |
| @@ -206,7 +207,7 @@ func (l *csLexer) readNumber() *csToken { | |||
| 206 | 207 | start := l.index | |
| 207 | 208 | for { | |
| 208 | 209 | l.index++ | |
| 209 | - | if !(l.index < len(l.units) && (!isCSBoundary(l.units[l.index]) || l.units[l.index] == '.')) { | |
| 210 | + | if l.index >= len(l.units) || (isCSBoundary(l.units[l.index]) && l.units[l.index] != '.') { | |
| 210 | 211 | break | |
| 211 | 212 | } | |
| 212 | 213 | } | |
| @@ -407,28 +408,12 @@ func (p *csParser) fail(t ErrorType, tok *csToken) *Error { | |||
| 407 | 408 | } | |
| 408 | 409 | return &Error{ | |
| 409 | 410 | Type: t, | |
| 410 | - | Message: t.description() + ": '" + tok.raw + "'. Located at position " + itoa(tok.index+1) + " within expression: " + p.expression, | |
| 411 | + | Message: t.description() + ": '" + tok.raw + "'. Located at position " + strconv.Itoa(tok.index+1) + " within expression: " + p.expression, | |
| 411 | 412 | Pos: p.position(tok.index), | |
| 412 | 413 | Token: tok.raw, | |
| 413 | 414 | } | |
| 414 | 415 | } | |
| 415 | 416 | ||
| 416 | - | func itoa(n int) string { return strings.TrimSpace(strings.Repeat(" ", 0) + intString(n)) } | |
| 417 | - | ||
| 418 | - | func intString(n int) string { | |
| 419 | - | if n == 0 { | |
| 420 | - | return "0" | |
| 421 | - | } | |
| 422 | - | var b [20]byte | |
| 423 | - | i := len(b) | |
| 424 | - | for n > 0 { | |
| 425 | - | i-- | |
| 426 | - | b[i] = byte('0' + n%10) | |
| 427 | - | n /= 10 | |
| 428 | - | } | |
| 429 | - | return string(b[i:]) | |
| 430 | - | } | |
| 431 | - | ||
| 432 | 417 | // position converts an absolute index into the line and column of the | |
| 433 | 418 | // JavaScript-style Position (line breaks are U+000A). | |
| 434 | 419 | func (p *csParser) position(index int) Position { | |
| @@ -3,6 +3,7 @@ | |||||
| 3 | package expr | 3 | package expr | ||
| 4 | 4 | ||||
| 5 | import ( | 5 | import ( | ||
| 6 | + | "strconv" | |||
| 6 | "strings" | 7 | "strings" | ||
| 7 | "unicode/utf16" | 8 | "unicode/utf16" | ||
| 8 | ) | 9 | ) | ||
| @@ -206,7 +207,7 @@ func (l *csLexer) readNumber() *csToken { | |||||
| 206 | start := l.index | 207 | start := l.index | ||
| 207 | for { | 208 | for { | ||
| 208 | l.index++ | 209 | l.index++ | ||
| 209 | - | if !(l.index < len(l.units) && (!isCSBoundary(l.units[l.index]) || l.units[l.index] == '.')) { | 210 | + | if l.index >= len(l.units) || (isCSBoundary(l.units[l.index]) && l.units[l.index] != '.') { |
| 210 | break | 211 | break | ||
| 211 | } | 212 | } | ||
| 212 | } | 213 | } | ||
| @@ -407,28 +408,12 @@ func (p *csParser) fail(t ErrorType, tok *csToken) *Error { | |||||
| 407 | } | 408 | } | ||
| 408 | return &Error{ | 409 | return &Error{ | ||
| 409 | Type: t, | 410 | Type: t, | ||
| 410 | - | Message: t.description() + ": '" + tok.raw + "'. Located at position " + itoa(tok.index+1) + " within expression: " + p.expression, | 411 | + | Message: t.description() + ": '" + tok.raw + "'. Located at position " + strconv.Itoa(tok.index+1) + " within expression: " + p.expression, |
| 411 | Pos: p.position(tok.index), | 412 | Pos: p.position(tok.index), | ||
| 412 | Token: tok.raw, | 413 | Token: tok.raw, | ||
| 413 | } | 414 | } | ||
| 414 | } | 415 | } | ||
| 415 | 416 | ||||
| 416 | - | func itoa(n int) string { return strings.TrimSpace(strings.Repeat(" ", 0) + intString(n)) } | |||
| 417 | - | ||||
| 418 | - | func intString(n int) string { | |||
| 419 | - | if n == 0 { | |||
| 420 | - | return "0" | |||
| 421 | - | } | |||
| 422 | - | var b [20]byte | |||
| 423 | - | i := len(b) | |||
| 424 | - | for n > 0 { | |||
| 425 | - | i-- | |||
| 426 | - | b[i] = byte('0' + n%10) | |||
| 427 | - | n /= 10 | |||
| 428 | - | } | |||
| 429 | - | return string(b[i:]) | |||
| 430 | - | } | |||
| 431 | - | ||||
| 432 | // position converts an absolute index into the line and column of the | 417 | // position converts an absolute index into the line and column of the | ||
| 433 | // JavaScript-style Position (line breaks are U+000A). | 418 | // JavaScript-style Position (line breaks are U+000A). | ||
| 434 | func (p *csParser) position(index int) Position { | 419 | func (p *csParser) position(index int) Position { | ||
M expr/runner_strict_test.go +2 -2
| @@ -11,8 +11,8 @@ import ( | |||
| 11 | 11 | // knownRunnerDifference classifies the answers of the corpus on which this | |
| 12 | 12 | // package knowingly differs from the runner. It returns the reason, or "". | |
| 13 | 13 | func knownRunnerDifference(g, r runnerCase) string { | |
| 14 | - | switch { | |
| 15 | - | case r.E == "-0" || r.E == "toJSON(-0)": | |
| 14 | + | switch r.E { | |
| 15 | + | case "-0", "toJSON(-0)": | |
| 16 | 16 | // .NET Core formats negative zero as "-0". The runner's newer copy | |
| 17 | 17 | // (src/Sdk/Expressions/EvaluationResult.cs) maps it back to "0" and the | |
| 18 | 18 | // shared fixtures (coerce_string.json) say "0", so "0" is kept. | |
| @@ -11,8 +11,8 @@ import ( | |||||
| 11 | // knownRunnerDifference classifies the answers of the corpus on which this | 11 | // knownRunnerDifference classifies the answers of the corpus on which this | ||
| 12 | // package knowingly differs from the runner. It returns the reason, or "". | 12 | // package knowingly differs from the runner. It returns the reason, or "". | ||
| 13 | func knownRunnerDifference(g, r runnerCase) string { | 13 | func knownRunnerDifference(g, r runnerCase) string { | ||
| 14 | - | switch { | 14 | + | switch r.E { |
| 15 | - | case r.E == "-0" || r.E == "toJSON(-0)": | 15 | + | case "-0", "toJSON(-0)": |
| 16 | // .NET Core formats negative zero as "-0". The runner's newer copy | 16 | // .NET Core formats negative zero as "-0". The runner's newer copy | ||
| 17 | // (src/Sdk/Expressions/EvaluationResult.cs) maps it back to "0" and the | 17 | // (src/Sdk/Expressions/EvaluationResult.cs) maps it back to "0" and the | ||
| 18 | // shared fixtures (coerce_string.json) say "0", so "0" is kept. | 18 | // shared fixtures (coerce_string.json) say "0", so "0" is kept. | ||