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); }
public static Module ParseModule(string readerName, TextReader reader, ParserErrorHandler errorHandler) { antlr.TokenStreamSelector selector = new antlr.TokenStreamSelector(); BooLexer lexer = new BooLexer(reader); lexer.setTabSize(TabSize); lexer.setFilename(readerName); lexer.setTokenObjectClass(TokenObjectClass); lexer.Initialize(selector, TabSize, TokenObjectClass); IndentTokenStreamFilter filter = new IndentTokenStreamFilter(lexer, WS, INDENT, DEDENT, EOS); selector.select(filter); BooParser parser = new BooParser(selector); parser.setFilename(readerName); parser.Error += errorHandler; Module module = parser.start(); module.Name = CreateModuleName(readerName); return(module); }
public static antlr.TokenStream CreateBooLexer(int tabSize, string readerName, TextReader reader) { var selector = new antlr.TokenStreamSelector(); var lexer = new BooLexer(reader); lexer.setFilename(readerName); lexer.Initialize(selector, tabSize, BooToken.TokenCreator); var filter = new WSATokenStreamFilter(lexer); selector.select(filter); return(selector); }
public static antlr.TokenStream CreateBooLexer(int tabSize, string readerName, TextReader reader) { antlr.TokenStreamSelector selector = new antlr.TokenStreamSelector(); BooLexer lexer = new BooLexer(reader); lexer.setFilename(readerName); lexer.Initialize(selector, tabSize, BooToken.TokenCreator); IndentTokenStreamFilter filter = new IndentTokenStreamFilter(lexer, WS, INDENT, DEDENT, EOL); selector.select(filter); return(selector); }
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); }
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); }
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()); }
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()); }