Skip to content

Commit 71debba

Browse files
committed
fix: locations
1 parent 29bbf38 commit 71debba

18 files changed

+121
-94
lines changed

packages/pgen/compile.ts

-26
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,13 @@ export const compile = (node: g.Grammar): t.File => {
2424
definedRules.add(rule.name);
2525
}
2626

27-
// const exports = t.returnStatement(t.objectExpression(
28-
// names.map(name => t.objectProperty(
29-
// t.identifier(name),
30-
// emitCall('compile', [
31-
// t.identifier(name),
32-
// t.arrowFunctionExpression([], t.identifier('space')),
33-
// ]),
34-
// false,
35-
// true,
36-
// ))
37-
// ));
38-
3927
return t.file(t.program([
4028
...imports,
4129
t.exportNamedDeclaration(t.tsModuleDeclaration(
4230
t.identifier(astGlobal),
4331
t.tsModuleBlock(ast),
4432
)),
4533
...stmts,
46-
// t.exportNamedDeclaration(t.variableDeclaration("const", [
47-
// t.variableDeclarator(t.identifier("getParser"), t.arrowFunctionExpression(
48-
// [withType(
49-
// t.identifier(primGlobal),
50-
// t.tsTypeReference(
51-
// t.tsQualifiedName(t.identifier(libGlobal), t.identifier('Algebra')),
52-
// )
53-
// )],
54-
// t.blockStatement([
55-
// ...stmts,
56-
// exports,
57-
// ]),
58-
// ))
59-
// ])),
6034
]));
6135
};
6236

packages/pgen/transform.ts

-4
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,3 @@ const transformSeq = ({ exprs }: g.Seq): Transform<Expr> => (ctx) => {
325325
e.push(Eps);
326326
return e.reduceRight((prev, expr) => Ap(expr, prev, 'r'));
327327
};
328-
329-
// const spaced = (node: Expr): Transform<Expr> => (ctx) => {
330-
// return Ap(node, Star(Call("space$noSkip", [])), 'l');
331-
// };

packages/runtime/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Context, createContext, getSuccess, isFailure, Parser } from "./runtime";
22

33
export { Parser, Located, alt, any, ap, app, compile, debug, eof, eps, field, left, lex, loc, lookNeg, lookPos, opt, plus, pure, ref, regex, right, rule, sat, seq, star, str, stry, terminal, where, withLoc } from './spaced';
4-
export { Loc, LocEmpty, LocRange } from './located';
4+
export * from './loc';
55

