Class: Parser

EBNF~ Parser

Wraps a method parser.parse() which recognizes input and builds a tree of nested lists, calls action functions, if any.


new Parser(grammar [, skip])

Creates a parser; see parser() factory method.

Parameters:
Name Type Argument Description
grammar module:EBNF~Grammar

represents grammar and states.

skip RegExp <optional>

a pattern to define ignorable character sequences, by default white space, must not accept empty input, must not use d, g, or y flag, should not be anchored, should use (:? )rather than ( ) for grouping.

Properties:
Name Type Argument Description
scanner module:Base~Scanner <nullable>

tokenizes input.

tuples Array.<?module:Base~Tuple>

tokenized input during recognition.

index number

index of next tuple during recognition.

current module:Base~Tuple <nullable>

current input tuple or null for end of input during recognition.

ruleStack Array.<?module:EBNF:Rule>

currently activated rules during recognition.

grammar module:EBNF~Grammar

represents the grammar, counts errors; concurrent recognition will trash error counting.

actions Object.<string, module:Base~Action> <nullable>

maps rule names to action functions or methods during recognition.

data Object <nullable>

context for all actions during recognition.

Properties
Name Type Description
parser module:Base~Parser

set to this unless already defined by caller.

oop boolean

true if .actions is set to a singleton with ClassAction methods.

Source:

Extends

Methods


act(name, result)

Calls an action method. Checks argument count unless grammar.config.noargs is set or the method expects no arguments, i.e., has a rest parameter.

Parameters:
Name Type Description
name string

rule name to match.

result Array

list of arguments.

Inherited From:
Overrides:
Source:
Returns:

action method result or unchanged result.


call(target, method, args)

Checks if argument and parameter count of a method match unless the method expects no parameters, or has a rest parameter or grammar.config.noargs is true.

Parameters:
Name Type Argument Description
target object

to apply method to.

method function

to check.

args Object <repeatable>

arguments to pass.

Inherited From:
Overrides:
Source:
Returns:

method result.

Type
Object
Example

super.method(arg1, .. argn)

this.parser.call(this, super.method, arg1, .. argn)

error(s)

Displays a message and the rule stack, if any; lets grammar count it as an error.

Parameters:
Name Type Argument Description
s Array.<object> <repeatable>

message, to be displayed; prefixed by .current and joined by blanks.

Source:
Returns:

the message.

Type
string

next(caller)

Advances .index and, therefore, .current to the next element of .tuples, if any. Finds or creates null as .current to indicate end of input. Ignores illegal characters but only reports the first in a sequence. Implements lookahead trace.

Parameters:
Name Type Description
caller string

for trace.

Source:

parse(input [, actions], arg)

Recognizes an input sentence. Requires grammar.expect(). Resets and reports .errors for the grammar.

Parameters:
Name Type Argument Default Description
input string

to process.

actions function | Object <optional>
null

a function is assumed to be a class and a singleton is created with this as constructor argument. The object maps rule names to action methods.

arg Object <repeatable>

used as further constructor arguments.

Overrides:
Source:
Throws:

error message, also reported by parser.error().

Type
string
Returns:

the collected sequence of values or the value produced by the action of the start rule. The parsing methods return the following types, where object refers to the result produced by the action of a rule.

class returns
Lit string
Token string
Alt Array
Opt null|Array
Seq Array
Some Array<Array>
Rule Array|object
NT Array|object
Type
Array | Object