/// <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);
        }
Beispiel #2
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));
        }
        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));
        }
Beispiel #4
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);
        }
        /// <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));
        }
Beispiel #6
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);
        }
Beispiel #7
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));
        }
Beispiel #8
0
        public override SyntaxTreeNode Parse(string text)
        {
            var pResult = FirstParser.Parse(text) ?? SecondParser.Parse(text);

            return(pResult);
        }