コード例 #1
0
        //*
        // * Returns the length of the maximum number of elements matching
        // * the string starting at the specified position. This method
        // * allows no backtracking, i.e. no skips..
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param count the start count, normally zero (0)
        // *
        // * @return the length of the longest matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        private int MatchPossessive(Matcher m, LookAheadReader input, int start, int count)
        {
            int length    = 0;
            int subLength = 1;

            // Match as many elements as possible
            while (subLength > 0 && count < max)
            {
                subLength = elem.Match(m, input, start + length, 0);
                if (subLength >= 0)
                {
                    count  += 1;
                    length += subLength;
                }
            }

            // Return result
            if (min <= count && count <= max)
            {
                return(length);
            }
            else
            {
                return(-1);
            }
        }
コード例 #2
0
        //*
        // * Returns the length of a matching string starting at the
        // * specified position. The number of matches to skip can also
        // * be specified, but numbers higher than zero (0) cause a
        // * failed match for any element that doesn't attempt to
        // * combine other elements.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the longest matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        public override int Match(Matcher m, LookAheadReader input, int start, int skip)
        {
            int c = 0;

            if (skip != 0)
            {
                return(-1);
            }
            for (int i = 0; i <= value.Length - 1; i++)
            {
                c = input.Peek(start + i);
                if (c < 0)
                {
                    m.SetReadEndOfString();
                    return(-1);
                }
                if (m.IsCaseInsensitive())
                {
                    c = Convert.ToInt32(Char.ToLower(Convert.ToChar(c)));
                }
                if (c != Convert.ToInt32(value[i]))
                {
                    return(-1);
                }
            }
            return(value.Length);
        }
コード例 #3
0
        //*
        // * Returns the length of the longest possible matching string
        // * starting at the specified position. The number of matches
        // * to skip can also be specified.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the longest matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        private int MatchGreedy(Matcher m, LookAheadReader input, int start, int skip)
        {
            // Check for simple case
            if (skip == 0)
            {
                return(MatchPossessive(m, input, start, 0));
            }

            // Find all matches
            if (matchStart != start)
            {
                matchStart = start;
                matches    = new BitArray(10);
                FindMatches(m, input, start, 0, 0, 0);
            }
            for (int i = matches.Count - 1; i >= 0; i += -1)
            {
                // Find first non-skipped match
                if (matches[i])
                {
                    if (skip == 0)
                    {
                        return(i);
                    }
                    skip -= 1;
                }
            }
            return(-1);
        }
コード例 #4
0
        //*
        // * Returns the length of a matching string starting at the
        // * specified position. The number of matches to skip can also be
        // * specified.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        public override int Match(Matcher m, LookAheadReader input, int start, int skip)
        {
            if (skip == 0)
            {
                matchStart = -1;
                matches    = null;
            }
            switch (type)
            {
            case RepeatType.GREEDY:
                return(MatchGreedy(m, input, start, skip));

            case RepeatType.RELUCTANT:
                return(MatchReluctant(m, input, start, skip));

            case RepeatType.POSSESSIVE:
                if (skip == 0)
                {
                    return(MatchPossessive(m, input, start, 0));
                }

                break;
            }
            return(-1);
        }
コード例 #5
0
        //*
        // * Creates a new matcher with the specified element.
        // *
        // * @param e the base regular expression element
        // * @param input the input character stream to work with
        // * @param ignoreCase the character case ignore flag
        //

        internal Matcher(Element e, LookAheadReader input, bool ignoreCase)
        {
            this.element    = e;
            this.input      = input;
            this.ignoreCase = ignoreCase;
            this.Reset();
        }
コード例 #6
0
        //*
        // * Returns the length of a matching string starting at the
        // * specified position. The number of matches to skip can also
        // * be specified, but numbers higher than zero (0) cause a
        // * failed match for any element that doesn't attempt to
        // * combine other elements.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the longest matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        public override int Match(Matcher m, LookAheadReader input, int start, int skip)
        {
            int length  = 0;
            int length1 = -1;
            int length2 = -1;
            int skip1   = 0;
            int skip2   = 0;

            while (length >= 0 && skip1 + skip2 <= skip)
            {
                length1 = elem1.Match(m, input, start, skip1);
                length2 = elem2.Match(m, input, start, skip2);
                if (length1 >= length2)
                {
                    length = length1;
                    skip1 += 1;
                }
                else
                {
                    length = length2;
                    skip2 += 1;
                }
            }
            return(length);
        }
