aboutsummaryrefslogtreecommitdiff
path: root/src/ast.rs
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-04-03 06:34:47 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-04-03 06:34:47 +0200
commit38b60098e42b4a899eb574f65fa14d4fdbe82f9b (patch)
tree3956fe1ab8107c5feb043fd466a969da8f5b1f9a /src/ast.rs
parentd20acec5efcad4b3cd493f914d10e3d11cdfee4d (diff)
downloadele-38b60098e42b4a899eb574f65fa14d4fdbe82f9b.tar.gz
ele-38b60098e42b4a899eb574f65fa14d4fdbe82f9b.zip
Init
Diffstat (limited to 'src/ast.rs')
-rw-r--r--src/ast.rs106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/ast.rs b/src/ast.rs
new file mode 100644
index 0000000..54128d8
--- /dev/null
+++ b/src/ast.rs
@@ -0,0 +1,106 @@
+/// Position in the filter string (line and column, 1-indexed).
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct Position {
+ pub line: usize,
+ pub col: usize,
+}
+
+impl std::fmt::Display for Position {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}:{}", self.line, self.col)
+ }
+}
+
+/// Binary operators: AND / OR.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum BinaryOp {
+ And,
+ Or,
+}
+
+/// Unary operators: negation.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum UnaryOp {
+ Minus,
+ Not,
+}
+
+/// Comparison operators.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum CompareOp {
+ Equals,
+ NotEquals,
+ LessThan,
+ LessThanEquals,
+ GreaterThan,
+ GreaterThanEquals,
+ Has,
+ MatchesRegexp,
+ NotMatchesRegexp,
+}
+
+impl CompareOp {
+ pub fn as_str(&self) -> &'static str {
+ match self {
+ CompareOp::Equals => "=",
+ CompareOp::NotEquals => "!=",
+ CompareOp::LessThan => "<",
+ CompareOp::LessThanEquals => "<=",
+ CompareOp::GreaterThan => ">",
+ CompareOp::GreaterThanEquals => ">=",
+ CompareOp::Has => ":",
+ CompareOp::MatchesRegexp => "=~",
+ CompareOp::NotMatchesRegexp => "!~",
+ }
+ }
+}
+
+/// The AST of a parsed AIP-160 filter expression.
+#[derive(Debug, Clone, PartialEq)]
+pub enum Expr {
+ /// Binary expression: `left AND right` or `left OR right`.
+ Binary {
+ op: BinaryOp,
+ left: Box<Expr>,
+ right: Box<Expr>,
+ pos: Position,
+ },
+
+ /// Unary expression: `-expr` or `NOT expr`.
+ Unary {
+ op: UnaryOp,
+ expr: Box<Expr>,
+ pos: Position,
+ },
+
+ /// Comparison expression: `left op right`.
+ Comparison {
+ op: CompareOp,
+ left: Box<Expr>,
+ right: Box<Expr>,
+ pos: Position,
+ },
+
+ /// A simple name or literal value.
+ Name {
+ name: String,
+ pos: Position,
+ /// `true` if this was a quoted string literal.
+ is_string: bool,
+ /// `true` if inside a parenthesized (composite) expression.
+ is_composite: bool,
+ },
+
+ /// A member access expression: `holder.member`.
+ Member {
+ holder: Box<Expr>,
+ member: String,
+ pos: Position,
+ },
+
+ /// A function call expression: `fn(args...)`.
+ Function {
+ func: Box<Expr>,
+ args: Vec<Expr>,
+ },
+}