Skip to content

Latest commit

 

History

History
709 lines (422 loc) · 10.5 KB

File metadata and controls

709 lines (422 loc) · 10.5 KB

Table of Contents

grammar

set

Return the charset as a string.

union

Return the union of this charset and the charset defined by specs.

Parameters

  • specs ...any

difference

Return the difference between this charset and the charset defined by specs.

Parameters

  • specs ...any

charset

Match one character if present in the given charset.

Charset may be specified either:

  • as a range ("0-9");
  • as string containing the individual charaters of the set ("0123456789");
  • as any iterable over a string (including another Charset object).

Parameters

  • specs ...any

litteral

Match a string of characters.

Capture the entire string

Parameters

  • str

nat

Negative lookarround.

Move the token pointer relative to the current position and check the given program do not match. Can be used as a negative lookbehind (delta < 0) or lookafter (delta >= 0). In all cases, restore the input to its previous position on exit.

Capture nothing.

Parameters

  • delta
  • programs ...any

not

The not predicate.

Succeed if the given program fails with the current input. Do not consume any character.

Capture nothing.

Parameters

  • programs ...any

and

The and predicate

Succeed if the given program succeed, but without consuming any character. Can be used as a positive lookahead.

Capture nothing.

Parameters

  • programs ...any

any

Match any character. Fail only if there is no more token to process.

Capture the matched character.

rule

Match a rule of the grammar.

Parameters

  • name string The name of the rule to match. The rule does not have already exist. It might be defined later.

concat

Concatenation of several programs.

Will match the sequence: programs[0],programs1,programs2..programs[n]

Parameters

  • programs ...any

choice

Ordered choice.

Try to match each alternative in its own turn. Stop trying once there is a match. Fail if there is no match.

Parameters

  • alternatives ...any

zeroOrOne

The zero-or-one quantifier.

Match 0 or 1 repetition of a program.

If there is no match, capture the undefined special value.

Parameters

  • programs ...any

optional

A variation of tThe zero-or-one quantifier.

This form takes only one program as an argument, but it allows specifying the default value to substitute if there is no match.

Parameters

  • program
  • defaultValue

zeroOrMore

The zero-or-more quantifier.

Match 0, 1 or several repetitions of the same program.

Parameters

  • programs ...any

oneOrMore

The one-or-more quantifier.

Match 1 or several repetitions of the same program.

Parameters

  • programs ...any

capture

Capture the data from a sub-program into an array.

This allow data capture without requiring to define a new rule specifically for that.

Parameters

  • programs ...any

join

Capture the data from a sub-program into a string

This allow data capture without requiring to define a new rule specifically for that.

Parameters

  • programs ...any

string

Macro equivalent to join(oneOrMore(...)).

Match 1 or several repetition of a pattern and convert the capture into a string.

Parameters

  • programs ...any

except

Match any pattern except if ay of the rest PEG match.

Macro equivalent to `not(tail[0]), not(tail1), ... not(tail[n]), head());

Parameters

  • head
  • tail ...any

anyExcept

Match any character except if ay of the rest PEG match.

Macro equivalent to `not(rest[0]), not(rest1), ... not(rest[n]), any());

Parameters

  • rest ...any

Grammar

The Grammar class.

Examples

// Create a new grammar
const grammar = new peg.Grammar();

define

Define a new rule.

Parameters

  • name string The name of the rule
  • program
  • action

parser

Return a new Parser for the grammar.

Parameters

  • start string The name of the rule to match.
  • context

parser

Parser

The Parser class.

Parameters

  • grammar Grammar The grammar
  • start String The rule to match
  • context

dump

Dump debugging informations.

The debugging informations contains:

The disassembled code for the VM at the current position. A dump of the backtrack stack. A dump of the operational ("call") stack.

pushd

Push a value onto the stack.

Parameters

  • value

char

Test for a character. Fail if it doesn't match.

Parameters

  • c

charset

Match any character in a string. Fail if it doesn't match.

Parameters

  • set

move

Move the tx. Fail if it would move before the first token.

Parameters

  • delta

any

Test for any character. Fail if there is no character at the current position (i.e.: before the first or after the last character).

fail

Force a failure. Return to the last saved backtrack point or stop the machine and signal failure.

end

Stop the machine. Signal success.

call

Replace the current stack frame by the result of an external (JavaScript) function.

Parameters

  • fct

jsr

Jump to subroutine (or subrule ?)

Save the current pc and code, then jump on the first instruction for the given nonterminal.

Parameters

  • nonterminal

ret

Return from a call.

Parameters

  • fct

frame

  • **See: drop() **
  • **See: reduce() **

Create a stack frame.

drop

  • **See: frame() **
  • **See: reduce() **

Drop the current stack frame.

reduce

  • **See: drop() **
  • **See: frame() **

Reduce the current stack frame to only one item. If a callable is given as a parameter, replace the current stack frame by the result of the function applied to the data on the frame. If no user function is specified, pack the data in an array.

Parameters

  • fct callable? The callable to use to pack the data.

choice

Create a backtracking entry on the stack

Parameters

  • offset

commit

Commit a choice. Discard the topmost backtrack stack entry and jump to instruction.

Parameters

  • offset integer The position of the next instruction to execute, relative to the current pc.

accept

accept one or more characters. Run the machine as long as we have token to process.

Parameters

  • tokens

run

continue execution until the machine stops.

Call this function when you now there will no more tokens to append using the accept() call.

restart

Restart the parser at the current token

XXX Is this private?

skip

Skip n tokens from the input

XXX Is this private?

Parameters

  • n

result

Return the top-most object on the stack if the machine has stopped. Return undefined otherwise

matchAll

Return a generator for all the tokens parts matching the parser's grammar, possibly skipping an arbitrary number of input tokens betwenn matches.