summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/ast.rs120
-rw-r--r--src/syntax/highlight.rs26
-rw-r--r--src/syntax/mod.rs92
3 files changed, 119 insertions, 119 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs
index 10e5ec70..425af0c1 100644
--- a/src/syntax/ast.rs
+++ b/src/syntax/ast.rs
@@ -67,9 +67,9 @@ impl Markup {
Some(MarkupNode::Text(s.clone()))
}
NodeKind::Escape(c) => Some(MarkupNode::Text((*c).into())),
+ NodeKind::NonBreakingSpace => Some(MarkupNode::Text('\u{00A0}'.into())),
NodeKind::EnDash => Some(MarkupNode::Text('\u{2013}'.into())),
NodeKind::EmDash => Some(MarkupNode::Text('\u{2014}'.into())),
- NodeKind::NonBreakingSpace => Some(MarkupNode::Text('\u{00A0}'.into())),
NodeKind::Strong => node.cast().map(MarkupNode::Strong),
NodeKind::Emph => node.cast().map(MarkupNode::Emph),
NodeKind::Raw(raw) => Some(MarkupNode::Raw(raw.as_ref().clone())),
@@ -219,16 +219,16 @@ pub enum Expr {
Lit(Lit),
/// An identifier: `left`.
Ident(Ident),
+ /// A code block: `{ let x = 1; x + 2 }`.
+ Code(CodeBlock),
+ /// A template block: `[*Hi* there!]`.
+ Template(TemplateBlock),
+ /// A grouped expression: `(1 + 2)`.
+ Group(GroupExpr),
/// An array expression: `(1, "hi", 12cm)`.
Array(ArrayExpr),
/// A dictionary expression: `(thickness: 3pt, pattern: dashed)`.
Dict(DictExpr),
- /// A template expression: `[*Hi* there!]`.
- Template(TemplateExpr),
- /// A grouped expression: `(1 + 2)`.
- Group(GroupExpr),
- /// A block expression: `{ let x = 1; x + 2 }`.
- Block(BlockExpr),
/// A unary operation: `-x`.
Unary(UnaryExpr),
/// A binary operation: `a + b`.
@@ -269,15 +269,15 @@ impl TypedNode for Expr {
fn from_red(node: RedRef) -> Option<Self> {
match node.kind() {
NodeKind::Ident(_) => node.cast().map(Self::Ident),
- NodeKind::Array => node.cast().map(Self::Array),
- NodeKind::Dict => node.cast().map(Self::Dict),
- NodeKind::Template => node.cast().map(Self::Template),
- NodeKind::Group => node.cast().map(Self::Group),
- NodeKind::Block => node.cast().map(Self::Block),
- NodeKind::Unary => node.cast().map(Self::Unary),
- NodeKind::Binary => node.cast().map(Self::Binary),
- NodeKind::Call => node.cast().map(Self::Call),
- NodeKind::Closure => node.cast().map(Self::Closure),
+ NodeKind::CodeBlock => node.cast().map(Self::Code),
+ NodeKind::TemplateBlock => node.cast().map(Self::Template),
+ NodeKind::GroupExpr => node.cast().map(Self::Group),
+ NodeKind::ArrayExpr => node.cast().map(Self::Array),
+ NodeKind::DictExpr => node.cast().map(Self::Dict),
+ NodeKind::UnaryExpr => node.cast().map(Self::Unary),
+ NodeKind::BinaryExpr => node.cast().map(Self::Binary),
+ NodeKind::CallExpr => node.cast().map(Self::Call),
+ NodeKind::ClosureExpr => node.cast().map(Self::Closure),
NodeKind::WithExpr => node.cast().map(Self::With),
NodeKind::LetExpr => node.cast().map(Self::Let),
NodeKind::SetExpr => node.cast().map(Self::Set),
@@ -298,12 +298,12 @@ impl TypedNode for Expr {
fn as_red(&self) -> RedRef<'_> {
match self {
Self::Lit(v) => v.as_red(),
+ Self::Code(v) => v.as_red(),
+ Self::Template(v) => v.as_red(),
Self::Ident(v) => v.as_red(),
Self::Array(v) => v.as_red(),
Self::Dict(v) => v.as_red(),
- Self::Template(v) => v.as_red(),
Self::Group(v) => v.as_red(),
- Self::Block(v) => v.as_red(),
Self::Unary(v) => v.as_red(),
Self::Binary(v) => v.as_red(),
Self::Call(v) => v.as_red(),
@@ -407,8 +407,44 @@ pub enum LitKind {
}
node! {
+ /// A code block: `{ let x = 1; x + 2 }`.
+ CodeBlock: CodeBlock
+}
+
+impl CodeBlock {
+ /// The list of expressions contained in the block.
+ pub fn exprs(&self) -> impl Iterator<Item = Expr> + '_ {
+ self.0.children().filter_map(RedRef::cast)
+ }
+}
+
+node! {
+ /// A template block: `[*Hi* there!]`.
+ TemplateBlock: TemplateBlock
+}
+
+impl TemplateBlock {
+ /// The contents of the template.
+ pub fn body(&self) -> Markup {
+ self.0.cast_first_child().expect("template is missing body")
+ }
+}
+
+node! {
+ /// A grouped expression: `(1 + 2)`.
+ GroupExpr: GroupExpr
+}
+
+impl GroupExpr {
+ /// The wrapped expression.
+ pub fn expr(&self) -> Expr {
+ self.0.cast_first_child().expect("group is missing expression")
+ }
+}
+
+node! {
/// An array expression: `(1, "hi", 12cm)`.
- ArrayExpr: Array
+ ArrayExpr: ArrayExpr
}
impl ArrayExpr {
@@ -420,7 +456,7 @@ impl ArrayExpr {
node! {
/// A dictionary expression: `(thickness: 3pt, pattern: dashed)`.
- DictExpr: Dict
+ DictExpr: DictExpr
}
impl DictExpr {
@@ -448,44 +484,8 @@ impl Named {
}
node! {
- /// A template expression: `[*Hi* there!]`.
- TemplateExpr: Template
-}
-
-impl TemplateExpr {
- /// The contents of the template.
- pub fn body(&self) -> Markup {
- self.0.cast_first_child().expect("template is missing body")
- }
-}
-
-node! {
- /// A grouped expression: `(1 + 2)`.
- GroupExpr: Group
-}
-
-impl GroupExpr {
- /// The wrapped expression.
- pub fn expr(&self) -> Expr {
- self.0.cast_first_child().expect("group is missing expression")
- }
-}
-
-node! {
- /// A block expression: `{ let x = 1; x + 2 }`.
- BlockExpr: Block
-}
-
-impl BlockExpr {
- /// The list of expressions contained in the block.
- pub fn exprs(&self) -> impl Iterator<Item = Expr> + '_ {
- self.0.children().filter_map(RedRef::cast)
- }
-}
-
-node! {
/// A unary operation: `-x`.
- UnaryExpr: Unary
+ UnaryExpr: UnaryExpr
}
impl UnaryExpr {
@@ -545,7 +545,7 @@ impl UnOp {
node! {
/// A binary operation: `a + b`.
- BinaryExpr: Binary
+ BinaryExpr: BinaryExpr
}
impl BinaryExpr {
@@ -717,7 +717,7 @@ pub enum Associativity {
node! {
/// An invocation of a function: `foo(...)`.
- CallExpr: Call
+ CallExpr: CallExpr
}
impl CallExpr {
@@ -786,7 +786,7 @@ impl CallArg {
node! {
/// A closure expression: `(x, y) => z`.
- ClosureExpr: Closure
+ ClosureExpr: ClosureExpr
}
impl ClosureExpr {
diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs
index 82f1ea0e..20ea4039 100644
--- a/src/syntax/highlight.rs
+++ b/src/syntax/highlight.rs
@@ -104,10 +104,10 @@ impl Category {
/// Determine the highlighting category of a node given its parent.
pub fn determine(child: RedRef, parent: RedRef) -> Option<Category> {
match child.kind() {
- NodeKind::LeftBracket => Some(Category::Bracket),
- NodeKind::RightBracket => Some(Category::Bracket),
NodeKind::LeftBrace => Some(Category::Bracket),
NodeKind::RightBrace => Some(Category::Bracket),
+ NodeKind::LeftBracket => Some(Category::Bracket),
+ NodeKind::RightBracket => Some(Category::Bracket),
NodeKind::LeftParen => Some(Category::Bracket),
NodeKind::RightParen => Some(Category::Bracket),
NodeKind::Comma => Some(Category::Punctuation),
@@ -176,13 +176,13 @@ impl Category {
NodeKind::Auto => Some(Category::Auto),
NodeKind::Ident(_) => match parent.kind() {
NodeKind::Named => None,
- NodeKind::Closure if child.span().start == parent.span().start => {
+ NodeKind::ClosureExpr if child.span().start == parent.span().start => {
Some(Category::Function)
}
NodeKind::WithExpr => Some(Category::Function),
NodeKind::SetExpr => Some(Category::Function),
NodeKind::ShowExpr => Some(Category::Function),
- NodeKind::Call => Some(Category::Function),
+ NodeKind::CallExpr => Some(Category::Function),
_ => Some(Category::Variable),
},
NodeKind::Bool(_) => Some(Category::Bool),
@@ -202,18 +202,18 @@ impl Category {
NodeKind::TextInLine(_) => None,
NodeKind::List => None,
NodeKind::Enum => None,
- NodeKind::Array => None,
- NodeKind::Dict => None,
+ NodeKind::CodeBlock => None,
+ NodeKind::TemplateBlock => None,
+ NodeKind::GroupExpr => None,
+ NodeKind::ArrayExpr => None,
+ NodeKind::DictExpr => None,
NodeKind::Named => None,
- NodeKind::Template => None,
- NodeKind::Group => None,
- NodeKind::Block => None,
- NodeKind::Unary => None,
- NodeKind::Binary => None,
- NodeKind::Call => None,
+ NodeKind::UnaryExpr => None,
+ NodeKind::BinaryExpr => None,
+ NodeKind::CallExpr => None,
NodeKind::CallArgs => None,
NodeKind::Spread => None,
- NodeKind::Closure => None,
+ NodeKind::ClosureExpr => None,
NodeKind::ClosureParams => None,
NodeKind::WithExpr => None,
NodeKind::LetExpr => None,
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index 85f2013c..a3393eda 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -481,14 +481,14 @@ impl ExactSizeIterator for Children<'_> {}
/// the parser.
#[derive(Debug, Clone, PartialEq)]
pub enum NodeKind {
- /// A left square bracket: `[`.
- LeftBracket,
- /// A right square bracket: `]`.
- RightBracket,
/// A left curly brace: `{`.
LeftBrace,
/// A right curly brace: `}`.
RightBrace,
+ /// A left square bracket: `[`.
+ LeftBracket,
+ /// A right square bracket: `]`.
+ RightBracket,
/// A left round parenthesis: `(`.
LeftParen,
/// A right round parenthesis: `)`.
@@ -642,30 +642,30 @@ pub enum NodeKind {
Fraction(f64),
/// A quoted string: `"..."`.
Str(EcoString),
+ /// A code block: `{ let x = 1; x + 2 }`.
+ CodeBlock,
+ /// A template block: `[*Hi* there!]`.
+ TemplateBlock,
+ /// A grouped expression: `(1 + 2)`.
+ GroupExpr,
/// An array expression: `(1, "hi", 12cm)`.
- Array,
+ ArrayExpr,
/// A dictionary expression: `(thickness: 3pt, pattern: dashed)`.
- Dict,
+ DictExpr,
/// A named pair: `thickness: 3pt`.
Named,
- /// A template expression: `[*Hi* there!]`.
- Template,
- /// A grouped expression: `(1 + 2)`.
- Group,
- /// A block expression: `{ let x = 1; x + 2 }`.
- Block,
/// A unary operation: `-x`.
- Unary,
+ UnaryExpr,
/// A binary operation: `a + b`.
- Binary,
+ BinaryExpr,
/// An invocation of a function: `f(x, y)`.
- Call,
+ CallExpr,
/// A function call's argument list: `(x, y)`.
CallArgs,
/// Spreaded arguments or a parameter sink: `..x`.
Spread,
/// A closure expression: `(x, y) => z`.
- Closure,
+ ClosureExpr,
/// A closure's parameters: `(x, y)`.
ClosureParams,
/// A with expression: `f with (x, y: 1)`.
@@ -724,16 +724,16 @@ pub enum ErrorPos {
}
impl NodeKind {
- /// Whether this is some kind of bracket.
- pub fn is_bracket(&self) -> bool {
- matches!(self, Self::LeftBracket | Self::RightBracket)
- }
-
/// Whether this is some kind of brace.
pub fn is_brace(&self) -> bool {
matches!(self, Self::LeftBrace | Self::RightBrace)
}
+ /// Whether this is some kind of bracket.
+ pub fn is_bracket(&self) -> bool {
+ matches!(self, Self::LeftBracket | Self::RightBracket)
+ }
+
/// Whether this is some kind of parenthesis.
pub fn is_paren(&self) -> bool {
matches!(self, Self::LeftParen | Self::RightParen)
@@ -782,10 +782,10 @@ impl NodeKind {
| Self::List
| Self::Raw(_)
| Self::Math(_) => Some(TokenMode::Markup),
- Self::Template
+ Self::TemplateBlock
| Self::Space(_)
- | Self::Block
| Self::Ident(_)
+ | Self::CodeBlock
| Self::LetExpr
| Self::SetExpr
| Self::ShowExpr
@@ -794,7 +794,7 @@ impl NodeKind {
| Self::WhileExpr
| Self::ForExpr
| Self::ImportExpr
- | Self::Call
+ | Self::CallExpr
| Self::IncludeExpr
| Self::LineComment
| Self::BlockComment
@@ -808,10 +808,10 @@ impl NodeKind {
/// A human-readable name for the kind.
pub fn as_str(&self) -> &'static str {
match self {
- Self::LeftBracket => "opening bracket",
- Self::RightBracket => "closing bracket",
Self::LeftBrace => "opening brace",
Self::RightBrace => "closing brace",
+ Self::LeftBracket => "opening bracket",
+ Self::RightBracket => "closing bracket",
Self::LeftParen => "opening paren",
Self::RightParen => "closing paren",
Self::Star => "star",
@@ -883,18 +883,18 @@ impl NodeKind {
Self::Percentage(_) => "percentage",
Self::Fraction(_) => "`fr` value",
Self::Str(_) => "string",
- Self::Array => "array",
- Self::Dict => "dictionary",
+ Self::CodeBlock => "code block",
+ Self::TemplateBlock => "template block",
+ Self::GroupExpr => "group",
+ Self::ArrayExpr => "array",
+ Self::DictExpr => "dictionary",
Self::Named => "named argument",
- Self::Template => "template",
- Self::Group => "group",
- Self::Block => "block",
- Self::Unary => "unary expression",
- Self::Binary => "binary expression",
- Self::Call => "call",
+ Self::UnaryExpr => "unary expression",
+ Self::BinaryExpr => "binary expression",
+ Self::CallExpr => "call",
Self::CallArgs => "call arguments",
Self::Spread => "parameter sink",
- Self::Closure => "closure",
+ Self::ClosureExpr => "closure",
Self::ClosureParams => "closure parameters",
Self::WithExpr => "`with` expression",
Self::LetExpr => "`let` expression",
@@ -932,10 +932,10 @@ impl Hash for NodeKind {
fn hash<H: Hasher>(&self, state: &mut H) {
std::mem::discriminant(self).hash(state);
match self {
- Self::LeftBracket => {}
- Self::RightBracket => {}
Self::LeftBrace => {}
Self::RightBrace => {}
+ Self::LeftBracket => {}
+ Self::RightBracket => {}
Self::LeftParen => {}
Self::RightParen => {}
Self::Star => {}
@@ -1007,18 +1007,18 @@ impl Hash for NodeKind {
Self::Percentage(v) => v.to_bits().hash(state),
Self::Fraction(v) => v.to_bits().hash(state),
Self::Str(v) => v.hash(state),
- Self::Array => {}
- Self::Dict => {}
+ Self::CodeBlock => {}
+ Self::TemplateBlock => {}
+ Self::GroupExpr => {}
+ Self::ArrayExpr => {}
+ Self::DictExpr => {}
Self::Named => {}
- Self::Template => {}
- Self::Group => {}
- Self::Block => {}
- Self::Unary => {}
- Self::Binary => {}
- Self::Call => {}
+ Self::UnaryExpr => {}
+ Self::BinaryExpr => {}
+ Self::CallExpr => {}
Self::CallArgs => {}
Self::Spread => {}
- Self::Closure => {}
+ Self::ClosureExpr => {}
Self::ClosureParams => {}
Self::WithExpr => {}
Self::LetExpr => {}