antlr4ng
Version:
Alternative JavaScript/TypeScript runtime for ANTLR4
63 lines (62 loc) • 1.99 kB
TypeScript
import { Parser } from "../../Parser.js";
import { ParseTree } from "../ParseTree.js";
import { Token } from "../../Token.js";
import { XPathElement } from "./XPathElement.js";
/**
* Represent a subset of XPath XML path syntax for use in identifying nodes in
* parse trees.
*
* Split path into words and separators `/` and `//` via ANTLR
* itself then walk path elements from left to right. At each separator-word
* pair, find set of nodes. Next stage uses those as work list.
*
* The basic interface is
* {@link XPath.findAll ParseTree.findAll}`(tree, pathString, parser)`.
* But that is just shorthand for:
*
* ```
* let p = new XPath(parser, pathString);
* return p.evaluate(tree);
* ```
*
* See `TestXPath` for descriptions. In short, this
* allows operators:
*
* | | |
* | --- | --- |
* | `/` | root |
* | `//` | anywhere |
* | `!` | invert; this much appear directly after root or anywhere operator |
*
* and path elements:
*
* | | |
* | --- | --- |
* | `ID` | token name |
* | `'string'` | any string literal token from the grammar |
* | `expr` | rule name |
* | `*` | wildcard matching any node |
*
* Whitespace is not allowed.
*/
export declare class XPath {
static readonly WILDCARD: string;
static readonly NOT: string;
protected path: string;
protected elements: XPathElement[];
protected parser: Parser;
constructor(parser: Parser, path: string);
static findAll(tree: ParseTree, xpath: string, parser: Parser): Set<ParseTree>;
split(path: string): XPathElement[];
/**
* Return a list of all nodes starting at `t` as root that satisfy the
* path. The root `/` is relative to the node passed to {@link evaluate}.
*/
evaluate(t: ParseTree): Set<ParseTree>;
/**
* Convert word like `*` or `ID` or `expr` to a path
* element. `anywhere` is `true` if `//` precedes the
* word.
*/
protected getXPathElement(wordToken: Token, anywhere: boolean): XPathElement;
}