Ejemplo n.º 1
0
        /// <summary>
        /// Inner parse method
        /// </summary>
        /// <param name="scanner">scanner</param>
        /// <returns>the match</returns>
        protected override ParserMatch ParseMain(IScanner scanner)
        {
            long offset = scanner.Offset;

            ParserMatch m = FirstParser.Parse(scanner);

            if (!m.Success)
            {
                scanner.Seek(offset);
                return(m);
            }

            while (!scanner.AtEnd)
            {
                offset = scanner.Offset;

                ParserMatch b = SecondParser.Parse(scanner);
                if (!b.Success)
                {
                    scanner.Seek(offset);
                    return(m);
                }
                ParserMatch a = FirstParser.Parse(scanner);
                if (!a.Success)
                {
                    scanner.Seek(offset);
                    return(m);
                }

                m.Concat(b);
                m.Concat(a);
            }

            return(m);
        }
Ejemplo n.º 2
0
        public override SyntaxTreeNode Parse(string text)
        {
            var firstResult = FirstParser.Parse(text);

            if (firstResult is null)
            {
                return(null);
            }

            var secondResult = SecondParser.Parse(firstResult.Rest);

            if (secondResult is null)
            {
                return(null);
            }

            var children = new List <SyntaxTreeNode> {
                firstResult, secondResult
            };
            // [todo] change it on concatination
            var parsedText = secondResult.Rest != string.Empty ?
                             text.Replace(secondResult.Rest, "") :
                             text;

            return(new SyntaxTreeNode(parsedText, secondResult.Rest, this, children));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Inner parse method
        /// </summary>
        /// <param name="scanner">scanner</param>
        /// <returns>the match</returns>
        protected override ParserMatch ParseMain(IScanner scanner)
        {
            // save scanner state
            long offset = scanner.Offset;

            // apply the first parser
            ParserMatch m = FirstParser.Parse(scanner);

            // if m1 successful, do m2
            if (m.Success)
            {
                ParserMatch m2 = SecondParser.Parse(scanner);
                if (m2.Success)
                {
                    m.Concat(m2);
                }
                else
                {
                    m = ParserMatch.CreateFailureMatch(scanner, offset);
                }
            }

            // restoring parser failed, rewind scanner
            if (!m.Success)
            {
                scanner.Seek(offset);
            }

            return(m);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Inner parse method
        /// </summary>
        /// <param name="scanner">scanner</param>
        /// <returns>the match</returns>
        protected override ParserMatch ParseMain(IScanner scanner)
        {
            // save scanner state
            long offset = scanner.Offset;

            // apply the first parser
            ParserMatch m = FirstParser.Parse(scanner);

            // if m1 successful, do m2
            if (m.Success)
            {
                return(m);
            }

            // not found try the next
            scanner.Seek(offset);

            // apply the second parser
            m = SecondParser.Parse(scanner);
            if (m.Success)
            {
                return(m);
            }

            scanner.Seek(offset);
            return(ParserMatch.CreateFailureMatch(scanner));
        }
Ejemplo n.º 5
0
            public override Parser <TInput> VisitFirst(FirstParser <TInput> parser)
            {
                if (state.State == 0)
                {
                    state.State = 1;
                    return(parser.Parsers[0]);
                }
                else if (state.LastResult < 0)
                {
                    if (state.LastResult < state.BestFailedResult)
                    {
                        state.BestFailedResult = state.LastResult;
                    }

                    if (state.State < parser.Parsers.Count)
                    {
                        var next = parser.Parsers[state.State];
                        state.State++;
                        return(next);
                    }
                    else
                    {
                        state.InputLength = state.BestFailedResult;
                        return(null);
                    }
                }
                else
                {
                    state.InputLength = state.LastResult;
                    return(null);
                }
            }
Ejemplo n.º 6
0
 public override void InitGrammar(Grammar grammar, string ruleName)
 {
     Grammar  = grammar;
     RuleName = ruleName;
     FirstParser.InitGrammar(grammar, ruleName);
     SecondParser.InitGrammar(grammar, ruleName);
 }
Ejemplo n.º 7
0
        public void Parse_Test()
        {
            var parser = new FirstParser <char, char>(new[]
            {
                Match('a'),
                Match('X'),
                Match('1')
            });

            parser.CanMatch("a").Should().BeTrue();
            parser.CanMatch("X").Should().BeTrue();
            parser.CanMatch("1").Should().BeTrue();

            parser.CanMatch("b").Should().BeFalse();
        }
Ejemplo n.º 8
0
        private void BtnMargeClick(object sender, EventArgs e)
        {
            listPreview.Items.Clear();

            var listMergedQuote = QuoteParseManager.Merge(listResultContent.Items.Cast <string>().ToList());

            if (rbtnFirstParserSaveFormat.Checked)
            {
                ListStringMergedQuotes = SecondParser.QuoteListToString(listMergedQuote).ToArray();
            }
            if (rbtnSecondParserSaveFormat.Checked)
            {
                ListStringMergedQuotes = FirstParser.QuoteListToString(listMergedQuote).ToArray();
            }

            listPreview.Items.AddRange(ListStringMergedQuotes);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Inner parse method
        /// </summary>
        /// <param name="scanner">scanner</param>
        /// <returns>the match</returns>
        protected override ParserMatch ParseMain(IScanner scanner)
        {
            long        offset = scanner.Offset;
            ParserMatch m      = FirstParser.Parse(scanner);

            if (m.Success)
            {
                StringScanner firstMatch = new StringScanner(m.Value);
                ParserMatch   m2         = SecondParser.Parse(firstMatch);
                if (m2.Success)
                {
                    return(m);
                }
            }

            scanner.Seek(offset);
            return(ParserMatch.CreateFailureMatch(scanner));
        }
Ejemplo n.º 10
0
    private bool Accept <TInput, TOutput>(FirstParser <TInput, TOutput> p, BnfStringifyVisitor state)
    {
        var children = p.GetChildren().ToList();

        if (children.Count == 1)
        {
            state.Append(children[0]);
            return(true);
        }

        state.Append("(", children[0]);

        for (int i = 1; i <= children.Count - 1; i++)
        {
            state.Append(" | ", children[i]);
        }

        state.Append(')');
        return(true);
    }
Ejemplo n.º 11
0
        /// <summary>
        /// Inner parse method
        /// </summary>
        /// <param name="scanner">scanner</param>
        /// <returns>the match</returns>
        protected override ParserMatch ParseMain(Scanners.IScanner scanner)
        {
            ParserMatch m = FirstParser.TryAccept(scanner);

            if (!m.Success)
            {
                return(m);
            }

            // doing difference
            ParserMatch d = SecondParser.TryAccept(scanner);

            if (d.Success)
            {
                if (d.Length >= m.Length)
                {
                    return(ParserMatch.CreateFailureMatch(scanner, m.Offset, 0));
                }
            }

            // ok
            FirstParser.Parse(scanner);
            return(m);
        }
Ejemplo n.º 12
0
            public override Parser <TInput> VisitFirst(FirstParser <TInput> parser)
            {
                if (state.State == 0)
                {
                    state.State           = 1;
                    state.NextOutputStart = state.OutputStart;
                    return(parser.Parsers[0]);
                }
                else if (state.LastResult < 0)
                {
                    if (state.LastResult < state.BestFailedResult)
                    {
                        state.BestFailedResult = state.LastResult;
                    }

                    output.SetCount(state.OriginalOutputCount);

                    if (state.State < parser.Parsers.Count)
                    {
                        var next = parser.Parsers[state.State];
                        state.State++;
                        state.NextOutputStart = state.OutputStart;
                        return(next);
                    }
                    else
                    {
                        state.InputLength = state.BestFailedResult;
                        return(null);
                    }
                }
                else
                {
                    state.InputLength = state.LastResult;
                    return(null);
                }
            }
Ejemplo n.º 13
0
        /// <summary>
        /// Inner parse method
        /// match (first but not second) or (second but not first)
        /// </summary>
        /// <param name="scanner">scanner</param>
        /// <returns>the match</returns>
        protected override ParserMatch ParseMain(Scanners.IScanner scanner)
        {
            long startOffset = scanner.Offset;

            ParserMatch m1      = FirstParser.Parse(scanner);
            long        offset1 = scanner.Offset;

            scanner.Seek(startOffset);

            ParserMatch m2 = SecondParser.Parse(scanner);

            if (m1.Success && !m2.Success)
            {
                scanner.Seek(offset1);
                return(m1);
            }
            if (m2.Success && !m1.Success)
            {
                return(m2);
            }

            scanner.Seek(startOffset);
            return(ParserMatch.CreateFailureMatch(scanner));
        }
Ejemplo n.º 14
0
 public override int VisitFirst(FirstParser <TInput> parser, int start)
 {
     return(VisitFirst(parser.Parsers, start));
 }
Ejemplo n.º 15
0
        public override SyntaxTreeNode Parse(string text)
        {
            var pResult = FirstParser.Parse(text) ?? SecondParser.Parse(text);

            return(pResult);
        }
 public override void VisitFirst(FirstParser <TInput> parser)
 {
     WriteAlternation(parser.Parsers);
 }
 public override Parser <TInput> VisitFirst(FirstParser <TInput> parser)
 {
     return(NextBest(parser.Parsers));
 }