@clickhouse/datatype-parser 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/dist/parser.js +25 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -308,7 +308,6 @@ The module structure tracks the C++ sources one-to-one:
|
|
|
308
308
|
| `src/parser.ts` | `src/parser.cpp` + `parser.h` | the `ParserDataType::parseImpl` port |
|
|
309
309
|
| `src/json.ts` | `src/json.cpp` | the byte-faithful JSON serializer |
|
|
310
310
|
| `src/index.ts` | — | public barrel |
|
|
311
|
-
| `tool/main.ts` | `tool/main.cpp` | the `chdt-parse` CLI |
|
|
312
311
|
|
|
313
312
|
The lexer and parser deliberately preserve the original control flow, branch
|
|
314
313
|
ordering, helper names, and `pos` save/restore points. A few signatures changed
|
package/dist/parser.js
CHANGED
|
@@ -40,6 +40,17 @@ function isWordCharOrDollar(c) {
|
|
|
40
40
|
function isEnumTypeUpper(u) {
|
|
41
41
|
return u === "ENUM" || u === "ENUM8" || u === "ENUM16";
|
|
42
42
|
}
|
|
43
|
+
/// Does `text` parse in FULL as a numeric literal — the TS equivalent of the
|
|
44
|
+
/// C++ `strtod` (float) / `strtoull` (integer) + end-pointer check? The lexer
|
|
45
|
+
/// is deliberately lenient and can hand back a half-formed lexeme such as `1e`
|
|
46
|
+
/// (an exponent with no digits); those must be rejected. Integer lexemes the
|
|
47
|
+
/// lexer emits are pure digit runs (so always valid); floats may carry a
|
|
48
|
+
/// malformed exponent, so the optional exponent here requires >= 1 digit.
|
|
49
|
+
function isFullyParsedNumber(text, isFloat) {
|
|
50
|
+
if (isFloat)
|
|
51
|
+
return /^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/.test(text);
|
|
52
|
+
return /^\d+$/.test(text);
|
|
53
|
+
}
|
|
43
54
|
class Parser {
|
|
44
55
|
tokens;
|
|
45
56
|
pos = 0;
|
|
@@ -419,13 +430,26 @@ class Parser {
|
|
|
419
430
|
}
|
|
420
431
|
if (this.type() !== TokenType.Number)
|
|
421
432
|
return null;
|
|
433
|
+
/// The lexer only SKIPS over a number without checking correctness (the
|
|
434
|
+
/// server's `Lexer` does the same: "not to parse a number or check
|
|
435
|
+
/// correctness, but only to skip it"). The server then rejects a malformed
|
|
436
|
+
/// literal when it converts the token text into a `Field`; this port has no
|
|
437
|
+
/// such stage, so validate here by parsing the lexeme in full. Without it a
|
|
438
|
+
/// bare exponent like `1e` would be emitted verbatim into a `Float64`
|
|
439
|
+
/// literal's JSON `value`, yielding invalid JSON (`"value":1e`). Mirrors the
|
|
440
|
+
/// C++ `strtod` / `strtoull` + end-pointer check in `parser.cpp`.
|
|
441
|
+
const text = this.cur().text;
|
|
442
|
+
if (!isFullyParsedNumber(text, this.cur().is_float)) {
|
|
443
|
+
this.setHardError(this.cur().begin, `malformed numeric literal: '${text}'`);
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
422
446
|
const node = makeNode(NodeKind.Literal);
|
|
423
447
|
node.value_type = this.cur().is_float
|
|
424
448
|
? "Float64"
|
|
425
449
|
: negative
|
|
426
450
|
? "Int64"
|
|
427
451
|
: "UInt64";
|
|
428
|
-
node.value = (negative ? "-" : "") +
|
|
452
|
+
node.value = (negative ? "-" : "") + text;
|
|
429
453
|
this.advance();
|
|
430
454
|
return node;
|
|
431
455
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clickhouse/datatype-parser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Standalone ClickHouse data-type string parser — a TypeScript port of the chdt C++ library.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
42
|
"lint": "eslint --max-warnings=0 .",
|
|
43
43
|
"lint:fix": "eslint . --fix",
|
|
44
|
+
"pack": "npm pack",
|
|
44
45
|
"prepack": "npm run build",
|
|
45
|
-
"parse": "node tool/main.ts",
|
|
46
46
|
"bench": "vitest bench --run",
|
|
47
47
|
"test": "vitest run",
|
|
48
48
|
"test:watch": "vitest",
|