summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/ast.rs2
-rw-r--r--src/syntax/highlight.rs2
-rw-r--r--src/syntax/mod.rs29
3 files changed, 27 insertions, 6 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs
index ed74dfe5..bea4ef00 100644
--- a/src/syntax/ast.rs
+++ b/src/syntax/ast.rs
@@ -53,7 +53,7 @@ macro_rules! node {
node! {
/// The syntactical root capable of representing a full parsed document.
- Markup
+ Markup: NodeKind::Markup(_)
}
impl Markup {
diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs
index 21af060f..9f7365a8 100644
--- a/src/syntax/highlight.rs
+++ b/src/syntax/highlight.rs
@@ -154,7 +154,7 @@ impl Category {
NodeKind::Str(_) => Some(Category::String),
NodeKind::Error(_, _) => Some(Category::Invalid),
NodeKind::Unknown(_) => Some(Category::Invalid),
- NodeKind::Markup => None,
+ NodeKind::Markup(_) => None,
NodeKind::Space(_) => None,
NodeKind::Parbreak => None,
NodeKind::Text(_) => None,
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index b72e5843..388d0bb0 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -64,6 +64,14 @@ impl Green {
}
}
+ /// Whether the node is a leaf node in the green tree.
+ pub fn is_leaf(&self) -> bool {
+ match self {
+ Green::Node(n) => n.children().is_empty(),
+ Green::Token(_) => true,
+ }
+ }
+
/// Change the type of the node.
pub fn convert(&mut self, kind: NodeKind) {
match self {
@@ -361,6 +369,11 @@ impl<'a> RedRef<'a> {
Span::new(self.id, self.offset, self.offset + self.green.len())
}
+ /// Whether the node is a leaf node.
+ pub fn is_leaf(self) -> bool {
+ self.green.is_leaf()
+ }
+
/// The error messages for this node and its descendants.
pub fn errors(self) -> Vec<Error> {
if !self.green.erroneous() {
@@ -385,6 +398,14 @@ impl<'a> RedRef<'a> {
}
}
+ /// Perform a depth-first search starting at this node.
+ pub fn all_children(&self) -> Vec<Self> {
+ let mut res = vec![self.clone()];
+ res.extend(self.children().flat_map(|child| child.all_children().into_iter()));
+
+ res
+ }
+
/// Convert the node to a typed AST node.
pub fn cast<T>(self) -> Option<T>
where
@@ -562,8 +583,8 @@ pub enum NodeKind {
Include,
/// The `from` keyword.
From,
- /// Template markup.
- Markup,
+ /// Template markup of which all lines must start in some column.
+ Markup(usize),
/// One or more whitespace characters.
Space(usize),
/// A forced line break: `\`.
@@ -738,7 +759,7 @@ impl NodeKind {
/// Whether this token appears in Markup.
pub fn mode(&self) -> Option<TokenMode> {
match self {
- Self::Markup
+ Self::Markup(_)
| Self::Linebreak
| Self::Parbreak
| Self::Text(_)
@@ -823,7 +844,7 @@ impl NodeKind {
Self::Import => "keyword `import`",
Self::Include => "keyword `include`",
Self::From => "keyword `from`",
- Self::Markup => "markup",
+ Self::Markup(_) => "markup",
Self::Space(_) => "space",
Self::Linebreak => "forced linebreak",
Self::Parbreak => "paragraph break",