Пример #1
0
        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((Map)result, "name,value".Split(','), "myname", 5);

            result = ParseLoadJson("{name:\"myname\",value:5}");
            EPAssertionUtil.AssertPropsMap((Map)result, "name,value".Split(','), "myname", 5);

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

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

            Assert.AreEqual(1, inner.Count);

            String 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" +
                          "}";
            ITree tree = ParseJson(json).First;

            ASTUtil.DumpAST(tree);
            Object 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.Render());
        }
Пример #2
0
        public static String PrintNode(ITree node)
        {
            var writer = new StringWriter();

            ASTUtil.DumpAST(writer, node, 0);
            return(writer.ToString());
        }
Пример #3
0
        private Object ParseLoadJson(String expression)
        {
            Pair <ITree, CommonTokenStream> parsed = ParseJson(expression);

            EsperEPL2GrammarParser.StartJsonValueRuleContext 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()));
        }
Пример #4
0
        public void TestGetPropertyNameEscaped()
        {
            String PROPERTY = "a\\.b\\.c";
            Pair <ITree, CommonTokenStream> parsed = SupportParserHelper.ParseEventProperty(PROPERTY);
            ITree propertyNameExprNode             = parsed.First.GetChild(0);

            ASTUtil.DumpAST(propertyNameExprNode);
            String propertyName = ((IRuleNode)propertyNameExprNode).GetText();

            Assert.AreEqual(PROPERTY, propertyName);
        }
Пример #5
0
        public void TestGetPropertyName()
        {
            String PROPERTY = "a('aa').b[1].c";

            // Should parse and result in the exact same property name
            Pair <ITree, CommonTokenStream> parsed = SupportParserHelper.ParseEventProperty(PROPERTY);
            ITree propertyNameExprNode             = parsed.First.GetChild(0);

            ASTUtil.DumpAST(propertyNameExprNode);
            String propertyName = ((IRuleNode)propertyNameExprNode).GetText();

            Assert.AreEqual(PROPERTY, propertyName);

            // Try AST with tokens separated, same property name
            parsed = SupportParserHelper.ParseEventProperty("a(    'aa'   ). b [ 1 ] . c");
            propertyNameExprNode = parsed.First.GetChild(0);
            propertyName         = ((IRuleNode)propertyNameExprNode).GetText();
            Assert.AreEqual(PROPERTY, propertyName);
        }
Пример #6
0
        /// <summary>
        /// Parse expression using the rule the ParseRuleSelector instance supplies.
        /// </summary>
        /// <param name="expression">text to parse</param>
        /// <param name="eplStatementErrorMsg">text for error</param>
        /// <param name="addPleaseCheck">true to include depth paraphrase</param>
        /// <param name="parseRuleSelector">parse rule to select</param>
        /// <param name="rewriteScript">if set to <c>true</c> [rewrite script].</param>
        /// <returns>
        /// AST - syntax tree
        /// </returns>
        /// <exception cref="EPException">IOException parsing expression ' + expression + '\''</exception>
        /// <throws>EPException when the AST could not be parsed</throws>
        public static ParseResult Parse(String expression, String eplStatementErrorMsg, bool addPleaseCheck, ParseRuleSelector parseRuleSelector, bool rewriteScript)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug(".parse Parsing expr=" + expression);
            }

            ICharStream input;

            try
            {
                input = new NoCaseSensitiveStream(expression);
            }
            catch (IOException ex)
            {
                throw new EPException("IOException parsing expression '" + expression + '\'', ex);
            }

            var lex = NewLexer(input);

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

            ITree tree;

            try
            {
                tree = parseRuleSelector.Invoke(parser);
            }
            catch (RecognitionException ex)
            {
                tokens.Fill();
                if (rewriteScript && IsContainsScriptExpression(tokens))
                {
                    return(HandleScriptRewrite(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 ex)
                {
                    Log.Debug("Token-fill produced exception: " + ex.Message, ex);
                }

                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Error parsing statement [" + eplStatementErrorMsg + "]", e);
                }
                if (e.InnerException is RecognitionException)
                {
                    if (rewriteScript && IsContainsScriptExpression(tokens))
                    {
                        return(HandleScriptRewrite(tokens, eplStatementErrorMsg, addPleaseCheck, parseRuleSelector));
                    }
                    throw ExceptionConvertor.ConvertStatement((RecognitionException)e.InnerException, eplStatementErrorMsg, addPleaseCheck, parser);
                }
                else
                {
                    throw;
                }
            }

            // if we are re-writing scripts and contain a script, then rewrite
            if (rewriteScript && IsContainsScriptExpression(tokens))
            {
                return(HandleScriptRewrite(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);
            }
            else if (tree is EsperEPL2GrammarParser.StartPatternExpressionRuleContext)
            {
                var pattern = (EsperEPL2GrammarParser.StartPatternExpressionRuleContext)tree;
                expressionWithoutAnnotation = GetNoAnnotation(expression, pattern.annotationEnum(), tokens);
            }

            return(new ParseResult(tree, expressionWithoutAnnotation, tokens, Collections.GetEmptyList <String>()));
        }