diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-01-03 12:29:35 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-01-03 12:32:17 +0100 |
| commit | 29b31c4a5ac4cde311c4d38b3d70130e7d27ba76 (patch) | |
| tree | fe4e5dbd2166a69af90e69578ad4602725cdb63c /src/syntax | |
| parent | 54962e6dcd002fd27918827996155fd7dc4e1cff (diff) | |
New import syntax
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/ast.rs | 35 | ||||
| -rw-r--r-- | src/syntax/kind.rs | 8 | ||||
| -rw-r--r-- | src/syntax/parser.rs | 8 | ||||
| -rw-r--r-- | src/syntax/parsing.rs | 33 | ||||
| -rw-r--r-- | src/syntax/tokens.rs | 2 |
5 files changed, 38 insertions, 48 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index ccad77c2..4ef0b2a6 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -1430,29 +1430,26 @@ impl ForPattern { } node! { - /// A module import: `import a, b, c from "utils.typ"`. + /// A module import: `import "utils.typ": a, b, c`. ModuleImport } impl ModuleImport { - /// The items to be imported. - pub fn imports(&self) -> Imports { - self.0 - .children() - .find_map(|node| match node.kind() { - SyntaxKind::Star => Some(Imports::Wildcard), - SyntaxKind::ImportItems => { - let items = node.children().filter_map(SyntaxNode::cast).collect(); - Some(Imports::Items(items)) - } - _ => None, - }) - .expect("module import is missing items") + /// The module or path from which the items should be imported. + pub fn source(&self) -> Expr { + self.0.cast_last_child().expect("module import is missing source") } - /// The path to the file that should be imported. - pub fn path(&self) -> Expr { - self.0.cast_last_child().expect("module import is missing path") + /// The items to be imported. + pub fn imports(&self) -> Option<Imports> { + self.0.children().find_map(|node| match node.kind() { + SyntaxKind::Star => Some(Imports::Wildcard), + SyntaxKind::ImportItems => { + let items = node.children().filter_map(SyntaxNode::cast).collect(); + Some(Imports::Items(items)) + } + _ => None, + }) } } @@ -1471,8 +1468,8 @@ node! { } impl ModuleInclude { - /// The path to the file that should be included. - pub fn path(&self) -> Expr { + /// The module or path from which the content should be included. + pub fn source(&self) -> Expr { self.0.cast_last_child().expect("module include is missing path") } } diff --git a/src/syntax/kind.rs b/src/syntax/kind.rs index 27a8da0f..54d5c81d 100644 --- a/src/syntax/kind.rs +++ b/src/syntax/kind.rs @@ -125,8 +125,8 @@ pub enum SyntaxKind { Import, /// The `include` keyword. Include, - /// The `from` keyword. - From, + /// The `as` keyword. + As, /// Markup of which all lines must have a minimal indentation. /// @@ -387,7 +387,7 @@ impl SyntaxKind { Self::Return => "keyword `return`", Self::Import => "keyword `import`", Self::Include => "keyword `include`", - Self::From => "keyword `from`", + Self::As => "keyword `as`", Self::Markup { .. } => "markup", Self::Text(_) => "text", Self::Linebreak => "linebreak", @@ -514,7 +514,7 @@ impl Hash for SyntaxKind { Self::Return => {} Self::Import => {} Self::Include => {} - Self::From => {} + Self::As => {} Self::Markup { min_indent } => min_indent.hash(state), Self::Text(s) => s.hash(state), Self::Linebreak => {} diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index a0b1e7d1..74be792f 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -237,7 +237,7 @@ impl<'s> Parser<'s> { self.tokens.set_mode(match kind { Group::Bracket | Group::Strong | Group::Emph => TokenMode::Markup, Group::Math | Group::MathRow(_, _) => TokenMode::Math, - Group::Brace | Group::Paren | Group::Expr | Group::Imports => TokenMode::Code, + Group::Brace | Group::Paren | Group::Expr => TokenMode::Code, }); match kind { @@ -249,7 +249,6 @@ impl<'s> Parser<'s> { Group::Math => self.assert(SyntaxKind::Dollar), Group::MathRow(l, _) => self.assert(SyntaxKind::Atom(l.into())), Group::Expr => self.repeek(), - Group::Imports => self.repeek(), } } @@ -274,7 +273,6 @@ impl<'s> Parser<'s> { Group::Math => Some((SyntaxKind::Dollar, true)), Group::MathRow(_, r) => Some((SyntaxKind::Atom(r.into()), true)), Group::Expr => Some((SyntaxKind::Semicolon, false)), - Group::Imports => None, } { if self.current.as_ref() == Some(&end) { // If another group closes after a group with the missing @@ -346,7 +344,6 @@ impl<'s> Parser<'s> { .next() .map_or(false, |group| group.kind == Group::Math), Some(SyntaxKind::Semicolon) => self.inside(Group::Expr), - Some(SyntaxKind::From) => self.inside(Group::Imports), Some(SyntaxKind::Atom(s)) => match s.as_str() { ")" => self.inside(Group::MathRow('(', ')')), "}" => self.inside(Group::MathRow('{', '}')), @@ -377,7 +374,6 @@ impl<'s> Parser<'s> { match self.groups.last().map(|group| group.kind) { Some(Group::Strong | Group::Emph) => n >= 2, - Some(Group::Imports) => n >= 1, Some(Group::Expr) if n >= 1 => { // Allow else and method call to continue on next line. self.groups.iter().nth_back(1).map(|group| group.kind) @@ -541,8 +537,6 @@ pub enum Group { MathRow(char, char), /// A group ended by a semicolon or a line break: `;`, `\n`. Expr, - /// A group for import items, ended by a semicolon, line break or `from`. - Imports, } impl Group { diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index 900e0e67..7f557fac 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -1052,27 +1052,26 @@ fn for_pattern(p: &mut Parser) -> ParseResult { fn module_import(p: &mut Parser) -> ParseResult { p.perform(SyntaxKind::ModuleImport, |p| { p.assert(SyntaxKind::Import); + expr(p)?; - if !p.eat_if(SyntaxKind::Star) { - // This is the list of identifiers scenario. - p.perform(SyntaxKind::ImportItems, |p| { - p.start_group(Group::Imports); - let marker = p.marker(); - let items = collection(p, false).1; - if items == 0 { - p.expected("import items"); - } - p.end_group(); + if !p.eat_if(SyntaxKind::Colon) || p.eat_if(SyntaxKind::Star) { + return Ok(()); + } - marker.filter_children(p, |n| match n.kind() { - SyntaxKind::Ident(_) | SyntaxKind::Comma => Ok(()), - _ => Err("expected identifier"), - }); + // This is the list of identifiers scenario. + p.perform(SyntaxKind::ImportItems, |p| { + let marker = p.marker(); + let items = collection(p, false).1; + if items == 0 { + p.expected("import items"); + } + marker.filter_children(p, |n| match n.kind() { + SyntaxKind::Ident(_) | SyntaxKind::Comma => Ok(()), + _ => Err("expected identifier"), }); - }; + }); - p.expect(SyntaxKind::From)?; - expr(p) + Ok(()) }) } diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index 98b6d8a8..02bbd3a4 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -684,7 +684,7 @@ fn keyword(ident: &str) -> Option<SyntaxKind> { "return" => SyntaxKind::Return, "import" => SyntaxKind::Import, "include" => SyntaxKind::Include, - "from" => SyntaxKind::From, + "as" => SyntaxKind::As, _ => return None, }) } |