コード例 #7
0
        //*
        // * Returns the length of a matching string starting at the
        // * specified position. The number of matches to skip can also be
        // * specified, but numbers higher than zero (0) cause a failed
        // * match for any element that doesn't attempt to combine other
        // * elements.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the longest matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        public override int Match(Matcher m, LookAheadReader input, int start, int skip)
        {
            int length1 = -1;
            int length2 = 0;
            int skip1   = 0;
            int skip2   = 0;

            while (skip >= 0)
            {
                length1 = elem1.Match(m, input, start, skip1);
                if (length1 < 0)
                {
                    return(-1);
                }
                length2 = elem2.Match(m, input, start + length1, skip2);
                if (length2 < 0)
                {
                    skip1 += 1;
                    skip2  = 0;
                }
                else
                {
                    skip2 += 1;
                    skip  -= 1;
                }
            }

            return(length1 + length2);
        }
コード例 #8
0
        //*
        // * Resets this tokenizer for usage with another input stream.
        // * This method will clear all the internal state in the
        // * tokenizer as well as close the previous input stream. It is
        // * normally called in order to reuse a parser and tokenizer
        // * pair for parsing another input stream.
        // *
        // * @param input the new input stream to read
        // *
        // * @since 1.5
        //

        public void Reset(TextReader input)
        {
            this.input.Close();
            this.input         = new LookAheadReader(input);
            this.previousToken = null;
            stringMatcher.Reset();
            for (int i = 0; i <= regexpMatchers.Count - 1; i++)
            {
                ((RegExpTokenMatcher)regexpMatchers[i]).Reset(this.input);
            }
        }
コード例 #9
0
        //*
        // * Checks if the automaton matches an input stream. The
        // * matching will be performed from a specified position. This
        // * method will not read any characters from the stream, just
        // * peek ahead. The comparison can be done either in
        // * case-sensitive or case-insensitive mode.
        // *
        // * @param input the input stream to check
        // * @param pos the starting position
        // * @param caseInsensitive the case-insensitive flag
        // *
        // * @return the match value, or
        // * null if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        public object MatchFrom(LookAheadReader input, int pos, bool caseInsensitive)
        {
            object    result = null;
            Automaton state  = default(Automaton);
            int       c      = 0;

            c = input.Peek(pos);
            if (tree != null && c >= 0)
            {
                state = tree.Find(Convert.ToChar(c), caseInsensitive);
                if (state != null)
                {
                    result = state.MatchFrom(input, pos + 1, caseInsensitive);
                }
            }
            return((result == null) ? value : result);
        }
コード例 #10
0
        //*
        // * Returns the length of a matching string starting at the
        // * specified position. The number of matches to skip can also be
        // * specified, but numbers higher than zero (0) cause a failed
        // * match for any element that doesn't attempt to combine other
        // * elements.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        public override int Match(Matcher m, LookAheadReader input, int start, int skip)
        {
            int c = 0;

            if (skip != 0)
            {
                return(-1);
            }
            c = input.Peek(start);
            if (c < 0)
            {
                m.SetReadEndOfString();
                return(-1);
            }
            if (m.IsCaseInsensitive())
            {
                c = Convert.ToInt32(Char.ToLower(Convert.ToChar(c)));
            }
            return(InSet(Convert.ToChar(c)) ? 1 : -1);
        }
コード例 #11
0
        //*
        // * Finds all matches and adds the lengths to the matches set.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param length the match length at the start position
        // * @param count the number of sub-elements matched
        // * @param attempt the number of match attempts here
        // *
        // * @throws IOException if an I/O error occurred
        //

        private void FindMatches(Matcher m, LookAheadReader input, int start, int length, int count, int attempt)
        {
            int subLength = 0;

            // Check match ending here
            if (count > max)
            {
                return;
            }
            if (min <= count && attempt == 0)
            {
                if (matches.Length <= length)
                {
                    matches.Length = length + 10;
                }
                matches[length] = true;
            }

            // Check element match
            subLength = elem.Match(m, input, start, attempt);
            if (subLength < 0)
            {
                return;
            }
            else if (subLength == 0)
            {
                if (min == count + 1)
                {
                    if (matches.Length <= length)
                    {
                        matches.Length = length + 10;
                    }
                    matches[length] = true;
                }
                return;
            }

            // Find alternative and subsequent matches
            FindMatches(m, input, start, length, count, attempt + 1);
            FindMatches(m, input, start + subLength, length + subLength, count + 1, 0);
        }
