1 /*
2  * Copyright (c) 2012-2019 The ANTLR Project. All rights reserved.
3  * Use of this file is governed by the BSD 3-clause license that
4  * can be found in the LICENSE.txt file in the project root.
5  */
6 
7 module antlr.v4.runtime.tree.pattern.RuleTagToken;
8 
9 import antlr.v4.runtime.CharStream;
10 import antlr.v4.runtime.IllegalArgumentException;
11 import antlr.v4.runtime.Token;
12 import antlr.v4.runtime.TokenConstantDefinition;
13 import antlr.v4.runtime.TokenSource;
14 import std.conv;
15 import std.format;
16 import std.variant;
17 
18 /**
19  * @uml
20  * A {@link Token} object representing an entire subtree matched by a parser
21  * rule; e.g., {@code <expr>}. These tokens are created for {@link TagChunk}
22  * chunks where the tag corresponds to a parser rule.
23  */
24 class RuleTagToken : Token
25 {
26 
27     /**
28      * @uml
29      * This is the backing field for {@link #getRuleName}.
30      */
31     private string ruleName;
32 
33     /**
34      * @uml
35      * The token type for the current token. This is the token type assigned to
36      * the bypass alternative for the rule during ATN deserialization.
37      */
38     private int bypassTokenType;
39 
40     /**
41      * @uml
42      * This is the backing field for {@link #getLabel}.
43      */
44     private string label;
45 
46     /**
47      * @uml
48      * Constructs a new instance of {@link RuleTagToken} with the specified rule
49      * name and bypass token type and no label.
50      *
51      *  @param ruleName The name of the parser rule this rule tag matches.
52      *  @param bypassTokenType The bypass token type assigned to the parser rule.
53      *
54      *  @exception IllegalArgumentException if {@code ruleName} is {@code null}
55      * or empty.
56      */
57     public this(string ruleName, int bypassTokenType)
58     {
59         this(ruleName, bypassTokenType, null);
60     }
61 
62     /**
63      * @uml
64      * Constructs a new instance of {@link RuleTagToken} with the specified rule
65      *  name, bypass token type, and label.
66      *
67      *  @param ruleName The name of the parser rule this rule tag matches.
68      *  @param bypassTokenType The bypass token type assigned to the parser rule.
69      *  @param label The label associated with the rule tag, or {@code null} if
70      * the rule tag is unlabeled.
71      *
72      *  @exception IllegalArgumentException if {@code ruleName} is {@code null}
73      *  or empty.
74      */
75     public this(string ruleName, int bypassTokenType, string label)
76     {
77     if (ruleName is null || ruleName.length == 0) {
78             throw new IllegalArgumentException("ruleName cannot be null or empty.");
79         }
80         this.ruleName = ruleName;
81         this.bypassTokenType = bypassTokenType;
82         this.label = label;
83     }
84 
85     /**
86      * @uml
87      * Gets the name of the rule associated with this rule tag.
88      *
89      *  @return The name of the parser rule associated with this rule tag.
90      */
91     public string getRuleName()
92     {
93         return ruleName;
94     }
95 
96     /**
97      * @uml
98      * Gets the label associated with the rule tag.
99      *
100      *  @return The name of the label associated with the rule tag, or
101      * {@code null} if this is an unlabeled rule tag.
102      */
103     public string getLabel()
104     {
105         return label;
106     }
107 
108     /**
109      * @uml
110      * @override
111      * <p>Rule tag tokens are always placed on the {@link #DEFAULT_CHANNEL}.</p>
112      */
113     public override int getChannel()
114     {
115         return TokenConstantDefinition.DEFAULT_CHANNEL;
116     }
117 
118     /**
119      * @uml
120      * @override
121      * <p>This method returns the rule tag formatted with {@code <} and {@code >}
122      * delimiters.</p>
123      */
124     public override Variant getText()
125     {
126         if (label !is null) {
127             Variant r = format("<%s:%s>", label, ruleName);
128             return r;
129         }
130         Variant r = format("<%s>", ruleName);
131         return r;
132     }
133 
134     /**
135      * @uml
136      * @override
137      * <p>Rule tag tokens have types assigned according to the rule bypass
138      * transitions created during ATN deserialization.</p>
139      */
140     public override int getType()
141     {
142         return bypassTokenType;
143     }
144 
145     /**
146      * @uml
147      * @override
148      */
149     public override int getLine()
150     {
151         return 0;
152     }
153 
154     /**
155      * @uml
156      * @override
157      */
158     public override int getCharPositionInLine()
159     {
160         return -1;
161     }
162 
163     /**
164      * @uml
165      * @override
166      */
167     public override size_t getTokenIndex()
168     {
169         return to!size_t(-1);
170     }
171 
172     /**
173      * @uml
174      * @override
175      */
176     public override size_t startIndex()
177     {
178         return -1;
179     }
180 
181     /**
182      * @uml
183      * @override
184      */
185     public override size_t stopIndex()
186     {
187         return -1;
188     }
189 
190     /**
191      * @uml
192      * @override
193      */
194     public override TokenSource getTokenSource()
195     {
196         return null;
197     }
198 
199     /**
200      * @uml
201      * @override
202      */
203     public override CharStream getInputStream()
204     {
205         return null;
206     }
207 
208     /**
209      * @uml
210      * @override
211      */
212     public override string toString()
213     {
214         return ruleName ~ ":" ~ to!string(bypassTokenType);
215     }
216 
217 }