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

Aliases

Alpha
alias Alpha = charRange!('A', 'Z')
Undocumented in source.
alpha
alias alpha = charRange!('a', 'z')
Undocumented in source.
backquote
alias backquote = named!(literal!"`", "backquote")
Undocumented in source.
backslash
alias backslash = named!(literal!"\\", "backslash")
Undocumented in source.
blank
alias blank = or!(space, endOfLine)
Undocumented in source.
digit
alias digit = charRange!('0', '9')
Undocumented in source.
digits
alias digits = named!(fuse!(discardChildren!(oneOrMore!digit)), "digits")
Undocumented in source.
doublequote
alias doublequote = named!(literal!"\"", "doublequote")
Undocumented in source.
endOfInput
alias endOfInput = eoi
Undocumented in source.
endOfLine
alias endOfLine = named!(or!(literal!("\r\n"), literal!("\n"), literal!("\r")), "endOfLine")
Undocumented in source.
eol
alias eol = endOfLine
Undocumented in source.
hexDigit
alias hexDigit = or!(charRange!('0', '9'), charRange!('a', 'f'), charRange!('A', 'F'))
Undocumented in source.
ident
alias ident = and!(oneOrMore!(or!(alpha, Alpha, literal!("_"))), zeroOrMore!(or!(digit, alpha, Alpha, literal!("_"))))
Undocumented in source.
identifier
alias identifier = named!(fuse!(discardChildren!ident), "identifier")
Undocumented in source.
qualifiedIdentifier
alias qualifiedIdentifier = named!(fuse!(discardChildren!(and!(identifier, zeroOrMore!(and!(literal!".", identifier))))), "qualifiedIdentifier")
Undocumented in source.
quote
alias quote = named!(literal!"'", "quote")
Undocumented in source.
slash
alias slash = named!(literal!"/", "slash")
Undocumented in source.
space
alias space = or!(literal!(" "), literal!("\t"), literal!("\v"))
Undocumented in source.
spaces
alias spaces = named!(fuse!(discardChildren!(oneOrMore!space)), "spaces")
Undocumented in source.
spacing
alias spacing = named!(discard!(zeroOrMore!blank), "spacing")
Undocumented in source.
tab
alias tab = named!(literal!"\t", "tab")
Undocumented in source.

Classes

TraceLogger
class TraceLogger

Overrides FileLogger to remove the time stamp.

Trie
class Trie(V)

Compile-time switch trie from Brian Schott

TrieNode
class TrieNode(V)
Undocumented in source.

Enums

IFCHAIN
anonymousenum 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.

any
string any(GetName g)
Undocumented in source. Be warned that the author may not have intended to support it.
eoi
ParseTree eoi(ParseTree p)
ParseTree eoi(string input)

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

eoi
string eoi(GetName g)
Undocumented in source. Be warned that the author may not have intended to support it.
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).

eps
ParseTree eps(string input)
Undocumented in source. Be warned that the author may not have intended to support it.
eps
string eps(GetName g)
Undocumented in source. Be warned that the author may not have intended to support it.
fail
ParseTree fail(ParseTree p)
ParseTree fail(string input)

Basic rule, that always fail without consuming.

fail
string fail(GetName g)
Undocumented in source. Be warned that the author may not have intended to support it.
generateCaseTrie
string generateCaseTrie(string[] args)
Undocumented in source. Be warned that the author may not have intended to support it.
getName
string getName()
Undocumented in source. Be warned that the author may not have intended to support it.
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(ParseTree p)

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

printCaseStatements
string printCaseStatements(TrieNode!(V) node, string indentString)
Undocumented in source. Be warned that the author may not have intended to support it.
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.

stringified
string stringified(string inp)
Undocumented in source. Be warned that the author may not have intended to support it.
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.

wordBoundary
string wordBoundary(GetName g)
Undocumented in source. Be warned that the author may not have intended to support it.

Manifest constants

KEYWORDS
enum KEYWORDS;
Undocumented in source.

Mixin templates

decimateTree
mixintemplate decimateTree()

Mixin to simplify a parse tree inside a grammar

Structs

GetName
struct GetName
Undocumented in source.
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

AddSpace
template AddSpace(alias sp)
Undocumented in source.
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.

wrapAround
template wrapAround(alias before, alias target, alias after)
Undocumented in source.
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