]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / tree / TerminalNodeImpl.cpp
1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2  * Use of this file is governed by the BSD 3-clause license that
3  * can be found in the LICENSE.txt file in the project root.
4  */
5
6 #include "misc/Interval.h"
7 #include "Token.h"
8 #include "RuleContext.h"
9 #include "tree/ParseTreeVisitor.h"
10
11 #include "tree/TerminalNodeImpl.h"
12
13 using namespace antlr4;
14 using namespace antlr4::tree;
15
16 TerminalNodeImpl::TerminalNodeImpl(Token *symbol_) : symbol(symbol_) {
17 }
18
19 Token* TerminalNodeImpl::getSymbol() {
20   return symbol;
21 }
22
23 void TerminalNodeImpl::setParent(RuleContext *parent_) {
24   this->parent = parent_;
25 }
26
27 misc::Interval TerminalNodeImpl::getSourceInterval() {
28   if (symbol == nullptr) {
29     return misc::Interval::INVALID;
30   }
31
32   size_t tokenIndex = symbol->getTokenIndex();
33   return misc::Interval(tokenIndex, tokenIndex);
34 }
35
36 antlrcpp::Any TerminalNodeImpl::accept(ParseTreeVisitor *visitor) {
37   return visitor->visitTerminal(this);
38 }
39
40 std::string TerminalNodeImpl::getText() {
41   return symbol->getText();
42 }
43
44 std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/, bool /*pretty*/) {
45   return toString();
46 }
47
48 std::string TerminalNodeImpl::toString() {
49   if (symbol->getType() == Token::EOF) {
50     return "<EOF>";
51   }
52   return symbol->getText();
53 }
54
55 std::string TerminalNodeImpl::toStringTree(bool /*pretty*/) {
56   return toString();
57 }