Exemplo n.º 1
0
        public static StringLiteralToken ParseToken(string exp, ref int pos)
        {
            pos++;
            StringBuilder builder    = new StringBuilder();
            int           posOfAspas = exp.IndexOf('\'', pos);

            if (posOfAspas == -1)
            {
                throw new SyntaxErrorException("Sequência de caracteres não terminada");
            }
            StringLiteralToken result = new StringLiteralToken {
                Value = exp.Substring(pos, posOfAspas - pos)
            };

            pos = posOfAspas + 1;
            return(result);
        }
Exemplo n.º 2
0
        public IToken GetNextToken()
        {
            if (tokens >= maxTokens)
            {
                throw new ExpressionException("Número máximo de tokens excedido.");
            }

            tokens++;

            if (pos == expString.Length)
            {
                return(new EndToken());
            }

            SkipWhiteSpaces(expString, ref pos);

            if (char.IsDigit(expString[pos]))
            {
                return(NumberLiteralToken.ParseToken(expString, ref pos));
            }
            else if (char.IsLetter(expString[pos]))
            {
                IdentifierToken itoken = IdentifierToken.ParseToken(expString, ref pos);
                switch (itoken.IdentifierName)
                {
                case "e":
                    return(new EToken());

                case "ou":
                    return(new OuToken());

                case "verdadeiro":
                    return(new BooleanLiteralToken(true));

                case "falso":
                    return(new BooleanLiteralToken(false));

                default:
                    return(itoken);
                }
            }
            else if (Regex.IsMatch(expString.Substring(pos, 1), OPERATORS))
            {
                if (expString[pos] == '=' && expString[pos + 1] == '>')
                {
                    pos += 2;
                    return(new LambdaInvokeToken());
                }

                return(OperatorToken.ParseToken(expString, ref pos));
            }
            else if (expString[pos] == '(')
            {
                pos++;
                return(new OpenParenthesisToken());
            }
            else if (expString[pos] == ')')
            {
                pos++;
                return(new CloseParenthesisToken());
            }
            else if (expString[pos] == ',')
            {
                pos++;
                return(new CommaToken());
            }
            else if (expString[pos] == '.')
            {
                pos++;
                return(new DotToken());
            }
            else if (expString[pos] == '\'')
            {
                return(StringLiteralToken.ParseToken(expString, ref pos));
            }
            else if (expString[pos] == ENDEXPRESSION)
            {
                return(new EndToken());
            }
            else
            {
                throw new SyntaxErrorException("Invalid token: " + expString[pos]);
            }
        }