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 package org.antlr.v4.runtime.atn;
8 
9 import org.antlr.v4.runtime.Lexer;
10 import org.antlr.v4.runtime.misc.MurmurHash;
11 
12 /**
13  * Implements the {@code pushMode} lexer action by calling
14  * {@link Lexer#pushMode} with the assigned mode.
15  *
16  * @author Sam Harwell
17  * @since 4.2
18  */
19 public final class LexerPushModeAction implements LexerAction {
20 	private final int mode;
21 
22 	/**
23 	 * Constructs a new {@code pushMode} action with the specified mode value.
24 	 * @param mode The mode value to pass to {@link Lexer#pushMode}.
25 	 */
LexerPushModeAction(int mode)26 	public LexerPushModeAction(int mode) {
27 		this.mode = mode;
28 	}
29 
30 	/**
31 	 * Get the lexer mode this action should transition the lexer to.
32 	 *
33 	 * @return The lexer mode for this {@code pushMode} command.
34 	 */
getMode()35 	public int getMode() {
36 		return mode;
37 	}
38 
39 	/**
40 	 * {@inheritDoc}
41 	 * @return This method returns {@link LexerActionType#PUSH_MODE}.
42 	 */
43 	@Override
getActionType()44 	public LexerActionType getActionType() {
45 		return LexerActionType.PUSH_MODE;
46 	}
47 
48 	/**
49 	 * {@inheritDoc}
50 	 * @return This method returns {@code false}.
51 	 */
52 	@Override
isPositionDependent()53 	public boolean isPositionDependent() {
54 		return false;
55 	}
56 
57 	/**
58 	 * {@inheritDoc}
59 	 *
60 	 * <p>This action is implemented by calling {@link Lexer#pushMode} with the
61 	 * value provided by {@link #getMode}.</p>
62 	 */
63 	@Override
execute(Lexer lexer)64 	public void execute(Lexer lexer) {
65 		lexer.pushMode(mode);
66 	}
67 
68 	@Override
hashCode()69 	public int hashCode() {
70 		int hash = MurmurHash.initialize();
71 		hash = MurmurHash.update(hash, getActionType().ordinal());
72 		hash = MurmurHash.update(hash, mode);
73 		return MurmurHash.finish(hash, 2);
74 	}
75 
76 	@Override
equals(Object obj)77 	public boolean equals(Object obj) {
78 		if (obj == this) {
79 			return true;
80 		}
81 		else if (!(obj instanceof LexerPushModeAction)) {
82 			return false;
83 		}
84 
85 		return mode == ((LexerPushModeAction)obj).mode;
86 	}
87 
88 	@Override
toString()89 	public String toString() {
90 		return String.format("pushMode(%d)", mode);
91 	}
92 }
93