コード例 #12
0
        //*
        // * Returns the length of the shortest possible matching string
        // * starting at the specified position. The number of matches to
        // * skip can also be specified.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the shortest matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        private int MatchReluctant(Matcher m, LookAheadReader input, int start, int skip)
        {
            // Find all matches
            if (matchStart != start)
            {
                matchStart = start;
                matches    = new BitArray(10);
                FindMatches(m, input, start, 0, 0, 0);
            }
            for (int i = 0; i <= matches.Count - 1; i++)
            {
                // Find first non-skipped match
                if (matches[i])
                {
                    if (skip == 0)
                    {
                        return(i);
                    }
                    skip -= 1;
                }
            }
            return(-1);
        }
コード例 #13
0
        //*
        // * Creates a new regular expression token matcher.
        // *
        // * @param pattern the pattern to match
        // * @param ignoreCase the character case ignore flag
        // * @param input the input stream to check
        // *
        // * @throws RegExpException if the regular expression couldn't
        // * be created properly
        //

        public RegExpTokenMatcher(TokenPattern pattern, bool ignoreCase, LookAheadReader input)
        {
            this.pattern = pattern;
            this.regExp  = new RegExp(pattern.Pattern, ignoreCase);
            this.matcher = regExp.Matcher(input);
        }
コード例 #14
0
        //*
        // * Returns the length of a matching string starting at the
        // * specified position. The number of matches to skip can also
        // * be specified, but numbers higher than zero (0) cause a
        // * failed match for any element that doesn't attempt to
        // * combine other elements.
        // *
        // * @param m the matcher being used
        // * @param input the input character stream to match
        // * @param start the starting position
        // * @param skip the number of matches to skip
        // *
        // * @return the length of the matching string, or
        // * -1 if no match was found
        // *
        // * @throws IOException if an I/O error occurred
        //

        public abstract int Match(Matcher m, LookAheadReader input, int start, int skip);
コード例 #15
0
        //*
        // * Checks if the token pattern matches the input stream. This
        // * method will also reset all flags in this matcher.
        // *
        // * @param input the input stream to match
        // *
        // * @return true if a match was found, or
        // * false otherwise
        // *
        // * @throws IOException if an I/O error occurred
        //

        public bool Match(LookAheadReader input)
        {
            this.Reset();
            m_match = (TokenPattern)start.MatchFrom(input, 0, ignoreCase);
            return(m_match != null);
        }
コード例 #16
0
        //*
        // * Creates a new tokenizer for the specified input stream. The
        // * tokenizer can be set to process tokens either in
        // * case-sensitive or case-insensitive mode.
        // *
        // * @param input the input stream to read
        // * @param ignoreCase the character case ignore flag
        // *
        // * @since 1.5
        //

        public Tokenizer(TextReader input, bool ignoreCase)
        {
            this.stringMatcher = new StringTokenMatcher(ignoreCase);
            this.input         = new LookAheadReader(input);
            this.ignoreCase    = ignoreCase;
        }
コード例 #17
0
        //*
        // * Resets the matcher for use with a new look-ahead character
        // * input stream. This will clear all flags and set the match
        // * length to a negative value.
        // *
        // * @param input the character input stream
        // *
        // * @since 1.5
        //

        private void Reset(LookAheadReader input)
        {
            this.input = input;
            this.Reset();
        }
コード例 #18
0
        //*
        // * Creates a new matcher for the specified look-ahead
        // * character input stream.
        // *
        // * @param input the character input stream
        // *
        // * @return the regular expresion matcher
        // *
        // * @since 1.5
        //

        private Matcher Matcher(LookAheadReader input)
        {
            return(new Matcher((Element)element.Clone(), input, ignoreCase));
        }
コード例 #19
0
        //*
        // * Resets the matcher for another character input stream. This
        // * will clear the results of the last match.
        // *
        // * @param input the new input stream to check
        //

        public void Reset(LookAheadReader input)
        {
            matcher.Reset(input);
        }