コード例 #1
0
ファイル: SupportParserHelper.cs プロジェクト: lanicon/nesper
 public static void DisplayAST(ITree ast)
 {
     Log.Debug(".displayAST...");
     if (Log.IsDebugEnabled)
     {
         ASTUtil.DumpAST(ast);
     }
 }
コード例 #2
0
ファイル: TestJsonParser.cs プロジェクト: lanicon/nesper
        private object ParseLoadJson(string expression)
        {
            var parsed = ParseJson(expression);
            var tree   = (EsperEPL2GrammarParser.StartJsonValueRuleContext)parsed.First;

            Assert.AreEqual(EsperEPL2GrammarParser.RULE_startJsonValueRule, ASTUtil.GetRuleIndexIfProvided(tree));
            ITree root = tree.GetChild(0);

            ASTUtil.DumpAST(root);
            return(ASTJsonHelper.Walk(parsed.Second, tree.jsonvalue()));
        }
コード例 #3
0
ファイル: TestJsonParser.cs プロジェクト: lanicon/nesper
        public void TestParse()
        {
            object result;

            Assert.AreEqual("abc", ParseLoadJson("\"abc\""));
            Assert.AreEqual("http://www.uri.com", ParseLoadJson("\"http://www.uri.com\""));
            Assert.AreEqual("new\nline", ParseLoadJson("\"new\\nline\""));
            Assert.AreEqual(" ~ ", ParseLoadJson("\" \\u007E \""));
            Assert.AreEqual("/", ParseLoadJson("\"\\/\""));
            Assert.AreEqual(true, ParseLoadJson("true"));
            Assert.AreEqual(false, ParseLoadJson("false"));
            Assert.AreEqual(null, ParseLoadJson("null"));
            Assert.AreEqual(10, ParseLoadJson("10"));
            Assert.AreEqual(-10, ParseLoadJson("-10"));
            Assert.AreEqual(20L, ParseLoadJson("20L"));
            Assert.AreEqual(5.5d, ParseLoadJson("5.5"));

            result = ParseLoadJson("{\"name\":\"myname\",\"value\":5}");
            EPAssertionUtil.AssertPropsMap(result.AsStringDictionary(), "name,value".SplitCsv(), "myname", 5);

            result = ParseLoadJson("{name:\"myname\",value:5}");
            EPAssertionUtil.AssertPropsMap(result.AsStringDictionary(), "name,value".SplitCsv(), "myname", 5);

            result = ParseLoadJson("[\"one\",2]");
            EPAssertionUtil.AssertEqualsExactOrder(new object[] { "one", 2 }, (IList <object>)result);

            result = ParseLoadJson("{\"one\": { 'a' : 2 } }");
            var inner = result.AsStringDictionary().Get("one").AsStringDictionary();

            Assert.AreEqual(1, inner.Count);

            var json = "{\n" +
                       "    \"glossary\": {\n" +
                       "        \"title\": \"example glossary\",\n" +
                       "\t\t\"GlossDiv\": {\n" +
                       "            \"title\": \"S\",\n" +
                       "\t\t\t\"GlossList\": {\n" +
                       "                \"GlossEntry\": {\n" +
                       "                    \"ID\": \"SGML\",\n" +
                       "\t\t\t\t\t\"SortAs\": \"SGML\",\n" +
                       "\t\t\t\t\t\"GlossTerm\": \"Standard Generalized Markup Language\",\n" +
                       "\t\t\t\t\t\"Acronym\": \"SGML\",\n" +
                       "\t\t\t\t\t\"Abbrev\": \"ISO 8879:1986\",\n" +
                       "\t\t\t\t\t\"GlossDef\": {\n" +
                       "                        \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n" +
                       "\t\t\t\t\t\t\"GlossSeeAlso\": [\"GML\", \"XML\"]\n" +
                       "                    },\n" +
                       "\t\t\t\t\t\"GlossSee\": \"markup\"\n" +
                       "                }\n" +
                       "            }\n" +
                       "        }\n" +
                       "    }\n" +
                       "}";
            var tree = ParseJson(json).First;

            ASTUtil.DumpAST(tree);
            var loaded = ParseLoadJson(json);

            Assert.AreEqual(
                "{\"glossary\"={\"title\"=\"example glossary\", \"GlossDiv\"={\"title\"=\"S\", \"GlossList\"={\"GlossEntry\"={\"ID\"=\"SGML\", \"SortAs\"=\"SGML\", \"GlossTerm\"=\"Standard Generalized Markup Language\", \"Acronym\"=\"SGML\", \"Abbrev\"=\"ISO 8879:1986\", \"GlossDef\"={\"para\"=\"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\"=[\"GML\", \"XML\"]}, \"GlossSee\"=\"markup\"}}}}}",
                loaded.ToString());
        }
