public class SwitchDfa extends AbstractFaAction
implements an FaAction
which changes the Dfa
operated by the calling DfaRun
. This type of action is
useful whenever different parts of the input shall be handled by
different Dfa
s.
In a typical example, the input contains long stretches of
irrelevant text interspersed by parts which need closer
examination. Given that start and end of the relevant parts can
be detected by regular expressions rIn and
rOut, two Dfa
s can be set up like
this:
SwitchDfa toWork = new SwitchDfa(Drop.DROP); SwitchDfa toSkip = new SwitchDfa(Drop.DROP); Dfa skip = new Nfa(rIn, toWork) .or( other stuff ) .compile(DfaRun.UNMATCHED_DROP); Dfa work = new Nfa(rOut, toSkip) .or( other stuff ) .compile(DfaRun.UNMATCHED_COPY); // don't forget to set up the switches. toWork.setDfa(work); toSkip.setDfa(skip); DfaRun r = new DfaRun(skip, ...);
When rIn
is detected, toWork.invoke()
gets called which switches the calling DfaRun
to run
the automaton work
. When later the end of the
relevant part is detected by a match with rOut
, the
a call to toSkip.invoke()
switches back to automaton
skip
. Because of the mutual reference between the
automata and objects of this class, the constructor does not take
a Dfa
as an argument. Rather setDfa()
must be used.
Constructor and Description |
---|
SwitchDfa()
calls the 1 parameter constructor with
action==null . |
SwitchDfa(FaAction action)
|
Modifier and Type | Method and Description |
---|---|
void |
invoke(java.lang.StringBuilder out,
int start,
DfaRun r)
is called by methods of
DfaRun in case of a
match. |
void |
setDfa(Dfa dfa)
sets the
Dfa this action switches the calling
DfaRun to. |
mergeWith, setPriority
public SwitchDfa(FaAction action)
creates an FaAction
to switch the calling
DfaRun
object to another Dfa
. The
Dfa
must be set later with setDfa()
. The parameters influence the operation of the invoke()
callback as follows:
Dfa
, which you have to specify by
calling setDfa(monq.jfa.Dfa)
, will be entered into the calling
DfaRun
.action
is not
null
, the action will then be invoked. Without action,
the triggering text will show up in the
output. To drop it, set action
to Copy.COPY
.public SwitchDfa()
calls the 1 parameter constructor with
action==null
.
public void setDfa(Dfa dfa)
sets the Dfa
this action switches the calling
DfaRun
to. This function must be called
once before the action is used.
public void invoke(java.lang.StringBuilder out, int start, DfaRun r) throws CallbackException
FaAction
is called by methods of DfaRun
in case of a
match. Parameter yytext
contains the matching text
from position start
onwards. The callback may change
the whole of yytext
, but the under most
circumstances, only the matching text should be
changed. Parameter runner
is the DfaRun
object which called this function. Of interest are its fields
DfaRun.clientData
and DfaRun.collect
.
CallbackException