Skip to content

Commit 165157d

Browse files
committed
remove consumeEOF
1 parent 0f85f30 commit 165157d

File tree

7 files changed

+22
-42
lines changed

7 files changed

+22
-42
lines changed

Diff for: README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ npm i nanolex
1111
```
1212

1313
```ts
14-
import { createToken, nanolex, getComposedTokens } from "nanolex";
14+
import { EOF, createToken, nanolex, getComposedTokens } from "nanolex";
1515

1616
// Define tokens
1717
const Whitespace = createToken(/[ \t\n\r]+/, "WhiteSpace");
@@ -35,7 +35,6 @@ export function parser(value: string) {
3535
// Initiate grammar
3636
const {
3737
consume,
38-
consumeEOF,
3938
zeroOrOne,
4039
zeroOrMany,
4140
zeroOrManySep,
@@ -83,7 +82,7 @@ export function parser(value: string) {
8382
}
8483

8584
// Run the grammar
86-
const [output] = throwIfError(and([FUNCTION, consumeEOF()]));
85+
const [output] = throwIfError(and([FUNCTION, consume(EOF)]));
8786

8887
return output;
8988
}

Diff for: deno.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "@marcisbee/nanolex",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"exports": "./src/nanolex.ts"
55
}

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nanolex",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "Parser grammar builder",
55
"main": "./dist/nanolex.js",
66
"module": "./dist/nanolex.mjs",

Diff for: src/nanolex.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type ErrorToken = {
44
i: number;
55
};
66

7+
export const EOF = Symbol("EOF") as any as TokenLike;
8+
79
export function createToken(
810
token: RegExp | string,
911
name: string = typeof token === "string" ? token : token.source,
@@ -59,7 +61,6 @@ export function nanolex(
5961
return {
6062
consume,
6163
consumeUntil,
62-
consumeEOF,
6364
peek,
6465
oneOrMany: (rule: GrammarLike, transformer?: (value: any) => any) =>
6566
many(rule, 1, transformer),
@@ -307,7 +308,7 @@ export function nanolex(
307308
continue;
308309
}
309310

310-
if (!token.test(c)) {
311+
if (token === EOF || !token.test(c)) {
311312
i += 1;
312313

313314
output.push(c);
@@ -317,6 +318,10 @@ export function nanolex(
317318
return output;
318319
}
319320

321+
if (EOF) {
322+
return output;
323+
}
324+
320325
saveError({
321326
got: c,
322327
token,
@@ -339,7 +344,7 @@ export function nanolex(
339344
continue;
340345
}
341346

342-
if (token.test(c)) {
347+
if (token !== EOF && token.test(c)) {
343348
i += 1;
344349

345350
if (transform) {
@@ -358,6 +363,10 @@ export function nanolex(
358363
break;
359364
}
360365

366+
if (token === EOF && i >= chunksLength) {
367+
return;
368+
}
369+
361370
if (c == null) {
362371
saveError({
363372
got: c,
@@ -375,31 +384,6 @@ export function nanolex(
375384
return;
376385
};
377386
}
378-
function consumeEOF(): GrammarLike<undefined> {
379-
return (): any => {
380-
// consumeTimes += 1;
381-
let c: string;
382-
383-
while (((c = chunks[i]), i < chunksLength)) {
384-
if (!c) {
385-
i += 1;
386-
continue;
387-
}
388-
389-
const currI = i;
390-
if (!skipCheck && tokensSkip() !== undefined) {
391-
continue;
392-
}
393-
i = currI;
394-
395-
saveError({
396-
got: c,
397-
i,
398-
});
399-
return;
400-
}
401-
};
402-
}
403387
function throwIfError<T extends GrammarLike>(rule: T): any {
404388
const output = rule();
405389

Diff for: tests/css.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createToken, nanolex, getComposedTokens } from "../src/nanolex.ts";
1+
import { EOF, createToken, nanolex, getComposedTokens } from "../src/nanolex.ts";
22

33
const LineBreak = createToken(/[\n\r]/, "LineBreak");
44
const Whitespace = createToken(/[ \t]+/, "Whitespace");
@@ -47,7 +47,6 @@ const tokens = getComposedTokens([
4747
export function parser(value: string) {
4848
const {
4949
consume,
50-
consumeEOF,
5150
zeroOrOne,
5251
zeroOrMany,
5352
zeroOrManySep,
@@ -154,7 +153,7 @@ export function parser(value: string) {
154153
return zeroOrMany(RULESET)();
155154
}
156155

157-
const [output] = throwIfError(and([PROGRAM, consumeEOF()]));
156+
const [output] = throwIfError(and([PROGRAM, consume(EOF)]));
158157

159158
return output;
160159
}

Diff for: tests/jsexpression.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createToken, nanolex, getComposedTokens } from "../src/nanolex.ts";
1+
import { EOF, createToken, nanolex, getComposedTokens } from "../src/nanolex.ts";
22

33
const Whitespace = createToken(/[ \t\n\r]+/, "WhiteSpace");
44
const OperatorPlus = createToken("+");
@@ -9,7 +9,6 @@ const tokens = getComposedTokens([Whitespace, OperatorPlus, Integer]);
99
export function parser(value: string) {
1010
const {
1111
consume,
12-
consumeEOF,
1312
zeroOrOne,
1413
zeroOrMany,
1514
zeroOrManySep,
@@ -59,7 +58,7 @@ export function parser(value: string) {
5958
return or([BinaryExpression, Literal])();
6059
}
6160

62-
const [output] = throwIfError(and([Expression, consumeEOF()]));
61+
const [output] = throwIfError(and([Expression, consume(EOF)]));
6362

6463
return output;
6564
}

Diff for: tests/json.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createToken, nanolex, getComposedTokens } from "../src/nanolex.ts";
1+
import { EOF, createToken, nanolex, getComposedTokens } from "../src/nanolex.ts";
22

33
const Whitespace = createToken(/[ \t\n\r]+/, "WhiteSpace");
44
const True = createToken("true");
@@ -37,7 +37,6 @@ const tokens = getComposedTokens([
3737
export function parser(value: string) {
3838
const {
3939
consume,
40-
consumeEOF,
4140
zeroOrOne,
4241
zeroOrMany,
4342
zeroOrManySep,
@@ -112,7 +111,7 @@ export function parser(value: string) {
112111
))();
113112
}
114113

115-
const [output] = throwIfError(and([Json, consumeEOF()])) as any;
114+
const [output] = throwIfError(and([Json, consume(EOF)])) as any;
116115

117116
return output;
118117
}

0 commit comments

Comments
 (0)