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 /**
42  * @uml
43  * A {@link Token} object representing an entire subtree matched by a parser
44  * rule; e.g., {@code <expr>}. These tokens are created for {@link TagChunk}
45  * chunks where the tag corresponds to a parser rule.
46  */
47 class RuleTagToken : Token
48 {
49 
50     /**
51      * @uml
52      * This is the backing field for {@link #getRuleName}.
53      */
54     private string ruleName;
55 
56     /**
57      * @uml
58      * The token type for the current token. This is the token type assigned to
59      * the bypass alternative for the rule during ATN deserialization.
60      */
61     private int bypassTokenType;
62 
63     /**
64      * @uml
65      * This is the backing field for {@link #getLabel}.
66      */
67     private string label;
68 
69     /**
70      * @uml
71      * Constructs a new instance of {@link RuleTagToken} with the specified rule
72      * name and bypass token type and no label.
73      *
74      *  @param ruleName The name of the parser rule this rule tag matches.
75      *  @param bypassTokenType The bypass token type assigned to the parser rule.
76      *
77      *  @exception IllegalArgumentException if {@code ruleName} is {@code null}
78      * or empty.
79      */
80     public this(string ruleName, int bypassTokenType)
81     {
82         this(ruleName, bypassTokenType, null);
83     }
84 
85     /**
86      * @uml
87      * Constructs a new instance of {@link RuleTagToken} with the specified rule
88      *  name, bypass token type, and label.
89      *
90      *  @param ruleName The name of the parser rule this rule tag matches.
91      *  @param bypassTokenType The bypass token type assigned to the parser rule.
92      *  @param label The label associated with the rule tag, or {@code null} if
93      * the rule tag is unlabeled.
94      *
95      *  @exception IllegalArgumentException if {@code ruleName} is {@code null}
96      *  or empty.
97      */
98     public this(string ruleName, int bypassTokenType, string label)
99     {
100 	if (ruleName is null || ruleName.length == 0) {
101             throw new IllegalArgumentException("ruleName cannot be null or empty.");
102         }
103         this.ruleName = ruleName;
104         this.bypassTokenType = bypassTokenType;
105         this.label = label;
106     }
107 
108     /**
109      * @uml
110      * Gets the name of the rule associated with this rule tag.
111      *
112      *  @return The name of the parser rule associated with this rule tag.
113      */
114     public string getRuleName()
115     {
116         return ruleName;
117     }
118 
119     /**
120      * @uml
121      * Gets the label associated with the rule tag.
122      *
123      *  @return The name of the label associated with the rule tag, or
124      * {@code null} if this is an unlabeled rule tag.
125      */
126     public string getLabel()
127     {
128         return label;
129     }
130 
131     /**
132      * @uml
133      * @override
134      * <p>Rule tag tokens are always placed on the {@link #DEFAULT_CHANNEL}.</p>
135      */
136     public override int getChannel()
137     {
138         return TokenConstantDefinition.DEFAULT_CHANNEL;
139     }
140 
141     /**
142      * @uml
143      * @override
144      * <p>This method returns the rule tag formatted with {@code <} and {@code >}
145      * delimiters.</p>
146      */
147     public override string getText()
148     {
149         if (label !is null) {
150             return "<" ~ label ~ ":" ~ ruleName ~ ">";
151         }
152         return "<" ~ ruleName ~ ">";
153     }
154 
155     /**
156      * @uml
157      * @override
158      * <p>Rule tag tokens have types assigned according to the rule bypass
159      * transitions created during ATN deserialization.</p>
160      */
161     public override int getType()
162     {
163         return bypassTokenType;
164     }
165 
166     /**
167      * @uml
168      * @override
169      */
170     public override int getLine()
171     {
172         return 0;
173     }
174 
175     /**
176      * @uml
177      * @override
178      */
179     public override int getCharPositionInLine()
180     {
181         return -1;
182     }
183 
184     /**
185      * @uml
186      * @override
187      */
188     public override int getTokenIndex()
189     {
190         return -1;
191     }
192 
193     /**
194      * @uml
195      * @override
196      */
197     public override int getStartIndex()
198     {
199         return -1;
200     }
201 
202     /**
203      * @uml
204      * @override
205      */
206     public override int getStopIndex()
207     {
208         return -1;
209     }
210 
211     /**
212      * @uml
213      * @override
214      */
215     public override TokenSource getTokenSource()
216     {
217         return null;
218     }
219 
220     /**
221      * @uml
222      * @override
223      */
224     public override CharStream getInputStream()
225     {
226         return null;
227     }
228 
229     /**
230      * @uml
231      * @override
232      */
233     public override string toString()
234     {
235         return ruleName ~ ":" ~ to!string(bypassTokenType);
236     }
237 
238 }