Exemplo n.º 1
0
        protected override IEnumerable <RuleMatch> OnMatch(ExplorerContext context)
        {
            var primaryMatches = context.InvokeRule(_primary);

            foreach (var primaryMatch in primaryMatches)
            {
                var primaryText         = primaryMatch.Text;
                var excludingContext    = context.SubContext(primaryText.Length);
                var excludedMatches     = excludingContext.InvokeRule(_excluded);
                var excludedExactLength = from ex in excludedMatches
                                          where ex.Text.Length == primaryText.Length
                                          select ex;

                if (!excludedExactLength.Any())
                {
                    var match = new RuleMatch(
                        this,
                        primaryText,
                        () => RuleOutput.ComputeOutput(
                            primaryText,
                            new Lazy <object?>(() => primaryMatch.ComputeOutput())));

                    yield return(match);
                }
            }
        }
Exemplo n.º 2
0
        protected override IEnumerable <RuleMatch> OnMatch(ExplorerContext context)
        {
            var text = context.Text;

            if (text.HasContent)
            {
                var peekRaw = text.First();
                var peek    = IsCaseSensitive == false?char.ToUpperInvariant(peekRaw) : peekRaw;

                var first = IsCaseSensitive == false?char.ToUpperInvariant(First) : First;

                var last = IsCaseSensitive == false?char.ToUpperInvariant(Last) : Last;

                if (peek >= first && peek <= last)
                {
                    var matchText = text.Take(1);
                    var match     = new RuleMatch(
                        this,
                        matchText,
                        () => RuleOutput.ComputeOutput(matchText, new Lazy <object?>(matchText)));

                    return(new[] { match });
                }
            }

            return(RuleMatch.EmptyMatch);
        }
Exemplo n.º 3
0
 public void WriteResult(RuleMatch result)
 {
     Logger.Log($"Rule : {result.Rule.Caption}");
     Logger.Log($"File : {result.File}");
     Logger.Log($"Match: {result.Line}");
     Logger.Log($"Line : {result.LineNumber}");
     Logger.Log("-------------------------------");
 }
Exemplo n.º 4
0
        public SingleOutputModel(
            RuleMatch match,
            TimeSpan grammarDuration,
            TimeSpan matchDuration)
        {
            IsMatch = match != null;
            if (match != null)
            {
                Rule = match.Rule.RuleName;
                Text = match.Text.ToString();
                //  To force SubString to be serialized into strings
                Output = match.ComputeTypedOutput <object>();
            }

            Duration = new DurationModel(grammarDuration, matchDuration);
        }
Exemplo n.º 5
0
        private IEnumerable <RuleMatch> RecurseMatch(
            //  Used only for debugging purposes, to hook on the context ID of the entire sequence
            int masterContextID,
            ExplorerContext context,
            SubString originalText,
            int totalMatchLength,
            int iteration,
            ImmutableList <RuleMatch> childrenMatches)
        {
            var matches         = context.InvokeRule(_rule);
            var nonEmptyMatches = matches.Where(m => m.Text.Length != 0);

            foreach (var match in nonEmptyMatches)
            {
                var newTotalMatchLength = totalMatchLength + match.LengthWithInterleaves;
                var newChildrenMatches  = childrenMatches.Add(match);

                if (IsRepeatCountBelowMaximum(iteration + 1))
                {   //  Recurse to next iteration
                    var newContext        = context.MoveForward(match);
                    var downstreamMatches = RecurseMatch(
                        masterContextID,
                        newContext,
                        originalText,
                        newTotalMatchLength,
                        iteration + 1,
                        newChildrenMatches);

                    foreach (var m in downstreamMatches)
                    {
                        yield return(m);
                    }
                }
                //  We are returning the matches in decreasing order of text length, so the "current" one goes last
                if (IsRepeatCountInRange(iteration))
                {
                    var matchText     = originalText.Take(newTotalMatchLength);
                    var completeMatch = new RuleMatch(
                        this,
                        matchText,
                        () => ComputeOutput(matchText, newChildrenMatches));

                    yield return(completeMatch);
                }
            }
        }
Exemplo n.º 6
0
        protected override IEnumerable <RuleMatch> OnMatch(ExplorerContext context)
        {
            var text = context.Text;

            if (text.Length == 0)
            {
                return(RuleMatch.EmptyMatch);
            }
            else
            {
                var matchText = text.Take(1);
                var match     = new RuleMatch(
                    this,
                    matchText,
                    () => RuleOutput.ComputeOutput(matchText, new Lazy <object?>(matchText)));

                return(new[] { match });
            }
        }
Exemplo n.º 7
0
        protected override IEnumerable <RuleMatch> OnMatch(ExplorerContext context)
        {
            var text = context.Text;

            if (text.HasContent &&
                text.Length >= _literal.Length &&
                text.Take(_literal.Length).SequenceEqual(_literal, GetCharComparer()))
            {
                var matchText = text.Take(_literal.Length);
                var match     = new RuleMatch(
                    this,
                    matchText,
                    () => RuleOutput.ComputeOutput(
                        matchText,
                        new Lazy <object?>(() => matchText)));

                return(new[] { match });
            }
            else
            {
                return(RuleMatch.EmptyMatch);
            }
        }
Exemplo n.º 8
0
Arquivo: Dc.cs Projeto: alain1337/Dc
        void Preprocess(List <RuleMatch> rules)
        {
            RuleMatch prevRule = null;

            for (var i = 0; i < rules.Count; i++)
            {
                if (rules[i].Rule.Type == GrammarTokenType.Operator && (prevRule == null ||
                                                                        (prevRule.Rule.Type == GrammarTokenType.Operator && prevRule.Rule.Operands == 2) ||
                                                                        (prevRule.Rule.Type == GrammarTokenType.Operator && prevRule.Rule.Operands == 1 && prevRule.Rule.Associativity == GrammarTokenAssociativity.Right) ||
                                                                        (prevRule.Rule.Type == GrammarTokenType.LeftParenthesis)))
                {
                    if (rules[i].Lexem.Text == "-")
                    {
                        rules[i] = _sy.Match(new Lexem("u-", typeof(string), Kinds.Operator));
                    }
                    else if (rules[i].Lexem.Text == "+")
                    {
                        rules[i] = _sy.Match(new Lexem("u+", typeof(string), Kinds.Operator));
                    }
                }

                prevRule = rules[i];
            }
        }