1 /* 2 * [The "BSD license"] 3 * Copyright (c) 2013 Terence Parr 4 * Copyright (c) 2013 Sam Harwell 5 * Copyright (c) 2017 Egbert Voigt 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 module antlr.v4.runtime.tree.pattern.RuleTagToken; 33 34 import std.conv; 35 import antlr.v4.runtime.CharStream; 36 import antlr.v4.runtime.IllegalArgumentException; 37 import antlr.v4.runtime.Token; 38 import antlr.v4.runtime.TokenConstantDefinition; 39 import antlr.v4.runtime.TokenSource; 40 41 // Class RuleTagToken 42 /** 43 * @uml 44 * A {@link Token} object representing an entire subtree matched by a parser 45 * rule; e.g., {@code <expr>}. These tokens are created for {@link TagChunk} 46 * chunks where the tag corresponds to a parser rule. 47 */ 48 class RuleTagToken : Token 49 { 50 51 /** 52 * @uml 53 * This is the backing field for {@link #getRuleName}. 54 */ 55 private string ruleName; 56 57 /** 58 * @uml 59 * The token type for the current token. This is the token type assigned to 60 * the bypass alternative for the rule during ATN deserialization. 61 */ 62 private int bypassTokenType; 63 64 /** 65 * @uml 66 * This is the backing field for {@link #getLabel}. 67 */ 68 private string label; 69 70 /** 71 * @uml 72 * Constructs a new instance of {@link RuleTagToken} with the specified rule 73 * name and bypass token type and no label. 74 * 75 * @param ruleName The name of the parser rule this rule tag matches. 76 * @param bypassTokenType The bypass token type assigned to the parser rule. 77 * 78 * @exception IllegalArgumentException if {@code ruleName} is {@code null} 79 * or empty. 80 */ 81 public this(string ruleName, int bypassTokenType) 82 { 83 this(ruleName, bypassTokenType, null); 84 } 85 86 /** 87 * @uml 88 * Constructs a new instance of {@link RuleTagToken} with the specified rule 89 * name, bypass token type, and label. 90 * 91 * @param ruleName The name of the parser rule this rule tag matches. 92 * @param bypassTokenType The bypass token type assigned to the parser rule. 93 * @param label The label associated with the rule tag, or {@code null} if 94 * the rule tag is unlabeled. 95 * 96 * @exception IllegalArgumentException if {@code ruleName} is {@code null} 97 * or empty. 98 */ 99 public this(string ruleName, int bypassTokenType, string label) 100 { 101 if (ruleName is null || ruleName.length == 0) { 102 throw new IllegalArgumentException("ruleName cannot be null or empty."); 103 } 104 this.ruleName = ruleName; 105 this.bypassTokenType = bypassTokenType; 106 this.label = label; 107 } 108 109 /** 110 * @uml 111 * Gets the name of the rule associated with this rule tag. 112 * 113 * @return The name of the parser rule associated with this rule tag. 114 */ 115 public string getRuleName() 116 { 117 return ruleName; 118 } 119 120 /** 121 * @uml 122 * Gets the label associated with the rule tag. 123 * 124 * @return The name of the label associated with the rule tag, or 125 * {@code null} if this is an unlabeled rule tag. 126 */ 127 public string getLabel() 128 { 129 return label; 130 } 131 132 /** 133 * @uml 134 * @override 135 * <p>Rule tag tokens are always placed on the {@link #DEFAULT_CHANNEL}.</p> 136 */ 137 public override int getChannel() 138 { 139 return TokenConstantDefinition.DEFAULT_CHANNEL; 140 } 141 142 /** 143 * @uml 144 * @override 145 * <p>This method returns the rule tag formatted with {@code <} and {@code >} 146 * delimiters.</p> 147 */ 148 public override string getText() 149 { 150 if (label !is null) { 151 return "<" ~ label ~ ":" ~ ruleName ~ ">"; 152 } 153 return "<" ~ ruleName ~ ">"; 154 } 155 156 /** 157 * @uml 158 * @override 159 * <p>Rule tag tokens have types assigned according to the rule bypass 160 * transitions created during ATN deserialization.</p> 161 */ 162 public override int getType() 163 { 164 return bypassTokenType; 165 } 166 167 /** 168 * @uml 169 * @override 170 */ 171 public override int getLine() 172 { 173 return 0; 174 } 175 176 /** 177 * @uml 178 * @override 179 */ 180 public override int getCharPositionInLine() 181 { 182 return -1; 183 } 184 185 /** 186 * @uml 187 * @override 188 */ 189 public override int getTokenIndex() 190 { 191 return -1; 192 } 193 194 /** 195 * @uml 196 * @override 197 */ 198 public override int getStartIndex() 199 { 200 return -1; 201 } 202 203 /** 204 * @uml 205 * @override 206 */ 207 public override int getStopIndex() 208 { 209 return -1; 210 } 211 212 /** 213 * @uml 214 * @override 215 */ 216 public override TokenSource getTokenSource() 217 { 218 return null; 219 } 220 221 /** 222 * @uml 223 * @override 224 */ 225 public override CharStream getInputStream() 226 { 227 return null; 228 } 229 230 /** 231 * @uml 232 * @override 233 */ 234 public override string toString() 235 { 236 return ruleName ~ ":" ~ to!string(bypassTokenType); 237 } 238 239 }