Ejemplo n.º 1
0
            public void Test(int testNum)
            {
                StringCharSourceFile  input  = new StringCharSourceFile(Input);
                BooLexerCore          lexer  = new BooLexerCore(input, new Dictionary <string, Symbol>());
                IEnumerator <AstNode> lexerE = lexer.GetEnumerator();

                string[] toks = Toks.Split(',');
                AstNode  t;

                for (int i = 0; i < toks.Length; i++)
                {
                    var    _ = StringExt.SplitAt(toks[i], ':');
                    string wantType = _.A, wantText = _.B;

                    wantType = wantType.Trim();

                    // Get the next token
                    Assert.IsTrue(lexerE.MoveNext());
                    t = lexerE.Current;
                    string type = t.NodeType.Name;
                    string msg  = string.Format("Test[{0}][{1}]: Expected {2}<{3}>, got {4}<{5}>",
                                                testNum, i, wantType, wantText, type, t.SourceText);
                    Assert.AreEqual(wantType, type, msg);
                    Assert.AreEqual(wantText, t.SourceText, msg);
                }
                Assert.IsFalse(lexerE.MoveNext());
            }
Ejemplo n.º 2
0
        protected void DoTest(string Input, bool untilEnd, string expected)
        {
            // Lex and filter the input, then wrap it in an EnumerableSource and parse it
            StringCharSourceFile  input = new StringCharSourceFile(Input);
            IEnumerable <AstNode> lexer;

            if (untilEnd)
            {
                lexer = new BooLexerCore(input, new Dictionary <string, Symbol>());
            }
            else
            {
                lexer = new BooLexer(input, new Dictionary <string, Symbol>(), true);
            }
            IEnumerable <AstNode>      lexFilter = new VisibleTokenFilter <AstNode>(lexer);
            EnumerableSource <AstNode> source    = new EnumerableSource <AstNode>(lexFilter);
            int pos = 0;
            OneOperatorMatch <AstNode> expr = _parser.Parse((IParserSource <AstNode>)source, ref pos, untilEnd);

            // Build result string
            Assert.IsNotNull(expr);
            string result = BuildResult(expr);

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 3
0
			public void Test(int testNum) 
			{
				StringCharSourceFile input = new StringCharSourceFile(Input);
				BooLexerCore lexer = new BooLexerCore(input, new Dictionary<string, Symbol>());
				IEnumerator<AstNode> lexerE = lexer.GetEnumerator();

				string[] toks = Toks.Split(',');
				AstNode t;
				for(int i = 0; i < toks.Length; i++) {
					var _ = StringExt.SplitAt(toks[i], ':');
					string wantType = _.A, wantText = _.B;
					
					wantType = wantType.Trim();
					
					// Get the next token
					Assert.IsTrue(lexerE.MoveNext());
					t = lexerE.Current;
					string type = t.NodeType.Name;
					string msg = string.Format("Test[{0}][{1}]: Expected {2}<{3}>, got {4}<{5}>", 
						testNum, i, wantType, wantText, type, t.SourceText);
					Assert.AreEqual(wantType, type, msg);
					Assert.AreEqual(wantText, t.SourceText, msg);
				}
				Assert.IsFalse(lexerE.MoveNext());
			}
Ejemplo n.º 4
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;
            }
        }
Ejemplo n.º 5
0
		public void DoTest(string input, bool boo, bool success, params object[] outputs)
		{
			ILanguageStyle lang;
			ISourceFile src;
			IEnumerable<AstNode> lexer;
			if (boo) {
				lang = new BooLanguage();
				src = new StringCharSourceFile(input);
				lexer = new BooLexer(src, lang.StandardKeywords, false);
			} else {
				lang = new BooLanguage();
				src = new StringCharSourceFile(input);
				lexer = new BooLexerCore(src, lang.StandardKeywords);
			}
			EssentialTreeParser etp = new EssentialTreeParser();
			AstNode root = AstNode.New(SourceRange.Nowhere, GSymbol.Empty);

			Assert.AreEqual(success, etp.Parse(ref root, lexer));
			CheckOutput(root, outputs, 0);
		}
Ejemplo n.º 6
0
        public void DoTest(string input, bool boo, bool success, params object[] outputs)
        {
            ILanguageStyle        lang;
            ISourceFile           src;
            IEnumerable <AstNode> lexer;

            if (boo)
            {
                lang  = new BooLanguage();
                src   = new StringCharSourceFile(input);
                lexer = new BooLexer(src, lang.StandardKeywords, false);
            }
            else
            {
                lang  = new BooLanguage();
                src   = new StringCharSourceFile(input);
                lexer = new BooLexerCore(src, lang.StandardKeywords);
            }
            EssentialTreeParser etp  = new EssentialTreeParser();
            AstNode             root = AstNode.New(SourceRange.Nowhere, GSymbol.Empty);

            Assert.AreEqual(success, etp.Parse(ref root, lexer));
            CheckOutput(root, outputs, 0);
        }
