internal Group(CompiledGrammar owner, int index, string name, GroupAdvanceMode advanceMode, GroupEndingMode endingMode) : base(owner, index) { this.name = name; this.advanceMode = advanceMode; this.endingMode = endingMode; nesting = new GrammarObjectSet <Group>(); }
/// <summary> /// Gets the DFA states which may lead to one of the given (terminal) symbols. /// </summary> /// <param name="filter">The filter predicate, specifying all symbols that shall be requested.</param> public ICollection <DfaState> GetDfaStatesOfSymbols(Predicate <Symbol> filter) { if (filter == null) { throw new ArgumentNullException("filter"); } int symbolsFound = 0; GrammarObjectSet <Symbol> acceptSymbols = new GrammarObjectSet <Symbol>(); for (int i = 0; i < SymbolCount; i++) { Symbol symbol = GetSymbol(i); if (filter(symbol)) { acceptSymbols[symbol] = true; symbolsFound++; } } if (symbolsFound != 0) { Queue <DfaState> stateQueue = new Queue <DfaState>(); for (int i = 0; i < DfaStateCount; i++) { DfaState state = GetDfaState(i); if (acceptSymbols[state.AcceptSymbol]) { stateQueue.Enqueue(state); } } Debug.Assert(stateQueue.Count >= symbolsFound); Dictionary <DfaState, int> allowedStates = new Dictionary <DfaState, int>(); do { DfaState state = stateQueue.Dequeue(); int count; if (!allowedStates.TryGetValue(state, out count)) { foreach (DfaState originState in state.GetOriginStates()) { stateQueue.Enqueue(originState); } } allowedStates[state] = count + 1; } while (stateQueue.Count > 0); return(allowedStates.Keys); } return(new DfaState[0]); }
internal void Initialize(Symbol container, Symbol start, Symbol end, Group[] nesting) { if (container == null) { throw new ArgumentNullException("container"); } if (start == null) { throw new ArgumentNullException("start"); } if (end == null) { throw new ArgumentNullException("end"); } if (nesting == null) { throw new ArgumentNullException("nesting"); } if (this.container != null) { throw new InvalidOperationException("This group has already been initialized"); } GrammarObjectSet <Symbol> filter = new GrammarObjectSet <Symbol>(); this.container = container; this.start = start; this.end = end; filter.Set(EndSymbol); foreach (Group nested in nesting) { this.nesting.Set(nested); filter.Set(nested.StartSymbol); } if (advanceMode == GroupAdvanceMode.Character) { foreach (DfaState state in Owner.GetDfaStatesOfSymbols(filter.Contains)) { allowedDfaStates.Set(state); } } }