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 /**
40  * TODO add class description
41  */
42 class LexerIndexedCustomAction : LexerAction
43 {
44 
45     private int offset;
46 
47     private LexerAction action;
48 
49     /**
50      * @uml
51      * Constructs a new indexed custom action by associating a character offset
52      * with a {@link LexerAction}.
53      *
54      * <p>Note: This class is only required for lexer actions for which
55      * {@link LexerAction#isPositionDependent} returns {@code true}.</p>
56      *
57      *  @param offset The offset into the input {@link CharStream}, relative to
58      * the token start index, at which the specified lexer action should be
59      * executed.
60      *  @param action The lexer action to execute at a particular offset in the
61      * input {@link CharStream}.
62      */
63     public this(int offset, LexerAction action)
64     {
65 	this.offset = offset;
66         this.action = action;
67     }
68 
69     /**
70      * @uml
71      * Gets the location in the input {@link CharStream} at which the lexer
72      * action should be executed. The value is interpreted as an offset relative
73      * to the token start index.
74      *
75      *  @return The location in the input {@link CharStream} at which the lexer
76      * action should be executed.
77      */
78     public int getOffset()
79     {
80         return offset;
81     }
82 
83     /**
84      * @uml
85      * Gets the lexer action to execute.
86      *
87      *  @return A {@link LexerAction} object which executes the lexer action.
88      */
89     public LexerAction getAction()
90     {
91         return action;
92     }
93 
94     /**
95      * @uml
96      * {@inheritDoc}
97      *
98      *  @return This method returns the result of calling {@link #getActionType}
99      * on the {@link LexerAction} returned by {@link #getAction}.
100      */
101     public LexerActionType getActionType()
102     {
103         return action.getActionType();
104     }
105 
106     /**
107      * @uml
108      * {@inheritDoc}
109      *  @return This method returns {@code true}.
110      */
111     public bool isPositionDependent()
112     {
113 	return true;
114     }
115 
116     /**
117      * @uml
118      * {@inheritDoc}
119      *
120      * <p>This method calls {@link #execute} on the result of {@link #getAction}
121      * using the provided {@code lexer}.</p>
122      */
123     public void execute(InterfaceLexer lexer)
124     {
125         // assume the input stream position was properly set by the calling code
126         action.execute(lexer);
127     }
128 
129     /**
130      * @uml
131      * @nothrow
132      * @trusted
133      * @override
134      */
135     public override size_t toHash() @trusted nothrow
136     {
137         size_t hash = MurmurHash.initialize();
138         hash = MurmurHash.update(hash, offset);
139         hash = MurmurHash.update!LexerAction(hash, action);
140         return MurmurHash.finish(hash, 2);
141     }
142 
143     public bool equals(Object obj)
144     {
145         if (obj is this) {
146             return true;
147         }
148         else if (obj.classinfo != LexerIndexedCustomAction.classinfo) {
149             return false;
150         }
151         LexerIndexedCustomAction other = cast(LexerIndexedCustomAction)obj;
152         return offset == other.getOffset
153             && action == other.getAction;
154     }
155 
156 }