Skip to content

Latest commit

 

History

History
117 lines (101 loc) · 3.84 KB

File metadata and controls

117 lines (101 loc) · 3.84 KB

Builder

The Builder provides the most low-level stateless AST node construction API.

It can be used when constructing AST nodes from scratch manually, and it is also used by the parser to construct the AST nodes during the parsing process.

When parsing the AST nodes will typically have more information, such as the position in the source code, and other metadata. When constructing the AST nodes manually, this information is not available, but the Builder API can still be used as it permits to skip the metadata.

Examples

Construct a literal expression node:

import { Builder } from '@elastic/esql';

const node = Builder.expression.literal.numeric({ value: 42, literalType: 'integer' });

Returns:

{
  type: 'literal',
  literalType: 'integer',
  value: 42,
  name: '42',
  location: { min: 0, max: 0 },
  text: '',
  incomplete: false,
}

Construct a LIMIT command with a literal argument:

import { Builder } from '@elastic/esql';
const node = Builder.command({
  name: 'limit',
  args: [Builder.expression.literal.integer(10)],
});

Returns:

{
  "name": "limit",
  "args": [
    {
      "value": 10,
      "literalType": "integer",
      "location": { "min": 0, "max": 0 },
      "text": "",
      "incomplete": false,
      "type": "literal",
      "name": "10"
    }
  ],
  "location": { "min": 0, "max": 0 },
  "text": "",
  "incomplete": false,
  "type": "command"
}

API

  • .parserFields() — Constructs parser metadata fields (location, text, incomplete).
  • .command() — Constructs a command node.
  • .option() — Constructs a command option node.
  • .comment() — Constructs a comment node (single-line or multi-line).
  • .identifier() — Constructs an identifier node.
  • .expression
    • .query() — Constructs a query expression node.
    • .source
      • .node() — Constructs a source node from a string, string literal, or template.
      • .index() — Constructs an index source node.
    • .parens() — Constructs a parenthesized expression node.
    • .column() — Constructs a column expression node.
    • .order() — Constructs an order expression node.
    • .inlineCast() — Constructs an inline cast expression node.
    • .where() — Constructs a WHERE binary expression node.
    • .func
      • .node() — Constructs a raw function node.
      • .call() — Constructs a function call expression (variadic-call).
      • .unary() — Constructs a unary expression.
      • .postfix() — Constructs a postfix unary expression.
      • .binary() — Constructs a binary expression.
    • .list
      • .literal() — Constructs a list literal node.
      • .tuple() — Constructs a tuple list node.
      • .bare() — Constructs a bare list node.
    • .literal
      • .nil() — Constructs a NULL literal node.
      • .boolean() — Constructs a boolean literal node.
      • .numeric() — Constructs a numeric (integer or decimal) literal node.
      • .integer() — Constructs an integer literal node.
      • .decimal() — Constructs a decimal (floating point) literal node.
      • .timespan() — Constructs a timespan literal node (time duration or date period).
      • .string() — Constructs a string literal node.
    • .map() — Constructs a map expression node.
    • .entry() — Constructs a map entry node.
  • .param
    • .unnamed() — Constructs an unnamed parameter node.
    • .named() — Constructs a named parameter node.
    • .positional() — Constructs a positional parameter node.
    • .build() — Constructs a parameter node from a string, auto-detecting the type.
  • .header
    • .command() — Constructs a header command node.
    • .command.set() — Constructs a SET header command node.