1 /*
2 * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3 * Use of this file is governed by the BSD 3-clause license that
4 * can be found in the LICENSE.txt file in the project root.
5 */
6
7 module antlr.v4.runtime.atn.LexerPushModeAction;
8
9 import std.format;
10 import antlr.v4.runtime.InterfaceLexer;
11 import antlr.v4.runtime.atn.LexerAction;
12 import antlr.v4.runtime.atn.LexerActionType;
13 import antlr.v4.runtime.misc;
14
15 /**
16 * @uml
17 * Implements the {@code pushMode} lexer action by calling
18 * {@link Lexer#pushMode} with the assigned mode.
19 */
20 class LexerPushModeAction : LexerAction
21 {
22
23 public int mode;
24
25 /**
26 * @uml
27 * Constructs a new {@code pushMode} action with the specified mode value.
28 * @param mode The mode value to pass to {@link Lexer#pushMode}.
29 */
30 public this(int mode)
31 {
32 this.mode = mode;
33 }
34
35 /**
36 * @uml
37 * Get the lexer mode this action should transition the lexer to.
38 *
39 * @return The lexer mode for this {@code pushMode} command.
40 */
41 public int getMode()
42 {
43 return mode;
44 }
45
46 /**
47 * @uml
48 * @safe
49 * @nothrow
50 */
51 public LexerActionType getActionType() @safe nothrow
52 {
53 return LexerActionType.PUSH_MODE;
54 }
55
56 /**
57 * @uml
58 * {@inheritDoc}
59 * @return This method returns {@code false}.
60 */
61 public bool isPositionDependent()
62 {
63 return false;
64 }
65
66 /**
67 * @uml
68 * {@inheritDoc}
69 *
70 * <p>This action is implemented by calling {@link Lexer#pushMode} with the
71 * value provided by {@link #getMode}.</p>
72 */
73 public void execute(InterfaceLexer lexer)
74 {
75 lexer.pushMode(mode);
76 }
77
78 /**
79 * @uml
80 * @safe
81 * @nothrow
82 * @override
83 */
84 public override size_t toHash() @safe nothrow
85 {
86 size_t hash = MurmurHash.initialize();
87 hash = MurmurHash.update(hash, Utils.rank(getActionType));
88 return MurmurHash.finish(hash, 1);
89
90 }
91
92 public bool equals(Object obj)
93 {
94 if (obj == this) {
95 return true;
96 }
97 else if (obj.classinfo != LexerPushModeAction.classinfo) {
98 return false;
99 }
100 return mode == (cast(LexerPushModeAction)obj).mode;
101 }
102
103 /**
104 * @uml
105 * @override
106 */
107 public override string toString()
108 {
109 return format("pushMode(%d)", mode);
110 }
111
112 }
113
114 unittest
115 {
116 auto pushModeAction = new LexerPushModeAction(0);
117 }