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.TokenTagToken;
8 
9 import antlr.v4.runtime.CommonToken;
10 import std.conv;
11 import std.format;
12 import std.variant;
13 
14 /**
15  * @uml
16  * {@link Token} object representing a token of a particular type; e.g.,
17  * {@code <ID>}. These tokens are created for {@link TagChunk} chunks where the
18  * tag corresponds to a lexer rule or token type.
19  */
20 class TokenTagToken : CommonToken
21 {
22 
23     /**
24      * @uml
25      * This is the backing field for {@link #getTokenName}.
26      * @final
27      */
28     private string tokenName;
29 
30     /**
31      * @uml
32      * This is the backing field for {@link #getLabel}.
33      * @final
34      */
35     private string label;
36 
37     /**
38      * @uml
39      * Constructs a new instance of {@link TokenTagToken} for an unlabeled tag
40      * with the specified token name and type.
41      *
42      *  @param tokenName The token name.
43      *  @param type The token type.
44      */
45     public this(string tokenName, int type)
46     {
47         this(tokenName, type, null);
48     }
49 
50     /**
51      * @uml
52      * Constructs a new instance of {@link TokenTagToken} with the specified
53      *  token name, type, and label.
54      *
55      *  @param tokenName The token name.
56      *  @param type The token type.
57      *  @param label The label associated with the token tag, or {@code null} if
58      *  the token tag is unlabeled.
59      */
60     public this(string tokenName, int type, string label)
61     {
62         super(type);
63         this.tokenName = tokenName;
64         this.label = label;
65     }
66 
67     /**
68      * @uml
69      * Gets the token name.
70      *  @return The token name.
71      */
72     public string getTokenName()
73     {
74         return tokenName;
75     }
76 
77     /**
78      * @uml
79      * Gets the label associated with the rule tag.
80      *
81      *  @return The name of the label associated with the rule tag, or
82      *  {@code null} if this is an unlabeled rule tag.
83      */
84     public string getLabel()
85     {
86         return label;
87     }
88 
89     /**
90      * @uml
91      * <p>The implementation for {@link TokenTagToken} returns the token tag
92      *  formatted with {@code <} and {@code >} delimiters.</p>
93      * @override
94      */
95     public override Variant getText()
96     {
97         if (label !is null) {
98             Variant r = format("<%s:%s>", label, tokenName);
99             return r;
100         }
101         Variant r = format("<%s>", tokenName);
102         return r;
103     }
104 
105     /**
106      * @uml
107      * <p>The implementation for {@link TokenTagToken} returns a string of the form
108      *  {@code tokenName:type}.</p>
109      * @override
110      */
111     public override string toString()
112     {
113         return tokenName ~ ":" ~ to!string(type);
114     }
115 
116 }