Skip to content

Commit

Permalink
nix-sema: split evaluable & raw, add Select, OpHasAttr, Attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Jan 9, 2024
1 parent 691684c commit c3bddf3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
51 changes: 38 additions & 13 deletions nix-sema/src/syntax.rs → nix-sema/src/evaluable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Structured syntax nodes.
//! They should be able to represent any raw nix syntax.
use std::collections::HashMap;

/// Upstream type defined at [here](https://github.com/NixOS/nix/blob/8e865f3aba526394ca333efe7258bd8db0050fbb/src/libexpr/value.hh#L74)
pub type NixInt = i64;

Expand All @@ -20,18 +22,22 @@ pub struct Position {
pub column: u32,
}

pub enum Interpolable<'src> {
/// The fragment should be **escaped**.
/// This is a technical design decision, because "escaping" is tight to lexers.
Fragment(&'src str),
pub enum AttrName<'src> {
Normal(&'src str),
Expr(Box<EvaluableExpression<'src>>),
}

/// String interpolation. ${ expr }
Interpolation(RawExpression<'src>),
pub struct AttrDef<'src> {
pub inherited: bool,
pub e: Box<EvaluableExpression<'src>>,
pub pos: Position,
pub displ: Displacement,
}

/// Expressions, they can be evaluated after desugared / lowering.
pub enum RawExpression<'src> {
String(Vec<Interpolable<'src>>),
pub struct DynamicAttrDef<'src> {
pub name_expr: Box<EvaluableExpression<'src>>,
pub value_expr: Box<EvaluableExpression<'src>>,
pub pos: Position,
}

/// Evaluable expressions.
Expand Down Expand Up @@ -90,9 +96,28 @@ pub enum EvaluableExpression<'src> {
level: Level,
displ: Displacement,
},
}

pub enum Node<'src> {
RawExpression(RawExpression<'src>),
EvaluableExpression(EvaluableExpression<'src>),
Select {
pos: Position,

/// Selection body.
e: Option<Box<EvaluableExpression<'src>>>,

/// Default expr, e.g. `a.b.c or expr`
def: Option<Box<EvaluableExpression<'src>>>,

attr_path: Vec<AttrName<'src>>,
},

OpHasAttr {
e: Box<EvaluableExpression<'src>>,
attr_path: Vec<AttrName<'src>>,
},

Attrs {
recursive: bool,
pos: Position,
attrs: HashMap<&'src str, AttrDef<'src>>,
dynamic_attrs: Vec<DynamicAttrDef<'src>>,
},
}
12 changes: 11 additions & 1 deletion nix-sema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
//! The library aims to be parser-agnostic, and the design decision is trasform "raw" nix syntax to evaluable nodes.
//! Transformation could be erroneous and the library try to recover from most cases,
//! and also attach information for pretty-print or lsp purposes.
pub mod syntax;
use evaluable::EvaluableExpression;
use raw::RawExpression;

pub mod evaluable;
pub mod raw;

pub enum Node<'src> {
RawExpression(RawExpression<'src>),
EvaluableExpression(EvaluableExpression<'src>),
}

#[cfg(test)]
mod tests {
Expand Down
13 changes: 13 additions & 0 deletions nix-sema/src/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub enum Interpolable<'src> {
/// The fragment should be **escaped**.
/// This is a technical design decision, because "escaping" is tight to lexers.
Fragment(&'src str),

/// String interpolation. ${ expr }
Interpolation(RawExpression<'src>),
}

/// Expressions, they can be evaluated after desugared / lowering.
pub enum RawExpression<'src> {
String(Vec<Interpolable<'src>>),
}

0 comments on commit c3bddf3

Please sign in to comment.