src/jdk/nashorn/internal/runtime/regexp/joni/ScannerSupport.java

Thu, 16 May 2013 19:52:39 +0200

author
hannesw
date
Thu, 16 May 2013 19:52:39 +0200
changeset 273
98798a6336de
parent 115
e42fd1640ff9
child 447
7503f30c1355
permissions
-rw-r--r--

8012359: Increase code coverage in Joni
Reviewed-by: jlaskey, lagergren

hannesw@115 1 /*
hannesw@115 2 * Permission is hereby granted, free of charge, to any person obtaining a copy of
hannesw@115 3 * this software and associated documentation files (the "Software"), to deal in
hannesw@115 4 * the Software without restriction, including without limitation the rights to
hannesw@115 5 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
hannesw@115 6 * of the Software, and to permit persons to whom the Software is furnished to do
hannesw@115 7 * so, subject to the following conditions:
hannesw@115 8 *
hannesw@115 9 * The above copyright notice and this permission notice shall be included in all
hannesw@115 10 * copies or substantial portions of the Software.
hannesw@115 11 *
hannesw@115 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hannesw@115 13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hannesw@115 14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hannesw@115 15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hannesw@115 16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hannesw@115 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
hannesw@115 18 * SOFTWARE.
hannesw@115 19 */
hannesw@115 20 package jdk.nashorn.internal.runtime.regexp.joni;
hannesw@115 21
hannesw@115 22 import jdk.nashorn.internal.runtime.regexp.joni.encoding.IntHolder;
hannesw@115 23 import jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages;
hannesw@115 24 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
hannesw@115 25 import jdk.nashorn.internal.runtime.regexp.joni.exception.SyntaxException;
hannesw@115 26 import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
hannesw@115 27
hannesw@115 28 abstract class ScannerSupport extends IntHolder implements ErrorMessages {
hannesw@115 29
hannesw@115 30 protected final char[] chars; // pattern
hannesw@115 31 protected int p; // current scanner position
hannesw@115 32 protected int stop; // pattern end (mutable)
hannesw@115 33 private int lastFetched; // last fetched value for unfetch support
hannesw@115 34 protected int c; // current code point
hannesw@115 35
hannesw@115 36 private final int begin; // pattern begin position for reset() support
hannesw@115 37 private final int end; // pattern end position for reset() support
hannesw@115 38 protected int _p; // used by mark()/restore() to mark positions
hannesw@115 39
hannesw@273 40 private final static int INT_SIGN_BIT = 1 << 31;
hannesw@273 41
hannesw@115 42 protected ScannerSupport(char[] chars, int p, int end) {
hannesw@115 43 this.chars = chars;
hannesw@115 44 this.begin = p;
hannesw@115 45 this.end = end;
hannesw@115 46
hannesw@115 47 reset();
hannesw@115 48 }
hannesw@115 49
hannesw@115 50 protected int getBegin() {
hannesw@115 51 return begin;
hannesw@115 52 }
hannesw@115 53
hannesw@115 54 protected int getEnd() {
hannesw@115 55 return end;
hannesw@115 56 }
hannesw@115 57
hannesw@115 58 protected final int scanUnsignedNumber() {
hannesw@115 59 int last = c;
hannesw@115 60 int num = 0; // long ???
hannesw@115 61 while(left()) {
hannesw@115 62 fetch();
hannesw@115 63 if (Character.isDigit(c)) {
hannesw@115 64 int onum = num;
hannesw@115 65 num = num * 10 + EncodingHelper.digitVal(c);
hannesw@115 66 if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1;
hannesw@115 67 } else {
hannesw@115 68 unfetch();
hannesw@115 69 break;
hannesw@115 70 }
hannesw@115 71 }
hannesw@115 72 c = last;
hannesw@115 73 return num;
hannesw@115 74 }
hannesw@115 75
hannesw@115 76 protected final int scanUnsignedHexadecimalNumber(int maxLength) {
hannesw@115 77 int last = c;
hannesw@115 78 int num = 0;
hannesw@115 79 while(left() && maxLength-- != 0) {
hannesw@115 80 fetch();
hannesw@115 81 if (EncodingHelper.isXDigit(c)) {
hannesw@115 82 int onum = num;
hannesw@115 83 int val = EncodingHelper.xdigitVal(c);
hannesw@115 84 num = (num << 4) + val;
hannesw@115 85 if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1;
hannesw@115 86 } else {
hannesw@115 87 unfetch();
hannesw@115 88 break;
hannesw@115 89 }
hannesw@115 90 }
hannesw@115 91 c = last;
hannesw@115 92 return num;
hannesw@115 93 }
hannesw@115 94
hannesw@115 95 protected final int scanUnsignedOctalNumber(int maxLength) {
hannesw@115 96 int last = c;
hannesw@115 97 int num = 0;
hannesw@115 98 while(left() && maxLength-- != 0) {
hannesw@115 99 fetch();
hannesw@115 100 if (Character.isDigit(c) && c < '8') {
hannesw@115 101 int onum = num;
hannesw@115 102 int val = EncodingHelper.odigitVal(c);
hannesw@115 103 num = (num << 3) + val;
hannesw@115 104 if (((onum ^ num) & INT_SIGN_BIT) != 0) return -1;
hannesw@115 105 } else {
hannesw@115 106 unfetch();
hannesw@115 107 break;
hannesw@115 108 }
hannesw@115 109 }
hannesw@115 110 c = last;
hannesw@115 111 return num;
hannesw@115 112 }
hannesw@115 113
hannesw@115 114 protected final void reset() {
hannesw@115 115 p = begin;
hannesw@115 116 stop = end;
hannesw@115 117 }
hannesw@115 118
hannesw@115 119 protected final void mark() {
hannesw@115 120 _p = p;
hannesw@115 121 }
hannesw@115 122
hannesw@115 123 protected final void restore() {
hannesw@115 124 p = _p;
hannesw@115 125 }
hannesw@115 126
hannesw@115 127 protected final void inc() {
hannesw@115 128 lastFetched = p;
hannesw@115 129 p++;
hannesw@115 130 }
hannesw@115 131
hannesw@115 132 protected final void fetch() {
hannesw@115 133 lastFetched = p;
hannesw@115 134 c = chars[p++];
hannesw@115 135 }
hannesw@115 136
hannesw@115 137 protected int fetchTo() {
hannesw@115 138 lastFetched = p;
hannesw@115 139 return chars[p++];
hannesw@115 140 }
hannesw@115 141
hannesw@115 142 protected final void unfetch() {
hannesw@115 143 p = lastFetched;
hannesw@115 144 }
hannesw@115 145
hannesw@115 146 protected final int peek() {
hannesw@115 147 return p < stop ? chars[p] : 0;
hannesw@115 148 }
hannesw@115 149
hannesw@115 150 protected final boolean peekIs(int c) {
hannesw@115 151 return peek() == c;
hannesw@115 152 }
hannesw@115 153
hannesw@115 154 protected final boolean left() {
hannesw@115 155 return p < stop;
hannesw@115 156 }
hannesw@115 157
hannesw@115 158 protected void newSyntaxException(String message) {
hannesw@115 159 throw new SyntaxException(message);
hannesw@115 160 }
hannesw@115 161
hannesw@115 162 protected void newValueException(String message) {
hannesw@115 163 throw new ValueException(message);
hannesw@115 164 }
hannesw@115 165
hannesw@115 166 protected void newValueException(String message, String str) {
hannesw@115 167 throw new ValueException(message, str);
hannesw@115 168 }
hannesw@115 169
hannesw@115 170 protected void newValueException(String message, int p, int end) {
hannesw@115 171 throw new ValueException(message, new String(chars, p, end - p));
hannesw@115 172 }
hannesw@115 173
hannesw@115 174 protected void newInternalException(String message) {
hannesw@115 175 throw new InternalException(message);
hannesw@115 176 }
hannesw@115 177
hannesw@115 178 }

mercurial