Exemplo n.º 1
0
        public void Execute(string script)
        {
            var foregroundColor = Console.ForegroundColor;

            try
            {
                if (PreProcessorUtil.IsLoadLine(script))
                {
                    var filepath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, script);
                    script = FilePreProcessor.ProcessFile(filepath);
                }
                else if (PreProcessorUtil.IsRLine(script))
                {
                    var assemblyPath = PreProcessorUtil.GetPath(PreProcessorUtil.RString, script);
                    References = References.Union(new[] { assemblyPath });
                    return;
                }

                Console.ForegroundColor = ConsoleColor.Cyan;
                var result = ScriptEngine.Execute(script, References, DefaultNamespaces, ScriptPackSession);
                if (result != null)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(result.ToJsv()

                                      );
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\r\n" + ex + "\r\n");
            }
            Console.ForegroundColor = foregroundColor;
        }
Exemplo n.º 2
0
        protected virtual void InsertLineDirective(string path, List <string> fileLines)
        {
            Guard.AgainstNullArgument("fileLines", fileLines);

            var bodyIndex = fileLines.FindIndex(line => PreProcessorUtil.IsNonDirectiveLine(line) && !PreProcessorUtil.IsUsingLine(line));

            if (bodyIndex == -1)
            {
                return;
            }

            var directiveLine = string.Format("#line {0} \"{1}\"", bodyIndex + 1, path);

            fileLines.Insert(bodyIndex, directiveLine);
        }
Exemplo n.º 3
0
        protected virtual void ProcessLine(FilePreProcessorContext context, string line, bool isBeforeCode)
        {
            Guard.AgainstNullArgument("context", context);

            if (PreProcessorUtil.IsUsingLine(line))
            {
                var @using = PreProcessorUtil.GetPath(PreProcessorUtil.UsingString, line);
                if (!context.UsingStatements.Contains(@using))
                {
                    context.UsingStatements.Add(@using);
                }

                return;
            }

            if (PreProcessorUtil.IsRLine(line))
            {
                if (isBeforeCode)
                {
                    var reference = PreProcessorUtil.GetPath(PreProcessorUtil.RString, line);
                    if (!string.IsNullOrWhiteSpace(reference) && !context.References.Contains(reference))
                    {
                        context.References.Add(reference);
                    }
                }

                return;
            }

            if (PreProcessorUtil.IsLoadLine(line))
            {
                if (isBeforeCode)
                {
                    var filePath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, line);
                    if (!string.IsNullOrWhiteSpace(filePath) && !context.LoadedScripts.Contains(filePath))
                    {
                        ParseFile(filePath, context);
                    }
                }

                return;
            }

            // If we've reached this, the line is part of the body...
            context.Body.Add(line);
        }
Exemplo n.º 4
0
Arquivo: Repl.cs Projeto: waf/scriptcs
        public override ScriptResult Execute(string script, params string[] scriptArgs)
        {
            try
            {
                if (PreProcessorUtil.IsLoadLine(script))
                {
                    var filepath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, script);
                    if (FileSystem.FileExists(filepath))
                    {
                        var processorResult = FilePreProcessor.ProcessFile(filepath);
                        script = processorResult.Code;
                    }
                    else
                    {
                        throw new FileNotFoundException(string.Format("Could not find script '{0}'", filepath), filepath);
                    }
                }
                else if (PreProcessorUtil.IsRLine(script))
                {
                    var assemblyName = PreProcessorUtil.GetPath(PreProcessorUtil.RString, script);
                    var assemblyPath = FileSystem.GetFullPath(Path.Combine(Constants.BinFolder, assemblyName));
                    AddReferences(FileSystem.FileExists(assemblyPath) ? assemblyPath : assemblyName);

                    return new ScriptResult();
                }

                Console.ForegroundColor = ConsoleColor.Cyan;

                Buffer += script;

                var result = ScriptEngine.Execute(Buffer, _scriptArgs, References, DefaultNamespaces, ScriptPackSession);
                if (result != null)
                {
                    if (result.CompileExceptionInfo != null)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(result.CompileExceptionInfo.SourceException.ToString());
                    }

                    if (result.ExecuteExceptionInfo != null)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(result.ExecuteExceptionInfo.SourceException.ToString());
                    }

                    if (result.IsPendingClosingChar)
                    {
                        return result;
                    }

                    if (result.ReturnValue != null)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(result.ReturnValue.ToJsv());
                    }

                    Buffer = null;
                }

                return result;
            }
            catch (FileNotFoundException fileEx)
            {
                RemoveReferences(fileEx.FileName);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\r\n" + fileEx + "\r\n");
                return new ScriptResult { CompileExceptionInfo = ExceptionDispatchInfo.Capture(fileEx) };
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\r\n" + ex + "\r\n");
                return new ScriptResult { ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(ex) };
            }
            finally
            {
                Console.ResetColor();
            }
        }
Exemplo n.º 5
0
        private string ParseFile(string path, IEnumerable <string> file, ref List <string> usings, ref List <string> rs, ref List <string> loads)
        {
            var fileList  = file.ToList();
            var firstCode = fileList.FindIndex(l => PreProcessorUtil.IsNonDirectiveLine(l));

            var firstBody = fileList.FindIndex(l => PreProcessorUtil.IsNonDirectiveLine(l) && !PreProcessorUtil.IsUsingLine(l));

            // add #line before the actual code begins
            // +1 because we are in a zero indexed list, but line numbers are 1 indexed
            // we need to keep the original position of the actual line
            if (firstBody != -1)
            {
                _logger.DebugFormat("Added #line statement for file {0} at line {1}", path, firstBody);
                fileList.Insert(firstBody, string.Format(@"#line {0} ""{1}""", firstBody + 1, path));
            }

            for (var i = 0; i < fileList.Count; i++)
            {
                var line = fileList[i];
                if (PreProcessorUtil.IsUsingLine(line))
                {
                    usings.Add(line);
                }
                else if (PreProcessorUtil.IsRLine(line))
                {
                    if (i < firstCode)
                    {
                        rs.Add(line);
                    }
                    else
                    {
                        fileList[i] = string.Empty;
                    }
                }
                else if (PreProcessorUtil.IsLoadLine(line))
                {
                    if ((i < firstCode || firstCode < 0) && !loads.Contains(line))
                    {
                        var filepath    = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, line);
                        var filecontent = _fileSystem.IsPathRooted(filepath)
                                              ? _fileSystem.ReadFileLines(filepath)
                                              : _fileSystem.ReadFileLines(_fileSystem.CurrentDirectory + @"\" + filepath);

                        if (filecontent != null)
                        {
                            loads.Add(line);
                            _logger.DebugFormat("Parsing file {0}", path);
                            var parsed = ParseFile(filepath, filecontent, ref usings, ref rs, ref loads);
                            fileList[i] = parsed;
                        }
                    }
                    else
                    {
                        fileList[i] = string.Empty;
                    }
                }
            }

            var result = string.Join(_fileSystem.NewLine, fileList.Where(line => !PreProcessorUtil.IsUsingLine(line) && !PreProcessorUtil.IsRLine(line)));

            return(result);
        }