///|
/// A successfully parsed and statically validated XPath 1.0 expression.
///
/// The expression tree is deliberately private. Public callers retain the
/// source and can reuse the compiled value; the evaluator in this package can
/// access `expression_` through the private `ir` method.
pub struct CompiledXPath {
  priv source_ : String
  priv span_ : @diagnostic.SourceSpan
  priv expression_ : XPathExpr
}

///|
pub fn CompiledXPath::source(self : CompiledXPath) -> String {
  self.source_
}

///|
pub fn CompiledXPath::span(self : CompiledXPath) -> @diagnostic.SourceSpan {
  self.span_
}

///|
fn CompiledXPath::ir(self : CompiledXPath) -> XPathExpr {
  self.expression_
}

///|
pub fn compile(
  source : String,
  static_context : @context.StaticContext,
) -> CompiledXPath raise @diagnostic.XPathError {
  let limits = static_context.limits()
  if source.length() > limits.max_source_units() {
    raise LimitExceeded(phase=Lex, limit="max_source_units")
  }
  let tokens = lex_xpath(source, limits.max_tokens())
  let parsed = parse_xpath_tokens(tokens, limits.max_nesting())
  let expression = validate_parsed_xpath(parsed, static_context)
  {
    source_: source,
    span_: @diagnostic.SourceSpan::new(0, source.length()),
    expression_: expression,
  }
}