diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-07-08 21:16:16 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-07-08 21:16:16 +0200 |
| commit | 551e3af9d09a03aaa246cac46b98124bc10835ba (patch) | |
| tree | b075a93df7cb608eae9650e497fb6a1336d80212 /src | |
| parent | 5c327e249e03ac303e7fef40e2df6c6ef834db66 (diff) | |
Replace using with from
Diffstat (limited to 'src')
| -rw-r--r-- | src/parse/mod.rs | 48 | ||||
| -rw-r--r-- | src/parse/parser.rs | 10 | ||||
| -rw-r--r-- | src/parse/tokens.rs | 2 | ||||
| -rw-r--r-- | src/pretty.rs | 6 | ||||
| -rw-r--r-- | src/syntax/expr.rs | 4 | ||||
| -rw-r--r-- | src/syntax/token.rs | 6 |
6 files changed, 40 insertions, 36 deletions
diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 1d893ad9..c3d532a7 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -728,33 +728,29 @@ fn import_expr(p: &mut Parser) -> Option<Expr> { let start = p.next_start(); p.assert(Token::Import); - let mut import_expr = None; - if let Some(path) = expr(p) { - let imports = if p.expect(Token::Using) { - if p.eat_if(Token::Star) { - // This is the wildcard scenario. - Imports::Wildcard - } else { - // This is the list of identifier scenario. - p.start_group(Group::Expr, TokenMode::Code); - let items = collection(p).0; - if items.is_empty() { - p.expected_at("import items", p.prev_end()); - } - - let idents = idents(p, items); - p.end_group(); - Imports::Idents(idents) - } - } else { - Imports::Idents(vec![]) - }; + let imports = if p.eat_if(Token::Star) { + // This is the wildcard scenario. + Imports::Wildcard + } else { + // This is the list of identifiers scenario. + p.start_group(Group::Imports, TokenMode::Code); + let items = collection(p).0; + if items.is_empty() { + p.expected_at("import items", p.prev_end()); + } + p.end_group(); + Imports::Idents(idents(p, items)) + }; - import_expr = Some(Expr::Import(ImportExpr { - span: p.span(start), - imports, - path: Box::new(path), - })); + let mut import_expr = None; + if p.expect(Token::From) { + if let Some(path) = expr(p) { + import_expr = Some(Expr::Import(ImportExpr { + span: p.span(start), + imports, + path: Box::new(path), + })); + } } import_expr diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 8ea80d68..1e0f2f5d 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -51,6 +51,8 @@ pub enum Group { Stmt, /// A group for a single expression, ended by a line break. Expr, + /// A group for import items, ended by a semicolon, line break or `from`. + Imports, } impl<'s> Parser<'s> { @@ -128,6 +130,7 @@ impl<'s> Parser<'s> { Group::Brace => self.assert(Token::LeftBrace), Group::Stmt => {} Group::Expr => {} + Group::Imports => {} } } @@ -149,6 +152,7 @@ impl<'s> Parser<'s> { Group::Brace => Some((Token::RightBrace, true)), Group::Stmt => Some((Token::Semicolon, false)), Group::Expr => None, + Group::Imports => None, } { if self.next == Some(end) { // Bump the delimeter and return. No need to rescan in this case. @@ -361,6 +365,7 @@ impl<'s> Parser<'s> { Token::RightBracket => self.inside(Group::Bracket), Token::RightBrace => self.inside(Group::Brace), Token::Semicolon => self.inside(Group::Stmt), + Token::From => self.inside(Group::Imports), Token::Space(n) => n >= 1 && self.stop_at_newline(), _ => false, } { @@ -371,7 +376,10 @@ impl<'s> Parser<'s> { /// Whether the active group ends at a newline. fn stop_at_newline(&self) -> bool { let active = self.groups.last().map(|group| group.kind); - matches!(active, Some(Group::Stmt) | Some(Group::Expr)) + matches!( + active, + Some(Group::Stmt) | Some(Group::Expr) | Some(Group::Imports) + ) } /// Whether we are inside the given group. diff --git a/src/parse/tokens.rs b/src/parse/tokens.rs index aebe7b70..d979d005 100644 --- a/src/parse/tokens.rs +++ b/src/parse/tokens.rs @@ -496,7 +496,7 @@ fn keyword(id: &str) -> Option<Token<'static>> { "return" => Token::Return, "import" => Token::Import, "include" => Token::Include, - "using" => Token::Using, + "from" => Token::From, _ => return None, }) } diff --git a/src/pretty.rs b/src/pretty.rs index ff8841f3..46b82eb7 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -440,9 +440,9 @@ impl Pretty for ForPattern { impl Pretty for ImportExpr { fn pretty(&self, p: &mut Printer) { p.push_str("import "); - self.path.pretty(p); - p.push_str(" using "); self.imports.pretty(p); + p.push_str(" from "); + self.path.pretty(p); } } @@ -737,7 +737,7 @@ mod tests { roundtrip("#while x {y}"); roundtrip("#for x in y {z}"); roundtrip("#for k, x in y {z}"); - roundtrip("#import \"file.typ\" using *"); + roundtrip("#import * from \"file.typ\""); roundtrip("#include \"chapter1.typ\""); } diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index f5b79122..c4462ff8 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -59,7 +59,7 @@ pub enum Expr { While(WhileExpr), /// A for loop expression: `for x in y { z }`. For(ForExpr), - /// An import expression: `import "utils.typ" using a, b, c`. + /// An import expression: `import a, b, c from "utils.typ"`. Import(ImportExpr), /// An include expression: `include "chapter1.typ"`. Include(IncludeExpr), @@ -466,7 +466,7 @@ pub struct LetExpr { pub init: Option<Box<Expr>>, } -/// An import expression: `import "utils.typ" using a, b, c`. +/// An import expression: `import a, b, c from "utils.typ"`. #[derive(Debug, Clone, PartialEq)] pub struct ImportExpr { /// The source code location. diff --git a/src/syntax/token.rs b/src/syntax/token.rs index 425dac10..c14cf9f7 100644 --- a/src/syntax/token.rs +++ b/src/syntax/token.rs @@ -101,8 +101,8 @@ pub enum Token<'s> { Import, /// The `include` keyword. Include, - /// The `using` keyword. - Using, + /// The `from` keyword. + From, /// One or more whitespace characters. /// /// The contained `usize` denotes the number of newlines that were contained @@ -254,7 +254,7 @@ impl<'s> Token<'s> { Self::Return => "keyword `return`", Self::Import => "keyword `import`", Self::Include => "keyword `include`", - Self::Using => "keyword `using`", + Self::From => "keyword `from`", Self::Space(_) => "space", Self::Text(_) => "text", Self::UnicodeEscape(_) => "unicode escape sequence", |
