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  */
  20 package jdk.nashorn.internal.runtime.regexp.joni.ast;
  21 
  22 import jdk.nashorn.internal.runtime.regexp.joni.Config;
  23 import jdk.nashorn.internal.runtime.regexp.joni.Option;
  24 import jdk.nashorn.internal.runtime.regexp.joni.constants.EncloseType;
  25 
  26 public final class EncloseNode extends StateNode implements EncloseType {
  27 
  28     public int type;                // enclose type
  29     public int regNum;
  30     public int option;
  31     public Node target;             /* EncloseNode : ENCLOSE_MEMORY */
  32     public int callAddr;            // AbsAddrType
  33     public int minLength;           // OnigDistance
  34     public int maxLength;           // OnigDistance
  35     public int charLength;
  36     public int optCount;            // referenced count in optimize_node_left()
  37 
  38     // node_new_enclose / onig_node_new_enclose
  39     public EncloseNode(int type) {
  40         this.type = type;
  41         callAddr = -1;
  42     }
  43 
  44     // node_new_enclose_memory
  45     public EncloseNode(int option, boolean isNamed) {
  46         this(MEMORY);
  47         if (isNamed) setNamedGroup();
  48         if (Config.USE_SUBEXP_CALL) this.option = option;
  49     }
  50 
  51     // node_new_option
  52     public EncloseNode(int option, int i) {
  53         this(OPTION);
  54         this.option = option;
  55     }
  56 
  57     @Override
  58     public int getType() {
  59         return ENCLOSE;
  60     }
  61 
  62     @Override
  63     protected void setChild(Node newChild) {
  64         target = newChild;
  65     }
  66 
  67     @Override
  68     protected Node getChild() {
  69         return target;
  70     }
  71 
  72     public void setTarget(Node tgt) {
  73         target = tgt;
  74         tgt.parent = this;
  75     }
  76 
  77     @Override
  78     public String getName() {
  79         return "Enclose";
  80     }
  81 
  82     @Override
  83     public String toString(int level) {
  84         StringBuilder value = new StringBuilder(super.toString(level));
  85         value.append("\n  type: " + typeToString());
  86         value.append("\n  regNum: " + regNum);
  87         value.append("\n  option: " + Option.toString(option));
  88         value.append("\n  target: " + pad(target, level + 1));
  89         value.append("\n  callAddr: " + callAddr);
  90         value.append("\n  minLength: " + minLength);
  91         value.append("\n  maxLength: " + maxLength);
  92         value.append("\n  charLength: " + charLength);
  93         value.append("\n  optCount: " + optCount);
  94 
  95         return value.toString();
  96     }
  97 
  98     public String typeToString() {
  99         StringBuilder types = new StringBuilder();
 100         if (isStopBacktrack()) types.append("STOP_BACKTRACK ");
 101         if (isMemory()) types.append("MEMORY ");
 102         if (isOption()) types.append("OPTION ");
 103 
 104         return types.toString();
 105     }
 106 
 107     public void setEncloseStatus(int flag) {
 108         state |= flag;
 109     }
 110 
 111     public void clearEncloseStatus(int flag) {
 112         state &= ~flag;
 113     }
 114 
 115     public void clearMemory() {
 116         type &= ~MEMORY;
 117     }
 118 
 119     public void setMemory() {
 120         type |= MEMORY;
 121     }
 122 
 123     public boolean isMemory() {
 124         return (type & MEMORY) != 0;
 125     }
 126 
 127     public void clearOption() {
 128         type &= ~OPTION;
 129     }
 130 
 131     public void setOption() {
 132         type |= OPTION;
 133     }
 134 
 135     public boolean isOption() {
 136         return (type & OPTION) != 0;
 137     }
 138 
 139     public void clearStopBacktrack() {
 140         type &= ~STOP_BACKTRACK;
 141     }
 142 
 143     public void setStopBacktrack() {
 144         type |= STOP_BACKTRACK;
 145     }
 146 
 147     public boolean isStopBacktrack() {
 148         return (type & STOP_BACKTRACK) != 0;
 149     }
 150 
 151 }