mod avt;
mod path;
mod pattern;
mod types;
use std::collections::VecDeque;
use crate::MAX_DEPTH;
use crate::ast::{ArithmeticOp, Binding, CompareKind, ComparisonOp, Expr, Quantifier};
use crate::error::{Error, ErrorKind, Result};
use crate::lexer::{Lexer, Tok, Token};
use crate::names::{Namespaces, QName, XML_NAMESPACE};
pub(crate) use avt::parse_avt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum NameRole {
Element,
NoNamespace,
Function,
}
#[derive(Debug)]
pub(crate) struct Parsed {
pub(crate) expr: Box<Expr>,
pub(crate) depth: u32,
}
impl Parsed {
fn new(expr: Expr, depth: u32) -> Self {
Self {
expr: Box::new(expr),
depth,
}
}
fn leaf(expr: Expr) -> Self {
Self::new(expr, 1)
}
}
#[derive(Debug, Clone, Copy)]
enum BinaryOp {
Or,
And,
Comparison(ComparisonOp),
NodeComparison,
Range,
Arithmetic(ArithmeticOp),
Union,
}
impl BinaryOp {
const fn precedence(self) -> u8 {
match self {
Self::Or => 1,
Self::And => 2,
Self::Comparison(_) | Self::NodeComparison => 3,
Self::Range => 4,
Self::Arithmetic(ArithmeticOp::Add | ArithmeticOp::Subtract) => 5,
Self::Arithmetic(_) => 6,
Self::Union => 7,
}
}
const fn is_associative(self) -> bool {
!matches!(
self,
Self::Comparison(_) | Self::NodeComparison | Self::Range
)
}
fn build(self, lhs: Box<Expr>, rhs: Box<Expr>) -> Option<Expr> {
Some(match self {
Self::Or => Expr::Or(lhs, rhs),
Self::And => Expr::And(lhs, rhs),
Self::Comparison(op) => Expr::Comparison { op, lhs, rhs },
Self::NodeComparison => return None,
Self::Range => Expr::Range(lhs, rhs),
Self::Arithmetic(op) => Expr::Arithmetic { op, lhs, rhs },
Self::Union => Expr::Union(lhs, rhs),
})
}
}
pub(crate) struct Parser<'a> {
src: &'a str,
lexer: Lexer<'a>,
lookahead: VecDeque<Token>,
namespaces: &'a dyn Namespaces,
nesting: u32,
}
impl<'a> Parser<'a> {
pub(crate) fn new(src: &'a str, pos: usize, namespaces: &'a dyn Namespaces) -> Self {
Self {
src,
lexer: Lexer::new(src, pos),
lookahead: VecDeque::new(),
namespaces,
nesting: 0,
}
}
fn fill(&mut self, n: usize) -> Result<()> {
while self.lookahead.len() <= n {
let token = self.lexer.next_token()?;
self.lookahead.push_back(token);
}
Ok(())
}
fn peek_at(&mut self, n: usize) -> Result<&Token> {
self.fill(n)?;
self.lookahead.get(n).ok_or_else(|| {
Error::new(
ErrorKind::Syntax,
self.src,
self.src.len(),
"lookahead".into(),
)
})
}
fn peek(&mut self) -> Result<&Tok> {
Ok(&self.peek_at(0)?.tok)
}
fn peek2(&mut self) -> Result<&Tok> {
Ok(&self.peek_at(1)?.tok)
}
fn bump(&mut self) -> Result<Token> {
self.fill(0)?;
self.lookahead.pop_front().ok_or_else(|| {
Error::new(
ErrorKind::Syntax,
self.src,
self.src.len(),
"lookahead".into(),
)
})
}
fn offset(&mut self) -> Result<usize> {
Ok(self.peek_at(0)?.start)
}
fn error_at(&self, kind: ErrorKind, offset: usize, message: impl Into<String>) -> Error {
Error::new(kind, self.src, offset, message.into())
}
fn error_here(&mut self, kind: ErrorKind, message: impl Into<String>) -> Error {
match self.peek_at(0) {
Ok(token) => {
let offset = token.start;
self.error_at(kind, offset, message)
}
Err(e) => e,
}
}
fn unexpected(&mut self, expected: &str) -> Error {
match self.peek_at(0) {
Ok(token) => {
let (offset, found) = (token.start, token.tok.describe());
self.error_at(
ErrorKind::Syntax,
offset,
format!("expected {expected}, found {found}"),
)
}
Err(e) => e,
}
}
fn expect(&mut self, tok: &Tok, expected: &str) -> Result<Token> {
if self.peek()? == tok {
self.bump()
} else {
Err(self.unexpected(expected))
}
}
fn eat(&mut self, tok: &Tok) -> Result<bool> {
if self.peek()? == tok {
self.bump()?;
Ok(true)
} else {
Ok(false)
}
}
fn eat_word(&mut self, word: &str) -> Result<bool> {
if self.peek()?.is_word(word) {
self.bump()?;
Ok(true)
} else {
Ok(false)
}
}
fn expect_word(&mut self, word: &str) -> Result<()> {
if self.eat_word(word)? {
Ok(())
} else {
Err(self.unexpected(&format!("`{word}`")))
}
}
fn too_deep(&self, offset: usize) -> Error {
self.error_at(
ErrorKind::TooDeep,
offset,
format!("expression nested deeper than the maximum depth of {MAX_DEPTH}"),
)
}
fn checked(&self, depth: u32, offset: usize) -> Result<u32> {
if depth > MAX_DEPTH {
Err(self.too_deep(offset))
} else {
Ok(depth)
}
}
fn enter(&mut self) -> Result<()> {
if self.nesting >= MAX_DEPTH {
let offset = self.offset()?;
return Err(self.too_deep(offset));
}
self.nesting += 1;
Ok(())
}
fn leave(&mut self) {
self.nesting = self.nesting.saturating_sub(1);
}
fn above(&self, depths: impl IntoIterator<Item = u32>, offset: usize) -> Result<u32> {
let deepest = depths.into_iter().max().unwrap_or(0);
self.checked(deepest.saturating_add(1), offset)
}
fn resolve(
&self,
prefix: Option<String>,
local: String,
role: NameRole,
offset: usize,
) -> Result<QName> {
let namespace = match &prefix {
Some(p) if p == "xml" => Some(XML_NAMESPACE.to_owned()),
Some(p) => Some(self.namespaces.namespace_uri(p).ok_or_else(|| {
self.error_at(
ErrorKind::UndeclaredPrefix,
offset,
format!("prefix `{p}` is not declared"),
)
})?),
None => match role {
NameRole::Element => self.namespaces.default_element_namespace(),
NameRole::NoNamespace => None,
NameRole::Function => Some(crate::names::FN_NAMESPACE.to_owned()),
},
};
Ok(QName {
prefix,
namespace,
local,
})
}
fn resolve_prefix(&self, prefix: &str, offset: usize) -> Result<String> {
if prefix == "xml" {
return Ok(XML_NAMESPACE.to_owned());
}
self.namespaces.namespace_uri(prefix).ok_or_else(|| {
self.error_at(
ErrorKind::UndeclaredPrefix,
offset,
format!("prefix `{prefix}` is not declared"),
)
})
}
fn qname(&mut self, role: NameRole, expected: &str) -> Result<QName> {
let token = self.bump()?;
match token.tok {
Tok::Name { prefix, local } => self.resolve(prefix, local, role, token.start),
other => Err(self.error_at(
ErrorKind::Syntax,
token.start,
format!("expected {expected}, found {}", other.describe()),
)),
}
}
pub(crate) fn expect_end(&mut self) -> Result<()> {
if *self.peek()? == Tok::Eof {
Ok(())
} else {
let found = self.peek()?.describe();
Err(self.error_here(
ErrorKind::Syntax,
format!("unexpected {found} after the end of the expression"),
))
}
}
pub(crate) fn expr(&mut self) -> Result<Parsed> {
let offset = self.offset()?;
let first = self.expr_single()?;
if *self.peek()? != Tok::Comma {
return Ok(first);
}
self.sequence(first, offset)
}
fn sequence(&mut self, first: Parsed, offset: usize) -> Result<Parsed> {
let mut items = Items::new(first);
while self.eat(&Tok::Comma)? {
let item = self.expr_single()?;
items.push(item);
}
let depth = self.above([items.deepest], offset)?;
Ok(Parsed::new(Expr::Sequence(items.exprs), depth))
}
pub(crate) fn expr_single(&mut self) -> Result<Parsed> {
self.enter()?;
let parsed = match self.single_kind()? {
Single::For => self.for_expr(),
Single::Quantified => self.quantified_expr(),
Single::If => self.if_expr(),
Single::Binary => self.binary(),
}?;
self.leave();
Ok(parsed)
}
fn single_kind(&mut self) -> Result<Single> {
let keyword = match self.peek()? {
Tok::Name {
prefix: None,
local,
} if matches!(
local.as_str(),
"for" | "some" | "every" | "let" | "if" | "typeswitch"
) =>
{
local.clone()
}
_ => return Ok(Single::Binary),
};
let next_is_dollar = *self.peek2()? == Tok::Dollar;
let next_is_paren = *self.peek2()? == Tok::LParen;
Ok(match keyword.as_str() {
"for" if next_is_dollar => Single::For,
"some" | "every" if next_is_dollar => Single::Quantified,
"let" if next_is_dollar => {
return Err(self.error_here(ErrorKind::Unsupported, "`let` expression (XPath 3.0)"));
}
"if" if next_is_paren => Single::If,
"typeswitch" if next_is_paren => {
return Err(self.error_here(ErrorKind::Unsupported, "`typeswitch` expression"));
}
_ => Single::Binary,
})
}
fn bindings(&mut self) -> Result<Bound> {
let mut bound = Bound::default();
loop {
let variable = self.binding_variable()?;
let sequence = self.expr_single()?;
if !self.binding_end(&mut bound, variable, sequence)? {
return Ok(bound);
}
}
}
fn binding_variable(&mut self) -> Result<QName> {
self.expect(&Tok::Dollar, "`$`")?;
let variable = self.qname(NameRole::NoNamespace, "a variable name")?;
self.expect_word("in")?;
Ok(variable)
}
fn binding_end(
&mut self,
bound: &mut Bound,
variable: QName,
sequence: Parsed,
) -> Result<bool> {
bound.bindings.push(Binding {
variable,
sequence: *sequence.expr,
});
bound.depths.push(sequence.depth);
let offset = self.offset()?;
let levels = u32::try_from(bound.depths.len()).unwrap_or(u32::MAX);
self.checked(levels.saturating_add(1), offset)?;
self.eat(&Tok::Comma)
}
fn bound_depth(&self, depths: &[u32], body: u32, offset: usize) -> Result<u32> {
let mut depth = body;
for &sequence in depths.iter().rev() {
depth = self.above([depth, sequence], offset)?;
}
Ok(depth)
}
fn for_expr(&mut self) -> Result<Parsed> {
let offset = self.bump()?.start;
let bound = self.bindings()?;
self.expect_word("return")?;
let body = self.expr_single()?;
self.for_end(bound, body, offset)
}
fn for_end(&self, bound: Bound, body: Parsed, offset: usize) -> Result<Parsed> {
let depth = self.bound_depth(&bound.depths, body.depth, offset)?;
let expr = Expr::For {
bindings: bound.bindings,
body: body.expr,
};
Ok(Parsed::new(expr, depth))
}
fn quantified_expr(&mut self) -> Result<Parsed> {
let (quantifier, offset) = self.quantifier()?;
let bound = self.bindings()?;
self.expect_word("satisfies")?;
let satisfies = self.expr_single()?;
self.quantified_end(quantifier, bound, satisfies, offset)
}
fn quantifier(&mut self) -> Result<(Quantifier, usize)> {
let token = self.bump()?;
let quantifier = if token.tok.is_word("some") {
Quantifier::Some
} else {
Quantifier::Every
};
Ok((quantifier, token.start))
}
fn quantified_end(
&self,
quantifier: Quantifier,
bound: Bound,
satisfies: Parsed,
offset: usize,
) -> Result<Parsed> {
let depth = self.bound_depth(&bound.depths, satisfies.depth, offset)?;
let expr = Expr::Quantified {
quantifier,
bindings: bound.bindings,
satisfies: satisfies.expr,
};
Ok(Parsed::new(expr, depth))
}
fn if_expr(&mut self) -> Result<Parsed> {
let offset = self.if_start()?;
let condition = self.expr()?;
self.if_then()?;
let then = self.expr_single()?;
self.expect_word("else")?;
let otherwise = self.expr_single()?;
self.if_end([condition, then, otherwise], offset)
}
fn if_start(&mut self) -> Result<usize> {
let offset = self.bump()?.start;
self.expect(&Tok::LParen, "`(`")?;
Ok(offset)
}
fn if_then(&mut self) -> Result<()> {
self.expect(&Tok::RParen, "`)`")?;
self.expect_word("then")
}
fn if_end(&self, parts: [Parsed; 3], offset: usize) -> Result<Parsed> {
let depth = self.above(parts.iter().map(|p| p.depth), offset)?;
let [condition, then, otherwise] = parts;
let expr = Expr::If {
condition: condition.expr,
then: then.expr,
otherwise: otherwise.expr,
};
Ok(Parsed::new(expr, depth))
}
fn binary_op(&mut self) -> Result<Option<BinaryOp>> {
use CompareKind::{Eq, Ge, Gt, Le, Lt, Ne};
let general = |kind| Some(BinaryOp::Comparison(ComparisonOp::General(kind)));
let value = |kind| Some(BinaryOp::Comparison(ComparisonOp::Value(kind)));
let arithmetic = |op| Some(BinaryOp::Arithmetic(op));
let op = match self.peek()? {
Tok::Pipe => Some(BinaryOp::Union),
Tok::Star => arithmetic(ArithmeticOp::Multiply),
Tok::Plus => arithmetic(ArithmeticOp::Add),
Tok::Minus => arithmetic(ArithmeticOp::Subtract),
Tok::Eq => general(Eq),
Tok::Ne => general(Ne),
Tok::Lt => general(Lt),
Tok::Le => general(Le),
Tok::Gt => general(Gt),
Tok::Ge => general(Ge),
Tok::Precedes | Tok::Follows => Some(BinaryOp::NodeComparison),
Tok::Name {
prefix: None,
local,
} => match local.as_str() {
"intersect" | "except" => {
let message = format!("`{local}` operator");
return Err(self.error_here(ErrorKind::Unsupported, message));
}
"union" => {
return Err(self.error_here(
ErrorKind::Unsupported,
"`union` operator (only `|` is supported)",
));
}
"div" => arithmetic(ArithmeticOp::Divide),
"idiv" => arithmetic(ArithmeticOp::IntegerDivide),
"mod" => arithmetic(ArithmeticOp::Modulo),
"to" => Some(BinaryOp::Range),
"eq" => value(Eq),
"ne" => value(Ne),
"lt" => value(Lt),
"le" => value(Le),
"gt" => value(Gt),
"ge" => value(Ge),
"is" => Some(BinaryOp::NodeComparison),
"and" => Some(BinaryOp::And),
"or" => Some(BinaryOp::Or),
_ => None,
},
_ => None,
};
Ok(op)
}
fn binary(&mut self) -> Result<Parsed> {
let mut chain = Chain::default();
let first = self.operand()?;
chain.operands.push(first);
while self.chain_operator(&mut chain)? {
let operand = self.operand()?;
chain.operands.push(operand);
}
self.chain_end(chain)
}
fn chain_operator(&mut self, chain: &mut Chain) -> Result<bool> {
let Some(op) = self.binary_op()? else {
return Ok(false);
};
let precedence = op.precedence();
while let Some(&(top, _)) = chain.operators.last() {
if top.precedence() < precedence {
break;
}
if top.precedence() == precedence && !op.is_associative() {
return Ok(false);
}
self.reduce(chain)?;
}
if let BinaryOp::NodeComparison = op {
let found = match self.peek()? {
Tok::Name { .. } => "`is`".to_owned(),
other => other.describe(),
};
return Err(self.error_here(ErrorKind::Unsupported, format!("node comparison {found}")));
}
let offset = self.bump()?.start;
chain.operators.push((op, offset));
Ok(true)
}
fn chain_end(&self, mut chain: Chain) -> Result<Parsed> {
while !chain.operators.is_empty() {
self.reduce(&mut chain)?;
}
chain
.operands
.pop()
.ok_or_else(|| self.error_at(ErrorKind::Syntax, 0, "no operand"))
}
fn reduce(&self, chain: &mut Chain) -> Result<()> {
let internal = || self.error_at(ErrorKind::Syntax, 0, "operator without operands");
let (op, offset) = chain.operators.pop().ok_or_else(internal)?;
let rhs = chain.operands.pop().ok_or_else(internal)?;
let lhs = chain.operands.pop().ok_or_else(internal)?;
let depth = self.above([lhs.depth, rhs.depth], offset)?;
let expr = op.build(lhs.expr, rhs.expr).ok_or_else(internal)?;
chain.operands.push(Parsed::new(expr, depth));
Ok(())
}
fn at_words(&mut self, first: &str, second: &str) -> Result<bool> {
Ok(self.peek()?.is_word(first) && self.peek2()?.is_word(second))
}
fn operand(&mut self) -> Result<Parsed> {
let negations = self.negations()?;
let value = self.path_expr()?;
self.postfix(&negations, value)
}
fn negations(&mut self) -> Result<Vec<usize>> {
let mut negations = Vec::new();
loop {
match self.peek()? {
Tok::Minus => {
let offset = self.bump()?.start;
let levels = u32::try_from(negations.len()).unwrap_or(u32::MAX);
self.checked(levels.saturating_add(2), offset)?;
negations.push(offset);
}
Tok::Plus => return Err(self.error_here(ErrorKind::Unsupported, "unary `+`")),
_ => return Ok(negations),
}
}
}
fn postfix(&mut self, negations: &[usize], value: Parsed) -> Result<Parsed> {
let mut operand = value;
for &offset in negations.iter().rev() {
let depth = self.above([operand.depth], offset)?;
operand = Parsed::new(Expr::Negate(operand.expr), depth);
}
if self.at_words("cast", "as")? {
let offset = self.bump()?.start;
self.bump()?;
let target = self.single_type()?;
let depth = self.above([operand.depth], offset)?;
let expr = Expr::Cast {
operand: operand.expr,
target,
};
operand = Parsed::new(expr, depth);
}
if self.at_words("castable", "as")? {
let offset = self.bump()?.start;
self.bump()?;
let target = self.single_type()?;
let depth = self.above([operand.depth], offset)?;
let expr = Expr::Castable {
operand: operand.expr,
target,
};
operand = Parsed::new(expr, depth);
}
if self.at_words("treat", "as")? {
return Err(self.error_here(ErrorKind::Unsupported, "`treat as` expression"));
}
if self.at_words("instance", "of")? {
let offset = self.bump()?.start;
self.bump()?;
let target = self.sequence_type()?;
let depth = self.above([operand.depth], offset)?;
let expr = Expr::InstanceOf {
operand: operand.expr,
target,
};
operand = Parsed::new(expr, depth);
}
Ok(operand)
}
}
#[derive(Debug, Clone, Copy)]
enum Single {
For,
Quantified,
If,
Binary,
}
#[derive(Debug)]
struct Items {
exprs: Vec<Expr>,
deepest: u32,
}
impl Items {
fn new(first: Parsed) -> Self {
Self {
deepest: first.depth,
exprs: vec![*first.expr],
}
}
fn push(&mut self, item: Parsed) {
self.deepest = self.deepest.max(item.depth);
self.exprs.push(*item.expr);
}
}
#[derive(Debug, Default)]
struct Bound {
bindings: Vec<Binding>,
depths: Vec<u32>,
}
#[derive(Debug, Default)]
struct Chain {
operands: Vec<Parsed>,
operators: Vec<(BinaryOp, usize)>,
}