static void processCompoundPattern( const PatternMatchState & state, TransitionList & newTransitions ) { const Node & currentNode = state.getCurrentNode(); if ( state.isComplete() ) { // Сопоставление завершено Match::AttributesMap attributes; state.context.addAttributes( attributes, state.getAlternative().getBindings() ); // Строим набор аттрибутов for ( uint i = 0; i < newTransitions.size(); ++ i ) { Match & match = static_cast<Match&>( *newTransitions[i] ); if ( match.equals( state.getAlternative().getPattern(), state.startNode, currentNode, attributes ) ) { match.addVariant( state.releaseVariant() ); return; // Если сопоставление уже было найдено } } newTransitions.push_back( new Match( state.startNode, currentNode, state.getAlternative().getPattern(), state.releaseVariant(), attributes ) ); // TODO Optimize return; } ChainList chains; if ( const AnnotationMatcher * curMatcher = dynamic_cast<const AnnotationMatcher *>( &state.getCurrentMatcher() ) ) { TransitionList nextTransitions = curMatcher->buildTransitions( currentNode, state.context ); for ( uint i = 0, sz = nextTransitions.size(); i < sz; ++ i ) { PatternMatchState temp_state( state, nextTransitions[ i ], i == sz ); processCompoundPattern( temp_state, newTransitions ); } } else { const AnnotationChainMatcher & chainMatcher = static_cast<const AnnotationChainMatcher &>( state.getCurrentMatcher() ); chains.clear(); chainMatcher.buildChains( state.getCurrentNode(), state.context, chains ); for ( uint i = 0, sz = chains.size(); i < sz; ++ i ) { PatternMatchState temp_state( state, chains[i].first, chains[i].second, i == sz - 1 ); processCompoundPattern( temp_state, newTransitions); } } }
void PatternMatcher::buildTransitions( const text::Transition & transition, const patterns::Pattern & pattern, const patterns::Alternative & alt, const Context & context, TransitionList & results ) const { if ( const AnnotationMatcher * curMatcher = dynamic_cast<const AnnotationMatcher *>( &alt.getMatcher( 0 ) ) ) { if ( !curMatcher->matchTransition( transition, Context() ) ) return; PatternMatchState s0( pattern, alt, transition.start ); PatternMatchState state( s0, const_cast<text::Transition*>( &transition ), true ); processCompoundPattern( state, results ); } else { PatternMatchState s0( pattern, alt, transition.start ); const AnnotationChainMatcher & chainMatcher = dynamic_cast<const AnnotationChainMatcher &>( alt.getMatcher( 0 ) ); ChainList chains; // Список цепочек chainMatcher.buildChains( transition, s0.context, chains ); // Заполняем список цепочек // TODO Optimize: Необходимо отдельно рассмотреть случай пустой цепи for ( uint i = 0, sz = chains.size(); i < sz; ++ i ) { PatternMatchState state( s0, chains[i].first, chains[i].second, i == sz - 1 ); processCompoundPattern( state, results ); } } }