Ejemplo n.º 1
0
 /// <summary>
 /// Devuelve un valor entero que representa la prioridad de un token determinado.
 /// </summary>
 /// <param name="token"></param>
 /// <returns></returns>
 private static int GetTokenPriority(Token token)
 {
     if (token == null) return 10;
      switch (token.Type)
      {
     case TokenType.Asterisk:
     case TokenType.Slash:
     case TokenType.Percent:
        return 4;
     case TokenType.Plus:
     case TokenType.Minus:
        return 3;
     case TokenType.Equal:
     case TokenType.NotEqual:
     case TokenType.Greater:
     case TokenType.GreaterEqual:
     case TokenType.Less:
     case TokenType.LessEqual:
        return 2;
     case TokenType.Ampersand:
     case TokenType.Caret:
        return 1;
     case TokenType.Invalid:
        return -1;
     default:
        return 0;
      }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Se mueve siempre que pueda al proximo token.
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual void NextToken()
        {
            Token next = null;
            if (HasMoreTokens())
            {
                char c = Read();
                switch (c)
                {
                    case '+':
                        next = new Token(TokenType.Plus, "+");
                        break;

                    case '-':
                        next = new Token(TokenType.Minus, "-");
                        break;

                    case '*':
                        next = new Token(TokenType.Asterisk, "*");
                        break;

                    case '/':
                        next = new Token(TokenType.Slash, "/");
                        break;

                    case '%':
                        next = new Token(TokenType.Percent, "%");
                        break;

                    case '&':
                        if (Peek() != '&')
                            next = new Token(TokenType.Invalid, c.ToString());
                        else
                        {
                            Read();
                            next = new Token(TokenType.Ampersand, "&&");
                        }
                        break;

                    case '|':
                        if (Peek() != '|')
                            next = new Token(TokenType.Invalid, c.ToString());
                        else
                        {
                            Read();
                            next = new Token(TokenType.Caret, "||");
                        }
                        break;

                    case '(':
                        next = new Token(TokenType.OpenedParenthesis, "(");
                        break;

                    case ')':
                        next = new Token(TokenType.ClosedParenthesis, ")");
                        break;

                    case '[':
                        next = new Token(TokenType.OpenedBracket, "[");
                        break;

                    case ']':
                        next = new Token(TokenType.ClosedBracket, "]");
                        break;

                    case '.':
                        next = new Token(TokenType.Dot, ".");
                        break;

                    case ',':
                        next = new Token(TokenType.Comma, ",");
                        break;

                    case '=':
                        if (Peek() == '=')
                        {
                            Read();
                            next = new Token(TokenType.Equal, "==");
                        }
                        else
                            next = new Token(TokenType.Assign, "=");
                        break;

                    case '!':
                        if (Peek() == '=')
                        {
                            Read();
                            next = new Token(TokenType.NotEqual, "!=");
                        }
                        else next = new Token(TokenType.Invalid, "!");
                        break;

                    case '>':
                        if (Peek() == '=')
                        {
                            Read();
                            next = new Token(TokenType.GreaterEqual, ">=");
                        }
                        else
                            next = new Token(TokenType.Greater, ">");
                        break;

                    case '<':
                        if (Peek() == '=')
                        {
                            Read();
                            next = new Token(TokenType.LessEqual, "<=");
                        }
                        else
                            next = new Token(TokenType.Less, "<");
                        break;

                    default:
                        if (char.IsLetter(c))
                        {
                            string nextValue = ReadWord(c);
                            if (nextValue == "true" || nextValue == "True")
                                next = new Token(TokenType.True, nextValue.ToLowerInvariant());
                            else if (nextValue == "false" || nextValue == "False")
                                next = new Token(TokenType.False, nextValue.ToLowerInvariant());
                            else
                                next = new Token(TokenType.Name, nextValue);
                        }
                        else if (char.IsDigit(c))
                            next = new Token(TokenType.Constant, ReadNumber(c));
                        break;
                }
                if (next == null)
                    next = new Token(TokenType.Invalid, c.ToString());
            }
            current = next;
        }