V - Value stack type.public class PegLegParser<V> extends Object implements Supplier<PegLegParser<V>>
This class implements a complete PEG parser, a la https://en.wikipedia.org/wiki/Parsing_expression_grammar
I have long been a big fan of the Parboiled parser framework ( https://github.com/sirthias/parboiled/wiki ), especially for quick and dirty things. But I also always disliked the proxying/byte code manipulation in it. A looong while ago I looked at building one myself with just anon classes, around Java 6 timeframe, but it was super clunky. For a while now I have wanted to take another swing at it using lambdas; it seemed like a way to do all of what Parboiled (and Rats!, etc) did without needing anything too exotic.
Turns out, yup, works pretty well. An approach like this will never be the quickest parser to run, I mean, there is no packrat processing, no memoization, etc. So it is not a speed demon. But the intention was to make it super expressive and a speed demon to write parsers.
See the PegLeParser.adoc file for an overview on how it works, and see the unit test class for a set of example grammars.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
PegLegParser.CharTerminal<V>
Terminal based on some set of characters.
|
static interface |
PegLegParser.Exec
An execution, which return true to continue parsing, false to stop.
|
class |
PegLegParser.ParentRule |
static interface |
PegLegParser.PegLegRule<V>
A Rule.
|
static class |
PegLegParser.Ref<V>
Random holder class for intra rule parser data manipulation.
|
static class |
PegLegParser.RuleReturn<V>
Rule return.
|
static class |
PegLegParser.SourceFrame |
static class |
PegLegParser.SourcePosition |
static interface |
PegLegParser.TerminalRule<V>
Terminal rule, with optional ignoring of case.
|
static class |
PegLegParser.Values<V>
Stack of values.
|
| Modifier and Type | Field and Description |
|---|---|
PegLegParser.SourcePosition |
farthestSuccessfulPos |
| Constructor and Description |
|---|
PegLegParser() |
| Modifier and Type | Method and Description |
|---|---|
PegLegParser.PegLegRule<V> |
anyChar()
Rule with matches any character.
|
PegLegParser.CharTerminal<V> |
anyOf(CharSequence string)
Generates a rule which matches any single char in the char sequence.
|
PegLegParser.CharTerminal<V> |
ch(char ch)
Generate a terminal rule for a specific character.
|
PegLegParser.CharTerminal<V> |
charRange(char from,
char to)
Generate a terminal rule for a range of characters.
|
PegLegParser.CharTerminal<V> |
dictionaryOf(String... strings)
Dictionary lookup, optimized.
|
PegLegParser.PegLegRule<V> |
empty()
Returns true, matches nothing, consumes nothing.
|
PegLegParser.PegLegRule<V> |
eof() |
PegLegParser.PegLegRule<V> |
eol() |
protected IllegalArgumentException |
error(String message) |
PegLegParser.Exec |
ex(Runnable del)
Execs a blob of code, always continues parsing.
|
PegLegParser.ParentRule |
firstOf(Object... objs)
Core PEG rule "Choice", here called firstOf.
|
PegLegParser<V> |
get()
Get context object at the moment.
|
String |
getFailureMessage() |
PegLegParser.SourcePosition |
getFarthestSuccessfulPos() |
PegLegParser.RuleReturn<V> |
getLastReturn()
Last rule return.
|
PegLegParser.RuleReturn<V> |
getLastSuccessfulReturn() |
String |
getWhiteSpace() |
Optional<String> |
match()
The last rule's matched literal.
|
PegLegParser.PegLegRule<V> |
named(String name,
PegLegParser.PegLegRule<V> del)
name a rule
|
PegLegParser.CharTerminal<V> |
noneOf(CharSequence string)
Rule that matches any character not contained in the char sequence specified.
|
PegLegParser.PegLegRule<V> |
nothing()
Always fails to match, consumes nothing
|
PegLegParser.ParentRule |
onePlusOf(Object... objs)
Core PEG rule "OneOrMore", matches the given set of rules, greedily, one or more times.
|
PegLegParser.ParentRule |
optOf(Object... objs)
Core PEG rule "Optional", matches the given set of rules, greedily, zero or one times.
|
PegLegParser.RuleReturn<V> |
parse(PegLegParser.PegLegRule<V> rule)
A Parsing entry point.
|
List<String> |
parseTrail() |
PegLegParser.ParentRule |
seqOf(Object... objs)
Core PEG rule: "Sequence".
|
void |
setLineSeparator(String sep) |
void |
setWhiteSpace(String whiteSpace) |
PegLegParser.CharTerminal<V> |
str(CharSequence string)
Generate a rule for a specific string (char sequence)
|
PegLegParser.Exec |
testExec(PegLegParser.Exec del)
Execs a blob of code, stops parsing if returns false.
|
PegLegParser.ParentRule |
testNotOf(Object... objs)
Core PEG rule: "TestNot".
|
PegLegParser.ParentRule |
testOf(Object... objs)
Core PEG Rule, "Test" returns true if sequence of rules matches, but does not consume input.
|
PegLegParser.ParentRule |
timesOf(int min,
int max,
Object... objs)
Rule with matches only if matches set of rules between min and max times.
|
PegLegParser.ParentRule |
timesOf(int many,
Object... objs)
Rule with matches only if matches set of rules exactly many times.
|
PegLegParser<V> |
using(CharSequence input)
Reset parser for given input.
|
PegLegParser.Values<V> |
values()
Value stack.
|
PegLegParser.PegLegRule<V> |
ws()
Whitespace rule.
|
PegLegParser.CharTerminal<V> |
ws(char ch)
Generates a rule for the char, allowing any amount of preceding/following whitespace.
|
PegLegParser.CharTerminal<V> |
ws(CharSequence string)
Generates a rule for the char sequence, allowing any amount of preceding/following whitespace.
|
PegLegParser.ParentRule |
zeroPlusOf(Object... objs)
Core PEG rule "ZeroOrMore", matches the given set of rules, greedily, zero or more times.
|
public PegLegParser.SourcePosition farthestSuccessfulPos
public PegLegParser.RuleReturn<V> getLastReturn()
public PegLegParser.RuleReturn<V> getLastSuccessfulReturn()
public void setWhiteSpace(String whiteSpace)
public void setLineSeparator(String sep)
public String getWhiteSpace()
public PegLegParser<V> using(CharSequence input)
input - string inputpublic PegLegParser<V> get()
get in interface Supplier<PegLegParser<V>>protected IllegalArgumentException error(String message)
public PegLegParser.CharTerminal<V> ch(char ch)
ch - charpublic PegLegParser.CharTerminal<V> charRange(char from, char to)
from - lower boundto - upper bound.public PegLegParser.CharTerminal<V> str(CharSequence string)
string - char sequencepublic PegLegParser.CharTerminal<V> ws(CharSequence string)
string - char sequencepublic PegLegParser.CharTerminal<V> ws(char ch)
ch - characterpublic PegLegParser.PegLegRule<V> ws()
public PegLegParser.CharTerminal<V> anyOf(CharSequence string)
string - candidate chars.public PegLegParser.CharTerminal<V> dictionaryOf(String... strings)
strings - set of strings to matchpublic PegLegParser.CharTerminal<V> noneOf(CharSequence string)
string - char sequence not to matchpublic PegLegParser.PegLegRule<V> eof()
public PegLegParser.PegLegRule<V> eol()
public PegLegParser.ParentRule seqOf(Object... objs)
objs - rules/string literals/char literals/execspublic PegLegParser.ParentRule firstOf(Object... objs)
objs - rules/string literals/char literals/execspublic PegLegParser.ParentRule testOf(Object... objs)
objs - rules/string literals/char literals/execspublic PegLegParser.ParentRule testNotOf(Object... objs)
objs - rules/string literals/char literals/execspublic PegLegParser.ParentRule timesOf(int min, int max, Object... objs)
min - min times to matchmax - max times to matchobjs - rules/string literals/char literals/execspublic PegLegParser.ParentRule timesOf(int many, Object... objs)
many - times to matchobjs - rules/string literals/char literals/execspublic PegLegParser.ParentRule zeroPlusOf(Object... objs)
objs - rules/string literals/char literals/execspublic PegLegParser.ParentRule onePlusOf(Object... objs)
objs - rules/string literals/char literals/execspublic PegLegParser.ParentRule optOf(Object... objs)
objs - rules/string literals/char literals/execspublic PegLegParser.Exec testExec(PegLegParser.Exec del)
del - delegatepublic PegLegParser.Exec ex(Runnable del)
del - delegate callpublic PegLegParser.PegLegRule<V> anyChar()
public PegLegParser.PegLegRule<V> nothing()
public PegLegParser.PegLegRule<V> empty()
public PegLegParser.Values<V> values()
public PegLegParser.RuleReturn<V> parse(PegLegParser.PegLegRule<V> rule)
rule - rule to start withpublic PegLegParser.SourcePosition getFarthestSuccessfulPos()
public String getFailureMessage()
public PegLegParser.PegLegRule<V> named(String name, PegLegParser.PegLegRule<V> del)
name - namedel - delegateCopyright © 2020. All rights reserved.