internal IGrammarSymbol[] GetSymbols() { IGrammarSymbol[] currentSet = new IGrammarSymbol[this.TrueCount]; uint length = this.IsNegativeSet ? this.FullLength : this.Offset + this.Length; uint offset = this.IsNegativeSet ? 0 : this.Offset; for (uint i = offset, symbolIndex = 0; i < length; i++) { if (this[i]) { currentSet[symbolIndex++] = this.symbols[(int)i]; } } return(currentSet); }
/// <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); }
private IEnumerable <IGrammarSymbol> CreateRHS(IList <Token> rhsTokens) { var result = new LinkedList <IGrammarSymbol>(); var stack = new Stack <KeyValuePair <Group, int> >(); Alternative alternative = null; string annotation = null; Dictionary <string, string> annotationDictionary = new Dictionary <string, string>(); for (int i = 0; i < rhsTokens.Count; i++) { Token token = rhsTokens[i]; switch (token.Kind) { case Token.TokenKind.Annotation: { annotation = token.Value; string[] split = token.Value.Split(new[] { ':' }); if (split.Count() == 2) { annotationDictionary[split[0]] = split[1]; } break; } case Token.TokenKind.Identifier: { string nameToUse = annotation != null ? annotation : token.Value; string typeToUse = token.Value; bool isTypeFromGrammar = false; if (annotationDictionary.ContainsKey("name")) { nameToUse = annotationDictionary["name"]; } if (annotationDictionary.ContainsKey("type")) { typeToUse = annotationDictionary["type"]; isTypeFromGrammar = true; } var nonterminal = new NonTerminal { Name = nameToUse, GrammarType = token.Value, Type = typeToUse, IsTypeFromGrammar = isTypeFromGrammar, Annotations = new Dictionary <string, string> (annotationDictionary) }; annotation = null; annotationDictionary.Clear(); result.AddLast(nonterminal); if (stack.Count > 0) { stack.Peek().Key.Symbols.Add(nonterminal); } break; } case Token.TokenKind.String: { var terminal = new Terminal { Value = token.Value }; result.AddLast(terminal); if (stack.Count > 0) { stack.Peek().Key.Symbols.Add(terminal); } break; } case Token.TokenKind.Lparen: { var group = new Group(); stack.Push(new KeyValuePair <Group, int>(group, i)); annotation = null; annotationDictionary.Clear(); break; } case Token.TokenKind.Rparen: { // done with the group KeyValuePair <Group, int> groupAndMarker = stack.Pop(); Group group = groupAndMarker.Key; int marker = groupAndMarker.Value; Contract.Assert(group.Symbols.Count > 0); for (int j = 0; j < group.Symbols.Count; j++) { result.RemoveLast(); } result.AddLast(group); annotation = null; annotationDictionary.Clear(); break; } case Token.TokenKind.Plus: { // done with the group IGrammarSymbol prev = result.Last.Value; result.RemoveLast(); var plus = new Plus { Symbol = prev }; result.AddLast(plus); if (stack.Count > 0) { stack.Peek().Key.Symbols.Add(plus); } annotation = null; annotationDictionary.Clear(); break; } case Token.TokenKind.Star: { // done with the group IGrammarSymbol prev = result.Last.Value; result.RemoveLast(); var star = new Star { Symbol = prev }; result.AddLast(star); if (stack.Count > 0) { stack.Peek().Key.Symbols.Add(star); } annotation = null; annotationDictionary.Clear(); break; } case Token.TokenKind.Pipe: { if (alternative == null) { string typeToUse = "string"; if (annotationDictionary.ContainsKey("type")) { typeToUse = annotationDictionary["type"]; } alternative = new Alternative { Type = typeToUse }; } var group = new Group { Symbols = result.Where(e => !(e is Alternative)).ToList() }; if (group.Symbols.Count > 0) { alternative.Symbols.Add(group); } result.Clear(); result.AddLast(alternative); Contract.Assert(result.Count == 1); annotation = null; annotationDictionary.Clear(); break; } case Token.TokenKind.Arrow: annotation = null; break; } } if (alternative != null) { var group = new Group { Symbols = result.Where(e => !(e is Alternative)).ToList() }; if (group.Symbols.Count > 0) { alternative.Symbols.Add(group); } result.Clear(); result.AddLast(alternative); } return(result); }