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 "CommonToken.h"
8 #include "CharStream.h"
9
10 #include "CommonTokenFactory.h"
11
12 using namespace antlr4;
13
14 const std::unique_ptr<TokenFactory<CommonToken>> CommonTokenFactory::DEFAULT(new CommonTokenFactory);
15
CommonTokenFactory(bool copyText_)16 CommonTokenFactory::CommonTokenFactory(bool copyText_) : copyText(copyText_) {
17 }
18
CommonTokenFactory()19 CommonTokenFactory::CommonTokenFactory() : CommonTokenFactory(false) {
20 }
21
create(std::pair<TokenSource *,CharStream * > source,size_t type,const std::string & text,size_t channel,size_t start,size_t stop,size_t line,size_t charPositionInLine)22 std::unique_ptr<CommonToken> CommonTokenFactory::create(std::pair<TokenSource*, CharStream*> source, size_t type,
23 const std::string &text, size_t channel, size_t start, size_t stop, size_t line, size_t charPositionInLine) {
24
25 std::unique_ptr<CommonToken> t(new CommonToken(source, type, channel, start, stop));
26 t->setLine(line);
27 t->setCharPositionInLine(charPositionInLine);
28 if (text != "") {
29 t->setText(text);
30 } else if (copyText && source.second != nullptr) {
31 t->setText(source.second->getText(misc::Interval(start, stop)));
32 }
33
34 return t;
35 }
36
create(size_t type,const std::string & text)37 std::unique_ptr<CommonToken> CommonTokenFactory::create(size_t type, const std::string &text) {
38 return std::unique_ptr<CommonToken>(new CommonToken(type, text));
39 }
40