0.1.0 • Published 5 years ago

thinql-parse v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

thinql-parse

Parse ThinQL queries.

Installation

# npm
npm install thinql-parse

# yarn
yarn add thinql-parse

Example

const { parse } = require('thinql-parse')

const result = parse('hello world')

expect(result.toThinQL({ alwaysQuoteValues: true })).toEqual('"hello" "world"')

expect(result.toJSON()).toMatchObject({
  type: 'LogicalExpression',
  operator: {
    type: 'LogicalOperator',
    kind: 'and',
  },
  left: {
    type: 'FullTextSearch',
    value: {
      type: 'Value',
      value: 'hello',
      flags: '',
    },
    negate: false,
  },
  right: {
    type: 'FullTextSearch',
    value: {
      type: 'Value',
      value: 'world',
      flags: '',
    },
    negate: false,
  },
})

API

Exports

grammarPath: string

The absolute path to the compiled nearley grammar specification.

lexer: moo.Lexer

The Moo lexer instance.

parse(query: string): Node

Parses a ThinQL query and returns the root node of the resultant AST, which is a subclass of Node.

types: Object<string, { new(): Node }

A map of all node type classes.

Types

abstract Node

MemberType/Return TypeDescription
#tokenmoo.Token | nullThe token matched by the lexer, which includes information such as the line and column position of the node. Null for implied tokens.
#toJSON()ObjectConverts the node and its descendants into a plain serializable object recursively. Each node will include a type property whose value is the name of one of the subclasses of Node.
#toString()stringConverts the node and its descendants recursively into a string for inspection.
#toThinQL(options: toThinQLOptions)stringConverts the node and its descendants recursively into a ThinQL query.

Call extends Node

A node that represents an invocation of an environment-defined function.

MemberType/Return TypeDescription
.type'Call'The node type.
#calleestringThe name of the function.
#negateboolean | nullWhether to negate the result. Will be null if negating is syntactically invalid, such as when a call's return value is compared.
#typestringThe node type (as an instance member).
#argumentsArray<Value | Call>The arguments passed into the function.

abstract Operator extends Node

MemberType/Return TypeDescription
.symbolFor(kind: string)stringGet a symbol sequence that represents this operator in a query.
.operatorsObject<string, string | Array<string>>A map of operator token names to corresponding symbol sequence(s).

ComparisonOperator extends Operator

A node that represents an operator for comparison.

MemberType/Return TypeDescription
.type'ComparisonOperator'The node type.
.operators.eq'='Kind for the "is equal to" operator.
.operators.gt'>'Kind for the "is greater than" operator.
.operators.gte'>='Kind for the "is greater than or equal to" operator.
.operators.lt'<'Kind for the "is less than" operator.
.operators.lte'<='Kind for the "less than or equal to" operator.
.operators.match'*='Kind for the "matches" operator.
.operators.ne'!='Kind for the "is not equal to" operator.
#kind'eq' | 'gt' | 'gte' | 'lt' | 'lte' | 'match' | 'ne'A keyword that represents the comparison operator.

Condition extends Node

A node that represents a condition in which a property or call return value is compared against a value or return value.

MemberType/Return TypeDescription
.type'Condition'The node type.
#propertyProperty | CallThe property or call result being compared.
#operatorComparisonOperatorThe comparison operator.
#valueValue | CallThe value or call result the property is compared against.

FullTextSearch extends Node

A node that represents a search query against no particular field.

MemberType/Return TypeDescription
.type'FullTextSearch'The node type.
#valueValueThe value to search for.
#negatebooleanWhether to negate this filter.

Group extends Node

A node that represents an expression that will be evaluated as a group.

MemberType/Return TypeDescription
.type'Group'The node type.
#expressionLogicalExpressionThe expression wrapped inside the group.
#negatebooleanWhether to negate this filter.

LogicalExpression extends Node

A node that joins two expressions to produce a criterion in which either or both must be satisfied.

MemberType/Return TypeDescription
.type'LogicalExpression'The node type.
#operatorLogicalOperatorThe logical operator that joins the operands.
#leftCondition | FullTextSearch | Group | CallThe assertion on the left.
#rightCondition | FullTextSearch | Group | CallThe assertion on the right.

LogicalOperator extends Operator

A node that represents a logical operator.

MemberType/Return TypeDescription
.type'ComparisonOperator'The node type.
.operators.and['and', 'AND']Requires both criteria to be satisfied.
.operators.or['or', 'OR']Requires either criteria to be satisfied.
#kind'and' | 'or'A keyword that represents the logical operator.

Property extends Node

A node that represents a property that can be compared to a value.

MemberType/Return TypeDescription
.type'Property'The node type.
#namestringThe name of the property being compared.

Value extends Node

A node that represents any value: full text search, comparison subject, or function argument.

MemberType/Return TypeDescription
.type'Property'The node type.
#valuestringThe value text.
#flagsstringAny flags appended to the value.

toThinQLOptions

Options which can be passed to Node#toThinQL() to affect the output formatting.

PropertyTypeDescriptionDefault
alwaysQuotePropertiesbooleanAlways wrap property names with quotation marks.false
alwaysQuoteValuesbooleanAlways wrap values with quotation marks.false
propertyQuoteSymbol'"' | '"'The quotation mark symbol to wrap property names with.'"'
valueQuoteSymbol'"' | '"'The quotation mark symbol to wrap values with.'"'