예제 #1
0
        public override ParserMatch TryMatch(IScanner scan)
        {
            int offset = scan.Offset;

            var m = new ParserMatch(this, scan, offset, -1);

            while (!scan.EndOfInput)
            {
                offset = scan.Offset;

                var a = LeftParser.Parse(scan);

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

                var b = RightParser.Parse(scan);

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

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

            return m;
        }
예제 #2
0
        public override ParserMatch TryMatch(IScanner scan)
        {
            if (Parser == this) return scan.NoMatch;

            // save scanner state
            int offset = scan.Offset;

            var m = new ParserMatch(this, scan, 0, 0); // empty match with this parser

            // execution bound
            int count = 0;

            // lower bound, minimum number of executions
            while (count < LowerBound && !scan.EndOfInput)
            {
                var m_temp = Parser.Parse(scan);
                if (!m_temp.Success) break; // stop if not successful
                count++;
                m.AddSubmatch(m_temp);
            }

            if (count == LowerBound)
            {
                while (count < UpperBound && !scan.EndOfInput)
                {
                    var m_temp = Parser.Parse(scan);
                    if (!m_temp.Success) break; // stop if not successful
                    count++;
                    m.AddSubmatch(m_temp);
                }
            }
            else
            {
                m = scan.NoMatch;
            }

            if (m == null) m = scan.NoMatch;

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

            return m;
        }