This repository was archived by the owner on Feb 26, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +86
-3
lines changed
packages/parse-mapping-lookup/src Expand file tree Collapse file tree 3 files changed +86
-3
lines changed Original file line number Diff line number Diff 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+
177186export 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 ) ;
Original file line number Diff line number Diff line change 99 numberLiteral ,
1010 stringLiteral ,
1111 booleanLiteral ,
12- hexLiteral
12+ hexLiteral ,
13+ enumLiteral
1314} from "@truffle/parse-mapping-lookup/ast" ;
1415
1516const 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
Original file line number Diff line number Diff 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+
126154const literalP = numberLiteralP . pipe (
127155 or (
128156 stringLiteralP ,
129157 booleanLiteralP ,
130- hexLiteralP
158+ hexLiteralP ,
159+ enumLiteralP
131160 )
132161) ;
133162
You can’t perform that action at this time.
0 commit comments