Class: TokenStack

@citation-js/core.util.TokenStack(array)

Create a TokenStack for parsing strings with complex escape sequences.

Constructor

new TokenStack(array)

Parameters:
Name Type Description
array Array.<String>

list of tokens

Source:

Methods

(protected, static) getMatchCallback(pattern) → {module:@citation-js/core.util.TokenStack~match}

Get a single callback to match a token against one or several patterns.

Parameters:
Name Type Description
pattern module:@citation-js/core.util.TokenStack~pattern

pattern

Source:
Returns:

Match callback

Type
module:@citation-js/core.util.TokenStack~match

(protected, static) getPatternText(pattern) → {String}

Get string representation of pattern.

Parameters:
Name Type Description
pattern String | RegExp

pattern

Source:
Returns:

string representation

Type
String

(protected) consume(patternopt, options) → {String}

Consumes all consecutive tokens matching pattern. Throws if number of matched tokens not within range min-max.

Parameters:
Name Type Attributes Default Description
pattern module:@citation-js/core.util.TokenStack~pattern <optional>
/^[\s\S]$/

pattern

options Object
Properties
Name Type Attributes Default Description
inverse Boolean <optional>
false

invert pattern

min Number <optional>
0

mininum number of consumed tokens

max Number <optional>
Infinity

maximum number of matched tokens

tokenMap module:@citation-js/core.util.TokenStack~tokenMap <optional>

map tokens before returning

tokenFilter module:@citation-js/core.util.TokenStack~tokenFilter <optional>

filter tokens before returning

Source:
Throws:
  • Not enough tokens

    Type
    SyntaxError
  • Too many tokens

    Type
    SyntaxError
Returns:

consumed tokens

Type
String

(protected) consumeN(length) → {String}

Consume n tokens. Throws if not enough tokens left

Parameters:
Name Type Description
length Number

number of tokens

Source:
Throws:

Not enough tokens left

Type
SyntaxError
Returns:

consumed tokens

Type
String

(protected) consumeSequence(sequence) → {String}

Consume a pattern spanning multiple tokens ('sequence').

Parameters:
Name Type Description
sequence module:@citation-js/core.util.TokenStack~sequence

sequence

Source:
Throws:

Expected sequence, got tokens

Type
SyntaxError
Returns:

consumed tokens

Type
String

(protected) consumeToken(patternopt, options) → {String}

Consume a single token if possible, and throw if not.

Parameters:
Name Type Attributes Default Description
pattern module:@citation-js/core.util.TokenStack~pattern <optional>
/^[\s\S]$/

pattern

options Object
Properties
Name Type Attributes Default Description
inverse Boolean <optional>
false

invert pattern

spaced Boolean <optional>
true

allow leading and trailing whitespace

Source:
Throws:

Unexpected token at index: Expected pattern, got token

Type
SyntaxError
Returns:

token

Type
String

(protected) consumeWhitespace(patternopt, options) → {String}

Consume a single token if possible, and throw if not.

Parameters:
Name Type Attributes Default Description
pattern module:@citation-js/core.util.TokenStack~pattern <optional>
/^\s$/

whitespace pattern

options Object
Properties
Name Type Attributes Default Description
optional Boolean <optional>
true

allow having no whitespace

Source:
Throws:

Unexpected token at index: Expected whitespace, got token

Type
SyntaxError
Returns:

matched whitespace

Type
String

(protected) matches(pattern) → {Boolean}

Match current token against pattern.

Parameters:
Name Type Description
pattern module:@citation-js/core.util.TokenStack~pattern

pattern

Source:
Returns:

match

Type
Boolean

(protected) matchesSequence(pattern) → {Boolean}

Match current token against pattern.

Parameters:
Name Type Description
pattern module:@citation-js/core.util.TokenStack~sequence

pattern

Source:
Returns:

match

Type
Boolean

(protected) tokensLeft() → {Number}

Get a number representing the number of tokens that are left.

Source:
Returns:

tokens left

Type
Number

Type Definitions

match(token, index, stack) → {Boolean}

Parameters:
Name Type Description
token String

token

index Number

token index

stack Array.<String>

token stack

Source:
Returns:

match or not

Type
Boolean

pattern

TokenStack pattern

Type:
Source:

sequence

TokenStack pattern sequence

Type:
Source:

tokenFilter(token) → {Boolean}

Parameters:
Name Type Description
token String

token

Source:
Returns:

keep or not

Type
Boolean

tokenMap(token) → {String}

Parameters:
Name Type Description
token String

token

Source:
Returns:

new token

Type
String