private void NullCheck(IGrammarSymbolSet symbols) { if (NullInst != null && NullInst.symbols == null) { NullInst.symbols = symbols; } }
public GrammarVocabulary(IGrammarSymbolSet symbols, IGrammarSymbol[] elements) : this() { if (symbols == null) { throw new ArgumentNullException("symbols"); } if (elements == null) { throw new ArgumentNullException("elements"); } NullCheck(symbols); int[] indices = new int[elements.Length]; for (int i = 0; i < elements.Length; i++) { indices[i] = symbols.IndexOf(elements[i]); } int maxIndex = indices.Length == 0 ? 0 : indices.Max(); BitArray values = new BitArray(maxIndex + 1); for (int i = 0; i < elements.Length; i++) { values[indices[i]] = true; } this.symbols = symbols; base.Set(values.ObtainFiniteSeries(symbols.Count), 0, (uint)values.Length); }
/// <summary> /// Creates a new <see cref="GrammarAmbiguousSymbol"/> with the <paramref name="symbols"/>, <paramref name="set"/>, <paramref name="ambiguityNumber"/> and <paramref name="ambiguityCount"/> provided. /// </summary> /// <param name="symbols">The <see cref="IGrammarSymbolSet"/> which denotes the grammar's full set of symbols.</param> /// <param name="set">The <see cref="IEnumerable{T}"/> which denotes the <see cref="IGrammarTokenSymbol"/> set represented by the <see cref="GrammarAmbiguousSymbol"/>.</param> /// <param name="ambiguityNumber">The <see cref="Int32"/> value denoting the ordinal number of the ambiguity relative to the full set of ambiguities.</param> /// <param name="ambiguityCount">The <see cref="Int32"/> value denoting the total number of ambiguities within the current grammar.</param> public GrammarAmbiguousSymbol(IGrammarSymbolSet symbols, IEnumerable <IGrammarTokenSymbol> set, int ambiguityNumber, int ambiguityCount, RegularLanguageDFAState[] edgeStates) : base(set.ToList()) { this._symbols = symbols; this.AmbiguityCount = ambiguityCount; this.AmbiguityNumber = ambiguityNumber; this._edgeStates = edgeStates; }
/// <summary> /// Creates a new <see cref="GrammarVocabulary"/> with the /// <paramref name="symbols"/> provided. /// </summary> /// <param name="symbols">The <see cref="IGrammarSymbolSet"/> /// associated to the active language.</param> public GrammarVocabulary(IGrammarSymbolSet symbols) : this() { this.symbols = symbols; if (symbols != null) { NullCheck(symbols); } }
public GrammarVocabulary DisambiguateVocabulary() { var symbols = this.GetSymbols(); var ambiguousSymbolSet = symbols.Where(k => k is IGrammarAmbiguousSymbol).ToArray(); symbols = symbols.Except(ambiguousSymbolSet).ToArray(); var demystifiedAmbiguousSymbols = (from ambig in ambiguousSymbolSet.Cast <IGrammarAmbiguousSymbol>() from symbol in ambig.AmbiguityKey.GetSymbols() select symbol).ToArray(); symbols = symbols.Concat(demystifiedAmbiguousSymbols).Distinct().ToArray(); return(new GrammarVocabulary(this.symbols, symbols)); }
public static SyntacticalNFAState BuildNFA(this IProductionRuleItem item, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements) { SyntacticalNFAState result = null; if (item is IProductionRuleGroupItem) { result = ((IProductionRuleSeries)item).BuildNFA(symbols, lookup, replacements); } else if (item is ILiteralCharReferenceProductionRuleItem) { result = ((ILiteralCharReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements); } else if (item is ILiteralStringReferenceProductionRuleItem) { result = ((ILiteralStringReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements); } else if (item is ITokenReferenceProductionRuleItem) { result = ((ITokenReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements); } else if (item is IRuleReferenceProductionRuleItem) { result = ((IRuleReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements); } else { throw new ArgumentException("series"); } if (result == null) { return(null); } var source = replacements.ContainsKey(item) ? (IProductionRuleSource)replacements[item] : (IProductionRuleSource)item; result.HandleRepeatCycle <GrammarVocabulary, SyntacticalNFAState, SyntacticalDFAState, IProductionRuleSource, SyntacticalNFARootState, IProductionRuleItem>(item, item, OilexerGrammarInliningCore.ProductionRuleRootStateClonerCache, () => new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols)); List <SyntacticalNFAState> flatForm = new List <SyntacticalNFAState>(); SyntacticalNFAState.FlatlineState(result, flatForm); result.SetInitial(source); foreach (var fState in flatForm) { fState.SetIntermediate(source); } foreach (var edge in result.ObtainEdges()) { edge.SetFinal(source); } return(result); }
/// <summary> /// Creates a new <see cref="GrammarVocabulary"/> with the /// <paramref name="symbols"/> provided. /// </summary> /// <param name="symbols">The <see cref="IGrammarSymbolSet"/> /// associated to the active language.</param> public GrammarVocabulary(IGrammarSymbolSet symbols, IGrammarSymbol element) : this() { if (symbols == null) { throw new ArgumentNullException("symbols"); } if (element == null) { throw new ArgumentNullException("element"); } NullCheck(symbols); int index = symbols.IndexOf(element); BitArray values = new BitArray(1); values[0] = true; this.symbols = symbols; base.Set(values.ObtainFiniteSeries(symbols.Count), (uint)index, 1U); }
public static SyntacticalNFAState BuildNFA(this IProductionRule rule, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements) { SyntacticalNFAState state = null; foreach (var item in rule) { if (state == null) { state = item.BuildNFA(symbols, lookup, replacements); } else { var nextState = item.BuildNFA(symbols, lookup, replacements); if (nextState != null) { state.Concat(nextState); } } } return(state); }
public static IEnumerable <ITokenSource> FilterSourceTokenItemsInternal(this IEnumerable <IInlinedTokenItem> tokenItems, IGrammarSymbolSet symbols) { var subSymbols = symbols.Where(k => k is IGrammarTokenSymbol).Cast <IGrammarTokenSymbol>().ToArray(); var grouped = (from t in tokenItems group t by t.Root).ToDictionary(k => k.Key, v => { var relevantSubset = subSymbols.Where(k => k.Source == v.Key); var firstElement = relevantSubset.FirstOrDefault(); if (firstElement == null) { return(new ITokenSource[0]); } if (firstElement is IGrammarVariableSymbol || firstElement is IGrammarConstantEntrySymbol) { return (IEnumerable <ITokenSource>) new ITokenSource[1] { firstElement.Source } } ; else if (firstElement is IGrammarConstantItemSymbol) { var furtherRelevantSubset = relevantSubset.Where(k => k is IGrammarConstantItemSymbol).Cast <IGrammarConstantItemSymbol>().Where(k => Enumerable.Contains <ITokenSource>(v, k.SourceItem)); return((IEnumerable <ITokenSource>)furtherRelevantSubset.Select(k => k.SourceItem).Cast <ITokenSource>().ToArray()); } return((IEnumerable <ITokenSource>)(new ITokenSource[0])); }); if (grouped.Count == 0) { return(new ITokenSource[0]); } return(grouped.Values.Aggregate((a, b) => a.Concat(b))); }
public static IEnumerable <ITokenSource> FilterSourceTokenItems(this IEnumerable <IInlinedTokenItem> tokenItems, IGrammarSymbolSet symbols) { return(from item in FilterSourceTokenItemsInternal(tokenItems, symbols) orderby item is IOilexerGrammarTokenEntry ? ((IOilexerGrammarTokenEntry)(item)).Name : ((ITokenItem)(item)).Name select item); }
public static SyntacticalNFAState BuildNFA(this IRuleReferenceProductionRuleItem ruleReference, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements) { SyntacticalNFAState state = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols); GrammarVocabulary movement = new GrammarVocabulary(symbols, symbols[ruleReference.Reference]); var stateEnd = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols); state.MoveTo(movement, stateEnd); return(state); }
public static SyntacticalNFAState BuildNFA(this ITokenReferenceProductionRuleItem tokenReference, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements) { SyntacticalNFAState state = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols); var symbol = symbols[tokenReference.Reference]; GrammarVocabulary movement = null; if (symbol is IGrammarConstantItemSymbol) { IGrammarConstantItemSymbol constantItemSymbol = (IGrammarConstantItemSymbol)symbol; movement = new GrammarVocabulary(symbols, (from s in symbols let cis = s as IGrammarConstantItemSymbol where cis != null && cis.Source == constantItemSymbol.Source select cis).ToArray()); } else { movement = new GrammarVocabulary(symbols, symbol); } var stateEnd = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols); state.MoveTo(movement, stateEnd); return(state); }
public static SyntacticalNFAState BuildNFA <T, TLiteral>(this ILiteralReferenceProductionRuleItem <T, TLiteral> item, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements) where TLiteral : ILiteralTokenItem <T> { SyntacticalNFAState state = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols); GrammarVocabulary movement = new GrammarVocabulary(symbols, symbols[item.Literal]); var stateEnd = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols); state.MoveTo(movement, stateEnd); return(state); }
public static SyntacticalNFAState BuildNFA(this IProductionRuleSeries series, SyntacticalNFAState root, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements) { SyntacticalNFAState state = root; bool first = true; foreach (var expression in series) { if (state == null) { state = expression.BuildNFA(symbols, lookup, replacements); first = false; } else { var expressionNFA = expression.BuildNFA(symbols, lookup, replacements); if (first) { bool isEdge = expressionNFA.IsEdge; first = false; state.Union(expressionNFA); if (state.IsEdge && !isEdge) { state.IsEdge = isEdge; } } else { state.Union(expressionNFA); } } } if (!(series is IProductionRuleGroupItem)) { List <SyntacticalNFAState> flatForm = new List <SyntacticalNFAState>(); SyntacticalNFAState.FlatlineState(state, flatForm); var source = (replacements.ContainsKey(series) ? (IProductionRuleSource)replacements[series] : (IProductionRuleSource)series); state.SetInitial(source); foreach (var fState in flatForm) { fState.SetIntermediate(source); } foreach (var edge in state.ObtainEdges()) { edge.SetFinal(source); } } return(state); }
public static SyntacticalNFAState BuildNFA(this IProductionRuleSeries series, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements) { return(series.BuildNFA(null, symbols, lookup, replacements)); }