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.LexerModeAction;
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 // Class LexerModeAction
41 /**
42  * @uml
43  * Implements the {@code mode} lexer action by calling {@link Lexer#mode} with
44  * the assigned mode.
45  */
46 class LexerModeAction : LexerAction
47 {
48 
49     public int mode;
50 
51     public this(int mode)
52     {
53         this.mode = mode;
54     }
55 
56     /**
57      * @uml
58      * Get the lexer mode this action should transition the lexer to.
59      *
60      *  @return The lexer mode for this {@code mode} command.
61      */
62     public int getMode()
63     {
64         return mode;
65     }
66 
67     /**
68      * @uml
69      * {@inheritDoc}
70      *  @return This method returns {@link LexerActionType#MODE}.
71      * @safe
72      * @nothrow
73      */
74     public LexerActionType getActionType() @safe nothrow
75     {
76         return LexerActionType.MODE;
77     }
78 
79     /**
80      * @uml
81      * {@inheritDoc}
82      *  @return This method returns {@code false}.
83      */
84     public bool isPositionDependent()
85     {
86         return false;
87     }
88 
89     /**
90      * @uml
91      * {@inheritDoc}
92      *
93      *  <p>This action is implemented by calling {@link Lexer#mode} with the
94      *  value provided by {@link #getMode}.</p>
95      */
96     public void execute(InterfaceLexer lexer)
97     {
98         lexer.mode(mode);
99     }
100 
101     /**
102      * @uml
103      * @safe
104      * @nothrow
105      * @override
106      */
107     public override size_t toHash() @safe nothrow
108     {
109         size_t hash = MurmurHash.initialize();
110         hash = MurmurHash.update(hash, Utils.rank(getActionType));
111         hash = MurmurHash.update(hash, mode);
112         return MurmurHash.finish(hash, 2);
113     }
114 
115     public bool equals(Object obj)
116     {
117         if (obj == this) {
118             return true;
119         }
120         else if (obj.classinfo != LexerModeAction.classinfo) {
121             return false;
122         }
123         return mode == (cast(LexerModeAction)obj).mode;
124     }
125 
126     /**
127      * @uml
128      * @override
129      */
130     public override string toString()
131     {
132         return format("mode(%d)", mode);
133     }
134 
135 }