1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2013 Terence Parr
4  *  Copyright (c) 2013 Sam Harwell
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *  1. Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *  2. Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *  3. The name of the author may not be used to endorse or promote products
17  *     derived from this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 module antlr.v4.runtime.atn.LexerChannelAction;
32 
33 import std.format;
34 import antlr.v4.runtime.InterfaceLexer;
35 import antlr.v4.runtime.atn.LexerAction;
36 import antlr.v4.runtime.atn.LexerActionType;
37 import antlr.v4.runtime.misc.MurmurHash;
38 import antlr.v4.runtime.misc.Utils;
39 
40 /**
41  * @uml
42  * @final
43  */
44 class LexerChannelAction : LexerAction
45 {
46 
47     private int channel;
48 
49     /**
50      * @uml
51      * Constructs a new {@code channel} action with the specified channel value.
52      *  @param channel The channel value to pass to {@link Lexer#setChannel}.
53      */
54     public this(int channel)
55     {
56         this.channel = channel;
57     }
58 
59     /**
60      * @uml
61      * Gets the channel to use for the {@link Token} created by the lexer
62      *  @return The channel to use for the {@link Token} created by the lexer.
63      */
64     public int getChannel()
65     {
66         return channel;
67     }
68 
69     /**
70      * @uml
71      * @override
72      * @safe
73      * @nothrow
74      */
75     public override LexerActionType getActionType() @safe nothrow
76     {
77         return LexerActionType.CHANNEL;
78     }
79 
80     /**
81      * @uml
82      * @override
83      */
84     public override bool isPositionDependent()
85     {
86         return false;
87     }
88 
89     /**
90      * @uml
91      * <p>This action is implemented by calling {@link Lexer#setChannel} with the
92      * value provided by {@link #getChannel}.</p>
93      * @override
94      */
95     public override void execute(InterfaceLexer lexer)
96     {
97         lexer.setChannel(channel);
98     }
99 
100     /**
101      * @uml
102      * @override
103      */
104     public override size_t toHash()
105     {
106         size_t hash = MurmurHash.initialize();
107         hash = MurmurHash.update(hash, Utils.rank(getActionType));
108         hash = MurmurHash.update(hash, channel);
109         return MurmurHash.finish(hash, 2);
110     }
111 
112     public bool equals(Object obj)
113     {
114         if (obj is this) {
115             return true;
116         }
117         else if (obj.classinfo != LexerChannelAction.classinfo) {
118             return false;
119         }
120         return channel == (cast(LexerChannelAction)obj).channel;
121     }
122 
123     /**
124      * @uml
125      * @override
126      */
127     public override string toString()
128     {
129         return format("channel(%d)", channel);
130     }
131 
132 }