Skip to content

Commit

Permalink
refactor(template-compiler): better AST
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Nov 6, 2024
1 parent 390a5cb commit 5158482
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 47 deletions.
16 changes: 16 additions & 0 deletions glass-easel-template-compiler/src/parse/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ use crate::binding_map::{BindingMapCollector, BindingMapKeys};
use super::{ParseErrorKind, ParseState, Position, TemplateStructure};

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Expression {
/// A reference to a data scopes.
///
/// Some syntax could generate a new variable that can be used in expressions.
/// This variable is called a "scope".
/// These syntax includes:
///
/// - a script module (a.k.a. `<wxs />` element) introduces its name that can be used in the whole file;
/// - a `wx:for` introduces `item` and `index` that can be used in its subtree;
/// - each slot value (a.k.a. `slot:` attribute) introduces a new scope that can be used in the subtree of its element.
///
/// These scopes are indexed based on following rules.
///
/// - In an expression, its usable scopes are the scopes introduced by all its ancestors, where closer parents has larger indices.
/// - For any `wx:for` parent, `item` has a smaller index than `index`.
/// - The script module scopes are file-global so they always occupies the smallest indices.
ScopeRef {
location: Range<Position>,
index: usize,
Expand Down
16 changes: 16 additions & 0 deletions glass-easel-template-compiler/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,22 @@ pub struct Position {
pub utf16_col: u32,
}

impl PartialOrd for Position {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Position {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
if self.line == other.line {
self.utf16_col.cmp(&other.utf16_col)
} else {
self.line.cmp(&other.line)
}
}
}

impl Position {
/// Get the line-column offsets (in UTF-16) in the source code.
pub fn line_col_utf16<'s>(&self) -> (usize, usize) {
Expand Down
Loading

0 comments on commit 5158482

Please sign in to comment.