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

Commit

Permalink
Add support for enum literals
Browse files Browse the repository at this point in the history
  • Loading branch information
g. nicholas d'andrea committed Apr 24, 2021
1 parent f9c9138 commit 540659d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/parse-mapping-lookup/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,31 @@ export interface HexLiteral extends Literal {
value: string;
}

export interface EnumLiteral extends Literal {
type: "enum";
value: {
contract?: Identifier;
enumeration: Identifier;
member: Identifier;
};
}

export const {
booleanLiteral,
numberLiteral,
stringLiteral,
hexLiteral,
enumLiteral
} = LiteralGenerics.makeConstructors<{
boolean: BooleanLiteral;
number: NumberLiteral;
string: StringLiteral;
hex: HexLiteral;
enum: EnumLiteral;
}>({
boolean: {},
number: {},
string: {},
hex: {}
hex: {},
enum: {}
} as const);
44 changes: 43 additions & 1 deletion packages/parse-mapping-lookup/src/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
numberLiteral,
stringLiteral,
booleanLiteral,
hexLiteral
hexLiteral,
enumLiteral
} from "@truffle/parse-mapping-lookup/ast";

const testCases = [
Expand Down Expand Up @@ -106,6 +107,47 @@ const testCases = [
{
expression: `m[hex"deadbee"]`,
errors: true
},
{
expression: `m[Direction.North]`,
result: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [
indexAccess({
index: enumLiteral({
value: {
enumeration: identifier({ name: "Direction" }),
member: identifier({ name: "North" })
}
})
})
]
})
})
},
{
expression: `m[Geography.Direction.North]`,
result: expression({
root: identifier({ name: "m" }),
pointer: pointer({
path: [
indexAccess({
index: enumLiteral({
value: {
contract: identifier({ name: "Geography" }),
enumeration: identifier({ name: "Direction" }),
member: identifier({ name: "North" })
}
})
})
]
})
})
},
{
expression: `m[North]`,
errors: true
}
];

Expand Down
42 changes: 41 additions & 1 deletion packages/parse-mapping-lookup/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
qthen,
map,
exactly,
maybe,
many,
manyBetween,
stringify,
Expand All @@ -31,6 +32,7 @@ import {
stringLiteral,
booleanLiteral,
hexLiteral,
enumLiteral,
memberLookup,
pointer
} from "./ast";
Expand Down Expand Up @@ -123,11 +125,49 @@ const hexLiteralP = digit(16).pipe(exactly(2)).pipe(
map(value => hexLiteral({ value: `0x${value}` }))
);

const localEnumLiteralP = identifierP.pipe(
then(
string("."),
identifierP
),
).pipe(
map(([enumeration, _, member]) => enumLiteral({
value: { enumeration, member }
}))
);

const enumLiteralP = identifierP.pipe(
then(
string(".").pipe(
qthen(identifierP)
),
string(".").pipe(
qthen(identifierP),
maybe()
)
),
).pipe(
map(args => {
if (args[2]) {
const [contract, enumeration, member] = args;
return enumLiteral({
value: { contract, enumeration, member }
});
} else {
const [enumeration, member] = args;
return enumLiteral({
value: { enumeration, member }
});
}
})
);

const literalP = numberLiteralP.pipe(
or(
stringLiteralP,
booleanLiteralP,
hexLiteralP
hexLiteralP,
enumLiteralP
)
);

Expand Down

0 comments on commit 540659d

Please sign in to comment.