Skip to content

Commit 8670192

Browse files
author
Dean Karn
authored
fix exponential number parsing (#7)
1 parent 63627d6 commit 8670192

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.3.1] - 2022-07-19
10+
### Fixed
11+
- Fixed number parsing for exponential numbers eg. 1e10.
12+
913
## [0.3.0] - 2022-07-19
1014
### Added
1115
- Added BETWEEN operator support <value> BETWEEN <value> <value>
@@ -31,8 +35,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3135
### Added
3236
- Initial conversion from https://github.com/rust-playground/ksql.
3337

34-
[Unreleased]: https://github.com/go-playground/ksql/compare/v0.3.0...HEAD
35-
[0.2.0]: https://github.com/go-playground/ksql/compare/v0.2.0...v0.3.0
38+
[Unreleased]: https://github.com/go-playground/ksql/compare/v0.3.1...HEAD
39+
[0.3.1]: https://github.com/go-playground/ksql/compare/v0.3.0...v0.3.1
40+
[0.3.0]: https://github.com/go-playground/ksql/compare/v0.2.0...v0.3.0
3641
[0.2.0]: https://github.com/go-playground/ksql/compare/v0.1.1...v0.2.0
3742
[0.1.1]: https://github.com/go-playground/ksql/compare/v0.1.0...v0.1.1
3843
[0.1.0]: https://github.com/go-playground/ksql/commit/v0.1.0

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ksql
22
=====
3-
![Project status](https://img.shields.io/badge/version-0.3.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-0.3.1-green.svg)
44
[![GoDoc](https://godoc.org/github.com/go-playground/ksql?status.svg)](https://pkg.go.dev/github.com/go-playground/ksql)
55
![License](https://img.shields.io/dub/l/vibe-d.svg)
66

@@ -69,7 +69,7 @@ Expressions support most mathematical and string expressions see below for detai
6969
| `CloseBracket` | `]` | N/A |
7070
| `Comma` | `,` | N/A |
7171
| `QuotedString` | `"sample text"` | Must start and end with an unescaped `"` character |
72-
| `Number` | `123.45` | Must start and end with a valid `0-9` digit. |
72+
| `Number` | ` 123.45 ` | Must start and end with a space or '+' or '-' when hard coded value in expression and supports `0-9 +- e` characters for numbers and exponent notation. |
7373
| `BooleanTrue` | `true` | Accepts `true` as a boolean only. |
7474
| `BooleanFalse` | `false` | Accepts `false` as a boolean only. |
7575
| `SelectorPath` | `.selector_path` | Starts with a `.` and ends with whitespace blank space. This crate currently uses [gjson](https://github.com/tidwall/gjson.rs) and so the full gjson syntax for identifiers is supported. |

lexer.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ func tokenizeSingleToken(data []byte) (result LexerResult, err error) {
6767
result = LexerResult{kind: Equals, len: 1}
6868
}
6969
case '+':
70-
result = LexerResult{kind: Add, len: 1}
70+
if len(data) > 1 && isDigit(data[1]) {
71+
result, err = tokenizeNumber(data)
72+
} else {
73+
result = LexerResult{kind: Add, len: 1}
74+
}
7175
case '-':
72-
result = LexerResult{kind: Subtract, len: 1}
76+
if len(data) > 1 && isDigit(data[1]) {
77+
result, err = tokenizeNumber(data)
78+
} else {
79+
result = LexerResult{kind: Subtract, len: 1}
80+
}
7381
case '*':
7482
result = LexerResult{kind: Multiply, len: 1}
7583
case '/':

lexer_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,31 @@ func Test(t *testing.T) {
337337
input: "BETWEEN ",
338338
tokens: []Token{{kind: Between, start: 0, len: 7}},
339339
},
340+
{
341+
name: "parse negative number",
342+
input: " -1.23 ",
343+
tokens: []Token{{kind: Number, start: 1, len: 5}},
344+
},
345+
{
346+
name: "parse positive number",
347+
input: " +1.23 ",
348+
tokens: []Token{{kind: Number, start: 1, len: 5}},
349+
},
350+
{
351+
name: "parse positive number",
352+
input: " +1.23 ",
353+
tokens: []Token{{kind: Number, start: 1, len: 5}},
354+
},
355+
{
356+
name: "parse negative exponential number",
357+
input: " -1e10 ",
358+
tokens: []Token{{kind: Number, start: 1, len: 5}},
359+
},
360+
{
361+
name: "parse positive exponential number",
362+
input: " +1e10 ",
363+
tokens: []Token{{kind: Number, start: 1, len: 5}},
364+
},
340365
}
341366

342367
for _, tc := range tests {

parser_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,24 @@ func TestParser(t *testing.T) {
500500
src: ``,
501501
expected: false,
502502
},
503+
{
504+
name: "parse exponent number eq",
505+
exp: `1e3 == 1000`,
506+
src: ``,
507+
expected: true,
508+
},
509+
{
510+
name: "parse negative exponent number eq",
511+
exp: `-1e-3 == -0.001`,
512+
src: ``,
513+
expected: true,
514+
},
515+
{
516+
name: "parse positive exponent number eq",
517+
exp: `+1e-3 == 0.001`,
518+
src: ``,
519+
expected: true,
520+
},
503521
}
504522

505523
for _, tc := range tests {

0 commit comments

Comments
 (0)