ScanEnvironment.java revision 953:221a84ef44c0
1/*
2 * Permission is hereby granted, free of charge, to any person obtaining a copy of
3 * this software and associated documentation files (the "Software"), to deal in
4 * the Software without restriction, including without limitation the rights to
5 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
6 * of the Software, and to permit persons to whom the Software is furnished to do
7 * so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all
10 * copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 * SOFTWARE.
19 */
20package jdk.nashorn.internal.runtime.regexp.joni;
21
22import static jdk.nashorn.internal.runtime.regexp.joni.BitStatus.bsClear;
23
24import jdk.nashorn.internal.runtime.regexp.joni.ast.Node;
25import jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages;
26import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
27
28public final class ScanEnvironment {
29
30    private static final int SCANENV_MEMNODES_SIZE = 8;
31
32    int option;
33    final int caseFoldFlag;
34    final public Syntax syntax;
35    int captureHistory;
36    int btMemStart;
37    int btMemEnd;
38    int backrefedMem;
39
40    final public Regex reg;
41
42    public int numMem;
43
44    public Node memNodes[];
45
46
47    public ScanEnvironment(final Regex regex, final Syntax syntax) {
48        this.reg = regex;
49        option = regex.options;
50        caseFoldFlag = regex.caseFoldFlag;
51        this.syntax = syntax;
52    }
53
54    public void clear() {
55        captureHistory = bsClear();
56        btMemStart = bsClear();
57        btMemEnd = bsClear();
58        backrefedMem = bsClear();
59
60        numMem = 0;
61        memNodes = null;
62    }
63
64    public int addMemEntry() {
65        if (numMem++ == 0) {
66            memNodes = new Node[SCANENV_MEMNODES_SIZE];
67        } else if (numMem >= memNodes.length) {
68            final Node[]tmp = new Node[memNodes.length << 1];
69            System.arraycopy(memNodes, 0, tmp, 0, memNodes.length);
70            memNodes = tmp;
71        }
72
73        return numMem;
74    }
75
76    public void setMemNode(final int num, final Node node) {
77        if (numMem >= num) {
78            memNodes[num] = node;
79        } else {
80            throw new InternalException(ErrorMessages.ERR_PARSER_BUG);
81        }
82    }
83
84    public int convertBackslashValue(final int c) {
85        if (syntax.opEscControlChars()) {
86            switch (c) {
87            case 'n': return '\n';
88            case 't': return '\t';
89            case 'r': return '\r';
90            case 'f': return '\f';
91            case 'a': return '\007';
92            case 'b': return '\010';
93            case 'e': return '\033';
94            case 'v':
95                if (syntax.op2EscVVtab()) return 11; // ???
96                break;
97            default:
98                break;
99            }
100        }
101        return c;
102    }
103
104    void ccEscWarn(final String s) {
105        if (Config.USE_WARN) {
106            if (syntax.warnCCOpNotEscaped() && syntax.backSlashEscapeInCC()) {
107                reg.warnings.warn("character class has '" + s + "' without escape");
108            }
109        }
110    }
111
112}
113