1 module antlr.v4.runtime.CommonTokenFactory;
2 
3 import std.typecons;
4 import antlr.v4.runtime.TokenFactory;
5 import antlr.v4.runtime.CommonToken;
6 import antlr.v4.runtime.CharStream;
7 import antlr.v4.runtime.TokenSource;
8 import antlr.v4.runtime.misc.Interval;
9 
10 alias TokenFactorySourcePair = Tuple!(TokenSource, "a", CharStream, "b");
11 
12 /**
13  * This default implementation of {@link TokenFactory} creates
14  * {@link CommonToken} objects.
15  * <p>
16  * This token factory does not explicitly copy token text when constructing
17  * tokens.</p>
18  */
19 class CommonTokenFactory : TokenFactory!CommonToken
20 {
21 
22     /**
23      * The single instance of CommonTokenFactory.
24      * @uml
25      * @__gshared
26      */
27     private static __gshared CommonTokenFactory instance_;
28 
29     /**
30      * @uml
31      * Indicates whether {@link CommonToken#setText} should be called after
32      * constructing tokens to explicitly set the text. This is useful for cases
33      * where the input stream might not be able to provide arbitrary substrings
34      * of text from the input after the lexer creates a token (e.g. the
35      * implementation of {@link CharStream#getText} in
36      * {@link UnbufferedCharStream} throws an
37      * {@link UnsupportedOperationException}). Explicitly setting the token text
38      * allows {@link Token#getText} to be called at any time regardless of the
39      * input stream implementation.
40      *
41      * <p>
42      * The default value is {@code false} to avoid the performance and memory
43      * overhead of copying text for every token unless explicitly requested.</p>
44      */
45     protected bool copyText;
46 
47     /**
48      * @uml
49      * Constructs a {@link CommonTokenFactory} with the specified value for
50      * {@link #copyText}.
51      *
52      * <p>
53      * then {@code copyText} is {@code false}, the {@link #DEFAULT} instance
54      * should be used instead of constructing a new instance.</p>
55      *
56      *  @param copyText The value for {@link #copyText}.
57      */
58     public this(bool copyText)
59     {
60         this.copyText = copyText;
61     }
62 
63     /**
64      * @uml
65      * Constructs a {@link CommonTokenFactory} with {@link #copyText} set to
66      * {@code false}.
67      *
68      * <p>
69      * The {@link #DEFAULT} instance should be used instead of calling this
70      * directly.</p>
71      */
72     public this()
73     {
74         this(false);
75     }
76 
77     public CommonToken create(TokenFactorySourcePair source, int type, string text, int channel,
78         int start, int stop, int line, int charPositionInLine)
79     {
80         CommonToken t = new CommonToken(source, type, channel, start, stop);
81         t.setLine(line);
82         t.setCharPositionInLine(charPositionInLine);
83         if (text !is null) {
84             t.setText(text);
85         }
86         else if (copyText && source.b !is null ) {
87             t.setText(source.b.getText(Interval.of(start,stop)));
88         }
89         return t;
90     }
91 
92     public CommonToken create(int type, string text)
93     {
94         return new CommonToken(type, text);
95     }
96 
97     /**
98      * Creates the single instance of CommonTokenFactory.
99      * @uml
100      * @shared
101      */
102     private shared static this()
103     {
104         instance_ = new CommonTokenFactory;
105     }
106 
107     /**
108      * Returns: A single default instance of CommonTokenFactory.
109      */
110     public static CommonTokenFactory DEFAULT()
111     {
112         return instance_;
113     }
114 
115 }