public Token(int linenumber, int positioninline, String text, Config.Types description, int level, Token partner) { Linenumber = linenumber; PositionInLine = positioninline; Text = text; Description = description; Level = level; Partner = partner; }
internal TokenStringValue(Token[] parts, DocumentRange range) : base(range) { this.m_parts = parts; StringBuilder stringBuilder = new StringBuilder(); Token[] tokenArray = parts; for (int i = 0; i < (int)tokenArray.Length; i++) { Token token = tokenArray[i]; if (token.Type == TokenType.StringPart) { stringBuilder.Append(((TokenStringPart)token).StringValue); } } this.m_string = stringBuilder.ToString(); }
public static List<Token> tokenize(char[] input, Dictionary<char, double> variables) { List<Token> tokens = new List<Token>(); Token curToken = new Token(TokenType.Starting, ""); foreach (char c in input) { if (c == ' ') continue; bool error = false; switch (curToken.tokenType) { case TokenType.Starting: curToken.value += c; if (startingAllowed.Contains(c)) { curToken.tokenType = TokenType.LeftVariable; tokens.Add(curToken); curToken = new Token(TokenType.LeftVariable, ""); } else { error = true; } break; case TokenType.LeftVariable: curToken.value += c; if (c == '=') { curToken.tokenType = TokenType.LeftAssignment; tokens.Add(curToken); curToken = new Token(TokenType.LeftAssignment, ""); } else { error = true; } break; case TokenType.LeftAssignment: if (c == '-') { //tokens.Add(curToken); curToken = new Token(TokenType.Negating, c.ToString()); } else if (digits.Contains(c)) { curToken.value += c; curToken.tokenType = TokenType.Digit; } else if (variables.ContainsKey(c)) { curToken = new Token(TokenType.Variable, "" + c); } else if (c == '"') { curToken = new Token(TokenType.String, "" + c); } else { error = true; } break; case TokenType.Digit: if (digits.Contains(c)) { curToken.value += c; } else if (operators.Contains(c)) { tokens.Add(curToken); curToken = new Token(); curToken.value = c.ToString(); curToken.tokenType = TokenType.Operation; } else if (variables.ContainsKey(c)) { tokens.Add(curToken); tokens.Add(new Token(TokenType.Operation, "*")); curToken = new Token(TokenType.Variable, c.ToString()); } else { error = true; } break; case TokenType.Negating: if (digits.Contains(c)) { tokens.Add(curToken); curToken = new Token(); curToken.value = c.ToString(); curToken.tokenType = TokenType.Digit; } else if (variables.ContainsKey(c)) { tokens.Add(curToken); curToken = new Token(); curToken.value = c.ToString(); curToken.tokenType = TokenType.Variable; } break; case TokenType.Operation: if (c == '-') { tokens.Add(curToken); curToken = new Token(TokenType.Negating, c.ToString()); } else if (digits.Contains(c)) { tokens.Add(curToken); curToken = new Token(); curToken.value = c.ToString(); curToken.tokenType = TokenType.Digit; } else if (variables.ContainsKey(c)) { tokens.Add(curToken); curToken = new Token(); curToken.value = c.ToString(); curToken.tokenType = TokenType.Variable; } else { error = true; } break; case TokenType.Variable: if (variables.ContainsKey(c)) { tokens.Add(curToken); tokens.Add(new Token(TokenType.Operation, "*")); curToken = new Token(TokenType.Variable, "" + c); } else if (operators.Contains(c)) { tokens.Add(curToken); curToken = new Token(TokenType.Operation, "" + c); } else if (digits.Contains(c)) { tokens.Add(curToken); tokens.Add(new Token(TokenType.Operation, "*")); curToken = new Token(TokenType.Digit, "" + c); } else { error = true; } break; case TokenType.String: curToken.value += c; break; } if (error) { tokens.Add(new Token(TokenType.Error, curToken.value)); return tokens; } } if (curToken.tokenType != TokenType.Digit && curToken.tokenType != TokenType.Variable) { curToken.tokenType = TokenType.Error; } tokens.Add(curToken); return tokens; }
public void CheckToken(char lastChar) { Token t = new Token(); bool IsValid = false; bool isCharValid = false; if (_config.IsQuoteChar(lastChar)) { HandleQuote(t, lastChar); isCharValid = true; } else { double n; isCharValid = (_config.IsValidChar(lastChar) || _config.IsStatementOperatorChar(lastChar) || double.TryParse(lastChar.ToString(), out n)); if (IsNumber(lastChar)) { IsValid = true; _lastType = Config.Types.Number; } else if (IsValidChar(lastChar)) { IsValid = true; } else if (IsStatementOperatorChar(lastChar)) { IsValid = true; } if (!IsValid) if (_lastPartIsValid) { Console.WriteLine("Last part is valid"); // Create token SetTokenValues(t); if (t.Text == "if") _pManager.IfElse.Push(t); //Console.WriteLine("Push token"); else if (t.Text == "else") _pManager.IfElse.Pop().Partner = t; //Console.WriteLine("Pop token"); TokenList.AddLast(t); // Reset values ResetTokenValues(); if (isCharValid) CheckToken(lastChar); } bool addToken = false; Token to = new Token(); if(_config.IsLineEndChar(lastChar)) { Console.WriteLine("Line end found"); //HandleBracketChar(lastChar); _lastType = Config.Types.LineEnd; addToken = true; } else if (_config.IsBracketChar(lastChar)) { // Level ophogen _lastType = Config.Types.Bracket; switch(lastChar) { case '(': case '{': case '[': // open bracket _pManager.Bracket.Push(to); _level++; break; case ')': case '}': case ']': // close bracket Token tmp = _pManager.Bracket.Pop(); tmp.Partner = to; _level--; break; } addToken = true; } else if (_config.IsMathOperatorChar(lastChar)) { _lastType = Config.Types.MathOperator; addToken = true; } if (addToken) { AddToken(lastChar, to); isCharValid = true; } } if (!isCharValid && !_isQuoteActive && !_config.IsWhiteSpaceChar(lastChar)) { // Invalid char error Console.WriteLine("Invalid character found"); _lastType = Config.Types.Error; AddToken(lastChar, t); } }
private void SetTokenValues(Token t) { t.Linenumber = _lineNr; t.PositionInLine = _positionNr; t.Text = _lastValidatePart; t.Level = _level; t.Description = _lastType; }
private void HandleQuote(Token t, char c) { _validatePart += c; _isQuoteActive = !_isQuoteActive; if (!_isQuoteActive) { _lastType = Config.Types.String; _lastValidatePart = _validatePart; SetTokenValues(t); ResetTokenValues(); TokenList.AddLast(t); } }
private void AddToken(char c, Token t) { _lastValidatePart = c.ToString(); SetTokenValues(t); ResetTokenValues(); TokenList.AddLast(t); }