private static ConditionTokenType[] BuildCharIndexToTokenType() { CharToTokenType[] charToTokenType = { new CharToTokenType('(', ConditionTokenType.LeftParen), new CharToTokenType(')', ConditionTokenType.RightParen), new CharToTokenType('.', ConditionTokenType.Dot), new CharToTokenType(',', ConditionTokenType.Comma), new CharToTokenType('!', ConditionTokenType.Not), new CharToTokenType('-', ConditionTokenType.Minus), }; var result = new ConditionTokenType[128]; for (int i = 0; i < 128; ++i) { result[i] = ConditionTokenType.Invalid; } foreach (CharToTokenType cht in charToTokenType) { // Console.WriteLine("Setting up {0} to {1}", cht.ch, cht.tokenType); result[(int)cht.Character] = cht.TokenType; } return(result); }
private void ParseChar() { char[] separators = { '[', '#', '^', '*', '+', '@' }; var c = _full[_position]; ++_position; if (_currentlyParsing == ConditionTokenType.ValueCondition) { if (c == ']') { HandleBuffer(); return; // don't put into buffer } } else { if (separators.Contains(c)) { var bufferWasEmpty = _buffer == ""; // handle buffer HandleBuffer(); // start parsing something new if (c == '@') { if (bufferWasEmpty) // attribute name { _currentlyParsing = ConditionTokenType.AttributeName; } else // node@111 { _currentlyParsing = ConditionTokenType.NodeNumber; } } else if (c == '[') { _currentlyParsing = ConditionTokenType.ValueCondition; } else if (c == '#') { _currentlyParsing = ConditionTokenType.NodeIndex; } else if (c == '^') { _currentlyParsing = ConditionTokenType.Type; } else if (c == '*' || c == '+') { _conditions.Add(new ArbitraryNodeHierarchyCondition(c == '+')); } return; // don't put into buffer } } _buffer += c; }
public void InitTokenizer(string s) { _inputString = s; _position = 0; _tokenType = ConditionTokenType.BOF; GetNextToken(); }
/// <summary> /// Asserts current token type and advances to the next token. /// </summary> /// <param name="tokenType">Expected token type.</param> /// <remarks>If token type doesn't match, an exception is thrown.</remarks> public void Expect(ConditionTokenType tokenType) { if (this.TokenType != tokenType) { throw new ConditionParseException("Expected token of type: " + tokenType + ", got " + this.TokenType + " (" + this.TokenValue + ")."); } this.GetNextToken(); }
/// <summary> /// Asserts current token type and advances to the next token. /// </summary> /// <param name="tokenType">Expected token type.</param> /// <remarks>If token type doesn't match, an exception is thrown.</remarks> public void Expect(ConditionTokenType tokenType) { if (TokenType != tokenType) { throw new ConditionParseException($"Expected token of type: {tokenType}, got {TokenType} ({TokenValue})."); } GetNextToken(); }
private void HandleBuffer() { var c = GetConditionFromBuffer(); if (c != null) { _conditions.Add(c); } // reset state _currentlyParsing = ConditionTokenType.NodeName; _buffer = ""; }
/// <summary> /// Initializes a new instance of the CharToTokenType struct. /// </summary> /// <param name="character">The character.</param> /// <param name="tokenType">Type of the token.</param> public CharToTokenType(char character, ConditionTokenType tokenType) { Character = character; TokenType = tokenType; }
internal ConditionToken(ConditionTokenType type, string value) { _type = type; _value = value; }
public CharToTokenType(char ch, ConditionTokenType tokenType) { this.ch = ch; this.tokenType = tokenType; }
public void Expect(ConditionTokenType type) { if (_tokenType != type) throw new ConditionParseException("Expected token of type: " + type + ", got " + _tokenType + " (" + _tokenValue + ")."); GetNextToken(); }
/// <summary> /// Gets the next token and sets <see cref="TokenType"/> and <see cref="TokenValue"/> properties. /// </summary> public void GetNextToken() { if (this.TokenType == ConditionTokenType.EndOfInput) { throw new ConditionParseException("Cannot read past end of stream."); } this.SkipWhitespace(); this.TokenPosition = this.position; int i = this.PeekChar(); if (i == -1) { this.TokenType = ConditionTokenType.EndOfInput; return; } char ch = (char)i; if (Char.IsDigit(ch)) { this.ParseNumber(ch); return; } if (ch == '\'') { this.ParseSingleQuotedString(ch); return; } if (ch == '_' || Char.IsLetter(ch)) { this.ParseKeyword(ch); return; } this.ReadChar(); this.TokenValue = ch.ToString(); int nextChar = this.PeekChar(); if (ch == '<' && nextChar == '>') { this.TokenType = ConditionTokenType.NotEqual; this.TokenValue = "<>"; this.ReadChar(); return; } if (ch == '!' && nextChar == '=') { this.TokenType = ConditionTokenType.NotEqual; this.TokenValue = "!="; this.ReadChar(); return; } if (ch == '&' && nextChar == '&') { this.TokenType = ConditionTokenType.And; this.TokenValue = "&&"; this.ReadChar(); return; } if (ch == '|' && nextChar == '|') { this.TokenType = ConditionTokenType.Or; this.TokenValue = "||"; this.ReadChar(); return; } if (ch == '<' && nextChar == '=') { this.TokenType = ConditionTokenType.LessThanOrEqualTo; this.TokenValue = "<="; this.ReadChar(); return; } if (ch == '>' && nextChar == '=') { this.TokenType = ConditionTokenType.GreaterThanOrEqualTo; this.TokenValue = ">="; this.ReadChar(); return; } if (ch == '=' && nextChar == '=') { this.TokenType = ConditionTokenType.EqualTo; this.TokenValue = "=="; this.ReadChar(); return; } if (ch >= 32 && ch < 128) { ConditionTokenType tt = charIndexToTokenType[ch]; if (tt != ConditionTokenType.Invalid) { this.TokenType = tt; this.TokenValue = new string(ch, 1); return; } throw new ConditionParseException("Invalid punctuation: " + ch); } throw new ConditionParseException("Invalid token: " + ch); }
public void GetNextToken() { if (_tokenType == ConditionTokenType.EOF) { throw new Exception("Cannot read past end of stream."); } if (IgnoreWhiteSpace) { SkipWhitespace(); } ; _tokenPosition = _position; int i = PeekChar(); if (i == -1) { TokenType = ConditionTokenType.EOF; return; } char ch = (char)i; if (!IgnoreWhiteSpace && Char.IsWhiteSpace(ch)) { StringBuilder sb = new StringBuilder(); int ch2; while ((ch2 = PeekChar()) != -1) { if (!Char.IsWhiteSpace((char)ch2)) { break; } sb.Append((char)ch2); ReadChar(); } ; TokenType = ConditionTokenType.Whitespace; _tokenValue = sb.ToString(); return; } if (Char.IsDigit(ch)) { TokenType = ConditionTokenType.Number; string s = ""; s += ch; ReadChar(); while ((i = PeekChar()) != -1) { ch = (char)i; if (Char.IsDigit(ch) || (ch == '.')) { s += (char)ReadChar(); } else { break; }; } ; _tokenValue = s; return; } if (ch == '\'') { TokenType = ConditionTokenType.String; StringBuilder sb = new StringBuilder(); sb.Append(ch); ReadChar(); while ((i = PeekChar()) != -1) { ch = (char)i; sb.Append((char)ReadChar()); if (ch == '\'') { if (PeekChar() == (int)'\'') { sb.Append('\''); ReadChar(); } else { break; } } } ; _tokenValue = sb.ToString(); return; } if (ch == '_' || Char.IsLetter(ch)) { TokenType = ConditionTokenType.Keyword; StringBuilder sb = new StringBuilder(); sb.Append((char)ch); ReadChar(); while ((i = PeekChar()) != -1) { if ((char)i == '_' || (char)i == '-' || Char.IsLetterOrDigit((char)i)) { sb.Append((char)ReadChar()); } else { break; }; } ; _tokenValue = sb.ToString(); _tokenValueLowercase = _tokenValue.ToLower(); return; } ReadChar(); _tokenValue = ch.ToString(); if (ch == '<' && PeekChar() == (int)'>') { TokenType = ConditionTokenType.NE; _tokenValue = "<>"; ReadChar(); return; } if (ch == '!' && PeekChar() == (int)'=') { TokenType = ConditionTokenType.NE; _tokenValue = "!="; ReadChar(); return; } if (ch == '&' && PeekChar() == (int)'&') { TokenType = ConditionTokenType.And; _tokenValue = "&&"; ReadChar(); return; } if (ch == '|' && PeekChar() == (int)'|') { TokenType = ConditionTokenType.Or; _tokenValue = "||"; ReadChar(); return; } if (ch == '<' && PeekChar() == (int)'=') { TokenType = ConditionTokenType.LE; _tokenValue = "<="; ReadChar(); return; } if (ch == '>' && PeekChar() == (int)'=') { TokenType = ConditionTokenType.GE; _tokenValue = ">="; ReadChar(); return; } if (ch == '=' && PeekChar() == (int)'=') { TokenType = ConditionTokenType.EQ; _tokenValue = "=="; ReadChar(); return; } if (ch >= 32 && ch < 128) { ConditionTokenType tt = charIndexToTokenType[ch]; if (tt != ConditionTokenType.Invalid) { TokenType = tt; _tokenValue = new String(ch, 1); return; } else { throw new Exception("Invalid punctuation: " + ch); } } throw new Exception("Invalid token: " + ch); }
public bool IsToken(ConditionTokenType token) { return(_tokenType == token); }
/// <summary> /// Gets or sets a value indicating whether the specified token is of specified type. /// </summary> /// <param name="tokenType">The token type.</param> /// <returns> /// A value of <c>true</c> if current token is of specified type; otherwise, <c>false</c>. /// </returns> public bool IsToken(ConditionTokenType tokenType) { return(this.TokenType == tokenType); }
/// <summary> /// Gets or sets a value indicating whether the specified token is of specified type. /// </summary> /// <param name="tokenType">The token type.</param> /// <returns> /// A value of <c>true</c> if current token is of specified type; otherwise, <c>false</c>. /// </returns> public bool IsToken(ConditionTokenType tokenType) { return this.TokenType == tokenType; }
/// <summary> /// Gets the next token and sets <see cref="TokenType"/> and <see cref="TokenValue"/> properties. /// </summary> public void GetNextToken() { if (this.TokenType == ConditionTokenType.EndOfInput) { throw new ConditionParseException("Cannot read past end of stream."); } this.SkipWhitespace(); this.TokenPosition = this.TokenPosition; int i = this.PeekChar(); if (i == -1) { this.TokenType = ConditionTokenType.EndOfInput; return; } char ch = (char)i; if (char.IsDigit(ch)) { this.ParseNumber(ch); return; } if (ch == '\'') { this.ParseSingleQuotedString(ch); return; } if (ch == '_' || char.IsLetter(ch)) { this.ParseKeyword(ch); return; } if (ch == '}' || ch == ':') { // when condition is embedded this.TokenType = ConditionTokenType.EndOfInput; return; } this.TokenValue = ch.ToString(); var success = TryGetComparisonToken(ch); if (success) { return; } success = TryGetLogicalToken(ch); if (success) { return; } if (ch >= 32 && ch < 128) { ConditionTokenType tt = charIndexToTokenType[ch]; if (tt != ConditionTokenType.Invalid) { this.TokenType = tt; this.TokenValue = new string(ch, 1); this.ReadChar(); return; } throw new ConditionParseException("Invalid punctuation: " + ch); } throw new ConditionParseException("Invalid token: " + ch); }
private static ConditionTokenType[] BuildCharIndexToTokenType() { CharToTokenType[] charToTokenType = { new CharToTokenType('(', ConditionTokenType.LeftParen), new CharToTokenType(')', ConditionTokenType.RightParen), new CharToTokenType('.', ConditionTokenType.Dot), new CharToTokenType(',', ConditionTokenType.Comma), new CharToTokenType('!', ConditionTokenType.Not), new CharToTokenType('-', ConditionTokenType.Minus), }; var result = new ConditionTokenType[128]; for (int i = 0; i < 128; ++i) { result[i] = ConditionTokenType.Invalid; } foreach (CharToTokenType cht in charToTokenType) { // Console.WriteLine("Setting up {0} to {1}", cht.ch, cht.tokenType); result[(int)cht.Character] = cht.TokenType; } return result; }
/// <summary> /// Initializes a new instance of the CharToTokenType struct. /// </summary> /// <param name="character">The character.</param> /// <param name="tokenType">Type of the token.</param> public CharToTokenType(char character, ConditionTokenType tokenType) { this.Character = character; this.TokenType = tokenType; }
public bool IsToken(ConditionTokenType token) { return _tokenType == token; }