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