static Rule CreateRuleFromContext(DeltinScriptParser.Ow_ruleContext ruleContext)
        {
            string ruleName = ruleContext.STRINGLITERAL().GetText();

            ruleName = ruleName.Substring(1, ruleName.Length - 2);

            RuleEvent      ruleEvent = RuleEvent.Ongoing_Global;
            TeamSelector   team      = TeamSelector.All;
            PlayerSelector player    = PlayerSelector.All;

            {
                var additionalArgs = ruleContext.expr();

                foreach (var arg in additionalArgs)
                {
                    string type = arg.GetText().Split('.').ElementAtOrDefault(0);
                    string name = arg.GetText().Split('.').ElementAtOrDefault(1);

                    if (type == "Event")
                    {
                        if (Enum.TryParse(name, out RuleEvent setEvent))
                        {
                            ruleEvent = setEvent;
                        }
                        else
                        {
                            throw new SyntaxErrorException($"Unknown event type \"{arg.GetText()}\".", arg.start);
                        }
                    }
                    else if (type == "Team")
                    {
                        if (Enum.TryParse(name, out TeamSelector setTeam))
                        {
                            team = setTeam;
                        }
                        else
                        {
                            throw new SyntaxErrorException($"Unknown team type \"{arg.GetText()}\".", arg.start);
                        }
                    }
                    else if (type == "Player")
                    {
                        if (Enum.TryParse(name, out PlayerSelector setPlayer))
                        {
                            player = setPlayer;
                        }
                        else
                        {
                            throw new SyntaxErrorException($"Unknown player type \"{arg.GetText()}\".", arg.start);
                        }
                    }
                    else
                    {
                        throw new SyntaxErrorException($"Unknown rule argument \"{arg.GetText()}\".", arg.start);
                    }
                }
            }

            return(new Rule(ruleName, ruleEvent, team, player));
        }
        public RuleAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Ow_ruleContext ruleContext)
        {
            Name               = Extras.RemoveQuotes(ruleContext.STRINGLITERAL().GetText());
            Disabled           = ruleContext.DISABLED() != null;
            _missingBlockRange = DocRange.GetRange(ruleContext.RULE_WORD());

            GetRuleSettings(parseInfo, scope, ruleContext);

            // Get the conditions
            if (ruleContext.rule_if() == null)
            {
                Conditions = new RuleIfAction[0];
            }
            else
            {
                Conditions = new RuleIfAction[ruleContext.rule_if().Length];
                for (int i = 0; i < Conditions.Length; i++)
                {
                    Conditions[i]      = new RuleIfAction(parseInfo, scope, ruleContext.rule_if(i));
                    _missingBlockRange = DocRange.GetRange(ruleContext.rule_if(i));
                }
            }

            if (ruleContext.block() != null)
            {
                Block = new BlockAction(parseInfo, scope, ruleContext.block());
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Missing block.", _missingBlockRange);
            }
        }
        public RuleAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Ow_ruleContext ruleContext)
        {
            Name     = Extras.RemoveQuotes(ruleContext.STRINGLITERAL().GetText());
            Disabled = ruleContext.DISABLED() != null;
            DocRange ruleInfoRange = DocRange.GetRange(ruleContext.RULE_WORD());

            missingBlockRange = ruleInfoRange;

            GetRuleSettings(parseInfo, scope, ruleContext);

            // Store restricted calls
            CallInfo callInfo = new CallInfo(parseInfo.Script);

            // Get the conditions.
            if (ruleContext.rule_if() == null)
            {
                Conditions = new RuleIfAction[0];
            }
            else
            {
                Conditions = new RuleIfAction[ruleContext.rule_if().Length];
                for (int i = 0; i < Conditions.Length; i++)
                {
                    parseInfo.Script.AddCompletionRange(new CompletionRange(
                                                            scope,
                                                            DocRange.GetRange(ruleContext.rule_if(i).LEFT_PAREN(), ruleContext.rule_if(i).RIGHT_PAREN()),
                                                            CompletionRangeKind.Catch
                                                            ));

                    Conditions[i]     = new RuleIfAction(parseInfo.SetCallInfo(callInfo), scope, ruleContext.rule_if(i));
                    missingBlockRange = DocRange.GetRange(ruleContext.rule_if(i));
                }
            }

            // Get the block.
            if (ruleContext.block() != null)
            {
                Block = new BlockAction(parseInfo.SetCallInfo(callInfo), scope, ruleContext.block());
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Missing block.", missingBlockRange);
            }

            // Check restricted calls.
            callInfo.CheckRestrictedCalls(EventType);

            // Get the rule order priority.
            if (ruleContext.number() != null)
            {
                Priority = double.Parse(ruleContext.number().GetText());
            }

            ElementCountLens = new ElementCountCodeLens(ruleInfoRange, parseInfo.TranslateInfo.OptimizeOutput);
            parseInfo.Script.AddCodeLensRange(ElementCountLens);
        }
        public RuleAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Ow_ruleContext ruleContext)
        {
            Name               = Extras.RemoveQuotes(ruleContext.STRINGLITERAL().GetText());
            Disabled           = ruleContext.DISABLED() != null;
            _missingBlockRange = DocRange.GetRange(ruleContext.RULE_WORD());

            GetRuleSettings(parseInfo, scope, ruleContext);

            // Get the conditions.
            if (ruleContext.rule_if() == null)
            {
                Conditions = new RuleIfAction[0];
            }
            else
            {
                Conditions = new RuleIfAction[ruleContext.rule_if().Length];
                for (int i = 0; i < Conditions.Length; i++)
                {
                    parseInfo.Script.AddCompletionRange(new CompletionRange(
                                                            scope,
                                                            DocRange.GetRange(ruleContext.rule_if(i).LEFT_PAREN(), ruleContext.rule_if(i).RIGHT_PAREN()),
                                                            CompletionRangeKind.Catch
                                                            ));

                    Conditions[i]      = new RuleIfAction(parseInfo, scope, ruleContext.rule_if(i));
                    _missingBlockRange = DocRange.GetRange(ruleContext.rule_if(i));
                }
            }

            // Get the block.
            if (ruleContext.block() != null)
            {
                Block = new BlockAction(parseInfo, scope, ruleContext.block());
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Missing block.", _missingBlockRange);
            }

            // Get the rule order priority.
            if (ruleContext.number() != null)
            {
                Priority = double.Parse(ruleContext.number().GetText());
            }
        }
