예제 #1
0
        private ICompilationEntity[] ParseTopLevelInlineImport(Token atToken, TokenStream tokens)
        {
            string sourceFile   = null;
            string functionName = tokens.PeekValue();

            switch (functionName)
            {
            case "import":
                tokens.PopExpected("import");
                tokens.PopExpected("(");
                Token stringToken = tokens.Pop();
                tokens.PopExpected(")");
                tokens.PopExpected(";");
                sourceFile = PastelUtil.ConvertStringTokenToValue(stringToken.Value);
                break;

            case "importIfTrue":
            case "importIfFalse":
                tokens.Pop();
                tokens.PopExpected("(");
                Token  constantExpression = tokens.Pop();
                string constantValue      = PastelUtil.ConvertStringTokenToValue(constantExpression.Value);
                tokens.PopExpected(",");
                Token pathToken = tokens.Pop();
                tokens.PopExpected(")");
                tokens.PopExpected(";");
                object value = this.GetConstant(constantValue, false);
                if (!(value is bool))
                {
                    value = false;
                }
                bool valueBool = (bool)value;
                if (functionName == "importIfFalse")
                {
                    valueBool = !valueBool;
                }

                if (valueBool)
                {
                    sourceFile = PastelUtil.ConvertStringTokenToValue(pathToken.Value);
                }
                break;

            default:
                // intentional crash...
                tokens.PopExpected("import");
                break;
            }

            if (sourceFile != null)
            {
                string code = this.importCodeLoader.LoadCode(atToken, sourceFile);
                return(this.ParseText(sourceFile, code));
            }

            return(new ICompilationEntity[0]);
        }
예제 #2
0
        private Expression ParseEntityRoot(TokenStream tokens)
        {
            string next = tokens.PeekValue();

            switch (next)
            {
            case "true":
            case "false":
                return(new InlineConstant(PType.BOOL, tokens.Pop(), next == "true", this.currentCodeOwner));

            case "null":
                return(new InlineConstant(PType.NULL, tokens.Pop(), null, this.currentCodeOwner));

            case ".":
                Token dotToken = tokens.Pop();
                Token numToken = tokens.Pop();
                EnsureInteger(tokens.Pop(), false, false);
                string strValue = "0." + numToken.Value;
                double dblValue;
                if (!numToken.HasWhitespacePrefix && double.TryParse(strValue, out dblValue))
                {
                    return(new InlineConstant(PType.DOUBLE, dotToken, dblValue, this.currentCodeOwner));
                }
                throw new ParserException(dotToken, "Unexpected '.'");

            default: break;
            }
            char firstChar = next[0];

            switch (firstChar)
            {
            case '\'':
                return(new InlineConstant(PType.CHAR, tokens.Pop(), PastelUtil.ConvertStringTokenToValue(next), this.currentCodeOwner));

            case '"':
                return(new InlineConstant(PType.STRING, tokens.Pop(), PastelUtil.ConvertStringTokenToValue(next), this.currentCodeOwner));

            case '@':
                Token atToken             = tokens.PopExpected("@");
                Token compileTimeFunction = EnsureTokenIsValidName(tokens.Pop(), "Expected compile time function name.");
                if (!tokens.IsNext("("))
                {
                    tokens.PopExpected("(");
                }
                return(new CompileTimeFunctionReference(atToken, compileTimeFunction, this.currentCodeOwner));
            }

            if (firstChar >= '0' && firstChar <= '9')
            {
                Token numToken = tokens.Pop();
                if (tokens.IsNext("."))
                {
                    EnsureInteger(numToken, false, false);
                    Token dotToken = tokens.Pop();
                    if (dotToken.HasWhitespacePrefix)
                    {
                        throw new ParserException(dotToken, "Unexpected '.'");
                    }
                    Token decimalToken = tokens.Pop();
                    EnsureInteger(decimalToken, false, false);
                    if (decimalToken.HasWhitespacePrefix)
                    {
                        throw new ParserException(decimalToken, "Unexpected '" + decimalToken.Value + "'");
                    }
                    double dblValue;
                    if (double.TryParse(numToken.Value + "." + decimalToken.Value, out dblValue))
                    {
                        return(new InlineConstant(PType.DOUBLE, numToken, dblValue, this.currentCodeOwner));
                    }
                    throw new ParserException(decimalToken, "Unexpected token.");
                }
                else
                {
                    int numValue = EnsureInteger(numToken, true, true);
                    return(new InlineConstant(PType.INT, numToken, numValue, this.currentCodeOwner));
                }
            }

            if (tokens.IsNext("this"))
            {
                return(new ThisExpression(tokens.Pop(), this.currentCodeOwner));
            }

            if (IsValidName(tokens.PeekValue()))
            {
                return(new Variable(tokens.Pop(), this.currentCodeOwner));
            }

            throw new ParserException(tokens.Peek(), "Unrecognized expression.");
        }