// Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr type LL1Analyzer struct { atn *ATN } func NewLL1Analyzer(atn *ATN) *LL1Analyzer { la := new(LL1Analyzer) la.atn = atn return la } const ( // LL1AnalyzerHitPred is a special value added to the lookahead sets to indicate that we hit // a predicate during analysis if // // seeThruPreds==false LL1AnalyzerHitPred = TokenInvalidType ) // * // Calculates the SLL(1) expected lookahead set for each outgoing transition // of an {@link ATNState}. The returned array has one element for each // outgoing transition in {@code s}. If the closure from transition // i leads to a semantic predicate before Matching a symbol, the // element at index i of the result will be {@code nil}. // // @param s the ATN state // @return the expected symbols for each outgoing transition of {@code s}. func (la *LL1Analyzer) getDecisionLookahead(s ATNState) []*IntervalSet { if s == nil { return nil } count := len(s.GetTransitions()) look := make([]*IntervalSet, count) for alt := 0; alt < count; alt++ { look[alt] = NewIntervalSet() lookBusy := NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfEqInst, ClosureBusyCollection, "LL1Analyzer.getDecisionLookahead for lookBusy") la.look1(s.GetTransitions()[alt].getTarget(), nil, BasePredictionContextEMPTY, look[alt], lookBusy, NewBitSet(), false, false) // Wipe out lookahead for la alternative if we found nothing, // or we had a predicate when we !seeThruPreds if look[alt].length() == 0 || look[alt].contains(LL1AnalyzerHitPred) { look[alt] = nil } } return look } // Look computes the set of tokens that can follow s in the [ATN] in the // specified ctx. // // If ctx is nil and the end of the rule containing // s is reached, [EPSILON] is added to the result set. // // If ctx is not nil and the end of the outermost rule is // reached, [EOF] is added to the result set. // // Parameter s the ATN state, and stopState is the ATN state to stop at. This can be a // [BlockEndState] to detect epsilon paths through a closure. // // Parameter ctx is the complete parser context, or nil if the context // should be ignored // // The func returns the set of tokens that can follow s in the [ATN] in the // specified ctx. func (la *LL1Analyzer) Look(s, stopState ATNState, ctx RuleContext) *IntervalSet { r := NewIntervalSet() var lookContext *PredictionContext if ctx != nil { lookContext = predictionContextFromRuleContext(s.GetATN(), ctx) } la.look1(s, stopState, lookContext, r, NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfEqInst, ClosureBusyCollection, "LL1Analyzer.Look for la.look1()"), NewBitSet(), true, true) return r } //* // Compute set of tokens that can follow {@code s} in the ATN in the // specified {@code ctx}. // //
If {@code ctx} is {@code nil} and {@code stopState} or the end of the // rule containing {@code s} is reached, {@link Token//EPSILON} is added to // the result set. If {@code ctx} is not {@code nil} and {@code addEOF} is // {@code true} and {@code stopState} or the end of the outermost rule is // reached, {@link Token//EOF} is added to the result set.
// // @param s the ATN state. // @param stopState the ATN state to stop at. This can be a // {@link BlockEndState} to detect epsilon paths through a closure. // @param ctx The outer context, or {@code nil} if the outer context should // not be used. // @param look The result lookahead set. // @param lookBusy A set used for preventing epsilon closures in the ATN // from causing a stack overflow. Outside code should pass // {@code NewSet