Class: Factory

Base~ Factory

Contains configurable values, inventories, and factory methods to create scanners, tokenized input tuples, and precedence levels.


<abstract> new Factory()

Properties:
Name Type Description
config Object.<string, Object>

maps names to configurable values.

Properties
Name Type Description
log function

function to print strings, by default console.log.

lits RegExp

restricts literal representation, by default single-quoted; must be anchored.

tokens RegExp

restricts token names, by default alphanumeric; must be anchored.

nts RegExp

restricts non-terminal names, by default alphanumeric; must be anchored.

uniq string

prefix for unique non-terminal names, by default $-.

lits Array.<module:Base~Lit>

list of unique literals, can be pushed.

litsByName Object.<string, module:Base~Lit>

maps 'x' to unique literal.

tokens Array.<module:Base~Token>

list of unique tokens, can be pushed.

tokensByName Object.<string, module:Base~Token>

maps name to unique token.

levels Array.<module:Base~Precedence>

list of precedence levels, can be pushed.

nts Array.<module:Base~NT>

list of unique non-terminals, can be pushed.

ntsByName Object.<string, module:Base~NT>

maps name to unique non-terminal.

errors number

incremented by error() method; can be reset, e.g., to count during recognition.

noargs boolean

don't check for argument count errors in act() method.

Source:

Methods


add(item)

Adds a new symbol to the proper inventory or creates and adds new tokens. Must be called with a new, unique symbol or with a map of token names to patterns. Validates item names against .config. Token patterns must not accept empty input, must not use d, g, or y flag, should not be anchored, and should use (:? ) rather than ( ) for grouping.

Parameters:
Name Type Description
item Symbol | Object.<string, RegExp>

to add to the proper inventory or create and add.

Source:

assert(condition, s)

Displays a message and throws an error if a condition is not met; primarily used for stronger argument typing.

Parameters:
Name Type Argument Description
condition boolean

should be true.

s Array.<?object> <repeatable>

message, to be displayed; joined by blanks.

Source:
Throws:

message if condition is not met.

Type
string

dump( [a])

Displays an object as a string; in particular, nested arrays. This is useful because console.debug only reaches 3 levels.

Parameters:
Name Type Argument Description
a Object <optional>

the object to display; if omitted, returns an empty string.

Source:
Returns:
Type
string

error(s)

Displays a message and counts it as an error.

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

message, to be displayed; joined by blanks.

Source:
Returns:

the message.

Type
string

message(s)

Displays a message on the configured .log.

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

message, to be displayed; joined by blanks.

Source:
Returns:

the message.

Type
string

precedence(assoc, terminals)

Factory method to represent a list of terminals with equal precedence level and equal associativity. Creates a new Precedence object, adds it to .levels, adds .prec.level and .prec.assoc to all terminals in the list, and checks for duplicates.

Parameters:
Name Type Description
assoc string

associativity: '%left', '%right', or '%nonassoc'.

terminals Array.<?module:Base~T>

to add, null elements are ignored; no duplicates.

Source:
Returns:

representing the set, or null if there are no terminals.

Type
module:Base~Precedence

scanner( [skip] [, terminals])

Factory method to create a scanner.

Parameters:
Name Type Argument Description
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.

terminals Array.<T> <optional>

ordered list to create the lexical analysis pattern.

Source:
Returns:

the scanner.

Type
module:Base~Scanner

tuple(lineno, t [, value])

Factory method to create an element of a tokenized input stream.

Parameters:
Name Type Argument Default Description
lineno number

input position.

t module:Base~T <nullable>

terminal, i.e., literal or token object; scan() uses null for an illegal character.

value string <optional>
<nullable>
null

terminal's representation in the input.

Source:
Returns:

an element of a tokenized input stream.

Type
module:Base~Tuple