1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2013 Terence Parr
4  *  Copyright (c) 2013 Sam Harwell
5  *  Copyright (c) 2017 Egbert Voigt
6  *  All rights reserved.
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions
10  *  are met:
11  *
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. The name of the author may not be used to endorse or promote products
18  *     derived from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 module antlr.v4.runtime.atn.LexerIndexedCustomAction;
33 
34 import antlr.v4.runtime.atn.LexerAction;
35 import antlr.v4.runtime.atn.LexerActionType;
36 import antlr.v4.runtime.InterfaceLexer;
37 import antlr.v4.runtime.misc.MurmurHash;
38 
39 // Class LexerIndexedCustomAction
40 /**
41  * TODO add class description
42  */
43 class LexerIndexedCustomAction : LexerAction
44 {
45 
46     private int offset;
47 
48     private LexerAction action;
49 
50     /**
51      * @uml
52      * Constructs a new indexed custom action by associating a character offset
53      * with a {@link LexerAction}.
54      *
55      * <p>Note: This class is only required for lexer actions for which
56      * {@link LexerAction#isPositionDependent} returns {@code true}.</p>
57      *
58      *  @param offset The offset into the input {@link CharStream}, relative to
59      * the token start index, at which the specified lexer action should be
60      * executed.
61      *  @param action The lexer action to execute at a particular offset in the
62      * input {@link CharStream}.
63      */
64     public this(int offset, LexerAction action)
65     {
66 	this.offset = offset;
67         this.action = action;
68     }
69 
70     /**
71      * @uml
72      * Gets the location in the input {@link CharStream} at which the lexer
73      * action should be executed. The value is interpreted as an offset relative
74      * to the token start index.
75      *
76      *  @return The location in the input {@link CharStream} at which the lexer
77      * action should be executed.
78      */
79     public int getOffset()
80     {
81         return offset;
82     }
83 
84     /**
85      * @uml
86      * Gets the lexer action to execute.
87      *
88      *  @return A {@link LexerAction} object which executes the lexer action.
89      */
90     public LexerAction getAction()
91     {
92         return action;
93     }
94 
95     /**
96      * @uml
97      * {@inheritDoc}
98      *
99      *  @return This method returns the result of calling {@link #getActionType}
100      * on the {@link LexerAction} returned by {@link #getAction}.
101      */
102     public LexerActionType getActionType()
103     {
104         return action.getActionType();
105     }
106 
107     /**
108      * @uml
109      * {@inheritDoc}
110      *  @return This method returns {@code true}.
111      */
112     public bool isPositionDependent()
113     {
114 	return true;
115     }
116 
117     /**
118      * @uml
119      * {@inheritDoc}
120      *
121      * <p>This method calls {@link #execute} on the result of {@link #getAction}
122      * using the provided {@code lexer}.</p>
123      */
124     public void execute(InterfaceLexer lexer)
125     {
126         // assume the input stream position was properly set by the calling code
127         action.execute(lexer);
128     }
129 
130     /**
131      * @uml
132      * @nothrow
133      * @trusted
134      * @override
135      */
136     public override size_t toHash() @trusted nothrow
137     {
138         size_t hash = MurmurHash.initialize();
139         hash = MurmurHash.update(hash, offset);
140         hash = MurmurHash.update!LexerAction(hash, action);
141         return MurmurHash.finish(hash, 2);
142     }
143 
144     public bool equals(Object obj)
145     {
146         if (obj is this) {
147             return true;
148         }
149         else if (obj.classinfo != LexerIndexedCustomAction.classinfo) {
150             return false;
151         }
152         LexerIndexedCustomAction other = cast(LexerIndexedCustomAction)obj;
153         return offset == other.getOffset
154             && action == other.getAction;
155     }
156 
157 }