jfun.parsec
public abstract class Parser<Type> extends Object implements Serializable
A parser runs either on character level or token level. It takes as input a CharSequence object or a Tok[] array, recognizes certain patterns and returns a value.
Method Summary | |
---|---|
<T,R> Parser<R> | and(Parser<T> p, Map2<? super Type,? super T,R> m)
it sequentially run this and p, and then transforms the two return values with m to a new return value. |
<T,R> Parser<R> | and(String name, Parser<T> p, Map2<? super Type,? super T,R> m)
it sequentially run this and p, and then transforms the two return values with m to a new return value. |
Parser<Type> | atomize(String name)
Make sure 'this' is atomic. |
Parser<Type> | atomize()
Make sure 'this' is atomic. |
<To> Parser<To> | bind(ToParser<? super Type,To> tp)
if this parser succeeds, the returned value gets passed on to tp.
|
<To> Parser<To> | bind(String name, ToParser<? super Type,To> tp)
if this parser succeeds, the returned value gets passed on to tp.
|
<R extends Type> Parser<R> | cast()
To cast the current Parser to a Parser object that returns a subtype of the current type. |
<R extends Type> Parser<R> | cast(Class<R> type)
To cast the current Parser to a Parser object that returns a subtype of the current type. |
<R> Parser<R> | convert()
To convert the current Parser to a Parser object that returns any target type. |
<R> Parser<R> | convert(Class<R> type)
To convert the current Parser to a Parser object that returns any target type. |
Parser<Type> | followedBy(Parser<?> sep)
'this' and 'sep' are executed sequentially.
|
Parser<Type> | followedBy(String name, Parser<?> sep)
'this' and 'sep' are executed sequentially.
|
<R> Parser | ifelse(Parser<R> yes, Parser<? extends R> no)
run yes if this succeeds, no if this fails without consuming input;
fails otherwise. |
<R> Parser<R> | ifelse(String name, Parser<R> yes, Parser<R> no)
run yes if this succeeds, no if this fails without consuming input;
fails otherwise. |
<R> Parser<R> | ifelse(ToParser<? super Type,R> yes, Parser<R> no)
run yes if this succeeds, no if this fails without consuming input;
fails otherwise. |
<R> Parser<R> | ifelse(String name, ToParser<? super Type,R> yes, Parser<R> no)
run yes if this succeeds, no if this fails without consuming input;
fails otherwise. |
Parser<Type> | isReturn(String name, ObjectPredicate<? super Type> op)
Fails if the return value of this parser does not satisify the given predicate.
|
Parser<Type> | isReturn(ObjectPredicate<? super Type> op)
Fails if the return value of this parser does not satisify the given predicate.
|
Parser<Type> | isReturn(String name, ObjectPredicate<? super Type> op, String expecting)
Fails if the return value of this parser does not satisify the given predicate.
|
Parser<Type> | isReturn(ObjectPredicate<? super Type> op, String expecting)
Fails if the return value of this parser does not satisify the given predicate.
|
Parser<Type> | label(String lbl)
if fails and did not consume input,
reports an expecting error with the given label. |
Parser<Type> | label(String name, String lbl)
if fails and did not consume input,
reports an expecting error with the given label. |
Parser<Type> | lookahead(String name, int toknum)
By default, ifelse, plus, sum will not try to run the next branch if the previous branch failed
and consumed some input.
this is because the default look-ahead token is 1.
|
Parser<Type> | lookahead(int toknum)
By default, ifelse, plus, sum will not try to run the next branch if the previous branch failed
and consumed some input.
this is because the default look-ahead token is 1.
|
Parser<Type[]> | many(ArrayFactory<Type> af)
p.many(af) is equivalent to p* in EBNF.
|
Parser<Type[]> | many(String name, ArrayFactory<Type> af)
p.many(name, af) is equivalent to p* in EBNF.
|
Parser<Type[]> | many(Class<Type> elem_type)
p.many(elem_type) is equivalent to p* in EBNF.
|
Parser<Type[]> | many(String name, Class<Type> elem_type)
p.many(name, elem_type) is equivalent to p* in EBNF.
|
Parser<_> | many()
p.many() is equivalent to p* in EBNF.
|
Parser<_> | many(String name)
p.many(name) is equivalent to p* in EBNF.
|
Parser<Type[]> | many(ArrayFactory<Type> af, int min)
Runs this parser greedily for at least min times.
|
Parser<Type[]> | many(String name, ArrayFactory<Type> af, int min)
Runs this parser greedily for at least min times.
|
Parser<Type[]> | many(Class<Type> elem_type, int min)
Runs this parser greedily for at least min times.
|
Parser<Type[]> | many(String name, Class<Type> elem_type, int min)
Runs this parser greedily for at least min times.
|
Parser<_> | many(int min)
Runs this parser greedily for at least min times.
|
Parser<_> | many(String name, int min)
Runs this parser greedily for at least min times.
|
Parser<Type[]> | many1(Class<Type> elem_type)
p.many1(elem_type) is equivalent to p+ in EBNF.
|
Parser<Type[]> | many1(String name, Class<Type> elem_type)
p.many1(name, elem_type) is equivalent to p+ in EBNF.
|
Parser<Type[]> | many1(ArrayFactory<Type> af)
p.many1(af) is equivalent to p+ in EBNF.
|
Parser<Type[]> | many1(String name, ArrayFactory<Type> af)
p.many1(name, af) is equivalent to p+ in EBNF.
|
Parser<_> | many1()
p.many1() is equivalent to p+ in EBNF.
|
Parser<_> | many1(String name)
p.many1(name) is equivalent to p+ in EBNF.
|
<R> Parser<R> | map(Map<? super Type,R> m)
if this succeeds, the returned value is transformed with m to a new return value. |
<R> Parser<R> | map(String name, Map<? super Type,R> m)
if this succeeds, the returned value is transformed with m to a new return value. |
Parser<?> | not()
fails if 'this' succeeds. |
Parser<?> | not(String err)
fails if 'this' succeeds. |
Parser<?> | not(String name, String err)
fails if 'this' succeeds. |
Parser<Type> | option(Type def)
If this fails with no input consumed, the default value is returned.
p.option(name, def) = p | retn(def). |
Parser<Type> | option(String name, Type def)
If this fails with no input consumed, the default value is returned.
p.option(name, def) = p | retn(def). |
Parser<Type> | optional()
p.optional() is equivalent to p? |
Parser<Type> | optional(String name)
p.optional(name) is equivalent to p? |
Type | parse(CharSequence source, String moduleName)
To parse a source string. |
Type | parse(CharSequence source)
To parse a source string. |
Parser<Type> | peek()
this is a look-ahead operation.
|
Parser<Type> | peek(String name)
this is a look-ahead operation.
|
Parser<Type> | printError(String id, int min_steps)
Create a Parser object that traces the parsing error of this parser when it fails.
|
Parser<Type> | printError(String id)
Create a Parser object that traces the parsing error of this parser when it fails.
|
Parser<Type> | printResult(String id)
Create a Parser object that traces the parsing result of this parser when it succeeds.
|
Parser<Type> | printTrace(String id)
Create a Parser object that traces the parsing result of this parser when it terminates.
|
Parser<_> | repeat(int n)
Run Parser 'this' for n times.
|
Parser<Type[]> | repeat(Class<Type> etype, int n)
Run Parser 'this' for n times, collect the return values in an array
whose element type is etype. |
Parser<Type[]> | repeat(ArrayFactory<Type> af, int n)
Run Parser 'this' for n times, collect the return values in an array
created by the ArrayFactory object. |
Parser<_> | repeat(String name, int n)
Run Parser 'this' for n times.
|
Parser<Type[]> | repeat(String name, Class<Type> etype, int n)
Run Parser 'this' for n times, collect the return values in an array
whose element type is etype. |
Parser<Type[]> | repeat(String name, ArrayFactory<Type> af, int n)
Run Parser 'this' for n times, collect the return values in an array
created by the ArrayFactory object. |
<R> Parser<R> | seq(Parser<R> p)
if this parser succeeds,
the returned value is discarded and the next parser is excuted.
|
<R> Parser<R> | seq(String name, Parser<R> p)
if this parser succeeds,
the returned value is discarded and the next parser is excuted.
|
Parser<Type[]> | some(ArrayFactory<Type> af, int min, int max)
Runs this for at least min times and at most max times.
|
Parser<Type[]> | some(String name, ArrayFactory<Type> af, int min, int max)
Runs this for at least min times and at most max times.
|
Parser<Type[]> | some(Class<Type> elem_type, int min, int max)
Runs this for at least min times and at most max times.
|
Parser<Type[]> | some(String name, Class<Type> elem_type, int min, int max)
Runs this for at least min times and at most max times.
|
Parser<_> | some(int min, int max)
Runs this for at least min times and at most max times.
|
Parser<_> | some(String name, int min, int max)
Runs this for at least min times and at most max times.
|
Parser<_> | some(int max)
Runs this for up to max times.
|
Parser<Type[]> | some(Class<Type> etype, int max)
Runs this for up to max times.
|
Parser<Type[]> | some(ArrayFactory<Type> af, int max)
Runs this for up to max times.
|
Parser<_> | some(String name, int max)
Runs this for up to max times.
|
Parser<Type[]> | some(String name, Class<Type> etype, int max)
Runs this for up to max times.
|
Parser<Type[]> | some(String name, ArrayFactory<Type> af, int max)
Runs this for up to max times.
|
Parser<Type> | step(String name, int n)
lookahead looks at logical steps.
step(int) runs this parser and sets the number of logical steps.
|
Parser<Type> | step(String name)
lookahead looks at logical steps.
step() runs this parser and sets 1 logical step.
|
Parser<Type> | step(int n)
lookahead looks at logical steps.
step(int) runs this parser and sets the number of logical steps.
|
Parser<Type> | step()
lookahead looks at logical steps.
step() runs this parser and sets 1 logical step.
|
String | toString() |
Parser<Type> | trace(Trace<? super Type> trc)
Create a Parser object that traces the parsing result of this parser.
|
Parameters: p the next parser to run. m the transformation.
Returns: the new Parser object.
Parameters: name the name of the new parser. p the next parser to run. m the transformation.
Returns: the new Parser object.
Parameters: name the name of the new Parser.
Returns: the new Parser.
Returns: the new Parser.
Parameters: tp the next step.
Returns: the new Parser.
Parameters: name the name of the new parser. tp the next step.
Returns: the new Parser.
Parameters:
Returns: the Parser object that returns the expected type.
Since: version 1.0
Parameters:
Returns: the Parser object that returns the expected type.
Since: version 1.0
Parameters:
Returns: the Parser object that returns the expected type.
Since: version 1.0
Parameters:
Returns: the Parser object that returns the expected type.
Since: version 1.0
Parameters: sep the following parser.
Returns: the new Parser.
Parameters: name the name of the new Parser. sep the following parser.
Returns: the new Parser.
Parameters: yes the true branch. no the false branch.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. yes the true branch. no the false branch.
Returns: the new Parser object.
Parameters: yes the true branch. no the false branch.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. yes the true branch. no the false branch.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. op the predicate object.
Returns: the new Parser object.
Parameters: op the predicate object.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. op the predicate object. expecting the expected string.
Returns: the new Parser object.
Parameters: op the predicate object. expecting the expected string.
Returns: the new Parser object.
Parameters: lbl the label text.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. lbl the label text.
Returns: the new Parser object.
lookahead only affects one nesting level. Parsers.plus(p1,p2).ifelse(yes,no).lookahead(3) will not affect the Parsers.plus(p1,p2) nested within ifelse.
lookahead directly on top of lookahead will override the previous lookahead.
Parsers.plus(p1,p2).lookahead(3).lookahead(1)
is equivalent as Parsers.plus(p1, p2).lookahead(1).
lookahead looks at logical step.
by default, each terminal is one logical step.
atomize() combinator ensures at most 1 logical step for a parser.
Use step() combinator to fine control logical steps.
Parameters: name the name of the new Parser object. toknum the number of tokens to look ahead.
Returns: the new Parser object.
lookahead only affects one nesting level. Parsers.plus(p1,p2).ifelse(yes,no).lookahead(3) will not affect the Parsers.plus(p1,p2) nested within ifelse.
lookahead directly on top of lookahead will override the previous lookahead.
Parsers.plus(p1,p2).lookahead(3).lookahead(1)
is equivalent as Parsers.plus(p1, p2).lookahead(1).
lookahead looks at logical step.
by default, each terminal is one logical step.
atomize() combinator ensures at most 1 logical step for a parser.
Use step() combinator to fine control logical steps.
Parameters: toknum the number of tokens to look ahead.
Returns: the new Parser object.
Parameters: af the ArrayFactory.
Returns: the new Parser.
Parameters: name the name of the new parser. af the ArrayFactory.
Returns: the new Parser.
Parameters: elem_type the element type of the result array.
Returns: the new Parser.
Parameters: name the name of the new parser. elem_type the element type of the result array.
Returns: the new Parser.
Returns: the new Parser.
Parameters: name the name of the new parser.
Returns: the new Parser.
Parameters: af the ArrayFactory. min the minimal number of times to run this parser.
Returns: the new Parser.
Parameters: name the name of the new parser. af the ArrayFactory. min the minimal number of times to run this parser.
Returns: the new Parser.
Parameters: elem_type the element type of the result array. min the minimal number of times to run this parser.
Returns: the new Parser.
Parameters: name the name of the new parser. elem_type the element type of the result array. min the minimal number of times to run this parser.
Returns: the new Parser.
Returns: the new Parser.
Parameters: name the name of the new parser. min the minimal number of times to run this parser.
Returns: the new Parser.
Parameters: elem_type the element type of the result array.
Returns: the new Parser.
Parameters: name the name of the new parser. elem_type the element type of the result array.
Returns: the new Parser.
Parameters: af the ArrayFactory.
Returns: the new Parser.
Parameters: name the name of the new parser. af the ArrayFactory.
Returns: the new Parser.
Returns: the new Parser.
Parameters: name the name of the new parser.
Returns: the new Parser.
Parameters: m the map object to transform return value.
Returns: the new Parser.
Parameters: name the name of the new Parser. m the map object to transform return value.
Returns: the new Parser.
Returns: the new Parser.
Parameters: err the error message if fails.
Returns: the new Parser.
Parameters: name the name of the new Parser. err the error message if fails.
Returns: the new Parser.
Parameters: def the default value.
Returns: the new Parser.
Parameters: name the name of the new Parser. def the default value.
Returns: the new Parser.
Returns: the new Parser.
Parameters: name the name of the new Parser.
Returns: the new Parser.
Parameters: source the source string. moduleName the name of the module, this name appears in error message.
Returns: the result.
Since: v1.2
Parameters: source the source string.
Returns: the result.
Since: v1.2
Returns: the new Parser.
Parameters: name the name of the new Parser
Returns: the new Parser
Parameters: id the identifier of the parser object in the trace message. min_steps the minimal logical steps consumed to trigger the trace message.
Returns: the new Parser object.
Since: version 1.1
Parameters: id the identifier of the parser object in the trace message.
Returns: the new Parser object.
Since: version 1.1
Parameters: id the identifier of the parser object in the trace message.
Returns: the new Parser object.
Since: version 1.1
Parameters: id the identifier of the parser object in the trace message.
Returns: the new Parser object.
Since: version 1.1
Parameters: n the number of times to run.
Returns: the new Parser object.
Parameters: etype the array element type. n the number of times to run.
Returns: the new Parser object.
Parameters: af the ArrayFactory object. n the number of times to run.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. n the number of times to run.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. etype the array element type. n the number of times to run.
Returns: the new Parser object.
Parameters: name the name of the new Parser object. af the ArrayFactory object. n the number of times to run.
Returns: the new Parser object.
Parameters: p the next parser.
Returns: the new Parser.
Parameters: name the name of the new parser. p the next parser.
Returns: the new Parser.
Parameters: af the ArrayFactory. min the minimal number of times to run this parser. max the maximal number of times to run this parser.
Returns: the new Parser.
Parameters: name the name of the new parser. af the ArrayFactory. min the minimal number of times to run this parser. max the maximal number of times to run this parser.
Returns: the new Parser.
Parameters: elem_type the element type of the result array. min the minimal number of times to run this parser. max the maximal number of times to run this parser.
Returns: the new Parser.
Parameters: elem_type the element type of the result array. min the minimal number of times to run this parser. max the maximal number of times to run this parser.
Returns: the new Parser.
Parameters: min the minimal number of times to run this parser. max the maximal number of times to run this parser.
Returns: the new Parser.
Parameters: name the name of the new parser. min the minimal number of times to run this parser. max the maximal number of times to run this parser.
Returns: the new Parser.
Parameters: max the maximal number of times to run.
Returns: the new Parser.
Parameters: etype the element type of the result array. max the maximal number times to run.
Returns: the new Parser.
Parameters: max the maximal number of times to run.
Returns: the new Parser.
Parameters: name the name of the new parser. max the maximal number times to run.
Returns: the new Parser.
Parameters: name the name of the new parser. etype the element type of the result array. max the maximal number of times to run.
Returns: the new Parser.
Parameters: name the name of the new parser. af the ArrayFactory object. max the maximal number of times to run.
Returns: the new Parser.
Parameters: name the name of the new Parser object. n the number logical steps. n>=0 has to be true.
Returns: the new Parser object.
Parameters: name the name of the new Parser object.
Returns: the new Parser object.
Parameters: n the number logical steps. n>=0 has to be true.
Returns: the new Parser object.
Returns: the new Parser object.
Parameters: trc the Trace object.
Returns: the new Parser object.
Since: version 1.1