/// <summary> /// /// </summary> /// <param name="stream"></param> /// <returns></returns> /// <remarks> /// /// See http://www.dmtf.org/sites/default/files/standards/documents/DSP0221_3.0.0.pdf /// /// A.13 Names /// /// MOF names are identifiers with the format defined by the IDENTIFIER rule. /// No whitespace is allowed between the elements of the rules in this ABNF section. /// /// IDENTIFIER = firstIdentifierChar *( nextIdentifierChar ) /// firstIdentifierChar = UPPERALPHA / LOWERALPHA / UNDERSCORE /// nextIdentifierChar = firstIdentifierChar / decimalDigit /// elementName = localName / schemaQualifiedName /// localName = IDENTIFIER /// /// </remarks> private static IdentifierToken ReadIdentifierToken(ILexerStream stream) { var sourceChars = new List <SourceChar>(); var nameChars = new List <char>(); // firstIdentifierChar var peek = stream.Peek(); if (!StringValidator.IsFirstIdentifierChar(peek.Value)) { throw new InvalidOperationException( string.Format("Unexpected character '{0}' encountered", peek.Value)); } // *( nextIdentifierChar ) while (!stream.Eof) { peek = stream.Peek(); if (StringValidator.IsNextIdentifierChar(peek.Value)) { var @char = stream.Read(); sourceChars.Add(@char); nameChars.Add(@char.Value); } else { break; } } // return the result var extent = new SourceExtent(sourceChars); var name = new string(nameChars.ToArray()); return(new IdentifierToken(extent, name)); }
public static void IsFirstIdentifierCharTest(char value, bool expectedResult) { var result = StringValidator.IsFirstIdentifierChar(value); Assert.AreEqual(expectedResult, result); }
public Token NextToken() { var stream = this.Stream; var peek = stream.Peek(); switch (peek.Value) { case '$': return(Lexer.ReadAliasIdentifierToken(stream)); case ']': return(Lexer.ReadAttributeCloseToken(stream)); case '[': return(Lexer.ReadAttributeOpenToken(stream)); case '}': return(Lexer.ReadBlockCloseToken(stream)); case '{': return(Lexer.ReadBlockOpenToken(stream)); case ':': return(Lexer.ReadColonToken(stream)); case ',': return(Lexer.ReadCommaToken(stream)); case '/': return(Lexer.ReadCommentToken(stream)); case '=': return(Lexer.ReadEqualsOperatorToken(stream)); case ')': return(Lexer.ReadParenthesesCloseToken(stream)); case '(': return(Lexer.ReadParenthesesOpenToken(stream)); case '#': return(Lexer.ReadPragmaToken(stream)); case ';': return(Lexer.ReadStatementEndToken(stream)); case '"': return(Lexer.ReadStringLiteralToken(stream)); default: if (StringValidator.IsWhitespace(peek.Value)) { return(Lexer.ReadWhitespaceToken(stream)); } else if (StringValidator.IsFirstIdentifierChar(peek.Value)) { var identifier = Lexer.ReadIdentifierToken(stream); if (StringValidator.IsFalse(identifier.Name)) { /// A.17.6 Boolean value /// /// booleanValue = TRUE / FALSE /// /// FALSE = "false" ; keyword: case insensitive /// TRUE = "true" ; keyword: case insensitive return(new BooleanLiteralToken(identifier.Extent, false)); } else if (StringValidator.IsTrue(identifier.Name)) { /// A.17.6 Boolean value /// /// booleanValue = TRUE / FALSE /// /// FALSE = "false" ; keyword: case insensitive /// TRUE = "true" ; keyword: case insensitive return(new BooleanLiteralToken(identifier.Extent, true)); } else if (StringValidator.IsNull(identifier.Name)) { /// A.17.7 Null value /// /// nullValue = NULL /// /// NULL = "null" ; keyword: case insensitive /// ; second return(new NullLiteralToken(identifier.Extent)); } else { return(identifier); } } else if ((peek.Value == '+') || (peek.Value == '-') || (StringValidator.IsDecimalDigit(peek.Value))) { return(Lexer.ReadNumericLiteralToken(stream)); } else { throw new UnexpectedCharacterException(peek); } } }