|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.jparsec.misc.Mapper<T>
public abstract class Mapper<T>
Allows mapping arbitrary number of Parser
results to an object of T
type-safely
using the map
method defined in subclass, or the curried constructor
defined in the target class.
The sequence(Parser[])
method creates a parser that runs a series of
parser objects and maps the return values via the map
method defined in the
subclass (or the curried constructor in target class).
For example:
Parser<Foo> fooParser = new Mapper<Foo>() { Foo map(String s, Integer n, Bar bar, Baz baz) { return new Foo(s, n, bar, baz); } }.sequence(stringParser, integerParser, barParser, bazParser);
Alternatively, instead of sequencing the operands and operators directly,
a parser of Unary
or Binary
can be returned to cooperate with
OperatorTable
, Parser.prefix(Parser)
,
Parser.postfix(Parser)
, Parser.infixl(Parser)
,
Parser.infixn(Parser)
or Parser.infixr(Parser)
.
Another useful utility provide by this class, is the curry(Class, Object[])
method.
It allows currying constructor defined in the target class so that no explicit mapping code
is needed. For the above example, it can be more concisely written as:
Parser<Foo> fooParser = Mapper.curry(Foo.class) .sequence(stringParser, integerParser, barParser, bazParser);
NOTE: cglib is required on the classpath.
Constructor Summary | |
---|---|
protected |
Mapper()
Default constructor that uses the map method defined in subclass for mapping. |
Method Summary | ||
---|---|---|
static Parser<?> |
_(Parser<?> parser)
Wraps parser so that it will be skipped when applied in sequence(Parser[]) ,
prefix(Parser[]) or postfix(Parser[]) . |
|
Parser<Binary<T>> |
binary()
A Parser that returns a Binary instance that invokes the underlying
map method or curried constructor with the two parameters of the
Map2.map(Object, Object) method. |
|
static
|
curry(Class<? extends T> clazz,
Object... curryArgs)
A Mapper that curries the only public constructor defined in clazz
and invokes it with parameters returned by the sequentially executed Parser objects. |
|
Parser<Binary<T>> |
infix(Parser<?>... operator)
A Parser that runs operator sequentially and returns a Binary instance,
which will pass along its first parameter, followed by the return values of operator ,
followed by its second parameter to the underlying map method or curried constructor. |
|
Parser<Binary<T>> |
infix(Parser<?> operator)
A Parser that runs operator and returns a Binary instance,
which will pass along its first parameter, followed by the return value of operator ,
followed by its second parameter to the underlying map method or curried constructor. |
|
Parser<Unary<T>> |
postfix(Parser<?>... operator)
A Parser that runs operator sequentially and returns a Unary instance,
which will pass along its only parameter followed by the return values of operator
to the underlying map method or curried constructor. |
|
Parser<Unary<T>> |
postfix(Parser<?> operator)
A Parser that runs operator and returns a Unary instance,
which will pass along its only parameter followed by the return value of operator
to the underlying map method or curried constructor. |
|
Parser<Unary<T>> |
prefix(Parser<?>... operator)
A Parser that runs operator sequentially and returns a Unary instance,
which will pass along the return values of operator followed by its only parameter
to the underlying map method or curried constructor. |
|
Parser<Unary<T>> |
prefix(Parser<?> operator)
A Parser that runs operator and returns a Unary instance,
which will pass along the return value of operator followed by its only parameter
to the underlying map method or curried constructor. |
|
Parser<T> |
sequence(Parser<?>... parsers)
A Parser that sequentially runs parsers and invokes the underlying
map method or curried constructor using the returned values. |
|
String |
toString()
Returns the string representation of this object. |
|
Parser<Unary<T>> |
unary()
A Parser that returns a Unary instance that invokes the underlying
map method or curried constructor with the only parameter of the
Map.map(Object) method. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
protected Mapper()
map
method defined in subclass for mapping.
Method Detail |
---|
public static <T> Mapper<T> curry(Class<? extends T> clazz, Object... curryArgs)
Mapper
that curries the only public constructor defined in clazz
and invokes it with parameters returned by the sequentially executed Parser
objects.
For example, to parse an expression with binary operator and create an instance of
the following object model:
class BinaryExpression implements Expression { public BinaryExpression(Expression left, Operator op, Expression right) {...} ... }The parser that parses this expression with binary operator can be written as:
Parser<Expression> binary(Parser<Expression> operand, Parser<Operator> operator) { return Sequencer.<Expression>curry(BinaryExpression.class) .sequence(operand, operator, operand); }Which is equivalent to the more verbose but reflection-free version:
Parser<Expression> binary(Parser<Expression> expr, Parser<Operator> op) { return Parsers.sequence(expr, op, expr, new Map3<Expression, Operator, Expression, Expression>() { public Expression map(Expression left, Operator op, Expression right) { return new BinaryExpression(left, op, right); } }); }
public final Parser<T> sequence(Parser<?>... parsers)
Parser
that sequentially runs parsers
and invokes the underlying
map
method or curried constructor using the returned values.
public final Parser<Unary<T>> unary()
Parser
that returns a Unary
instance that invokes the underlying
map
method or curried constructor with the only parameter of the
Map.map(Object)
method.
public final Parser<Binary<T>> binary()
Parser
that returns a Binary
instance that invokes the underlying
map
method or curried constructor with the two parameters of the
Map2.map(Object, Object)
method.
public final Parser<Unary<T>> prefix(Parser<?> operator)
Parser
that runs operator
and returns a Unary
instance,
which will pass along the return value of operator
followed by its only parameter
to the underlying map
method or curried constructor.
For example:
Parser<Unary<Expression>> prefixOperator(Parser<Operator> op) { return new Mapper<Expression>() { Expression map(Operator operator, Expression operand) { return new PrefixExpression(operator, operand); } }.prefix(op); }Or alternatively, by using the
curry(Class, Object[])
method: Parser<Unary<Expression>> prefixOperator(Parser<Operator> op) { return Mapper.<Expression>curry(PrefixExpression.class).prefix(op); }
Useful when the returned parser is used in Parser.prefix(Parser)
or
OperatorTable
.
public final Parser<Unary<T>> prefix(Parser<?>... operator)
Parser
that runs operator
sequentially and returns a Unary
instance,
which will pass along the return values of operator
followed by its only parameter
to the underlying map
method or curried constructor.
Use this version instead of prefix(Parser)
if the operator is composed of more
than one components. For example, the Java label statement (like here:
) can be
modeled as a prefix operator applied to statements:
Parser<Unary<Statement>> label = new Mapper<Statement>() { Statement map(String label, Statement statement) { return new LabelStatement(label, statement); } }.prefix(Terminals.STRING, _(terminal(":")));Or alternatively, by using the
curry(Class, Object[])
method:
Parser<Unary<Statement>> label = Mapper.<Statement>curry(LabelStatement.class) .prefix(Terminals.STRING, _(terminal(":")));
Useful when the returned parser is used in Parser.prefix(Parser)
or
OperatorTable
.
public final Parser<Unary<T>> postfix(Parser<?> operator)
Parser
that runs operator
and returns a Unary
instance,
which will pass along its only parameter followed by the return value of operator
to the underlying map
method or curried constructor.
For example:
Parser<Binary<Expression>> postfixOperator(Parser<Operator> op) { return new Mapper<Expression>() { Expression map(Expression operand, Operator operator) { return new PostfixExpression(operand, operator); } }.postfix(op); }Or alternatively, by using the
curry(Class, Object[])
method: Parser<Unary<Expression>> postfixOperator(Parser<Operator> op) { return Mapper.<Expression>curry(PostfixExpression.class).postfix(op); }
Useful when the returned parser is used in Parser.postfix(Parser)
or
OperatorTable
.
public final Parser<Unary<T>> postfix(Parser<?>... operator)
Parser
that runs operator
sequentially and returns a Unary
instance,
which will pass along its only parameter followed by the return values of operator
to the underlying map
method or curried constructor.
Use this version instead of postfix(Parser)
if the operator is composed of more
than one components.
For example, in order to parse an array slice syntax such as array[from, to]
,
where array
, from
and to
are all themselves expressions,
the [from, to]
part can be modeled as a postfix operator that turns the
array
expression to an array slice expression.
The parser can be written as:
Parser<Unary<Expression>> slice(Parser<Expression bound) { return new Mapper<Expression>() { Expression map(Expression array, Expression from, Expression to) { return new ArraySliceExpression(array, from, to); } }.postfix(_(terminal("[")), bound, _(terminal(",")), bound, _(terminal("]"))); }Or alternatively, by using the
curry(Class, Object[])
method:
Parser<Unary<Expression>> slice(Parser<Expression bound) { return Mapper.<Expression>curry(ArraySliceExpression.class) .postfix(_(terminal("[")), bound, _(terminal(",")), bound, _(terminal("]"))); }
Useful when the returned parser is used in Parser.postfix(Parser)
or
OperatorTable
.
public final Parser<Binary<T>> infix(Parser<?> operator)
Parser
that runs operator
and returns a Binary
instance,
which will pass along its first parameter, followed by the return value of operator
,
followed by its second parameter to the underlying map
method or curried constructor.
For example:
Parser<Binary<Expression>> infixOperator(Parser<Operator> op) { return new Mapper<Expression>() { Expression map(Expression left, Operator operator, Expression right) { return new InfixExpression(left, operand, right); } }.infix(op); }Or alternatively, by using the
curry(Class, Object[])
method: Parser<Binary<Expression>> infixOperator(Parser<Operator> op) { return Mapper.<Expression>curry(InfixExpression.class).infix(op); }
Useful when the returned parser is used in Parser.infixl(Parser)
,
Parser.infixn(Parser)
, Parser.infixr(Parser)
or OperatorTable
.
public final Parser<Binary<T>> infix(Parser<?>... operator)
Parser
that runs operator
sequentially and returns a Binary
instance,
which will pass along its first parameter, followed by the return values of operator
,
followed by its second parameter to the underlying map
method or curried constructor.
Use this version instead of infix(Parser)
if the operator is composed of more
than one components.
For example, in order to parse the Java ternary ?:
operator, we can model the
? consequence :
part as a right associative infix operator that binds the condition and
the alternative expression together as a composite expression. The parser can be written as:
Parser<Binary<Expression>> conditional(Parser<Expression> expr) { return new Mapper<Expression>() { Expression map(Expression condition, Expression consequence, Expression alternative) { return new ConditionalExpression(condition, consequence, alternative); } }.postfix(_(terminal("?")), expr, _(terminal(":"))); }Or alternatively, by using the
curry(Class, Object[])
method:
Parser<Binary<Expression>> conditional(Parser<Expression> expr) { return Mapper.<Expression>.curry(ConditionalExpression.class) .postfix(_(terminal("?")), expr, _(terminal(":"))); }
Useful when the returned parser is used in Parser.infixl(Parser)
,
Parser.infixn(Parser)
, Parser.infixr(Parser)
or OperatorTable
.
public static final Parser<?> _(Parser<?> parser)
parser
so that it will be skipped when applied in sequence(Parser[])
,
prefix(Parser[])
or postfix(Parser[])
. For example, the following code
maps the two expressions after "if" and "else" to the constructor of IfElseExpression
and skips the return values of the keyword "if" and "else".
Parser<IfElseExpression> expression = curry(IfElseExpression.class).sequence( _(word("if")), expr, _(word("else")), expr);
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |