caffeine_lang/cql/parser

Types

pub type Exp {
  OperatorExpr(
    numerator: Exp,
    denominator: Exp,
    operator: Operator,
  )
  Primary(primary: Primary)
}

Constructors

pub type ExpContainer {
  ExpContainer(exp: Exp)
}

Constructors

  • ExpContainer(exp: Exp)
pub type Operator {
  Add
  Sub
  Mul
  Div
}

Constructors

  • Add
  • Sub
  • Mul
  • Div
pub type Primary {
  PrimaryWord(word: Word)
  PrimaryExp(exp: Exp)
}

Constructors

  • PrimaryWord(word: Word)
  • PrimaryExp(exp: Exp)
pub type Query {
  Query(exp: Exp)
}

Constructors

  • Query(exp: Exp)
pub type Word {
  Word(value: String)
}

Constructors

  • Word(value: String)

Values

pub fn debug_exp(exp: Exp) -> String

Pretty print an expression tree for debugging. Useful for visualizing the AST structure.

pub fn do_parse_expr(input: String) -> Result(Exp, String)

Core parsing logic. Handles two cases:

  1. Fully parenthesized expressions: (expr) -> unwrap and parse inner
  2. Non-parenthesized: try to split by operators in precedence order

Operator precedence (lowest to highest): +, -, *, / We search for lowest precedence first to build correct AST structure.

pub fn is_balanced_parens(
  input: String,
  pos: Int,
  count: Int,
) -> Bool

Check if parentheses are balanced from position pos with initial count.

Args:

  • input: The string to check
  • pos: Starting position (0-indexed)
  • count: Initial parenthesis depth (1 means we’re inside one open paren)

Returns True only if:

  1. Count reaches exactly 0 at the end of the string
  2. Count never reaches 0 before the end (no premature closing)

This ensures that for “(A + B)”, starting at pos=1 with count=1, we verify the closing ‘)’ is at the very end.

pub fn parse_expr(input: String) -> Result(ExpContainer, String)

Parse an expression string into an ExpContainer.

Examples:

  • “A + B” -> Addition of A and B
  • “(A + B) / C” -> Division with parenthesized addition
  • “A * B + C / D - E” -> Mixed operators with precedence
Search Document