ScannerSupport.java revision 953:221a84ef44c0
1155192Srwatson/* 2155192Srwatson * Permission is hereby granted, free of charge, to any person obtaining a copy of 3170407Srwatson * this software and associated documentation files (the "Software"), to deal in 4155192Srwatson * the Software without restriction, including without limitation the rights to 5155192Srwatson * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 6155192Srwatson * of the Software, and to permit persons to whom the Software is furnished to do 7155192Srwatson * so, subject to the following conditions: 8155192Srwatson * 9155192Srwatson * The above copyright notice and this permission notice shall be included in all 10155192Srwatson * copies or substantial portions of the Software. 11155192Srwatson * 12155192Srwatson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13155192Srwatson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14155192Srwatson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15155192Srwatson * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16155192Srwatson * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17155192Srwatson * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18155192Srwatson * SOFTWARE. 19155192Srwatson */ 20155192Srwatsonpackage jdk.nashorn.internal.runtime.regexp.joni; 21155192Srwatson 22155192Srwatsonimport jdk.nashorn.internal.runtime.regexp.joni.encoding.IntHolder; 23155192Srwatsonimport jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages; 24155192Srwatson 25155192Srwatsonabstract class ScannerSupport extends IntHolder implements ErrorMessages { 26155192Srwatson 27155192Srwatson protected final char[] chars; // pattern 28155192Srwatson protected int p; // current scanner position 29155192Srwatson protected int stop; // pattern end (mutable) 30155192Srwatson private int lastFetched; // last fetched value for unfetch support 31155192Srwatson protected int c; // current code point 32155192Srwatson 33155192Srwatson private final int begin; // pattern begin position for reset() support 34155192Srwatson private final int end; // pattern end position for reset() support 35155192Srwatson protected int _p; // used by mark()/restore() to mark positions 36155192Srwatson 37155192Srwatson private final static int INT_SIGN_BIT = 1 << 31; 38155192Srwatson 39155192Srwatson protected ScannerSupport(final char[] chars, final int p, final int end) { 40155192Srwatson this.chars = chars; 41155192Srwatson this.begin = p; 42155192Srwatson this.end = end; 43155192Srwatson 44155192Srwatson reset(); 45164033Srwatson } 46155192Srwatson 47155192Srwatson protected int getBegin() { 48155192Srwatson return begin; 49155192Srwatson } 50155192Srwatson 51155192Srwatson protected int getEnd() { 52155192Srwatson return end; 53155192Srwatson } 54155192Srwatson 55155192Srwatson protected final int scanUnsignedNumber() { 56155192Srwatson final int last = c; 57155192Srwatson int num = 0; // long ??? 58155192Srwatson while(left()) { 59155192Srwatson fetch(); 60155192Srwatson if (Character.isDigit(c)) { 61155406Srwatson final int onum = num; 62156291Srwatson num = num * 10 + EncodingHelper.digitVal(c); 63155406Srwatson if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1; 64155406Srwatson } else { 65155192Srwatson unfetch(); 66155192Srwatson break; 67155192Srwatson } 68155192Srwatson } 69155192Srwatson c = last; 70155192Srwatson return num; 71155406Srwatson } 72155406Srwatson 73156888Srwatson protected final int scanUnsignedHexadecimalNumber(int maxLength) { 74170407Srwatson final int last = c; 75155192Srwatson int num = 0; 76155192Srwatson while(left() && maxLength-- != 0) { 77155192Srwatson fetch(); 78155192Srwatson if (EncodingHelper.isXDigit(c)) { 79155192Srwatson final int onum = num; 80170196Srwatson final int val = EncodingHelper.xdigitVal(c); 81170196Srwatson num = (num << 4) + val; 82170196Srwatson if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1; 83155192Srwatson } else { 84155192Srwatson unfetch(); 85156889Srwatson break; 86156889Srwatson } 87155192Srwatson } 88155192Srwatson c = last; 89162176Srwatson return num; 90162176Srwatson } 91155192Srwatson 92156889Srwatson protected final int scanUnsignedOctalNumber(int maxLength) { 93156889Srwatson final int last = c; 94161813Swsalamon int num = 0; 95161813Swsalamon while(left() && maxLength-- != 0) { 96155192Srwatson fetch(); 97155192Srwatson if (Character.isDigit(c) && c < '8') { 98155192Srwatson final int onum = num; 99155192Srwatson final int val = EncodingHelper.odigitVal(c); 100156889Srwatson num = (num << 3) + val; 101155192Srwatson if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1; 102155192Srwatson } else { 103156889Srwatson unfetch(); 104155192Srwatson break; 105156889Srwatson } 106155192Srwatson } 107155192Srwatson c = last; 108155192Srwatson return num; 109155192Srwatson } 110156889Srwatson 111155192Srwatson protected final void reset() { 112155192Srwatson p = begin; 113155192Srwatson stop = end; 114155192Srwatson } 115155192Srwatson 116156889Srwatson protected final void mark() { 117155192Srwatson _p = p; 118155192Srwatson } 119170196Srwatson 120170196Srwatson protected final void restore() { 121170196Srwatson p = _p; 122170196Srwatson } 123170196Srwatson 124170196Srwatson protected final void inc() { 125155192Srwatson lastFetched = p; 126156889Srwatson p++; 127156889Srwatson } 128156889Srwatson 129155192Srwatson protected final void fetch() { 130155192Srwatson lastFetched = p; 131155192Srwatson c = chars[p++]; 132155192Srwatson } 133156889Srwatson 134155192Srwatson protected int fetchTo() { 135155192Srwatson lastFetched = p; 136170196Srwatson return chars[p++]; 137170196Srwatson } 138155192Srwatson 139159261Srwatson protected final void unfetch() { 140155192Srwatson p = lastFetched; 141155192Srwatson } 142159261Srwatson 143159261Srwatson protected final int peek() { 144159261Srwatson return p < stop ? chars[p] : 0; 145155192Srwatson } 146159261Srwatson 147155192Srwatson protected final boolean peekIs(final int c) { 148156889Srwatson return peek() == c; 149156889Srwatson } 150170196Srwatson 151170196Srwatson protected final boolean left() { 152155192Srwatson return p < stop; 153156889Srwatson } 154155192Srwatson 155155192Srwatson} 156155406Srwatson