66
export type ParseResult<T> = ParseResultSuccess<T> | ParseResultError
77
export type ParseResultError = {

packages/runtime/lib/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Parser } from "./runtime";
22
export { Parser, Located, alt, any, ap, app, compile, debug, eof, eps, field, left, lex, loc, lookNeg, lookPos, opt, plus, pure, ref, regex, right, rule, sat, seq, star, str, stry, terminal, where, withLoc } from './spaced';
3-
export { Loc, LocEmpty, LocRange } from './located';
3+
export * from './loc';
44
export type ParseResult<T> = ParseResultSuccess<T> | ParseResultError;
55
export type ParseResultError = {
66
readonly $: "error";

packages/runtime/lib/index.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

packages/runtime/lib/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
var desc = Object.getOwnPropertyDescriptor(m, k);
5+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6+
desc = { enumerable: true, get: function() { return m[k]; } };
7+
}
8+
Object.defineProperty(o, k2, desc);
9+
}) : (function(o, m, k, k2) {
10+
if (k2 === undefined) k2 = k;
11+
o[k2] = m[k];
12+
}));
13+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
14+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15+
};
216
Object.defineProperty(exports, "__esModule", { value: true });
317
exports.parse = exports.withLoc = exports.where = exports.terminal = exports.stry = exports.str = exports.star = exports.seq = exports.sat = exports.rule = exports.right = exports.regex = exports.ref = exports.pure = exports.plus = exports.opt = exports.lookPos = exports.lookNeg = exports.loc = exports.lex = exports.left = exports.field = exports.eps = exports.eof = exports.debug = exports.compile = exports.app = exports.ap = exports.any = exports.alt = void 0;
418
const runtime_1 = require("./runtime");
@@ -32,6 +46,7 @@ Object.defineProperty(exports, "stry", { enumerable: true, get: function () { re
3246
Object.defineProperty(exports, "terminal", { enumerable: true, get: function () { return spaced_1.terminal; } });
3347
Object.defineProperty(exports, "where", { enumerable: true, get: function () { return spaced_1.where; } });
3448
Object.defineProperty(exports, "withLoc", { enumerable: true, get: function () { return spaced_1.withLoc; } });
49+
__exportStar(require("./loc"), exports);
3550
const parse = (grammar) => (text) => {
3651
const ctx = (0, runtime_1.createContext)(text);
3752
const result = grammar(ctx);

packages/runtime/lib/loc.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export type Loc = LocRange | LocEmpty;
2+
export type LocRange = {
3+
readonly $: 'range';
4+
readonly start: number;
5+
readonly end: number;
6+
};
7+
export declare const rangeLoc: (start: number, end: number) => LocRange;
8+
export type LocEmpty = {
9+
readonly $: 'empty';
10+
readonly at: number;
11+
};
12+
export declare const emptyLoc: (at: number) => LocEmpty;
13+
export declare const isEmptyLoc: (loc: Loc) => loc is LocEmpty;
14+
export declare const mergeLoc: (left: Loc, right: Loc) => Loc;
15+
//# sourceMappingURL=loc.d.ts.map

packages/runtime/lib/loc.d.ts.map

+1
Original file line numberDiff line numberDiff line change

packages/runtime/lib/loc.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.mergeLoc = exports.isEmptyLoc = exports.emptyLoc = exports.rangeLoc = void 0;
4+
const rangeLoc = (start, end) => {
5+
if (start === 32 && end === 32)
6+
debugger;
7+
return { $: 'range', start, end };
8+
};
9+
exports.rangeLoc = rangeLoc;
10+
const emptyLoc = (at) => ({ $: 'empty', at });
11+
exports.emptyLoc = emptyLoc;
12+
const isEmptyLoc = (loc) => loc.$ === 'empty';
13+
exports.isEmptyLoc = isEmptyLoc;
14+
const mergeLoc = (left, right) => {
15+
return (0, exports.isEmptyLoc)(left) ? right : (0, exports.isEmptyLoc)(right) ? left : (0, exports.rangeLoc)(left.start, right.end);
16+
};
17+
exports.mergeLoc = mergeLoc;

packages/runtime/lib/located.d.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import * as P from './runtime';
2-
export type Parser<T> = P.Parser<[T, Loc]>;
3-
export type Loc = LocRange | LocEmpty;
4-
export type LocRange = {
5-
readonly $: 'range';
6-
readonly start: number;
7-
readonly end: number;
8-
};
9-
export type LocEmpty = {
10-
readonly $: 'empty';
11-
readonly at: number;
12-
};
2+
import * as L from './loc';
3+
export type Parser<T> = P.Parser<readonly [T, L.Loc]>;
134
export declare const rule: <T>(child: Parser<T>) => Parser<T>;
145
export declare const pure: <const T>(t: T) => Parser<T>;
156
export declare const ap: <T, U>(left: Parser<(t: T) => U>, right: Parser<T>) => Parser<U>;
@@ -35,5 +26,5 @@ export declare const lookNeg: <T>(child: Parser<T>) => Parser<undefined>;
3526
export declare const eof: Parser<undefined>;
3627
export declare const debug: <T>(child: Parser<T>) => Parser<T>;
3728
export declare const where: Parser<number>;
38-
export declare const withLoc: <T>(child: Parser<T>) => Parser<[T, Loc]>;
29+
export declare const withLoc: <T>(child: Parser<T>) => Parser<[T, L.Loc]>;
3930
//# sourceMappingURL=located.d.ts.map

packages/runtime/lib/located.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

packages/runtime/lib/located.js

+21-17
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,37 @@ var __importStar = (this && this.__importStar) || (function () {
3535
Object.defineProperty(exports, "__esModule", { value: true });
3636
exports.withLoc = exports.where = exports.debug = exports.eof = exports.lookNeg = exports.lookPos = exports.opt = exports.plus = exports.fail = exports.eps = exports.any = exports.star = exports.ref = exports.app = exports.stry = exports.regex = exports.sat = exports.str = exports.alt = exports.field = exports.seq = exports.right = exports.left = exports.ap = exports.pure = exports.rule = void 0;
3737
const P = __importStar(require("./runtime"));
38-
const range = (start, end) => ({ $: 'range', start, end });
39-
const empty = (at) => ({ $: 'empty', at });
40-
const isEmpty = (loc) => loc.$ === 'empty';
41-
const span = (left, right) => {
42-
return isEmpty(left) ? right : isEmpty(right) ? left : range(left.start, right.end);
43-
};
38+
const L = __importStar(require("./loc"));
4439
const terminal = (p) => c => {
4540
const start = c.p;
4641
const r = p(c);
4742
if (P.isFailure(r))
4843
return P.failure;
49-
return P.success([P.getSuccess(r), range(start, c.p)]);
44+
return P.success([P.getSuccess(r), L.rangeLoc(start, c.p)]);
5045
};
5146
const rule = (child) => (ctx) => {
5247
const result = child(ctx);
5348
return result;
5449
};
5550
exports.rule = rule;
56-
const pure = (t) => terminal(P.pure(t));
51+
const pure = (t) => ctx => {
52+
return P.app(P.pure(t), t => [t, L.emptyLoc(ctx.p)])(ctx);
53+
};
5754
exports.pure = pure;
5855
const ap = (left, right) => {
59-
return P.app(P.seq(left, right), ([[f, l], [x, r]]) => [f(x), span(l, r)]);
56+
return P.app(P.seq(left, right), ([[f, l], [x, r]]) => [f(x), L.mergeLoc(l, r)]);
6057
};
6158
exports.ap = ap;
6259
const left = (left, right) => {
63-
return P.app(P.seq(left, right), ([[t, l], [, r]]) => [t, span(l, r)]);
60+
return P.app(P.seq(left, right), ([[t, l], [, r]]) => [t, L.mergeLoc(l, r)]);
6461
};
6562
exports.left = left;
6663
const right = (left, right) => {
67-
return P.app(P.seq(left, right), ([[, l], [u, r]]) => [u, span(l, r)]);
64+
return P.app(P.seq(left, right), ([[, l], [u, r]]) => [u, L.mergeLoc(l, r)]);
6865
};
6966
exports.right = right;
7067
const seq = (left, right) => {
71-
return P.app(P.seq(left, right), ([[t, l], [u, r]]) => [[t, u], span(l, r)]);
68+
return P.app(P.seq(left, right), ([[t, l], [u, r]]) => [[t, u], L.mergeLoc(l, r)]);
7269
};
7370
exports.seq = seq;
7471
const field = (left, key, right) => {
@@ -106,12 +103,13 @@ exports.ref = ref;
106103
const star = (child) => {
107104
return P.app(P.seq(exports.where, P.star(child)), ([[at], ls]) => [
108105
ls.map(([t]) => t),
109-
ls.map(([, l]) => l).reduce(span, empty(at))
106+
ls.map(([, l]) => l).reduce(L.mergeLoc, L.emptyLoc(at))
110107
]);
111108
};
112109
exports.star = star;
113110
exports.any = terminal(P.any);
114-
exports.eps = terminal(P.eps);
111+
const eps = (ctx) => P.success([P.EPS, L.emptyLoc(ctx.p)]);
112+
exports.eps = eps;
115113
exports.fail = P.fail;
116114
const plus = (child) => {
117115
return (0, exports.app)((0, exports.seq)(child, (0, exports.star)(child)), ([a, as]) => (as.unshift(a), as));
@@ -121,18 +119,24 @@ const opt = (child) => {
121119
return (0, exports.alt)(child, (0, exports.app)(exports.eps, () => undefined));
122120
};
123121
exports.opt = opt;
124-
exports.lookPos = P.lookPos;
122+
const lookPos = (child) => {
123+
const p = P.lookPos(child);
124+
return ctx => {
125+
return P.app(p, ([t]) => [t, L.emptyLoc(ctx.p)])(ctx);
126+
};
127+
};
128+
exports.lookPos = lookPos;
125129
const lookNeg = (child) => {
126130
const p = (0, exports.lookPos)(child);
127131
return ctx => {
128132
const r = p(ctx);
129-
return P.isSuccess(r) ? P.failure : P.success([undefined, empty(ctx.p)]);
133+
return P.isSuccess(r) ? P.failure : P.success([undefined, L.emptyLoc(ctx.p)]);
130134
};
131135
};
132136
exports.lookNeg = lookNeg;
133137
exports.eof = (0, exports.lookNeg)(exports.any);
134138
exports.debug = P.debug;
135-
const where = ctx => P.success([ctx.p, empty(ctx.p)]);
139+
const where = ctx => P.success([ctx.p, L.emptyLoc(ctx.p)]);
136140
exports.where = where;
137141
const withLoc = (child) => {
138142
return P.app(child, ([t, loc]) => [[t, loc], loc]);

0 commit comments

Comments
 (0)