@@ -8,6 +8,7 @@ use super::{
88 AstDisplay , Ident , Indenter , PackageName ,
99} ;
1010use crate :: parser:: Rule ;
11+ use pest:: Span ;
1112use pest_ast:: FromPest ;
1213use serde:: Serialize ;
1314use std:: fmt;
@@ -172,8 +173,8 @@ display!(InstantiationArgument);
172173#[ pest_ast( rule( Rule :: NamedInstantiationArgument ) ) ]
173174#[ serde( rename_all = "camelCase" ) ]
174175pub struct NamedInstantiationArgument < ' a > {
175- /// The identifier in the argument.
176- pub id : Ident < ' a > ,
176+ /// The name of the argument.
177+ pub name : InstantiationArgumentName < ' a > ,
177178 /// The colon in the argument.
178179 pub colon : Colon < ' a > ,
179180 /// The expression in the argument.
@@ -182,13 +183,46 @@ pub struct NamedInstantiationArgument<'a> {
182183
183184impl AstDisplay for NamedInstantiationArgument < ' _ > {
184185 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > , indenter : & mut Indenter ) -> fmt:: Result {
185- write ! ( f, "{id}{colon} " , id = self . id, colon = self . colon) ?;
186+ self . name . fmt ( f, indenter) ?;
187+ write ! ( f, "{colon} " , colon = self . colon) ?;
186188 self . expr . fmt ( f, indenter)
187189 }
188190}
189191
190192display ! ( NamedInstantiationArgument ) ;
191193
194+ /// Represents the argument name in an instantiation argument in the AST.
195+ #[ derive( Debug , Clone , Serialize , FromPest ) ]
196+ #[ pest_ast( rule( Rule :: InstantiationArgumentName ) ) ]
197+ #[ serde( rename_all = "camelCase" ) ]
198+ pub enum InstantiationArgumentName < ' a > {
199+ /// The argument name is an identifier.
200+ Ident ( Ident < ' a > ) ,
201+ /// The argument name is a string.
202+ String ( super :: String < ' a > ) ,
203+ }
204+
205+ impl < ' a > InstantiationArgumentName < ' a > {
206+ /// Gets the span of the instantiation argument name.
207+ pub fn span ( & self ) -> Span < ' a > {
208+ match self {
209+ InstantiationArgumentName :: Ident ( ident) => ident. 0 ,
210+ InstantiationArgumentName :: String ( string) => string. 0 ,
211+ }
212+ }
213+ }
214+
215+ impl AstDisplay for InstantiationArgumentName < ' _ > {
216+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > , _indenter : & mut Indenter ) -> fmt:: Result {
217+ match self {
218+ InstantiationArgumentName :: Ident ( ident) => ident. fmt ( f, _indenter) ,
219+ InstantiationArgumentName :: String ( string) => string. fmt ( f, _indenter) ,
220+ }
221+ }
222+ }
223+
224+ display ! ( InstantiationArgumentName ) ;
225+
192226/// Represents a nested expression in the AST.
193227#[ derive( Debug , Clone , Serialize , FromPest ) ]
194228#[ pest_ast( rule( Rule :: NestedExpr ) ) ]
@@ -223,11 +257,21 @@ pub enum PostfixExpr<'a> {
223257 NamedAccess ( NamedAccessExpr < ' a > ) ,
224258}
225259
260+ impl < ' a > PostfixExpr < ' a > {
261+ /// Gets the span of the postfix expression.
262+ pub fn span ( & self ) -> pest:: Span < ' a > {
263+ match self {
264+ PostfixExpr :: Access ( access) => access. span ( ) ,
265+ PostfixExpr :: NamedAccess ( access) => access. span ( ) ,
266+ }
267+ }
268+ }
269+
226270impl AstDisplay for PostfixExpr < ' _ > {
227271 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > , indenter : & mut Indenter ) -> fmt:: Result {
228272 match self {
229273 PostfixExpr :: Access ( access) => access. fmt ( f, indenter) ,
230- PostfixExpr :: NamedAccess ( named_access ) => named_access . fmt ( f, indenter) ,
274+ PostfixExpr :: NamedAccess ( access ) => access . fmt ( f, indenter) ,
231275 }
232276 }
233277}
@@ -245,6 +289,13 @@ pub struct AccessExpr<'a> {
245289 pub id : Ident < ' a > ,
246290}
247291
292+ impl < ' a > AccessExpr < ' a > {
293+ /// Gets the span of the access expression.
294+ pub fn span ( & self ) -> Span < ' a > {
295+ Span :: new ( self . dot . 0 . get_input ( ) , self . dot . 0 . start ( ) , self . id . 0 . end ( ) ) . unwrap ( )
296+ }
297+ }
298+
248299impl AstDisplay for AccessExpr < ' _ > {
249300 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > , _indenter : & mut Indenter ) -> fmt:: Result {
250301 write ! ( f, "{dot}{id}" , dot = self . dot, id = self . id)
@@ -266,6 +317,18 @@ pub struct NamedAccessExpr<'a> {
266317 pub close : CloseBracket < ' a > ,
267318}
268319
320+ impl < ' a > NamedAccessExpr < ' a > {
321+ /// Gets the span of the access expression.
322+ pub fn span ( & self ) -> Span < ' a > {
323+ Span :: new (
324+ self . open . 0 . get_input ( ) ,
325+ self . open . 0 . start ( ) ,
326+ self . close . 0 . end ( ) ,
327+ )
328+ . unwrap ( )
329+ }
330+ }
331+
269332impl AstDisplay for NamedAccessExpr < ' _ > {
270333 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > , _indenter : & mut Indenter ) -> fmt:: Result {
271334 write ! (
@@ -303,8 +366,8 @@ mod test {
303366
304367 roundtrip :: < Expr > (
305368 Rule :: Expr ,
306- "new foo:bar { foo, bar: (new baz:qux {...}), baz: foo[\" baz\" ].qux }" ,
307- "new foo:bar {\n foo,\n bar: (new baz:qux { ... }),\n baz: foo[\" baz\" ].qux,\n }" ,
369+ "new foo:bar { foo, \" bar\" : (new baz:qux {...}), \" baz\" : foo[\" baz\" ].qux }" ,
370+ "new foo:bar {\n foo,\n \" bar\" : (new baz:qux { ... }),\n \" baz\" : foo[\" baz\" ].qux,\n }" ,
308371 )
309372 . unwrap ( ) ;
310373 }
0 commit comments