Ejemplo n.º 7
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;
			}
		}
Ejemplo n.º 8
0
		public void Try(string testName, string inputStr, string tokStrs)
		{
			StringCharSourceFile input = new StringCharSourceFile(inputStr);
			BooLexer lexer = new BooLexer(input, BooLanguage.StandardKeywords, false, 2);
			IEnumerator<AstNode> lexerE = lexer.GetEnumerator();

			string[] toks = tokStrs.Split(',');
			AstNode t;
			int expectedIndent = 0;
			for (int i = 0; i < toks.Length; i++)
			{
				var _ = StringExt.SplitAt(toks[i], ':');
				string wantType = _.A, wantText = _.B;
				if (_.B != null)
					wantType = wantType.Trim();
				else {
					if (toks[i].Length == 0)
						continue;
					int temp;
					if (int.TryParse(toks[i], out temp)) {
						expectedIndent = temp;
						continue;
					}
					wantType = wantType.Trim();
					if (wantType[0] == '_')
						wantText = wantType.Substring(1);
					else if (wantType == "NEWLINE")
						wantText = "\n";
					else
						wantText = "";
				}
				

				// Get the next token
				Assert.IsTrue(lexerE.MoveNext());
				t = lexerE.Current;
				string type = t.NodeType.Name;
				
				string msg = string.Format("\"{0}\"[{1}]: Expected {2}<{3}>({4}), got {5}<{6}>({7})",
					testName, i, wantType, wantText, expectedIndent, type, t.SourceText, t.GetTag("LineIndentation"));
				msg = msg.Replace("\n", "\\n");

				Assert.AreEqual(wantType, type, msg);
				Assert.AreEqual(wantText, t.SourceText, msg);
				if (t.NodeType != Tokens.WS && t.NodeType != Tokens.DEDENT && t.HasTag("LineIndentation"))
					Assert.AreEqual(expectedIndent, (int)t.GetTag("LineIndentation"), msg);
			}
			Assert.IsFalse(lexerE.MoveNext());
		}
Ejemplo n.º 9
0
        public void Try(string testName, string inputStr, string tokStrs)
        {
            StringCharSourceFile  input  = new StringCharSourceFile(inputStr);
            BooLexer              lexer  = new BooLexer(input, BooLanguage.StandardKeywords, false, 2);
            IEnumerator <AstNode> lexerE = lexer.GetEnumerator();

            string[] toks = tokStrs.Split(',');
            AstNode  t;
            int      expectedIndent = 0;

            for (int i = 0; i < toks.Length; i++)
            {
                var    _ = StringExt.SplitAt(toks[i], ':');
                string wantType = _.A, wantText = _.B;
                if (_.B != null)
                {
                    wantType = wantType.Trim();
                }
                else
                {
                    if (toks[i].Length == 0)
                    {
                        continue;
                    }
                    int temp;
                    if (int.TryParse(toks[i], out temp))
                    {
                        expectedIndent = temp;
                        continue;
                    }
                    wantType = wantType.Trim();
                    if (wantType[0] == '_')
                    {
                        wantText = wantType.Substring(1);
                    }
                    else if (wantType == "NEWLINE")
                    {
                        wantText = "\n";
                    }
                    else
                    {
                        wantText = "";
                    }
                }


                // Get the next token
                Assert.IsTrue(lexerE.MoveNext());
                t = lexerE.Current;
                string type = t.NodeType.Name;

                string msg = string.Format("\"{0}\"[{1}]: Expected {2}<{3}>({4}), got {5}<{6}>({7})",
                                           testName, i, wantType, wantText, expectedIndent, type, t.SourceText, t.GetTag("LineIndentation"));
                msg = msg.Replace("\n", "\\n");

                Assert.AreEqual(wantType, type, msg);
                Assert.AreEqual(wantText, t.SourceText, msg);
                if (t.NodeType != Tokens.WS && t.NodeType != Tokens.DEDENT && t.HasTag("LineIndentation"))
                {
                    Assert.AreEqual(expectedIndent, (int)t.GetTag("LineIndentation"), msg);
                }
            }
            Assert.IsFalse(lexerE.MoveNext());
        }