public class IfContext extends AbstractFaAction
is an FaAction
to execute different client actions
depending on the Context
in which
IfContext.invoke()
is called. Typical usage:
nfa.or("regex", new IfContext(ctx1, action1) .ifthen(ctx2, action2) .ifthen(ctx3, action3) .elsedo(actionDefault))
The context objects ctx1
etc. must be acquired before
by use of a ContextManager
.
It is not necessary to add all valid contexts for a certain
"regex"
with the same IfAction
object. Any number of IfAction
objects may be bound to
the regular expression or to regular expressions which share
matches. When the Nfa
is compiled into a
Dfa
, mergeWith()
will combine all
actions into one.
Constructor and Description |
---|
IfContext()
create an action which does only perform default actions.
|
IfContext(Context ctx,
FaAction a)
creates an
FaAction to run the given client action if
called in the given Context . |
Modifier and Type | Method and Description |
---|---|
IfContext |
elsedo(FaAction a)
define the action to call when this object's invoke
method is called in a context for which no client action is
registered.
|
IfContext |
ifthen(Context ctx,
FaAction a)
defines to call the given action when this object's invoke method
is called in the given context.
|
void |
invoke(java.lang.StringBuilder yytext,
int start,
DfaRun r)
required by interface
FaAction . |
FaAction |
mergeWith(FaAction _other)
is eventually called by
Nfa.compile(monq.jfa.DfaRun.FailedMatchBehaviour, monq.jfa.FaAction) to create a
unified action from this object and another FaAction
object. |
java.lang.String |
toString() |
setPriority
public IfContext()
public IfContext ifthen(Context ctx, FaAction a)
defines to call the given action when this object's invoke method is called in the given context.
NOTE: The given Context
may be
null
to indicate that the action should be run if no
context is (yet) on the stack.
public IfContext elsedo(FaAction a)
define the action to call when this object's invoke
method is called in a context for which no client action is
registered. The default action used if this method is not called
mimics the behaviour of the calling DfaRun
for
unmatched text.
public FaAction mergeWith(FaAction _other)
FaAction
is eventually called by Nfa.compile(monq.jfa.DfaRun.FailedMatchBehaviour, monq.jfa.FaAction)
to create a
unified action from this object and another FaAction
object. During compilation of an Nfa into a Dfa, two
regular expressions assigned to different actions may have
overlapping matches. In this case one of the actions'
mergeWith
methods is called with the other action as
parameter to create a unified action object.
There are only four permissible results this method may return:
this
completely unchanged,other
completely unchanged,null
to signal that no unified action exists.Both objects, this
and other
must be
treated read-only. Changing them may lead to unexpected
behaviour which is very hard to track down. To understand why
this is so, consider the two regular expressions ax?
and ay?
bound to actions X
and
Y
respectively. Both expressions match the string
"a"
, which will cause a call to
mergeWith
of either one of the actions. If merging
would integrate, say, Y into X and return
X, the changed X would also be run
for the
match "ax"
which most likely was not intended.
To get a simple priority based selection of one of the actions,
consider extending AbstractFaAction
.
Note: For two competing actions it is completely random
which of the mergeWith()
methods is
called. Consequently, implementations of this method should be as
symmetric as possible.
mergeWith
in interface FaAction
mergeWith
in class AbstractFaAction
_other
- will never be null
.public void invoke(java.lang.StringBuilder yytext, int start, DfaRun r) throws CallbackException
FaAction
.CallbackException
public java.lang.String toString()
toString
in class java.lang.Object