示例#1
0
        /// <summary>
        /// Grab directives
        /// </summary>
        internal bool Parse()
        {
            while (firstCodeLine < ruleCode.Length &&
                   ruleCode[firstCodeLine].Length > 0 &&
                   ruleCode[firstCodeLine][0] == '.')
            {
                string directive = ruleCode[firstCodeLine].Substring(1);
                var    parts     = directive.Split('=');

                switch (parts[0].ToLowerInvariant())
                {
                case "lang":
                case "language":
                    if (parts.Length < 2)
                    {
                        logger.WriteWarning($"Unrecognized directive {directive}");
                        return(false);
                    }
                    else
                    {
                        switch (parts[1].ToUpperInvariant())
                        {
                        case "C#":
                        case "CS":
                        case "CSHARP":
                            Language = Languages.Csharp;
                            break;

                        default:
                            logger.WriteWarning($"Unrecognized language {parts[1]}");
                            return(false);
                        }
                    }
                    break;

                default:
                    logger.WriteWarning($"Unrecognized directive {directive}");
                    return(false);
                }//switch

                firstCodeLine++;
            }//while
            return(true);
        }
示例#2
0
 void FailParsingWithMessage(string message)
 {
     logger?.WriteWarning(message);
     parsingIssues = true;
 }
示例#3
0
        /// <summary>
        /// Grab directives
        /// </summary>
        public static (IPreprocessedRule preprocessedRule, bool parseSuccess) Read(string[] ruleCode, IAggregatorLogger logger = default)
        {
            var parsingIssues = false;

            void FailParsingWithMessage(string message)
            {
                logger?.WriteWarning(message);
                parsingIssues = true;
            }

            var directiveLineIndex = 0;
            var preprocessedRule   = new PreprocessedRule()
            {
                Language = RuleLanguage.Csharp
            };

            while (directiveLineIndex < ruleCode.Length &&
                   ruleCode[directiveLineIndex].Length > 0 &&
                   ruleCode[directiveLineIndex][0] == '.')
            {
                string directive = ruleCode[directiveLineIndex].Substring(1);
                var    parts     = directive.Split('=');

                switch (parts[0].ToLowerInvariant())
                {
                case "lang":
                case "language":
                    if (parts.Length < 2)
                    {
                        FailParsingWithMessage($"Invalid language directive {directive}");
                    }
                    else
                    {
                        switch (parts[1].ToUpperInvariant())
                        {
                        case "C#":
                        case "CS":
                        case "CSHARP":
                            preprocessedRule.Language = RuleLanguage.Csharp;
                            break;

                        default:
                        {
                            FailParsingWithMessage($"Unrecognized language {parts[1]}");
                            preprocessedRule.Language = RuleLanguage.Unknown;
                            break;
                        }
                        }
                    }
                    break;

                case "r":
                case "ref":
                case "reference":
                    if (parts.Length < 2)
                    {
                        FailParsingWithMessage($"Invalid reference directive {directive}");
                    }
                    else
                    {
                        preprocessedRule.References.Add(parts[1]);
                    }
                    break;

                case "import":
                case "imports":
                case "namespace":
                    if (parts.Length < 2)
                    {
                        FailParsingWithMessage($"Invalid import directive {directive}");
                    }
                    else
                    {
                        preprocessedRule.Imports.Add(parts[1]);
                    }
                    break;

                case "impersonate":
                    if (parts.Length < 2)
                    {
                        FailParsingWithMessage($"Invalid impersonate directive {directive}");
                    }
                    else
                    {
                        preprocessedRule.Impersonate = string.Equals("onBehalfOfInitiator", parts[1].TrimEnd(), StringComparison.OrdinalIgnoreCase);
                    }
                    break;

                default:
                {
                    FailParsingWithMessage($"Unrecognized directive {directive}");
                    break;
                }
                }//switch

                // this keep the same number of lines
                preprocessedRule.RuleCode.Add($"// {directive}");

                directiveLineIndex++;
            }//while

            preprocessedRule.FirstCodeLine = directiveLineIndex;

            preprocessedRule.RuleCode.AddRange(ruleCode.Skip(preprocessedRule.FirstCodeLine));

            var parseSuccessful = !parsingIssues;

            return(preprocessedRule, parseSuccessful);
        }