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