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);
            }
        }
예제 #2
0
        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;
            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);
        }
        private void GetRuleSettings(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Ow_ruleContext ruleContext)
        {
            DeltinScriptParser.ExprContext eventContext  = null;
            DeltinScriptParser.ExprContext teamContext   = null;
            DeltinScriptParser.ExprContext playerContext = null;

            foreach (var exprContext in ruleContext.expr())
            {
                missingBlockRange = DocRange.GetRange(exprContext);

                EnumValuePair enumSetting = (ExpressionTree.ResultingExpression(parseInfo.GetExpression(scope, exprContext)) as CallVariableAction)?.Calling as EnumValuePair;
                EnumData      enumData    = enumSetting?.Member.Enum;

                if (enumData == null || !ValidRuleEnums.Contains(enumData))
                {
                    parseInfo.Script.Diagnostics.Error("Expected enum of type " + string.Join(", ", ValidRuleEnums.Select(vre => vre.CodeName)) + ".", DocRange.GetRange(exprContext));
                }
                else
                {
                    var alreadySet = new Diagnostic("The " + enumData.CodeName + " rule setting was already set.", DocRange.GetRange(exprContext), Diagnostic.Error);

                    // Get the Event option.
                    if (enumData == EnumData.GetEnum <RuleEvent>())
                    {
                        if (_setEventType)
                        {
                            parseInfo.Script.Diagnostics.AddDiagnostic(alreadySet);
                        }
                        EventType     = (RuleEvent)enumSetting.Member.Value;
                        _setEventType = true;
                        eventContext  = exprContext;
                    }
                    // Get the Team option.
                    if (enumData == EnumData.GetEnum <Team>())
                    {
                        if (_setTeam)
                        {
                            parseInfo.Script.Diagnostics.AddDiagnostic(alreadySet);
                        }
                        Team        = (Team)enumSetting.Member.Value;
                        _setTeam    = true;
                        teamContext = exprContext;
                    }
                    // Get the Player option.
                    if (enumData == EnumData.GetEnum <PlayerSelector>())
                    {
                        if (_setPlayer)
                        {
                            parseInfo.Script.Diagnostics.AddDiagnostic(alreadySet);
                        }
                        Player        = (PlayerSelector)enumSetting.Member.Value;
                        _setPlayer    = true;
                        playerContext = exprContext;
                    }
                }
            }

            // Syntax error if changing the Team type when the Event type is set to Global.
            if (_setEventType && EventType == RuleEvent.OngoingGlobal)
            {
                if (Team != Team.All)
                {
                    parseInfo.Script.Diagnostics.Error("Can't change rule Team type with an event type of Ongoing Global.", DocRange.GetRange(teamContext));
                }
                if (Player != PlayerSelector.All)
                {
                    parseInfo.Script.Diagnostics.Error("Can't change rule Player type with an event type of Ongoing Global.", DocRange.GetRange(playerContext));
                }
            }
        }
예제 #7
0
        //private bool CreateInitialSkip = false;
        //private int SkipCountIndex = -1;

        public ParseRule(DeltinScriptParser.Ow_ruleContext ruleContext)
        {
            Rule        = CreateRuleFromContext(ruleContext);
            RuleContext = ruleContext;
            IsGlobal    = Rule.RuleEvent == RuleEvent.Ongoing_Global;
        }
예제 #8
0
        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);
        }
예제 #9
0
 public override Node VisitOw_rule(DeltinScriptParser.Ow_ruleContext context)
 {
     return(new RuleNode(context, this));
 }