Esempio n. 1
0
        public static Token ParseToken(string token, bool @throw = true, int index = -1)
        {
            while (token.Length > 0 && token[0] == ' ')
            {
                token = token.Remove(0);
            }

            if (token.Length > 0)
            {
                if (Interpreter.Definitions.ContainsKey(token))
                {
                    token = Interpreter.Definitions[token].To;
                }

                if (Utils.IsHexadecimal(token))
                {
                    token = long.Parse(token.Substring(2), NumberStyles.HexNumber).ToString();
                }

                Type type = Type.GetType(token, false);
                if (type != null)
                {
                    return(new Token(TokenType.TypeIdentifier, token, index));
                }

                switch (token)
                {
                case "true":
                case "false":
                    return(new Token(TokenType.BooleanLiteral, token, index));

                case Syntax.Statements.Return:
                    return(new Token(TokenType.ReturnStatement, token, index));

                case Syntax.Operators.Or:
                    return(new Token(TokenType.OrOperator, token, index));

                case Syntax.Operators.BitwiseOR:
                    return(new Token(TokenType.BitwiseOROperator, token, index));

                case Syntax.Operators.BitwiseAND:
                    return(new Token(TokenType.BitwiseANDOperator, token, index));

                case Syntax.Comparators.IsEqual:
                    return(new Token(TokenType.IsEqualOperator, token, index));

                case Syntax.Comparators.IsNotEqual:
                    return(new Token(TokenType.IsNotEqualOperator, token, index));

                case Syntax.Comparators.IsGreaterThan:
                    return(new Token(TokenType.IsGreaterThanOperator, token, index));

                case Syntax.Comparators.IsLessThan:
                    return(new Token(TokenType.IsLessThanOperator, token, index));

                case Syntax.Comparators.IsGreaterThanOrEqual:
                    return(new Token(TokenType.IsGreaterThanOrEqualOperator, token, index));

                case Syntax.Comparators.IsLessThanOrEqual:
                    return(new Token(TokenType.IsLessThanOrEqualOperator, token, index));

                case Syntax.Operators.Add:
                    return(new Token(TokenType.AddOperator, token, index));

                case Syntax.Operators.And:
                    return(new Token(TokenType.AndOperator, token, index));

                case Syntax.Operators.Subtract:
                    return(new Token(TokenType.SubtractOperator, token, index));

                case Syntax.Operators.Multiply:
                    return(new Token(TokenType.MultiplyOperator, token, index));

                case Syntax.Operators.Divide:
                    return(new Token(TokenType.DivideOperator, token, index));

                case Syntax.Operators.Modulo:
                    return(new Token(TokenType.ModuloOperator, token, index));

                case Syntax.Operators.Equal:
                    return(new Token(TokenType.EqualOperator, token, index));

                case "''":
                    return(new Token(TokenType.CharacterLiteral, "", index));                            // support empty Characters ('')

                case "{":
                    return(new Token(TokenType.StartBracket, token, index));

                case "}":
                    return(new Token(TokenType.EndBracket, token, index));

                case "(":
                case ")":
                    return(new Token(TokenType.Parenthesis, token, index));

                case Syntax.Conditionals.Else:
                    return(new Token(TokenType.ElseConditional, token, index));
                }

                if (token[0] == '\"' && token[token.Length - 1] == '\"' && token.OccurrencesOf("\"") == 2)
                {
                    return(new Token(TokenType.StringLiteral, token.Substring(1, token.Length - 2).FromBase64().Replace("\\\"", "\""), index));
                }
                else if (token[0] == '\'' && token[token.Length - 1] == '\'')                 // we can't ensure the length here because the contents are encoded
                {
                    return(new Token(TokenType.CharacterLiteral, token.Substring(1, token.Length - 2).FromBase64(), index));
                }
                else if (token.IndexOf(Syntax.Conditionals.If + "(") == 0 && token[token.Length - 1] == ')')
                {
                    return(new Token(TokenType.IfConditional, token, index));
                }
                else if (token.IndexOf(Syntax.Conditionals.While + "(") == 0 && token[token.Length - 1] == ')')
                {
                    return(new Token(TokenType.WhileConditional, token, index));
                }
                else if (token.IndexOf(Syntax.Conditionals.Loop + "(") == 0 && token[token.Length - 1] == ')')
                {
                    return(new Token(TokenType.LoopConditional, token, index));
                }
                else if (Utils.IsNumber(token))
                {
                    try
                    {
                        if (token.OccurrencesOf(".") == 1)
                        {
                            Convert.ToDecimal(token);
                            return(new Token(TokenType.DecimalLiteral, token, index));
                        }
                        else
                        {
                            long number = Convert.ToInt64(token);
                            if (number <= int.MaxValue && number >= int.MinValue)
                            {
                                return(new Token(TokenType.IntegerLiteral, token, index));
                            }
                            else
                            {
                                return(new Token(TokenType.Integer64Literal, token, index));
                            }
                        }
                    }
                    catch (FormatException)
                    {
                        throw new BaseException("Number literal is in a invalid format");
                    }
                    catch (OverflowException)
                    {
                        throw new BaseException("Number literal is either too large or small to be parsed");
                    }
                }
                else if (FunctionChain.IsChain(token))
                {
                    return(new Token(TokenType.FunctionChain, token, index));
                }
                else if (BaseFunction.IsFunctionCall(token))
                {
                    return(new Token(TokenType.FunctionCall, token, index));
                }
                else if (FastStruct.IsFastStruct(token))
                {
                    return(new Token(TokenType.FastStruct, token, index));
                }
                else if (Utils.IsValidObjectName(token) || FastStruct.IsValidMemberAccessor(token))
                {
                    return(new Token(TokenType.VariableIdentifier, token, index));
                }
            }

            if (@throw)
            {
                throw new BaseException("Unknown token: " + token);
            }

            return(null);
        }