1 module antlr.v4.runtime.tree.pattern.TagChunk;
2 
3 import antlr.v4.runtime.tree.pattern.Chunk;
4 import antlr.v4.runtime.IllegalArgumentException;
5 
6 // Class TagChunk
7 /**
8  * @uml
9  * Represents a placeholder tag in a tree pattern. A tag can have any of the
10  * following forms.
11  *
12  * <ul>
13  * <li>{@code expr}: An unlabeled placeholder for a parser rule {@code expr}.</li>
14  * <li>{@code ID}: An unlabeled placeholder for a token of type {@code ID}.</li>
15  * <li>{@code e:expr}: A labeled placeholder for a parser rule {@code expr}.</li>
16  * <li>{@code id:ID}: A labeled placeholder for a token of type {@code ID}.</li>
17  * </ul>
18  *
19  * This class does not perform any validation on the tag or label names aside
20  * from ensuring that the tag is a non-null, non-empty string.
21  */
22 class TagChunk : Chunk
23 {
24 
25     /**
26      * @uml
27      * This is the backing field for {@link #getTag}.
28      */
29     private string tag;
30 
31     /**
32      * @uml
33      * This is the backing field for {@link #getLabel}.
34      */
35     private string label;
36 
37     /**
38      * @uml
39      * Construct a new instance of {@link TagChunk} using the specified tag and
40      * no label.
41      * 	 *
42      *  @param tag The tag, which should be the name of a parser rule or token
43      * type.
44      *
45      *  @exception IllegalArgumentException if {@code tag} is {@code null} or
46      * empty.
47      */
48     public this(string tag)
49     {
50         this(null, tag);
51     }
52 
53     /**
54      * @uml
55      * Construct a new instance of {@link TagChunk} using the specified label
56      * and tag.
57      *
58      *  @param label The label for the tag. If this is {@code null}, the
59      *  {@link TagChunk} represents an unlabeled tag.
60      *  @param tag The tag, which should be the name of a parser rule or token
61      *  type.
62      *
63      *  @exception IllegalArgumentException if {@code tag} is {@code null} or
64      * empty.
65      */
66     public this(string label, string tag)
67     {
68         if (tag is null || tag.length == 0) {
69             throw new IllegalArgumentException("tag cannot be null or empty");
70         }
71         this.label = label;
72         this.tag = tag;
73     }
74 
75     /**
76      * @uml
77      * Get the tag for this chunk.
78      */
79     public string getTag()
80     {
81         return tag;
82     }
83 
84     /**
85      * @uml
86      * et the label, if any, assigned to this chunk.
87      *
88      *  @return The label assigned to this chunk, or {@code null} if no label is
89      * assigned to the chunk.
90      */
91     public string getLabel()
92     {
93         return label;
94     }
95 
96     /**
97      * @uml
98      * This method returns a text representation of the tag chunk. Labeled tags
99      * are returned in the form {@code label:tag}, and unlabeled tags are
100      * returned as just the tag name.
101      * @override
102      */
103     public override string toString()
104     {
105         if (label !is null) {
106             return label ~ ":" ~ tag;
107         }
108         return tag;
109     }
110 
111 }