コード例 #4
0
 public static string PrintNode(ITree node)
 {
     var writer = new StringWriter();
     ASTUtil.DumpAST(writer, node, 0);
     return writer.ToString();
 }
コード例 #5
0
        /// <summary>
        /// Parse expression using the rule the ParseRuleSelector instance supplies.
        /// </summary>
        /// <param name="expression">text to parse</param>
        /// <param name="parseRuleSelector">parse rule to select</param>
        /// <param name="addPleaseCheck">true to include depth paraphrase</param>
        /// <param name="eplStatementErrorMsg">text for error</param>
        /// <param name="rewriteScript">whether to rewrite script expressions</param>
        /// <returns>AST - syntax tree</returns>
        /// <throws>EPException                         when the AST could not be parsed</throws>
        /// <throws>StatementSpecCompileSyntaxException syntax exceptions</throws>
        public static ParseResult Parse(
            string expression,
            string eplStatementErrorMsg,
            bool addPleaseCheck,
            ParseRuleSelector parseRuleSelector,
            bool rewriteScript)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug(".parse Parsing expr=" + expression);
            }

            var input = new CaseInsensitiveInputStream(expression);
            var lex = NewLexer(input);

            var tokens = new CommonTokenStream(lex);
            var parser = ParseHelper.NewParser(tokens);

            ITree tree;
            try
            {
                tree = parseRuleSelector.InvokeParseRule(parser);
            }
            catch (RecognitionException ex)
            {
                tokens.Fill();
                if (rewriteScript)
                {
                    if (IsContainsScriptOrClassExpression(tokens))
                    {
                        return HandleScriptAndClassRewrite(tokens, eplStatementErrorMsg, addPleaseCheck, parseRuleSelector);
                    }
                }

                Log.Debug("Error parsing statement [" + expression + "]", ex);
                throw ExceptionConvertor.ConvertStatement(ex, eplStatementErrorMsg, addPleaseCheck, parser);
            }
            catch (Exception e)
            {
                try
                {
                    tokens.Fill();
                }
                catch (Exception)
                {
                    Log.Debug("Token-fill produced exception: " + e.Message, e);
                }

                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Error parsing statement [" + eplStatementErrorMsg + "]", e);
                }

                if (e.InnerException is RecognitionException recognitionException)
                {
                    if (rewriteScript)
                    {
                        if (IsContainsScriptOrClassExpression(tokens))
                        {
                            return HandleScriptAndClassRewrite(tokens, eplStatementErrorMsg, addPleaseCheck, parseRuleSelector);
                        }
                    }

                    throw ExceptionConvertor.ConvertStatement(recognitionException, eplStatementErrorMsg, addPleaseCheck, parser);
                }

                throw;
            }

            // if we are re-writing scripts and contain a script, then rewrite
            if (rewriteScript && IsContainsScriptOrClassExpression(tokens))
            {
                return HandleScriptAndClassRewrite(tokens, eplStatementErrorMsg, addPleaseCheck, parseRuleSelector);
            }

            if (Log.IsDebugEnabled)
            {
                Log.Debug(".parse Dumping AST...");
                ASTUtil.DumpAST(tree);
            }

            var expressionWithoutAnnotation = expression;
            if (tree is EsperEPL2GrammarParser.StartEPLExpressionRuleContext)
            {
                var epl = (EsperEPL2GrammarParser.StartEPLExpressionRuleContext) tree;
                expressionWithoutAnnotation = GetNoAnnotation(expression, epl.annotationEnum(), tokens);
            }

            return new ParseResult(
                tree,
                expressionWithoutAnnotation,
                tokens,
                EmptyList<string>.Instance,
                EmptyList<string>.Instance);

        }