示例#5
0
        public override Node VisitOw_rule(DeltinScriptParser.Ow_ruleContext context)
        {
            string    name  = context.STRINGLITERAL().GetText().Trim('"');
            BlockNode block = (BlockNode)VisitBlock(context.block());

            IExpressionNode[] conditions      = new IExpressionNode[context.rule_if()?.expr().Length ?? 0];
            Range[]           conditionRanges = new Range[context.rule_if()?.expr().Length ?? 0];
            for (int i = 0; i < conditions.Length; i++)
            {
                conditions[i]      = (IExpressionNode)VisitExpr(context.rule_if().expr()[i]);
                conditionRanges[i] = Range.GetRange(context.rule_if().expr()[i]);
            }

            RuleEvent      eventType = RuleEvent.OngoingGlobal;
            TeamSelector   team      = TeamSelector.All;
            PlayerSelector player    = PlayerSelector.All;

            Range eventRange  = null;
            Range teamRange   = null;
            Range playerRange = null;

            foreach (var ruleOption in context.rule_option())
            {
                string option      = ruleOption.PART(0).GetText();
                Range  optionRange = Range.GetRange(ruleOption.PART(0).Symbol);

                string value      = null;
                Range  valueRange = null;
                if (ruleOption.PART().Length == 2)
                {
                    value      = ruleOption.PART(1).GetText();
                    valueRange = Range.GetRange(ruleOption.PART(1).Symbol);
                }

                switch (option)
                {
                case "Event":
                    if (!Enum.TryParse <RuleEvent>(value, out eventType))
                    {
                        _diagnostics.Add(new Diagnostic($"{value} is not a valid Event type.", valueRange));
                    }
                    eventRange = Range.GetRange(ruleOption);
                    break;

                case "Team":
                    if (!Enum.TryParse <TeamSelector>(value, out team))
                    {
                        _diagnostics.Add(new Diagnostic($"{value} is not a valid Team type.", valueRange));
                    }
                    teamRange = Range.GetRange(ruleOption);
                    break;

                case "Player":
                    if (!Enum.TryParse <PlayerSelector>(value, out player))
                    {
                        _diagnostics.Add(new Diagnostic($"{value} is not a valid Player type.", valueRange));
                    }
                    playerRange = Range.GetRange(ruleOption);
                    break;

                default:
                    _diagnostics.Add(new Diagnostic($"{value} is not a valid rule option.", optionRange));
                    break;
                }
            }

            var node = new RuleNode(name, eventType, team, player, conditions, block, eventRange, teamRange, playerRange, Range.GetRange(context));

            CheckRange(node);
            return(node);
        }
        public RuleNode(DeltinScriptParser.Ow_ruleContext context, BuildAstVisitor visitor) : base(new Location(visitor.file, Range.GetRange(context)))
        {
            Name  = context.STRINGLITERAL().GetText().Trim('"');
            Block = (BlockNode)visitor.VisitBlock(context.block());

            Conditions = new Node[context.rule_if().Length];
            Range[] conditionRanges = new Range          [context.rule_if().Length];

            for (int i = 0; i < context.rule_if().Length; i++)
            {
                if (context.rule_if(i).expr() != null)
                {
                    Conditions[i] = visitor.VisitExpr(context.rule_if(i).expr());
                }

                // Get the range between the ().
                conditionRanges[i] = Range.GetRange(
                    context.rule_if(i).LEFT_PAREN().Symbol,
                    context.rule_if(i).RIGHT_PAREN().Symbol
                    );
            }

            RuleEvent      eventType = RuleEvent.OngoingGlobal;
            Team           team      = Team.All;
            PlayerSelector player    = PlayerSelector.All;

            Range eventRange  = null;
            Range teamRange   = null;
            Range playerRange = null;

            foreach (var ruleOption in context.@enum())
            {
                string option      = ruleOption.PART(0).GetText();
                Range  optionRange = Range.GetRange(ruleOption.PART(0).Symbol);

                string value      = ruleOption.PART(1)?.GetText();
                Range  valueRange = null;
                if (value != null)
                {
                    valueRange = Range.GetRange(ruleOption.PART(1).Symbol);
                }

                Range totalRange;
                if (ruleOption.PART(1) != null)
                {
                    totalRange = Range.GetRange(ruleOption.PART(0).Symbol, ruleOption.PART(1).Symbol);
                }
                else
                {
                    totalRange = Range.GetRange(ruleOption.PART(0));
                }

                switch (option)
                {
                case "Event":
                    if (eventRange != null)
                    {
                        visitor._diagnostics.Error("Event already set.", new Location(visitor.file, totalRange));
                    }

                    if (!Enum.TryParse <RuleEvent>(value, out eventType))
                    {
                        visitor._diagnostics.Error($"{value} is not a valid Event type.", new Location(visitor.file, valueRange));
                    }

                    eventRange = Range.GetRange(ruleOption);
                    break;

                case "Team":
                    if (teamRange != null)
                    {
                        visitor._diagnostics.Error("Team already set.", new Location(visitor.file, totalRange));
                    }

                    if (!Enum.TryParse <Team>(value, out team))
                    {
                        visitor._diagnostics.Error($"{value} is not a valid Team type.", new Location(visitor.file, valueRange));
                    }

                    teamRange = Range.GetRange(ruleOption);
                    break;

                case "Player":
                    if (playerRange != null)
                    {
                        visitor._diagnostics.Error("Player already set.", new Location(visitor.file, totalRange));
                    }

                    if (!Enum.TryParse <PlayerSelector>(value, out player))
                    {
                        visitor._diagnostics.Error($"{value} is not a valid Player type.", new Location(visitor.file, valueRange));
                    }

                    playerRange = Range.GetRange(ruleOption);
                    break;

                default:
                    visitor._diagnostics.Error($"{option} is not a valid rule option.", new Location(visitor.file, optionRange));
                    break;
                }
            }
            Event  = eventType;
            Team   = team;
            Player = player;

            SubRanges = ArrayBuilder <Range> .Build(eventRange, teamRange, playerRange, conditionRanges);
        }