pegged.peg

This module contains the engine behind Pegged, the expression templates building blocks to create a top-down recursive-descent parser.

The terminals and non-terminals described here are meant to be used inside a Pegged grammar. As such, they are a bit less user-friendly than what's output by pegged.grammar. For example they take a ParseTree as input, not a string.

See the /docs directory for the full documentation as markdown files.

Members

Classes

TraceLogger
class TraceLogger

Overrides FileLogger to remove the time stamp.

Trie
class Trie(V)

Compile-time switch trie from Brian Schott

Enums

IFCHAIN
enum IFCHAIN

CT Switch for testing 'keywords' implementations

Functions

any
ParseTree any(ParseTree p)
ParseTree any(string input)

Match any character. As long as there is at least a character left in the input, it succeeds. Conversely, it fails only if called at the end of the input.

eoi
ParseTree eoi(ParseTree p)
ParseTree eoi(string input)

Matches the end of input. Fails if there is any character left.

eps
ParseTree eps(ParseTree p)

eps matches the empty string (usually denoted by the Greek letter 'epsilon') and always succeeds. It's equivalent to literal!"" (for example, it creates a match of [""]: one match, the empty string).

fail
ParseTree fail(ParseTree p)
ParseTree fail(string input)

Basic rule, that always fail without consuming.

modify
ParseTree modify(ParseTree input)

Generic ParseTree modifier: predicate must be callable with a ParseTree and return a boolean. modifier must be callable with a ParseTree and return a ParseTree.

position
Position position(string s)

Given an input string, returns the position corresponding to the end of the string.

position
Position position(const ParseTree p)

Same as previous overload, but from the begin of P.input to p.end

setTraceConditionFunction
void setTraceConditionFunction(bool delegate(string ruleName, const ref ParseTree p) condition)
void setTraceConditionFunction(bool function(string ruleName, const ref ParseTree p) condition)

Supply a function to dynamically switch tracing on and off based on the rule name.

simplifyTree
ParseTree simplifyTree(ParseTree p)

Discard one-child nodes and replace them with their children. Most grammar tend to produce long one-child/one-child branches, simplifyTree compacts these.

size
size_t size(ParseTree p)
softCompare
bool softCompare(ParseTree p1, ParseTree p2)

To compare two trees for content (not bothering with node names) That's useful to compare the results from two different grammars.

traceAll
void traceAll()

Trace all rules.

traceNothing
void traceNothing()

Do not trace any rules.

wordBoundary
ParseTree wordBoundary(ParseTree p)
ParseTree wordBoundary(string input)

Predefined parser: matches word boundaries, as \b for regexes.

Manifest constants

KEYWORDS
enum KEYWORDS;

CT Switch for testing 'keywords' implementations

Mixin templates

decimateTree
mixin template decimateTree()

Mixin to simplify a parse tree inside a grammar

Structs

ParseTree
struct ParseTree

The basic parse tree, as used throughout the project. You can define your own parse tree node, but respect the basic layout.

Position
struct Position

To record a position in a text

Templates

action
template action(alias r, alias act)

Low-level representation for the expression 'r {act}'. That is, it applies rule 'r' on the input and then calls 'act' on the resulting ParseTree.

and
template and(rules...)

Basic operator: it matches if all its subrules (stored in the rules template parameter tuple) match the input successively. Its subrules parse trees are stored as its children and its matches field will contain all its subrules matches, in order.

caseInsensitiveLiteral
template caseInsensitiveLiteral(string s)

Represents a case insensitive literal in a PEG, like "abc"i or 'abc'i (or even ''i). It succeeds if a case insensitive comparison of a prefix of the input and its template parameter yields no difference and fails otherwise.

charRange
template charRange(dchar begin, dchar end)

Represents a range of chars, from begin to end, included. So charRange!('a','z') matches all English lowercase letters. If fails if the input is empty or does not begin with a character between begin and end.

defined
template defined(alias r, string name)

Internal helper template, to get a parse tree node with a name, while keeping the original node (see also named). For example, given:

discard
template discard(alias r)

Calls 'r' on the input and then discard everything 'r' returned: no children, no match and index put at the end of the match. It's the low-level engine behind ':r'.

discardChildren
template discardChildren(alias r)

Calls 'r' on the input and then discards its children nodes.

discardMatches
template discardMatches(alias r)

Calls 'r' on the input and then discards its matches.

drop
template drop(alias r)

Calls 'r' on the input and then discards everything 'r' did, except its matches (so that they propagate upwards). Equivalent to ';r'.

fuse
template fuse(alias r)

Concatenates a ParseTree's matches as one match and discards its children. Equivalent to the expression '~r'.

keep
template keep(alias r)

Makes 'r's result be kept when it would be discarded by the tree-decimation done by a grammar. Equivalent to '^r'.

keywords
template keywords(kws...)

or special case for literal list ("abstract"/"alias"/...)

list
template list(alias elem, alias sep)

A list of elem's separated by sep's. One element minimum.

list0
template list0(alias elem, alias sep)

A list of elem's separated by sep's. The empty list (no elem, no sep) is OK.

literal
template literal(string s)

Represents a literal in a PEG, like "abc" or 'abc' (or even ''). It succeeds if a prefix of the input is equal to its template parameter and fails otherwise.

longest_match
template longest_match(rules...)

Basic operator: it matches if one of its subrules (stored in the rules template parameter tuple) match the input. All subrules are tested and the one producing the longest match is taken.

named
template named(alias r, string name)

Internal helper template, to get a parse tree node with a name. For example, given:

negLookahead
template negLookahead(alias r)

Tries 'r' on the input. If it fails, the rule succeeds, without consuming any input. If 'r' succeeds, then negLookahead!r fails. Low-level implementation of '!r'.

oneOrMore
template oneOrMore(alias r)

Tries to match subrule 'r' one or more times. If 'r' fails from the very beginning, it fails and else succeeds.

option
template option(alias r)

Given a subrule 'r', represents the expression 'r?'. It tries to match 'r' and if this matches successfully, it returns this match. If 'r' failed, 'r?' is still a success, but without any child nor match.

or
template or(rules...)

Basic operator: it matches if one of its subrules (stored in the rules template parameter tuple) match the input. The subrules are tested in order, from rules[0] to rules[$-1].

posLookahead
template posLookahead(alias r)

Tries 'r' on the input. If it succeeds, the rule also succeeds, without consuming any input. If 'r' fails, then posLookahead!r also fails. Low-level implementation of '&r'.

propagate
template propagate(alias r)

Makes r disappear in a sequence, letting its children take its place. It's equivalent to the '%' operator. Given A <- B %C D and C <- E F, a successful parse for A will generate a three with four children: B, E, F and D parse trees.

spaceAnd
template spaceAnd(alias sp, rules...)

The engine formerly behind the '< ' Pegged rule: all sequence subelements of a rule are interspersed with a space-consuming rule, given as the first template parameter. It's not used by Pegged anymore but can be useful for low-level code. It might become deprecated, but it's not there yet.

zeroOrMore
template zeroOrMore(alias r)

Tries to match subrule 'r' zero or more times. It always succeeds, since if 'r' fails from the very beginning, it matched 'r' zero times...

Meta