internal Reduction(Rule rule, IList<Token> tokens): base() { if (rule == null) { throw new ArgumentNullException("rule"); } if (tokens == null) { throw new ArgumentNullException("tokens"); } this.rule = rule; Token[] tokenArray = new Token[tokens.Count]; tokens.CopyTo(tokenArray, 0); this.tokens = Array.AsReadOnly(tokenArray); }
/// <summary> /// Gets the grammar definition type of the given token /// </summary> /// <returns>The definition type.</returns> /// <param name="token">Token.</param> public static DefinitionType GetDefinitionType(Token token) { DefinitionType symbolType; bool there = symbolTypeMap.TryGetValue(token.Symbol.Name, out symbolType); if (!there) symbolType = DefinitionType.None; return symbolType; }
static CompilerMessage toErrorMessage(string message, Token token) { return new CompilerMessage (MessageSeverity.Error, message, string.Empty, token.Position.Line, token.Position.Column); }
/// <summary> /// Initializes a new instance of the <see cref="TokenXmlReader"/> class. /// </summary> /// <param name="nametable">The nametable (optional, may be <c>null</c>) to be used.</param> /// <param name="root">The root (if <c>null</c>, no nodes are returned from the reader).</param> public TokenXmlReader(XmlNameTable nametable, Token root) { this.nametable = nametable ?? new NameTable(); terminalNs = this.nametable.Add(TerminalNS); nonterminalNs = this.nametable.Add(NonterminalNS); tPrefix = this.nametable.Add("t"); nPrefix = this.nametable.Add("n"); lineAttribute = this.nametable.Add("line"); columnAttribute = this.nametable.Add("column"); readState = ReadState.Initial; if ((root != null) && (root.Symbol != null)) { current = root; } }
/// <summary> /// Reads the next node from the stream. /// </summary> /// <returns> /// true if the next node was read successfully; false if there are no more nodes to read. /// </returns> /// <exception cref="T:System.Xml.XmlException">An error occurred while parsing the XML. </exception> public override bool Read() { switch (readState) { case ReadState.Initial: if (current != null) { readState = ReadState.Interactive; return true; } readState = ReadState.EndOfFile; return false; case ReadState.Interactive: MoveToElement(); switch (elementPosition) { case ElementPosition.Start: if (IsTerminal) { elementPosition = ElementPosition.Text; return true; } else { Reduction reduction = current as Reduction; if (reduction != null) { if (reduction.Children.Count > 0) { stack.Push(new KeyValuePair<Token, int>(reduction, 0)); current = reduction.Children[0]; return true; } } } elementPosition = ElementPosition.End; return true; case ElementPosition.Text: elementPosition = ElementPosition.End; return true; case ElementPosition.End: if (stack.Count > 0) { KeyValuePair<Token, int> pair = stack.Pop(); current = pair.Key; Reduction reduction = (Reduction)current; if (pair.Value < (reduction.Children.Count-1)) { current = reduction.Children[pair.Value+1]; stack.Push(new KeyValuePair<Token, int>(pair.Key, pair.Value+1)); elementPosition = ElementPosition.Start; } return true; } readState = ReadState.EndOfFile; break; } break; } return false; }
//converts the given token to a CompletionData object static CompletionData tokenToCompletionData(Token token) { return new CompletionData (token.Text, getIcon(token)); }
static IconId getIcon(Token token) { return getIcon (TokenUtil.GetDefinitionType (token)); }
//only some tokens are allowed to follow a new line static bool isValidLineContinuationToken(Token token) { return token != null && (token.Symbol.Name == "=" || token.Symbol.Name == "::=" || token.Symbol.Name == "|" || token.Symbol.Name == "+" || token.Symbol.Name == "-" || TokenUtil.IsUserDefinitionToken (token)); }
static bool isDefinitionStart(Token token, Token previous) { return TokenUtil.IsNewLine (previous) && (TokenUtil.IsUserDefinitionToken (token) || !isValidLineContinuationToken (token)); }
/// <summary> /// Determines if the specified token is a grammar definition (property, set, terminal, or nonterminal) /// </summary> /// <returns><c>true</c> if is user definition token the specified token; otherwise, <c>false</c>.</returns> /// <param name="token">Token.</param> public static bool IsUserDefinitionToken(Token token) { return token != null && (token.Symbol.Name == "ParameterName" || token.Symbol.Name == "SetName" || (token.Symbol.Name == "Terminal" && !IsTerminalLiteral(token))|| token.Symbol.Name == "Nonterminal"); }
public static bool IsTerminalLiteral(Token token) { return token != null && token.Symbol.Name == "Terminal" && token.Text.StartsWith ("\'", StringComparison.Ordinal); }
/// <summary> /// Determines if the specified token is noise /// </summary> /// <returns><c>true</c> if the token is noise (whitespace, comment, etc); otherwise, <c>false</c>.</returns> /// <param name="token">Token to examine</param> public static bool IsNoise(Token token) { SymbolKind kind = token.Symbol.Kind; return kind != SymbolKind.Error && kind != SymbolKind.Terminal && kind != SymbolKind.End; }
/// <summary> /// Determines if is new line the specified token. /// </summary> /// <returns><c>true</c> if is new line the specified token; otherwise, <c>false</c>.</returns> /// <param name="token">Token.</param> public static bool IsNewLine(Token token) { return token != null && token.NameIs ("Newline"); }
/// <summary> /// True if the given token represents the End Of File /// </summary> /// <returns><c>true</c> if is end the specified token; otherwise, <c>false</c>.</returns> /// <param name="token">Token.</param> public static bool IsEnd(Token token) { return token!=null && (token.Symbol.Kind == SymbolKind.End); }
static TextSegment tokenToSegment(Token token) { //we don't want to have to retype delimiters for sets or nonterminals, // so filter down to just the content string str = trimDelimiters(token.Text); int startIndex = (int)token.Position.Index; //start index must be after the opening delimiter if (str.Length != token.Text.Length) startIndex+=1; return new TextSegment(startIndex, str.Length); }
//checks that the given token is one that could be renamed static bool tokenIsRenameable(Token token) { if (token == null) return false; var symbolType = TokenUtil.GetDefinitionType (token); return (symbolType == DefinitionType.SetName || symbolType == DefinitionType.Terminal || symbolType == DefinitionType.NonTerminal) && !TokenUtil.IsTerminalLiteral (token); //cant rename terminal literal }