예제 #1
0
        public virtual int Generate(string inputFilePath, string inputFileContents, string defaultNamespace, IntPtr[] outputFileContents, out uint outputSize, IVsGeneratorProgress progressCallback)
        {
            string inputFolder = Path.GetDirectoryName(inputFilePath);
            string oldCurDir   = Environment.CurrentDirectory;

            try {
                Environment.CurrentDirectory = inputFolder;                 // --macros should be relative to file being processed

                var sourceFile = new StringCharSourceFile(inputFileContents, inputFilePath);
                var sink       = ToMessageSink(progressCallback);

                var c = new Compiler(sink, sourceFile);

                var options = new BMultiMap <string, string>();
                var argList = G.SplitCommandLineArguments(defaultNamespace);
                UG.ProcessCommandLineArguments(argList, options, "", LEL.Compiler.ShortOptions, LEL.Compiler.TwoArgOptions);

                string _;
                var    KnownOptions = LEL.Compiler.KnownOptions;
                if (options.TryGetValue("help", out _) || options.TryGetValue("?", out _))
                {
                    LEL.Compiler.ShowHelp(KnownOptions);
                }

                Symbol minSeverity = MessageSink.Note;
                var    filter      = new SeverityMessageFilter(MessageSink.Console, minSeverity);

                if (LEL.Compiler.ProcessArguments(c, options))
                {
                    LEL.Compiler.WarnAboutUnknownOptions(options, MessageSink.Console, KnownOptions);
                    if (c != null)
                    {
                        c.MacroProcessor.PreOpenedNamespaces.Add(GSymbol.Get("LEL.Prelude"));
                        c.MacroProcessor.PreOpenedNamespaces.Add(GSymbol.Get("Loyc.LLParserGenerator"));
                        c.AddMacros(typeof(Loyc.LLParserGenerator.Macros).Assembly);
                        c.Run();
                    }

                    var outputBytes = Encoding.UTF8.GetBytes(c.Output.ToString());
                    c.Output              = null;        // no longer needed
                    outputSize            = (uint)outputBytes.Length;
                    outputFileContents[0] = Marshal.AllocCoTaskMem(outputBytes.Length);
                    Marshal.Copy(outputBytes, 0, outputFileContents[0], outputBytes.Length);
                }
                else
                {
                    outputFileContents[0] = IntPtr.Zero;
                    outputSize            = 0;
                }
                return(VSConstants.S_OK);
            } finally {
                Environment.CurrentDirectory = oldCurDir;
            }
        }
예제 #2
0
        private void RunLeMP(IList <string> args, string inputCode, string inputPath)
        {
            var options = new BMultiMap <string, string>();

            UG.ProcessCommandLineArguments(args, options, "", LeMP.Compiler.ShortOptions, LeMP.Compiler.TwoArgOptions);

            string _;
            var    KnownOptions = LeMP.Compiler.KnownOptions;

            if (options.TryGetValue("help", out _) || options.TryGetValue("?", out _))
            {
                var ms = new MemoryStream();
                LeMP.Compiler.ShowHelp(LeMP.Compiler.KnownOptions, new StreamWriter(ms));
                string output = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
                _outFileName = null;
                ShowOutput(output);
            }
            else
            {
                var sink       = MessageSink.FromDelegate(WriteMessage);
                var sourceFile = new InputOutput((UString)inputCode, Path.GetFileName(inputPath));

                var c = new Compiler(sink, sourceFile);
                c.Files = new List <InputOutput> {
                    sourceFile
                };
                c.Parallel = false;                 // only one file, parallel doesn't help

                if (LeMP.Compiler.ProcessArguments(c, options))
                {
                    LeMP.Compiler.WarnAboutUnknownOptions(options, sink, KnownOptions);

                    c.AddMacros(typeof(global::LeMP.StandardMacros).Assembly);
                    c.MacroProcessor.PreOpenedNamespaces.Add(GSymbol.Get("LeMP"));
                    c.MacroProcessor.PreOpenedNamespaces.Add(GSymbol.Get("LeMP.Prelude"));
                    if (inputPath.EndsWith(".les", StringComparison.OrdinalIgnoreCase))
                    {
                        c.MacroProcessor.PreOpenedNamespaces.Add(GSymbol.Get("LeMP.Prelude.Les"));
                    }

                    LempStarted = true;
                    new Thread(() => {
                        try {
                            c.Run();
                            // Must get OutFileName after calling Run()
                            _outFileName = sourceFile.OutFileName;
                            ShowOutput(c.Output.ToString());
                        } finally { LempStarted = false; }
                    }).Start();
                }
            }
        }
예제 #3
0
		public virtual int Generate(string inputFilePath, string inputFileContents, string defaultNamespace, IntPtr[] outputFileContents, out uint outputSize, IVsGeneratorProgress progressCallback)
		{
			string inputFolder = Path.GetDirectoryName(inputFilePath);
			string oldCurDir = Environment.CurrentDirectory;
			try {
 				Environment.CurrentDirectory = inputFolder; // --macros should be relative to file being processed

				var sourceFile = new StringCharSourceFile(inputFileContents, inputFilePath);
				var sink = ToMessageSink(progressCallback);
			
				var c = new Compiler(sink, sourceFile);

				var options = new BMultiMap<string, string>();
				var argList = G.SplitCommandLineArguments(defaultNamespace);
				UG.ProcessCommandLineArguments(argList, options, "", LEL.Compiler.ShortOptions, LEL.Compiler.TwoArgOptions);

				string _;
				var KnownOptions = LEL.Compiler.KnownOptions;
				if (options.TryGetValue("help", out _) || options.TryGetValue("?", out _))
					LEL.Compiler.ShowHelp(KnownOptions);

				Symbol minSeverity = MessageSink.Note;
				var filter = new SeverityMessageFilter(MessageSink.Console, minSeverity);

				if (LEL.Compiler.ProcessArguments(c, options)) {
					LEL.Compiler.WarnAboutUnknownOptions(options, MessageSink.Console, KnownOptions);
					if (c != null)
					{
						c.MacroProcessor.PreOpenedNamespaces.Add(GSymbol.Get("LEL.Prelude"));
						c.MacroProcessor.PreOpenedNamespaces.Add(GSymbol.Get("Loyc.LLParserGenerator"));
						c.AddMacros(typeof(Loyc.LLParserGenerator.Macros).Assembly);
						c.Run();
					}

					var outputBytes = Encoding.UTF8.GetBytes(c.Output.ToString());
					c.Output = null; // no longer needed
					outputSize = (uint)outputBytes.Length;
					outputFileContents[0] = Marshal.AllocCoTaskMem(outputBytes.Length);
					Marshal.Copy(outputBytes, 0, outputFileContents[0], outputBytes.Length);
				}
				else
				{
					outputFileContents[0] = IntPtr.Zero;
					outputSize = 0;
				}
				return VSConstants.S_OK;
			} finally {
				Environment.CurrentDirectory = oldCurDir;
			}
		}