diff options
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/ast.rs | 30 | ||||
| -rw-r--r-- | src/syntax/kind.rs | 9 | ||||
| -rw-r--r-- | src/syntax/parser.rs | 27 |
3 files changed, 31 insertions, 35 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 64f54e37..45f79685 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -121,11 +121,10 @@ pub enum Expr { MathIdent(MathIdent), /// An alignment point in a math formula: `&`. MathAlignPoint(MathAlignPoint), - /// A subsection in a math formula that is surrounded by matched delimiters: - /// `[x + y]`. + /// Matched delimiters surrounding math in a formula: `[x + y]`. MathDelimited(MathDelimited), - /// A base with optional sub- and superscripts in a math formula: `a_1^2`. - MathScript(MathScript), + /// A base with optional attachments in a formula: `a_1^2`. + MathAttach(MathAttach), /// A fraction in a math formula: `x/2`. MathFrac(MathFrac), /// An identifier: `left`. @@ -224,7 +223,7 @@ impl AstNode for Expr { SyntaxKind::MathIdent => node.cast().map(Self::MathIdent), SyntaxKind::MathAlignPoint => node.cast().map(Self::MathAlignPoint), SyntaxKind::MathDelimited => node.cast().map(Self::MathDelimited), - SyntaxKind::MathScript => node.cast().map(Self::MathScript), + SyntaxKind::MathAttach => node.cast().map(Self::MathAttach), SyntaxKind::MathFrac => node.cast().map(Self::MathFrac), SyntaxKind::Ident => node.cast().map(Self::Ident), SyntaxKind::None => node.cast().map(Self::None), @@ -285,7 +284,7 @@ impl AstNode for Expr { Self::MathIdent(v) => v.as_untyped(), Self::MathAlignPoint(v) => v.as_untyped(), Self::MathDelimited(v) => v.as_untyped(), - Self::MathScript(v) => v.as_untyped(), + Self::MathAttach(v) => v.as_untyped(), Self::MathFrac(v) => v.as_untyped(), Self::Ident(v) => v.as_untyped(), Self::None(v) => v.as_untyped(), @@ -709,8 +708,7 @@ node! { } node! { - /// A subsection in a math formula that is surrounded by matched delimiters: - /// `[x + y]`. + /// Matched delimiters surrounding math in a formula: `[x + y]`. MathDelimited } @@ -732,26 +730,26 @@ impl MathDelimited { } node! { - /// A base with an optional sub- and superscript in a formula: `a_1^2`. - MathScript + /// A base with optional attachments in a formula: `a_1^2`. + MathAttach } -impl MathScript { - /// The base of the script. +impl MathAttach { + /// The base, to which things are attached. pub fn base(&self) -> Expr { self.0.cast_first_match().unwrap_or_default() } - /// The subscript. - pub fn sub(&self) -> Option<Expr> { + /// The bottom attachment. + pub fn bottom(&self) -> Option<Expr> { self.0 .children() .skip_while(|node| !matches!(node.kind(), SyntaxKind::Underscore)) .find_map(SyntaxNode::cast) } - /// The superscript. - pub fn sup(&self) -> Option<Expr> { + /// The top attachment. + pub fn top(&self) -> Option<Expr> { self.0 .children() .skip_while(|node| !matches!(node.kind(), SyntaxKind::Hat)) diff --git a/src/syntax/kind.rs b/src/syntax/kind.rs index f0a0bc5a..aa4a5cfe 100644 --- a/src/syntax/kind.rs +++ b/src/syntax/kind.rs @@ -65,11 +65,10 @@ pub enum SyntaxKind { MathIdent, /// An alignment point in math: `&`. MathAlignPoint, - /// A subsection in a math formula that is surrounded by matched delimiters: - /// `[x + y]`. + /// Matched delimiters surrounding math in a formula: `[x + y]`. MathDelimited, - /// A base with optional sub- and superscripts in math: `a_1^2`. - MathScript, + /// A base with optional attachments in a formula: `a_1^2`. + MathAttach, /// A fraction in math: `x/2`. MathFrac, @@ -349,7 +348,7 @@ impl SyntaxKind { Self::MathAtom => "math atom", Self::MathAlignPoint => "math alignment point", Self::MathDelimited => "delimited math", - Self::MathScript => "math script", + Self::MathAttach => "math attachments", Self::MathFrac => "math fraction", Self::Hashtag => "hashtag", Self::LeftBrace => "opening brace", diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index f6ed2f5d..07730533 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -234,21 +234,20 @@ fn math_expr_prec(p: &mut Parser, min_prec: usize, stop: SyntaxKind) { SyntaxKind::Hashtag => embedded_code_expr(p), SyntaxKind::MathIdent => { p.eat(); + while p.directly_at(SyntaxKind::MathAtom) + && p.current_text() == "." + && matches!( + p.lexer.clone().next(), + SyntaxKind::MathIdent | SyntaxKind::MathAtom + ) + { + p.convert(SyntaxKind::Dot); + p.convert(SyntaxKind::Ident); + p.wrap(m, SyntaxKind::FieldAccess); + } if p.directly_at(SyntaxKind::MathAtom) && p.current_text() == "(" { math_args(p); p.wrap(m, SyntaxKind::FuncCall); - } else { - while p.directly_at(SyntaxKind::MathAtom) - && p.current_text() == "." - && matches!( - p.lexer.clone().next(), - SyntaxKind::MathIdent | SyntaxKind::MathAtom - ) - { - p.convert(SyntaxKind::Dot); - p.convert(SyntaxKind::Ident); - p.wrap(m, SyntaxKind::FieldAccess); - } } } @@ -362,10 +361,10 @@ fn math_class(text: &str) -> Option<MathClass> { fn math_op(kind: SyntaxKind) -> Option<(SyntaxKind, SyntaxKind, ast::Assoc, usize)> { match kind { SyntaxKind::Underscore => { - Some((SyntaxKind::MathScript, SyntaxKind::Hat, ast::Assoc::Right, 2)) + Some((SyntaxKind::MathAttach, SyntaxKind::Hat, ast::Assoc::Right, 2)) } SyntaxKind::Hat => { - Some((SyntaxKind::MathScript, SyntaxKind::Underscore, ast::Assoc::Right, 2)) + Some((SyntaxKind::MathAttach, SyntaxKind::Underscore, ast::Assoc::Right, 2)) } SyntaxKind::Slash => { Some((SyntaxKind::MathFrac, SyntaxKind::Eof, ast::Assoc::Left, 1)) |
