1 /*
2  * Copyright (c) 2012-2018 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.TerminalNodeImpl;
8 
9 import antlr.v4.runtime.InterfaceRecognizer;
10 import antlr.v4.runtime.RuleContext : RuleContext;
11 import antlr.v4.runtime.Token;
12 import antlr.v4.runtime.TokenConstantDefinition;
13 import antlr.v4.runtime.atn.StateNames;
14 import antlr.v4.runtime.misc.Interval;
15 import antlr.v4.runtime.tree.ParseTree;
16 import antlr.v4.runtime.tree.ParseTreeVisitor;
17 import antlr.v4.runtime.tree.TerminalNode;
18 import std.conv;
19 import std.variant;
20 
21 /**
22  * TODO add class description
23  */
24 class TerminalNodeImpl : TerminalNode
25 {
26 
27     public Token symbol;
28 
29     public ParseTree parent;
30 
31     public this(Token symbol)
32     {
33         this.symbol = symbol;
34     }
35 
36     public ParseTree getChild(int i)
37     {
38         return null;
39     }
40 
41     public ParseTree getParent()
42     {
43         return parent;
44     }
45 
46     public void setParent(RuleContext parent)
47     {
48         this.parent = parent;
49     }
50 
51     public Token getSymbol()
52     {
53         return symbol;
54     }
55 
56     public Object getPayload()
57     {
58         return cast(Object)symbol;
59     }
60 
61     public Interval getSourceInterval()
62     {
63         if (symbol is null)
64             return cast(Interval)Interval.INVALID;
65         auto tokenIndex = symbol.getTokenIndex();
66         return new Interval(to!int(tokenIndex), to!int(tokenIndex));
67 
68     }
69 
70     public int getChildCount()
71     {
72         return 0;
73     }
74 
75     public Variant accept(ParseTreeVisitor visitor)
76     {
77         return visitor.visitTerminal(this);
78     }
79 
80     public Variant getText()
81     {
82         return symbol.getText();
83     }
84 
85     public string toStringTree(InterfaceRecognizer parser)
86     {
87         return toString();
88     }
89 
90     /**
91      * @uml
92      * @override
93      */
94     public override string toString()
95     {
96         if (symbol.getType() == TokenConstantDefinition.EOF )
97             return "<EOF>";
98         return to!string(symbol.getText);
99     }
100 
101     public string toStringTree()
102     {
103         return toString();
104     }
105 
106 }