Debian Codegraph @

src/com/google/javascript/jscomp/PeepholeSimplifyRegExp.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.javascript.jscomp.regex.RegExpTree;
20 import com.google.javascript.rhino.Node;
21
22 /**
23 * Simplifies regular expression patterns and flags.
24 *
25 * @author Mike Samuel <[email protected]>
26 */
27 class PeepholeSimplifyRegExp extends AbstractPeepholeOptimization {
28
29 @Override
30 Node optimizeSubtree(Node subtree) {
31 if (subtree.isRegExp()) {
32 // Split regexp into pattern and flags.
33 String pattern = subtree.getFirstChild().getString();
34 String flags = subtree.getChildCount() == 2
35 ? subtree.getLastChild().getString() : "";
36 // Parse to an AST and optimize.
37 RegExpTree regexTree;
38 try {
39 regexTree = RegExpTree.parseRegExp(pattern, flags);
40 } catch (IllegalArgumentException ex) {
41 // Warnings are propagated in the CheckRegExp pass.
42 return subtree;
43 }
44 regexTree = regexTree.simplify(flags);
45 // Decompose the AST.
46 String literal = regexTree.toString();
47 String newPattern = literal.substring(1, literal.length() - 1);
48 // Remove unnecessary flags and order them consistently for gzip.
49 String newFlags = (
50 // The g flags cannot match or replace more than one instance if it is
51 // anchored at the front and back as in /^foo$/ and if the anchors are
52 // relative to the whole string.
53 // But if the regex has capturing groups, then the match operator
54 // would return capturing groups without the g flag.
55 (flags.contains("g")
56 && (!RegExpTree.matchesWholeInput(regexTree, flags)
57 || regexTree.hasCapturingGroup())
58 ? "g" : "")
59 // Remove the i flag if it doesn't have any effect.
60 // E.g. /[a-z0-9_]/i -> /\w/
61 + (flags.contains("i") && regexTree.isCaseSensitive() ? "i" : "")
62 // If the regular expression contains no anchors, then the m flag has
63 // no effect.
64 + (flags.contains("m") && regexTree.containsAnchor() ? "m" : ""));
65 // Update the original if something was done.
66 if (!(newPattern.equals(pattern) && newFlags.equals(flags))) {
67 subtree.getFirstChild().setString(newPattern);
68 if (!"".equals(newFlags)) {
69 subtree.getLastChild().setString(newFlags);
70 } else if (subtree.getChildCount() == 2) {
71 subtree.getLastChild().detachFromParent();
72 }
73 reportCodeChange();
74 }
75 }
76 return subtree;
77 }
78 }