Cyphon

Parser

Expression Parser Class

The ExpressionParser class is responsible for parsing search query expressions into a structured object format that can be easily processed or evaluated.

  • Command Parsing: Converts commands into a structured format with arguments and named arguments.
  • Logical Operators: Supports grouping commands using logical operators (and, or).

\

Methods:

Parse Method

public parse(expression: string): object
  • Purpose: Parses a search query expression into a structured object format.

  • Parameters:

    • expression (string): The search query string to parse.
  • Returns: An object representing the parsed expression.

  • Example:

    const parser = new ExpressionParser();
    const parsed = parser.parse("command1(arg1) and command2(arg2)");
    console.log(parsed);
    // Output: { and: [{ method: "command1", arguments: ["arg1"], namedArguments: {} }, { m

\

How it works

const expression = "method1(arg1, key1=value1) and method2(arg2, key2=value2)";
  • Tokenization:
    • Splits the input string into tokens using the operator regex.
    • Operators ( and , or) are identified separately.
  • Command Parsing:
    • Each token is processed into a Command object.
  • Grouping:
    • Commands are grouped based on logical operators ( and, or).
    • If no operator is specified, the default operator ( and) is used.
  • Structuring:
    • Groups are structured into a final object where operators are keys, and their commands are grouped as arrays.

Example

const parser = new ExpressionParser();
const expression = "method1(arg1, key1=value1) or method2(arg2) and method3()";
const parsedExpression = parser.parse(expression);
console.log(parsedExpression);

Output:

{
  "or": [
    { "method": "method1", "arguments": ["arg1"], "namedArguments": { "key1": "value1" } }
  ],
  "and": [
    { "method": "method2", "arguments": ["arg2"], "namedArguments": {} },
    { "method": "method3", "arguments": [], "namedArguments": {} }
  ]
}