@@ -7,6 +7,53 @@ use miette::SourceSpan;
77use semver:: Version ;
88use serde:: Serialize ;
99
10+ /// Represents an extern name following an `as` clause in the AST.
11+ #[ derive( Debug , Clone , Serialize ) ]
12+ #[ serde( rename_all = "camelCase" ) ]
13+ pub enum ExternName < ' a > {
14+ /// The "as" clause is an identifier.
15+ Ident ( Ident < ' a > ) ,
16+ /// The "as" clause is a string.
17+ String ( super :: String < ' a > ) ,
18+ }
19+
20+ impl ExternName < ' _ > {
21+ /// Gets the span of the extern name.
22+ pub fn span ( & self ) -> SourceSpan {
23+ match self {
24+ Self :: Ident ( ident) => ident. span ,
25+ Self :: String ( string) => string. span ,
26+ }
27+ }
28+
29+ /// Gets the string value of the extern name.
30+ pub fn as_str ( & self ) -> & str {
31+ match self {
32+ Self :: Ident ( ident) => ident. string ,
33+ Self :: String ( string) => string. value ,
34+ }
35+ }
36+ }
37+
38+ impl Peek for ExternName < ' _ > {
39+ fn peek ( lookahead : & mut Lookahead ) -> bool {
40+ Ident :: peek ( lookahead) || super :: String :: peek ( lookahead)
41+ }
42+ }
43+
44+ impl < ' a > Parse < ' a > for ExternName < ' a > {
45+ fn parse ( lexer : & mut Lexer < ' a > ) -> ParseResult < Self > {
46+ let mut lookahead = Lookahead :: new ( lexer) ;
47+ if Ident :: peek ( & mut lookahead) {
48+ Ok ( Self :: Ident ( Parse :: parse ( lexer) ?) )
49+ } else if super :: String :: peek ( & mut lookahead) {
50+ Ok ( Self :: String ( Parse :: parse ( lexer) ?) )
51+ } else {
52+ Err ( lookahead. error ( ) )
53+ }
54+ }
55+ }
56+
1057/// Represents an import statement in the AST.
1158#[ derive( Debug , Clone , Serialize ) ]
1259#[ serde( rename_all = "camelCase" ) ]
@@ -16,7 +63,7 @@ pub struct ImportStatement<'a> {
1663 /// The identifier of the imported item.
1764 pub id : Ident < ' a > ,
1865 /// The optional import name.
19- pub name : Option < super :: String < ' a > > ,
66+ pub name : Option < ExternName < ' a > > ,
2067 /// The type of the imported item.
2168 pub ty : ImportType < ' a > ,
2269}
0 commit comments