Skip to content

Commit 2edd75b

Browse files
authored
Merge pull request #16 from peterhuene/as-keyword
Use `as` in import and export statements.
2 parents f1e2fc2 + da8d1d6 commit 2edd75b

35 files changed

Lines changed: 126 additions & 126 deletions

crates/wac-parser/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ world w {
489489
/// Doc comment #9!
490490
let x = new foo:bar { };
491491
/// Doc comment #10!
492-
export x with "foo";
492+
export x as "foo";
493493
"#,
494494
r#"/// Doc comment for the package!
495495
package test:foo:bar@1.0.0;
@@ -524,7 +524,7 @@ world w {
524524
let x = new foo:bar {};
525525
526526
/// Doc comment #10!
527-
export x with "foo";
527+
export x as "foo";
528528
"#,
529529
)
530530
.unwrap()

crates/wac-parser/src/ast/export.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub struct ExportStatement<'a> {
1313
pub docs: Vec<DocComment<'a>>,
1414
/// The span of the export keyword.
1515
pub span: SourceSpan,
16-
/// The optional `with` string.
17-
pub with: Option<super::String<'a>>,
16+
/// The optional name to use for the export.
17+
pub name: Option<super::String<'a>>,
1818
/// The expression to export.
1919
pub expr: Expr<'a>,
2020
}
@@ -24,13 +24,13 @@ impl<'a> Parse<'a> for ExportStatement<'a> {
2424
let docs = Parse::parse(lexer)?;
2525
let span = parse_token(lexer, Token::ExportKeyword)?;
2626
let expr = Parse::parse(lexer)?;
27-
let with = parse_optional(lexer, Token::WithKeyword, Parse::parse)?;
27+
let name = parse_optional(lexer, Token::AsKeyword, Parse::parse)?;
2828
parse_token(lexer, Token::Semicolon)?;
2929
Ok(Self {
3030
docs,
3131
span,
3232
expr,
33-
with,
33+
name,
3434
})
3535
}
3636
}
@@ -53,8 +53,8 @@ mod test {
5353
)
5454
.unwrap();
5555
roundtrip(
56-
"package foo:bar; export foo.%with with \"foo\";",
57-
"package foo:bar;\n\nexport foo.%with with \"foo\";\n",
56+
"package foo:bar; export foo.%with as \"foo\";",
57+
"package foo:bar;\n\nexport foo.%with as \"foo\";\n",
5858
)
5959
.unwrap();
6060
roundtrip(
@@ -63,8 +63,8 @@ mod test {
6363
)
6464
.unwrap();
6565
roundtrip(
66-
"package foo:bar; export y.x.z with \"baz\";",
67-
"package foo:bar;\n\nexport y.x.z with \"baz\";\n",
66+
"package foo:bar; export y.x.z as \"baz\";",
67+
"package foo:bar;\n\nexport y.x.z as \"baz\";\n",
6868
)
6969
.unwrap();
7070
roundtrip(
@@ -73,8 +73,8 @@ mod test {
7373
)
7474
.unwrap();
7575
roundtrip(
76-
"package foo:bar; export y[\"x\"][\"z\"] with \"x\";",
77-
"package foo:bar;\n\nexport y[\"x\"][\"z\"] with \"x\";\n",
76+
"package foo:bar; export y[\"x\"][\"z\"] as \"x\";",
77+
"package foo:bar;\n\nexport y[\"x\"][\"z\"] as \"x\";\n",
7878
)
7979
.unwrap();
8080
roundtrip(
@@ -83,8 +83,8 @@ mod test {
8383
)
8484
.unwrap();
8585
roundtrip(
86-
"package foo:bar; export foo[\"bar\"].baz[\"qux\"] with \"qux\";",
87-
"package foo:bar;\n\nexport foo[\"bar\"].baz[\"qux\"] with \"qux\";\n",
86+
"package foo:bar; export foo[\"bar\"].baz[\"qux\"] as \"qux\";",
87+
"package foo:bar;\n\nexport foo[\"bar\"].baz[\"qux\"] as \"qux\";\n",
8888
)
8989
.unwrap();
9090

@@ -101,8 +101,8 @@ mod test {
101101
.unwrap();
102102

103103
roundtrip(
104-
"package foo:bar; export new foo:bar {} /* foo */ with \"foo\";",
105-
"package foo:bar;\n\nexport new foo:bar {} with \"foo\";\n",
104+
"package foo:bar; export new foo:bar {} /* foo */ as \"foo\";",
105+
"package foo:bar;\n\nexport new foo:bar {} as \"foo\";\n",
106106
)
107107
.unwrap();
108108

crates/wac-parser/src/ast/import.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub struct ImportStatement<'a> {
1515
pub docs: Vec<DocComment<'a>>,
1616
/// The identifier of the imported item.
1717
pub id: Ident<'a>,
18-
/// The optional `with` string.
19-
pub with: Option<super::String<'a>>,
18+
/// The optional import name.
19+
pub name: Option<super::String<'a>>,
2020
/// The type of the imported item.
2121
pub ty: ImportType<'a>,
2222
}
@@ -26,11 +26,11 @@ impl<'a> Parse<'a> for ImportStatement<'a> {
2626
let docs = Parse::parse(lexer)?;
2727
parse_token(lexer, Token::ImportKeyword)?;
2828
let id = Parse::parse(lexer)?;
29-
let with = parse_optional(lexer, Token::WithKeyword, Parse::parse)?;
29+
let name = parse_optional(lexer, Token::AsKeyword, Parse::parse)?;
3030
parse_token(lexer, Token::Colon)?;
3131
let ty = Parse::parse(lexer)?;
3232
parse_token(lexer, Token::Semicolon)?;
33-
Ok(Self { docs, id, with, ty })
33+
Ok(Self { docs, id, name, ty })
3434
}
3535
}
3636

@@ -195,8 +195,8 @@ mod test {
195195
.unwrap();
196196

197197
roundtrip(
198-
"package foo:bar; import x with \"y\": foo:bar:baz/qux/jam@1.2.3-preview+abc;",
199-
"package foo:bar;\n\nimport x with \"y\": foo:bar:baz/qux/jam@1.2.3-preview+abc;\n",
198+
"package foo:bar; import x as \"y\": foo:bar:baz/qux/jam@1.2.3-preview+abc;",
199+
"package foo:bar;\n\nimport x as \"y\": foo:bar:baz/qux/jam@1.2.3-preview+abc;\n",
200200
)
201201
.unwrap();
202202
}
@@ -210,8 +210,8 @@ mod test {
210210
.unwrap();
211211

212212
roundtrip(
213-
"package foo:bar; import x with \"foo\": func(x: string) -> string;",
214-
"package foo:bar;\n\nimport x with \"foo\": func(x: string) -> string;\n",
213+
"package foo:bar; import x as \"foo\": func(x: string) -> string;",
214+
"package foo:bar;\n\nimport x as \"foo\": func(x: string) -> string;\n",
215215
)
216216
.unwrap();
217217
}
@@ -225,8 +225,8 @@ mod test {
225225
.unwrap();
226226

227227
roundtrip(
228-
"package foo:bar; import x with \"foo\": interface { x: func(x: string) -> string; };",
229-
"package foo:bar;\n\nimport x with \"foo\": interface {\n x: func(x: string) -> string;\n};\n",
228+
"package foo:bar; import x as \"foo\": interface { x: func(x: string) -> string; };",
229+
"package foo:bar;\n\nimport x as \"foo\": interface {\n x: func(x: string) -> string;\n};\n",
230230
)
231231
.unwrap();
232232
}
@@ -240,8 +240,8 @@ mod test {
240240
.unwrap();
241241

242242
roundtrip(
243-
"package foo:bar; import x /*foo */ with \"foo\": y;",
244-
"package foo:bar;\n\nimport x with \"foo\": y;\n",
243+
"package foo:bar; import x /*foo */ as \"foo\": y;",
244+
"package foo:bar;\n\nimport x as \"foo\": y;\n",
245245
)
246246
.unwrap();
247247
}

crates/wac-parser/src/ast/printer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl<'a, W: Write> DocumentPrinter<'a, W> {
8383
id = self.source(statement.id.span)
8484
)?;
8585

86-
if let Some(with) = &statement.with {
87-
write!(self.writer, " with {with}", with = self.source(with.span))?;
86+
if let Some(name) = &statement.name {
87+
write!(self.writer, " as {name}", name = self.source(name.span))?;
8888
}
8989

9090
write!(self.writer, ": ")?;
@@ -751,8 +751,8 @@ impl<'a, W: Write> DocumentPrinter<'a, W> {
751751
write!(self.writer, "export ")?;
752752
self.expr(&stmt.expr)?;
753753

754-
if let Some(with) = &stmt.with {
755-
write!(self.writer, " with {with}", with = self.source(with.span))?;
754+
if let Some(name) = &stmt.name {
755+
write!(self.writer, " as {name}", name = self.source(name.span))?;
756756
}
757757

758758
write!(self.writer, ";")

crates/wac-parser/src/resolution/ast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ impl<'a> AstResolver<'a> {
229229
_ => kind,
230230
};
231231

232-
let (name, span) = if let Some(with) = stmt.with {
233-
(with.value, with.span)
232+
let (name, span) = if let Some(name) = stmt.name {
233+
(name.value, name.span)
234234
} else {
235235
// If the item is an instance with an id, use the id
236236
if let ItemKind::Instance(id) = kind {
@@ -348,7 +348,7 @@ impl<'a> AstResolver<'a> {
348348
) -> ResolutionResult<()> {
349349
log::debug!("resolving export statement");
350350
let item = self.expr(state, &stmt.expr)?;
351-
let (name, span) = if let Some(name) = stmt.with {
351+
let (name, span) = if let Some(name) = stmt.name {
352352
(name.value, name.span)
353353
} else {
354354
(
@@ -397,10 +397,10 @@ impl<'a> AstResolver<'a> {
397397
kind: definition.kind.as_str(&self.definitions).to_string(),
398398
span,
399399
definition: prev_span,
400-
help: if stmt.with.is_some() {
400+
help: if stmt.name.is_some() {
401401
None
402402
} else {
403-
Some("consider using a `with` clause to use a different name".into())
403+
Some("consider using an `as` clause to use a different name".into())
404404
},
405405
});
406406
}

crates/wac-parser/tests/parser/export.wac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export e;
77
export e["foo"];
88

99
/// Export an alias of an item with a different name
10-
export e["foo"] with "bar";
10+
export e["foo"] as "bar";

crates/wac-parser/tests/parser/export.wac.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"offset": 54,
2626
"length": 6
2727
},
28-
"with": null,
28+
"name": null,
2929
"expr": {
3030
"primary": {
3131
"ident": {
@@ -55,7 +55,7 @@
5555
"offset": 111,
5656
"length": 6
5757
},
58-
"with": null,
58+
"name": null,
5959
"expr": {
6060
"primary": {
6161
"ident": {
@@ -101,10 +101,10 @@
101101
"offset": 182,
102102
"length": 6
103103
},
104-
"with": {
104+
"name": {
105105
"value": "bar",
106106
"span": {
107-
"offset": 203,
107+
"offset": 201,
108108
"length": 5
109109
}
110110
},

crates/wac-parser/tests/parser/import.wac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import b: x;
1010
import c: foo:bar/baz;
1111

1212
/// Import by func type with kebab name
13-
import d with "hello-world": func(name: string);
13+
import d as "hello-world": func(name: string);
1414

1515
/// Import by inline interface
1616
import e: interface {

crates/wac-parser/tests/parser/import.wac.result

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"length": 1
2929
}
3030
},
31-
"with": null,
31+
"name": null,
3232
"ty": {
3333
"func": {
3434
"params": [],
@@ -55,7 +55,7 @@
5555
"length": 1
5656
}
5757
},
58-
"with": null,
58+
"name": null,
5959
"ty": {
6060
"ident": {
6161
"string": "x",
@@ -85,7 +85,7 @@
8585
"length": 1
8686
}
8787
},
88-
"with": null,
88+
"name": null,
8989
"ty": {
9090
"package": {
9191
"span": {
@@ -118,10 +118,10 @@
118118
"length": 1
119119
}
120120
},
121-
"with": {
121+
"name": {
122122
"value": "hello-world",
123123
"span": {
124-
"offset": 203,
124+
"offset": 201,
125125
"length": 13
126126
}
127127
},
@@ -132,13 +132,13 @@
132132
"id": {
133133
"string": "name",
134134
"span": {
135-
"offset": 223,
135+
"offset": 221,
136136
"length": 4
137137
}
138138
},
139139
"ty": {
140140
"string": {
141-
"offset": 229,
141+
"offset": 227,
142142
"length": 6
143143
}
144144
}
@@ -155,19 +155,19 @@
155155
{
156156
"comment": "Import by inline interface",
157157
"span": {
158-
"offset": 239,
158+
"offset": 237,
159159
"length": 30
160160
}
161161
}
162162
],
163163
"id": {
164164
"string": "e",
165165
"span": {
166-
"offset": 277,
166+
"offset": 275,
167167
"length": 1
168168
}
169169
},
170-
"with": null,
170+
"name": null,
171171
"ty": {
172172
"interface": {
173173
"items": [
@@ -177,7 +177,7 @@
177177
"id": {
178178
"string": "x",
179179
"span": {
180-
"offset": 296,
180+
"offset": 294,
181181
"length": 1
182182
}
183183
},
@@ -200,23 +200,23 @@
200200
{
201201
"comment": "Import by package path with version",
202202
"span": {
203-
"offset": 311,
203+
"offset": 309,
204204
"length": 39
205205
}
206206
}
207207
],
208208
"id": {
209209
"string": "f",
210210
"span": {
211-
"offset": 358,
211+
"offset": 356,
212212
"length": 1
213213
}
214214
},
215-
"with": null,
215+
"name": null,
216216
"ty": {
217217
"package": {
218218
"span": {
219-
"offset": 361,
219+
"offset": 359,
220220
"length": 17
221221
},
222222
"string": "foo:bar/baz@1.0.0",

crates/wac-parser/tests/resolution/alias.wac

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ type b = string;
66

77
type c = func();
88

9-
export a with "a2";
10-
export b with "b2";
11-
export c with "c2";
9+
export a as "a2";
10+
export b as "b2";
11+
export c as "c2";

0 commit comments

Comments
 (0)