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