Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Handles the repetition count for a greedy Curly. The matchInit
- * is called from the Prolog to save the index of where the group
- * beginning is stored. A zero length group check occurs in the
- * normal match but is skipped in the matchInit.
- */
- static class Loop extends Node {
- Node body;
- int countIndex; // local count index in matcher locals
- int beginIndex; // group beginning index
- int cmin, cmax;
- Loop(int countIndex, int beginIndex) {
- this.countIndex = countIndex;
- this.beginIndex = beginIndex;
- }
- boolean match(Matcher matcher, int i, CharSequence seq) {
- // Avoid infinite loop in zero-length case.
- if (i > matcher.locals[beginIndex]) {
- int count = matcher.locals[countIndex];
- // This block is for before we reach the minimum
- // iterations required for the loop to match
- if (count < cmin) {
- matcher.locals[countIndex] = count + 1;
- boolean b = body.match(matcher, i, seq);
- // If match failed we must backtrack, so
- // the loop count should NOT be incremented
- if (!b)
- matcher.locals[countIndex] = count;
- // Return success or failure since we are under
- // minimum
- return b;
- }
- // This block is for after we have the minimum
- // iterations required for the loop to match
- if (count < cmax) {
- matcher.locals[countIndex] = count + 1;
- boolean b = body.match(matcher, i, seq);
- // If match failed we must backtrack, so
- // the loop count should NOT be incremented
- if (!b)
- matcher.locals[countIndex] = count;
- else
- return true;
- }
- }
- return next.match(matcher, i, seq);
- }
- boolean matchInit(Matcher matcher, int i, CharSequence seq) {
- int save = matcher.locals[countIndex];
- boolean ret = false;
- if (0 < cmin) {
- matcher.locals[countIndex] = 1;
- ret = body.match(matcher, i, seq);
- } else if (0 < cmax) {
- matcher.locals[countIndex] = 1;
- ret = body.match(matcher, i, seq);
- if (ret == false)
- ret = next.match(matcher, i, seq);
- } else {
- ret = next.match(matcher, i, seq);
- }
- matcher.locals[countIndex] = save;
- return ret;
- }
- boolean study(TreeInfo info) {
- info.maxValid = false;
- info.deterministic = false;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment