예제 #1
0
 public RuleMatch(
     IRule rule,
     SubString text,
     Func <object?> outputFactory)
     : this(rule, text, text.Length, outputFactory)
 {
 }
예제 #2
0
        private static object ExtractDefaultOutput(
            SubString text,
            IEnumerable <RuleMatch> children,
            IImmutableDictionary <string, RuleMatch> namedChildren)
        {
            if (namedChildren != null && namedChildren.Count != 0)
            {
                var components = from c in namedChildren
                                 let output = c.Value.ComputeOutput()
                                              select KeyValuePair.Create(c.Key, output);

                var obj = ImmutableDictionary <string, object> .Empty.AddRange(components);

                return(obj);
            }
            else if (children != null)
            {
                var outputs = from c in children
                              select c.ComputeOutput();

                var obj = outputs.ToArray();

                return(obj);
            }
            else
            {
                return(text.ToString());
            }
        }
예제 #3
0
 public ExplorerContext(
     SubString text,
     IRule?interleaveRule = null,
     int?depth            = null)
     : this(
         text,
         interleaveRule,
         false,
         depth ?? DEFAULT_MAX_DEPTH,
         ImmutableHashSet <IRule> .Empty,
         new AmbiantRuleProperties())
 {
 }
예제 #4
0
        private RuleMatch(
            IRule rule,
            SubString text,
            int lengthWithInterleaves,
            Func <object?> outputFactory)
        {
            if (lengthWithInterleaves < text.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(lengthWithInterleaves));
            }

            Rule = rule ?? throw new ArgumentNullException(nameof(rule));
            Text = text;
            LengthWithInterleaves = lengthWithInterleaves;
            _outputFactory        = outputFactory;
        }
예제 #5
0
 private ExplorerContext(
     SubString text,
     IRule?interleaveRule,
     bool isInterleaveMatched,
     int depth,
     IImmutableSet <IRule> ruleNameExcepts,
     AmbiantRuleProperties ambiantRuleProperties)
 {
     if (depth <= 0)
     {
         throw new ArgumentOutOfRangeException(nameof(depth));
     }
     _interleaveRule        = interleaveRule;
     _isInterleaveMatched   = isInterleaveMatched;
     _ruleExcepts           = ruleNameExcepts;
     _ambiantRuleProperties = ambiantRuleProperties;
     Text      = text;
     Depth     = depth;
     ContextID = Guid.NewGuid().GetHashCode();
 }
예제 #6
0
        public RuleMatch Match(string?ruleName, SubString text, int?depth = null)
        {
            if (string.IsNullOrWhiteSpace(ruleName))
            {
                ruleName = DEFAULT_RULE_NAME;
            }
            if (!_ruleMap.ContainsKey(ruleName))
            {
                throw new ParsingException($"Unknown rule to match:  '{ruleName}'");
            }

            var rule               = _ruleMap[ruleName];
            var context            = new ExplorerContext(text, _interleaveRule, depth);
            var matches            = GetMatches(rule, context);
            var exactLengthMatches = from m in matches
                                     where m.LengthWithInterleaves == text.Length
                                     select m;
            //  Take the first available match (of right length)
            var match = exactLengthMatches.FirstOrDefault();

            return(match);
        }