示例#1
0
文件: Parser.cs 项目: peppy/osq2osb
        /// <summary>
        /// Initializes a new instance of the <see cref="Parser"/> class given a <see cref="Parser"/> to model after.
        /// </summary>
        /// <param name="other">Parser to copy the options and reader from.</param>
        public Parser(Parser other)
        {
            if(other == null) {
                throw new ArgumentNullException("other");
            }

            InputReader = other.InputReader;
            Options = other.Options.Clone();
        }
示例#2
0
        static void Main(string[] args)
        {
            if(args.Length == 0) {
                Console.WriteLine("Parsing from console...");

                var executionContext = new ExecutionContext();

                while(true) {
                    try {
                        using(var console = Console.OpenStandardInput())
                        using(var reader = new LocatedTextReaderWrapper(console)) {
                            var parser = new Parser(reader);

                            foreach(var node in parser.ReadNodes()) {
                                string output = node.Execute(executionContext);

                                Console.Write(output);
                            }
                        }
                    } catch(Exception e) {
                        Console.WriteLine("Error: " + e);
                    }
                }
            } else {
                watchers = new Dictionary<FileCollectionWatcher, string>();

                foreach(var filename in args) {
                    var watcher = new FileCollectionWatcher();
                    watcher.Changed += FileChanged;
                    watchers[watcher] = filename;

                    ParseFile(watcher, filename);

                    Console.WriteLine("Watching " + filename + " for changes...");
                }

                Thread.Sleep(-1);
            }
        }
示例#3
0
        private static void ParseFile(FileCollectionWatcher watcher, string filename)
        {
            Console.Write("Parsing " + filename + "...");

            using(var inputFile = File.Open(filename, FileMode.Open, FileAccess.Read))
            using(var outputFile = File.Open(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename)) + ".osb", FileMode.Create, FileAccess.Write)) {
                var executionContext = new ExecutionContext();

                using(var reader = new LocatedTextReaderWrapper(inputFile, new Location(filename)))
                using(var writer = new StreamWriter(outputFile)) {
                    try {
                        var parser = new Parser(reader);

                        foreach(var node in parser.ReadNodes()) {
                            string output = node.Execute(executionContext);

                            writer.Write(output);
                        }

                        watcher.Clear();
                    } catch(Exception e) {
                        Console.WriteLine("\nError: " + e);

                        return;
                    } finally {
                        if(!watcher.Contains(filename)) {
                            watcher.Add(filename);
                        }

                        foreach(string file in executionContext.Dependencies.Where((file) => !watcher.Contains(file))) {
                            watcher.Add(file);
                        }
                    }
                }
            }

            Console.WriteLine("  Done!");
        }
示例#4
0
文件: Parser.cs 项目: peppy/osq2osb
 /// <summary>
 /// Initializes a new instance of the <see cref="Parser"/> class given a <see cref="Parser"/> to model after and a new reader.
 /// </summary>
 /// <param name="other">Parser to copy the options from.</param>
 /// <param name="newReader">The new reader.</param>
 public Parser(Parser other, LocatedTextReaderWrapper newReader)
     : this(other)
 {
     InputReader = newReader;
 }
示例#5
0
文件: Encoder.cs 项目: peppy/osq2osb
 /// <summary>
 /// Initializes a new instance of the <see cref="Encoder"/> class.
 /// </summary>
 /// <param name="parser">The parser.</param>
 /// <param name="context">The execution context.</param>
 public Encoder(Parser parser, ExecutionContext context)
 {
     Parser = parser;
     ExecutionContext = context;
 }
示例#6
0
文件: Encoder.cs 项目: peppy/osq2osb
 /// <summary>
 /// Initializes a new instance of the <see cref="Encoder"/> class.
 /// </summary>
 /// <param name="parser">The parser.</param>
 public Encoder(Parser parser)
     : this(parser, new ExecutionContext())
 {
 }
示例#7
0
文件: Encoder.cs 项目: peppy/osq2osb
        /// <summary>
        /// Transforms the osq script into an osb script.
        /// </summary>
        /// <returns>The osb script.</returns>
        public string Encode()
        {
            scriptNodes = new List<ConvertedNode>();

            var output = new StringBuilder();

            using(var bufferingReader = new BufferingTextReaderWrapper(Parser.InputReader))
            using(var myReader = new LocatedTextReaderWrapper(bufferingReader, Parser.InputReader.Location.Clone())) { // Sorry we have to do this...
                var parser = new Parser(Parser, myReader);

                NodeBase node;

                while((node = parser.ReadNode()) != null) {
                    string curOutput = node.Execute(ExecutionContext);

                    output.Append(curOutput);

                    var converted = new ConvertedNode {
                        Node = node,
                        NodeOutput = curOutput,
                        OriginalScript = bufferingReader.BufferedText
                    };

                    scriptNodes.Add(converted);

                    bufferingReader.ClearBuffer();
                }
            }

            return output.ToString();
        }