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

Commit c4d40de

Browse files
author
g. nicholas d'andrea
committed
Add support for enum literals
1 parent f9c9138 commit c4d40de

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

packages/parse-mapping-lookup/src/ast.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,31 @@ export interface HexLiteral extends Literal {
174174
value: string;
175175
}
176176

177+
export interface EnumLiteral extends Literal {
178+
type: "enum";
179+
value: {
180+
contract?: Identifier;
181+
enumeration: Identifier;
182+
member: Identifier;
183+
};
184+
}
185+
177186
export const {
178187
booleanLiteral,
179188
numberLiteral,
180189
stringLiteral,
181190
hexLiteral,
191+
enumLiteral
182192
} = LiteralGenerics.makeConstructors<{
183193
boolean: BooleanLiteral;
184194
number: NumberLiteral;
185195
string: StringLiteral;
186196
hex: HexLiteral;
197+
enum: EnumLiteral;
187198
}>({
188199
boolean: {},
189200
number: {},
190201
string: {},
191-
hex: {}
202+
hex: {},
203+
enum: {}
192204
} as const);

packages/parse-mapping-lookup/src/parser.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
numberLiteral,
1010
stringLiteral,
1111
booleanLiteral,
12-
hexLiteral
12+
hexLiteral,
13+
enumLiteral
1314
} from "@truffle/parse-mapping-lookup/ast";
1415

1516
const testCases = [
@@ -106,6 +107,47 @@ const testCases = [
106107
{
107108
expression: `m[hex"deadbee"]`,
108109
errors: true
110+
},
111+
{
112+
expression: `m[Direction.North]`,
113+
result: expression({
114+
root: identifier({ name: "m" }),
115+
pointer: pointer({
116+
path: [
117+
indexAccess({
118+
index: enumLiteral({
119+
value: {
120+
enumeration: identifier({ name: "Direction" }),
121+
member: identifier({ name: "North" })
122+
}
123+
})
124+
})
125+
]
126+
})
127+
})
128+
},
129+
{
130+
expression: `m[Geography.Direction.North]`,
131+
result: expression({
132+
root: identifier({ name: "m" }),
133+
pointer: pointer({
134+
path: [
135+
indexAccess({
136+
index: enumLiteral({
137+
value: {
138+
contract: identifier({ name: "Geography" }),
139+
enumeration: identifier({ name: "Direction" }),
140+
member: identifier({ name: "North" })
141+
}
142+
})
143+
})
144+
]
145+
})
146+
})
147+
},
148+
{
149+
expression: `m[North]`,
150+
errors: true
109151
}
110152
];
111153

packages/parse-mapping-lookup/src/parser.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
qthen,
1616
map,
1717
exactly,
18+
maybe,
1819
many,
1920
manyBetween,
2021
stringify,
@@ -31,6 +32,7 @@ import {
3132
stringLiteral,
3233
booleanLiteral,
3334
hexLiteral,
35+
enumLiteral,
3436
memberLookup,
3537
pointer
3638
} from "./ast";
@@ -123,11 +125,38 @@ const hexLiteralP = digit(16).pipe(exactly(2)).pipe(
123125
map(value => hexLiteral({ value: `0x${value}` }))
124126
);
125127

128+
const enumLiteralP = identifierP.pipe(
129+
then(
130+
string(".").pipe(
131+
qthen(identifierP)
132+
),
133+
string(".").pipe(
134+
qthen(identifierP),
135+
maybe()
136+
)
137+
),
138+
).pipe(
139+
map(args => {
140+
if (args[2]) {
141+
const [contract, enumeration, member] = args;
142+
return enumLiteral({
143+
value: { contract, enumeration, member }
144+
});
145+
} else {
146+
const [enumeration, member] = args;
147+
return enumLiteral({
148+
value: { enumeration, member }
149+
});
150+
}
151+
})
152+
);
153+
126154
const literalP = numberLiteralP.pipe(
127155
or(
128156
stringLiteralP,
129157
booleanLiteralP,
130-
hexLiteralP
158+
hexLiteralP,
159+
enumLiteralP
131160
)
132161
);
133162

0 commit comments

Comments
 (0)