Пример #1
0
        /// <summary>
        /// Parse the AST constant node and return Object value.
        /// </summary>
        /// <param name="node">parse node for which to parse the string value</param>
        /// <returns>value matching AST node type</returns>
        public static Object Parse(IParseTree node)
        {
            if (node is ITerminalNode)
            {
                var terminal = (ITerminalNode)node;
                switch (terminal.Symbol.Type)
                {
                case EsperEPL2GrammarParser.BOOLEAN_TRUE:  return(BoolValue.ParseString(terminal.GetText()));

                case EsperEPL2GrammarParser.BOOLEAN_FALSE: return(BoolValue.ParseString(terminal.GetText()));

                case EsperEPL2GrammarParser.VALUE_NULL:    return(null);

                default:
                    throw ASTWalkException.From("Encountered unexpected constant type " + terminal.Symbol.Type, terminal.Symbol);
                }
            }
            else
            {
                var ruleNode  = (IRuleNode)node;
                var ruleIndex = ruleNode.RuleContext.RuleIndex;
                if (ruleIndex == EsperEPL2GrammarParser.RULE_number)
                {
                    return(ParseNumber(ruleNode, 1));
                }
                else if (ruleIndex == EsperEPL2GrammarParser.RULE_numberconstant)
                {
                    var number = FindChildRuleByType(ruleNode, EsperEPL2GrammarParser.RULE_number);
                    if (ruleNode.ChildCount > 1)
                    {
                        if (ASTUtil.IsTerminatedOfType(ruleNode.GetChild(0), EsperEPL2GrammarLexer.MINUS))
                        {
                            return(ParseNumber(number, -1));
                        }
                        return(ParseNumber(number, 1));
                    }
                    else
                    {
                        return(ParseNumber(number, 1));
                    }
                }
                else if (ruleIndex == EsperEPL2GrammarParser.RULE_stringconstant)
                {
                    bool requireUnescape = !IsRegexpNode(node);
                    return(StringValue.ParseString(node.GetText(), requireUnescape));
                }
                else if (ruleIndex == EsperEPL2GrammarParser.RULE_constant)
                {
                    return(Parse(ruleNode.GetChild(0)));
                }
                throw ASTWalkException.From("Encountered unrecognized constant", node.GetText());
            }
        }
Пример #2
0
        public static ExprTimePeriod TimePeriodGetExprAllParams(EsperEPL2GrammarParser.TimePeriodContext ctx, IDictionary <ITree, ExprNode> astExprNodeMap, VariableService variableService, StatementSpecRaw spec, ConfigurationInformation config)
        {
            var nodes = new ExprNode[8];

            for (var i = 0; i < ctx.ChildCount; i++)
            {
                var unitRoot = ctx.GetChild(i);

                ExprNode valueExpr;
                if (ASTUtil.IsTerminatedOfType(unitRoot.GetChild(0), EsperEPL2GrammarLexer.IDENT))
                {
                    var ident = unitRoot.GetChild(0).GetText();
                    valueExpr = ASTExprHelper.ResolvePropertyOrVariableIdentifier(ident, variableService, spec);
                }
                else
                {
                    var        @ref   = new Atomic <ExprNode>();
                    ExprAction action = (exprNode, astExprNodeMapX, nodeX) => {
                        astExprNodeMapX.Remove(nodeX);
                        @ref.Set(exprNode);
                    };
                    ASTExprHelper.RecursiveFindRemoveChildExprNode(unitRoot.GetChild(0), astExprNodeMap, action);
                    valueExpr = @ref.Get();
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_millisecondPart)
                {
                    nodes[7] = valueExpr;
                }
                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_secondPart)
                {
                    nodes[6] = valueExpr;
                }
                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_minutePart)
                {
                    nodes[5] = valueExpr;
                }
                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_hourPart)
                {
                    nodes[4] = valueExpr;
                }
                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_dayPart)
                {
                    nodes[3] = valueExpr;
                }
                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_weekPart)
                {
                    nodes[2] = valueExpr;
                }
                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_monthPart)
                {
                    nodes[1] = valueExpr;
                }
                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_yearPart)
                {
                    nodes[0] = valueExpr;
                }
            }

            ExprTimePeriod timeNode = new ExprTimePeriodImpl(config.EngineDefaults.ExpressionConfig.TimeZone,
                                                             nodes[0] != null, nodes[1] != null, nodes[2] != null, nodes[3] != null, nodes[4] != null, nodes[5] != null, nodes[6] != null, nodes[7] != null);

            if (nodes[0] != null)
            {
                timeNode.AddChildNode(nodes[0]);
            }
            if (nodes[1] != null)
            {
                timeNode.AddChildNode(nodes[1]);
            }
            if (nodes[2] != null)
            {
                timeNode.AddChildNode(nodes[2]);
            }
            if (nodes[3] != null)
            {
                timeNode.AddChildNode(nodes[3]);
            }
            if (nodes[4] != null)
            {
                timeNode.AddChildNode(nodes[4]);
            }
            if (nodes[5] != null)
            {
                timeNode.AddChildNode(nodes[5]);
            }
            if (nodes[6] != null)
            {
                timeNode.AddChildNode(nodes[6]);
            }
            if (nodes[7] != null)
            {
                timeNode.AddChildNode(nodes[7]);
            }
            return(timeNode);
        }