Back

Match manager class

The match manager implement two method ( IMatchMgr ) to confirm or cancel reduction, and to do reduce action. If the implementation throw ParserException then the parsing is aborted, if throw a runtime exception the error is stored and no more match manager invokation will be done when resuming parsing.

All the match manager must extends AbstractMatchMgr.

The adapter class AbstractMatchMgrAdapter always confirm reduction, only reduction action is to define.

If the implementation of confirmReduction return false then the reduction is canceled, this is a way to deal with ambigous grammar at semantic level.
The confirmReduction si call only if  needsToConfirmReduction return true.
To force the immediate reduction immediateReduction must return true.

The methods parameters:
parseSession
used to share data between match manager
fatherParseNode
only for reduction action, the father node of parse nodes. Match manager can set data to this parse node according son parse node data and parser session state.
parseNodes
all parse node of right side of the grammar rule to reduce
notWhiteIndexes
supposing "WS" non terminal is white space definition, a rule "A : B C ;" is declared in "%WS;" section, and B is a token, then the reduction action  will be invoked whith parse nodes for { WS, B, C }:
parseNodes[0] is parse node for WS
parseNodes[1] is parse node for B
parseNodes[2] is parse node for C
the not whits indexes are
notWhiteIndexes[0] is 1
notWhiteIndexes[1] is 2
not with indexes give index in parse node according to grammar symbol position in original rules:
B is position 0 in rule, and is 1 in parse node
C is position 1 in rule, and is 2 in parse node

It is possible to parse the whitespace content, informations in white space, if any, are not lost.

Father parse node is the root of AST tree where each parse node are in relation with a basic grammar rule reduction. But it is possible to create specific AST setting parse node data field. For example the implementation below used by parser of parser data generator construct a specific AST.

Parse node data must be acceded using AbstractMatchMgr#getUniqData or AbstractMatchMgr#getDataByAlternative , this to activate the lazy computation of data.

    @Override
    public void reduceAction(final IParseSessionForMatchMgr parseSession,
            final IParseNode fatherParseNode, final IParseNode[] parseNodes,
            final int[] notWhiteIndexes) {
        final RulesDef rulesDef =
            (RulesDef)
getUniqData(parseSession,parseNodes[notWhiteIndexes[0]]);
        final RuleDef ruleDef =
            (RuleDef)
getUniqData(parseSession,parseNodes[notWhiteIndexes[1]]);
        final RulesDef resultRulesDef = new RulesDef(rulesDef, ruleDef);
        fatherParseNode.setData(resultRulesDef);
    }



© 2008-2009, parser4j