Beispiel #1
0
        private bool LoadFile(string file)
        {
            RuleSet rules     = new RuleSet();
            bool    noProblem = true;

            rules.OnDeserializationError += delegate(object?sender, Newtonsoft.Json.Serialization.ErrorEventArgs e)
            {
                ErrorMessage message = new ErrorMessage(File: file,
                                                        Message: e.ErrorContext.Error.Message,
                                                        Path: e.ErrorContext.Path);

                if (e.ErrorContext.OriginalObject is Rule r && !string.IsNullOrEmpty(r.Id))
                {
                    message.RuleID = r.Id;
                }

                // Newtonsoft json throws some errors twice
                if (_messages.FirstOrDefault(x => (x.Message == message.Message && x.File == file)) == null)
                {
                    _messages.Add(message);
                }

                noProblem = false;
                e.ErrorContext.Handled = true;
            };

            rules.AddFile(file, null);

            if (noProblem)
            {
                _rules.AddRange(rules.AsEnumerable());
            }

            return(noProblem);
        }
        private void LoadFile(string file)
        {
            RuleSet rules = new RuleSet();

            try
            {
                rules.AddFile(file, null);
            }
            catch (Exception e)
            {
                Debug.Write(e.Message);//Ensure console message indicates problem for Build process
                WriteOnce.SafeLog(e.Message, NLog.LogLevel.Error);
                throw new Exception(ErrMsg.FormatString(ErrMsg.ID.VERIFY_RULE_FAILED, file));
            }

            _rules.AddRange(rules.AsEnumerable());
        }
Beispiel #3
0
        public void AddRuleRangeTest()
        {
            RuleSet rules = RuleSet.FromDirectory(Path.Combine("rules", "valid"), null);

            // Add Range
            RuleSet testRules = new RuleSet();

            testRules.AddRange(rules.ByLanguages(new string[] { "javascript" }));
            Assert.IsTrue(testRules.Count() > 0, "AddRange testRules is empty");

            // Add Rule
            testRules = new RuleSet();
            IEnumerable <Rule> list = rules.ByLanguages(new string[] { "javascript" });

            foreach (Rule r in list)
            {
                testRules.AddRule(r);
            }

            Assert.IsTrue(testRules.Count() > 0, "AddRule testRules is empty");
        }
Beispiel #4
0
        public object BuildFromNode(AstBuilder.AstNode astNode)
        {
            switch (astNode.Node)
            {
            case RuleType.RuleDefinitionElement:
            {
                var core            = astNode.GetAllSubNodes().First();
                var multiplierToken = astNode.GetAllSubNodes().Skip(1).OfType <Token>().FirstOrDefault();
                var multiplier      = Multilicator.One;
                if (multiplierToken != null)
                {
                    multiplier = multiplierToken.TokenType == TokenType.AnyMultiplier
                            ? Multilicator.Any
                            : multiplierToken.TokenType == TokenType.OneOrMoreMultiplier
                                ? Multilicator.OneOrMore
                                : multiplierToken.TokenType == TokenType.OneOrZeroMultiplier
                                    ? Multilicator.OneOrZero
                                    : Multilicator.One;
                }
                var ruleElement = new RuleElement(core is Rule ? ((Rule)core).Name : ((Token)core).Text, multiplier);
                return(ruleElement);
            }

            case RuleType.Parameter:
            {
                var paramName = astNode.GetAllSubNodes().Cast <Token>().First().Text;
                var values    = astNode.GetAllSubNodes().Skip(1).Cast <Token>().Select(t => (object)t.Text).ToArray();
                return(new KeyValuePair <string, object>(paramName, values.Length == 1 ? values[0] : values));
            }

            case RuleType.Settings:
            {
                var parameters = astNode.GetAllSubNodes().Cast <KeyValuePair <string, object> >().ToArray();
                return(parameters.ToDictionary(p => p.Key, p => p.Value));
            }

            case RuleType.AlternateElement:
            {
                var alternatives = astNode.GetAllSubNodes().ToArray();
                return(AddTempRule(new AlternateRule(string.Format("__alt{0}", counter++),
                                                     alternatives.Cast <Token>().Select(t => t.Text).ToArray())));
            }

            case RuleType.GroupElement:
            {
                var alternatives = astNode.GetAllSubNodes().Cast <RuleElement>().ToArray();
                return(AddTempRule(new SequenceRule(string.Format("__grp{0}", counter++), alternatives)));
            }

            case RuleType.RuleDefinition:
            {
                var ruleElements = astNode.GetAllSubNodes().OfType <RuleElement>().ToArray();
                var ruleName     = ((Token)astNode.GetAllSubNodes().First()).Text;
                var isProduction = astNode.GetAllSubNodes().Skip(1).OfType <Token>().Any(t => t.TokenType == TokenType.Production);
                var lookAhead    = astNode.GetAllSubNodes().OfType <LookAheadHint>().FirstOrDefault();
                var rule         = firstRule ? new StartRule(ruleName, ruleElements) : new SequenceRule(ruleName, ruleElements);
                if (!isProduction)
                {
                    rule.IgnoreProduction();
                }

                rule.LookAheadHint = lookAhead;
                firstRule          = false;
                return(rule);
            }

            case RuleType.LookAheadTokenList:
            {
                var tokens = astNode.GetAllSubNodes().Cast <Token>().ToArray();
                return(tokens);
            }

            case RuleType.LookAhead:
            {
                var items    = astNode.GetAllSubNodes().Cast <Token[]>().ToArray();
                var expected = items.Last().Select(t => new Berp.TokenType(t.Text.Substring(1))).ToArray();
                var skip     = items.Length > 1 ? items[0].Select(t => new Berp.TokenType(t.Text.Substring(1))).ToArray() : new Berp.TokenType[0];
                return(new LookAheadHint(expected, skip));
            }

            case RuleType.Grammar:
            {
                var rules    = astNode.GetAllSubNodes().OfType <Rule>().ToArray();
                var settings = astNode.GetAllSubNodes().OfType <Dictionary <string, object> >().FirstOrDefault();
                var ruleSet  = new RuleSet(settings);
                ruleSet.AddRange(rules);
                ruleSet.AddRange(tempRules);
                ruleSet.Resolve();
                return(ruleSet);
            }
            }

            throw new Exception("unknown node: " + astNode.Node);
        }