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