Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                DisplayDirections();
            }
            else if (!File.Exists(args[0]))
            {
                Console.WriteLine("Could not find parser script '{0}'", args[0]);
                Environment.Exit(1);
            }
            else if (args[1].Any(Path.GetInvalidPathChars().Contains))
            {
                Console.WriteLine("Invalid output file '{0}'", args[1]);
                Environment.Exit(1);
            }

            string code = "";

            try
            {
                code = File.ReadAllText(args[0]);
                Console.WriteLine("Parsing input file");
                var ast = AphidParser.Parse(code);
                var macroMutator = new AphidMacroMutator();
                var pipelineMutator = new PipelineToCallMutator();
                ast = macroMutator.MutateRecursively(pipelineMutator.Mutate(ast));
                Console.WriteLine("Generating parser");
                var parserGenerator = new ParserGenerator();
                var s = parserGenerator.Generate(ast);
                File.WriteAllText(args[1], s);
                Console.WriteLine("Parser written to '{0}'", args[1]);
            }
            catch (AphidParserException exception)
            {
                Console.WriteLine(ParserErrorMessage.Create(code, exception));
            }
            catch (Exception exception)
            {
                Console.WriteLine("Unexpected exception\r\n\r\n{0}\r\n", exception.Message);
            }
        }
Ejemplo n.º 2
0
        protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            LoadScriptExpression loadExp;
            AphidExpression      scriptExp;

            if (expression.Type != AphidExpressionType.LoadScriptExpression)
            {
                hasChanged = false;

                return(null);
            }
            else if ((loadExp = (LoadScriptExpression)expression).FileExpression.Type !=
                     AphidExpressionType.StringExpression)
            {
                var constantFolder = new ConstantFoldingMutator();
                var mutated        = constantFolder.Mutate(new List <AphidExpression> {
                    loadExp.FileExpression
                });

                if (mutated.Count != 1 || (scriptExp = mutated[0]).Type != AphidExpressionType.StringExpression)
                {
                    hasChanged = false;

                    return(null);
                    //throw new AphidParserException("Invalid load script operand", loadExp);
                }
            }
            else
            {
                scriptExp = loadExp.FileExpression;
            }

            var scriptStr = StringParser.Parse(((StringExpression)scriptExp).Value);

            var script = Loader.FindScriptFile(_applicationDirectory, scriptStr);

            if (!File.Exists(script))
            {
                throw new AphidParserException(
                          string.Format(
                              "Could not find script {0}",
                              scriptStr),
                          scriptExp);
            }

            Included.Add(script);

            List <AphidExpression> ast;

            if (AphidConfig.Current.ScriptCaching && !DisableCaching)
            {
                AphidByteCodeCache cache;

                if (UseImplicitReturns)
                {
                    cache = new AphidByteCodeCache(Loader.SearchPaths.ToArray());
                }
                else
                {
                    cache = new AphidByteCodeCache(Loader.SearchPaths.ToArray(), 0x1);
                }

                ast = cache.Read(script, out var cacheSources);

                for (var i = 0; i < cacheSources.Length; i++)
                {
                    Included.Add(cacheSources[i].Name);
                }
            }
            else
            {
                var code = AphidScript.Read(script);
                ast = AphidParser.Parse(code, script, useImplicitReturns: UseImplicitReturns);
            }

            if (PerformCommonTransformations)
            {
                var mutatedAst = new PartialOperatorMutator().Mutate(ast);
                mutatedAst = new AphidMacroMutator().Mutate(mutatedAst);
                mutatedAst = new AphidPreprocessorDirectiveMutator().Mutate(mutatedAst);
                mutatedAst = new ConstantFoldingMutator().Mutate(mutatedAst);
                hasChanged = true;

                return(mutatedAst);
            }

            hasChanged = true;

            return(ast);
        }
        public List<AphidExpression> ExpandMacros(List<AphidExpression> ast)
        {
            var mutator = new AphidMacroMutator();
            return mutator.MutateRecursively(ast);

            //var ast2 = new List<AphidExpression>(ast);
            //macros = macros.Concat(AphidMacro.Parse(ast)).ToArray();
            //var removed = ast2.RemoveAll(x => macros.Any(y => y.OriginalExpression == x));

            //return ExpandMacros(ast2, macros);
        }