Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Improve reporting for parse errors
Browse files Browse the repository at this point in the history
- Define custom type names for each form kind in the AST
- Update tests to look for one-of `{ trace, value }`
  • Loading branch information
g. nicholas d'andrea committed Apr 28, 2021
1 parent 5ab8016 commit 0a505d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
7 changes: 5 additions & 2 deletions packages/parse-mapping-lookup/src/meta/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ export type MakeParserCombinatorOptions<
const makeParserCombinator = <F extends Forms, K extends FormKind<F>>(
options: MakeParserCombinatorOptions<F, K>
): ParserCombinator<F, K> => {
const { definition: parser, tie, construct } = options;
const { kind, definition: parser, tie, construct } = options;

return parser({ construct, tie });
const combinator = parser({ construct, tie });
return Object.assign(combinator, {
type: `ast:${kind}`
});
};

// prettier-ignore
Expand Down
36 changes: 23 additions & 13 deletions packages/parse-mapping-lookup/src/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
const testCases = [
{
expression: `m[0]`,
result: expression({
value: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [indexAccess({ index: value({ contents: "0" }) })]
Expand All @@ -21,7 +21,7 @@ const testCases = [
},
{
expression: `m[0x0]`,
result: expression({
value: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [indexAccess({ index: value({ contents: "0x0" }) })]
Expand All @@ -30,7 +30,7 @@ const testCases = [
},
{
expression: `m["hello"]`,
result: expression({
value: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [indexAccess({ index: string({ contents: "hello" }) })]
Expand All @@ -39,7 +39,7 @@ const testCases = [
},
{
expression: `m["\\""]`,
result: expression({
value: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [indexAccess({ index: string({ contents: '"' }) })]
Expand All @@ -48,7 +48,7 @@ const testCases = [
},
{
expression: `s.m[0]`,
result: expression({
value: expression({
root: identifier({ name: "s" }),
pointer: pointer({
path: [
Expand All @@ -60,7 +60,7 @@ const testCases = [
},
{
expression: `m$[false]._k[true]`,
result: expression({
value: expression({
root: identifier({ name: "m$" }),
pointer: pointer({
path: [
Expand All @@ -73,7 +73,7 @@ const testCases = [
},
{
expression: `m["\\x41"]`,
result: expression({
value: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [indexAccess({ index: string({ contents: "A" }) })]
Expand All @@ -82,11 +82,13 @@ const testCases = [
},
{
expression: `m[`,
errors: true
trace: {
position: 2
}
},
{
expression: `m[hex"deadbeef"]`,
result: expression({
value: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [indexAccess({ index: value({ contents: `hex"deadbeef"` }) })]
Expand All @@ -95,7 +97,7 @@ const testCases = [
},
{
expression: `m[Direction.North]`,
result: expression({
value: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [
Expand All @@ -109,14 +111,22 @@ const testCases = [
];

describe("@truffle/parse-mapping-lookup", () => {
for (const { expression, errors = false, result: expected } of testCases) {
if (errors) {
for (const testCase of testCases) {
const { expression } = testCase;
if (testCase.trace) {
const { trace: expected } = testCase;

it(`fails to parse: ${expression}`, () => {
const result = parseExpression(expression);
expect(result.isOk).toBeFalsy();
expect(result.isOk).toBe(false);
expect(
// @ts-ignore
result.trace
).toMatchObject(expected);
});
} else {
it(`parses: ${expression}`, () => {
const { value: expected } = testCase;
const result = parseExpression(expression);
expect(result.isOk).toBeTruthy();
expect(result.value).toEqual(expected);
Expand Down

0 comments on commit 0a505d7

Please sign in to comment.