Skip to content

Method: requireAndRemovePlusSymbol(TokenStream)

1: package de.fhdw.wtf.parser;
2:
3: import de.fhdw.wtf.common.exception.parser.NoArrowException;
4: import de.fhdw.wtf.common.exception.parser.NoBracketCloseException;
5: import de.fhdw.wtf.common.exception.parser.NoBracketOpenException;
6: import de.fhdw.wtf.common.exception.parser.NoColonException;
7: import de.fhdw.wtf.common.exception.parser.NoCurlyBracketCloseException;
8: import de.fhdw.wtf.common.exception.parser.NoCurlyBracketOpenException;
9: import de.fhdw.wtf.common.exception.parser.NoDoubleSquareBracketCloseException;
10: import de.fhdw.wtf.common.exception.parser.NoEqualException;
11: import de.fhdw.wtf.common.exception.parser.NoGroupElementException;
12: import de.fhdw.wtf.common.exception.parser.NoGroupException;
13: import de.fhdw.wtf.common.exception.parser.NoIdentifierException;
14: import de.fhdw.wtf.common.exception.parser.NoPlusSymbolException;
15: import de.fhdw.wtf.common.exception.parser.NoSemicolonException;
16: import de.fhdw.wtf.common.exception.parser.NoSquareBracketCloseException;
17: import de.fhdw.wtf.common.exception.parser.NoSquareBracketOpenException;
18: import de.fhdw.wtf.common.stream.TokenStream;
19: import de.fhdw.wtf.common.token.IdentifierToken;
20: import de.fhdw.wtf.common.token.Token;
21: import de.fhdw.wtf.common.token.symbols.PlusSymbolToken;
22:
23: /**
24: * Contains util methods for Parser.
25: */
26: final class ParserUtils {
27:         
28:         /**
29:          * Constructor of {@link ParserUtils}.
30:          */
31:         private ParserUtils() {
32:         }
33:         
34:         /**
35:          * Takes the next Token from this.stream and checks if the Token is an IdentifierToken. If it is, it will be removed
36:          * from this.stream and returned, otherwise a NoIdentifierException will be returned.
37:          *
38:          * @param stream
39:          * stream
40:          * @return {@link IdentifierToken}
41:          * @throws NoIdentifierException
42:          * possible Exception
43:          */
44:         static IdentifierToken requireAndRemoveIdentifier(final TokenStream stream) throws NoIdentifierException {
45:                 final Token token = stream.peek();
46:                 if (token.isIdentifierToken()) {
47:                         final IdentifierToken iToken = (IdentifierToken) token;
48:                         stream.removeFirst();
49:                         return iToken;
50:                 } else {
51:                         throw NoIdentifierException.create(token);
52:                 }
53:         }
54:         
55:         /**
56:          * Throws a {@link NoArrowException} if the next Token of <code>this.stream</code> is not an ArrowToken.
57:          *
58:          * @param stream
59:          * stream
60:          * @throws NoArrowException
61:          * possible Exception
62:          */
63:         static void requireAndRemoveArrowToken(final TokenStream stream) throws NoArrowException {
64:                 if (!stream.peek().isArrowToken()) {
65:                         throw NoArrowException.create(stream.peek());
66:                 }
67:                 stream.next();
68:         }
69:         
70:         /**
71:          * Throws a NoColonException if the next Token of this.stream is a not a ColonToken.
72:          *
73:          * @param stream
74:          * stream
75:          * @throws NoColonException
76:          * possible Exception
77:          */
78:         static void requireAndRemoveColonToken(final TokenStream stream) throws NoColonException {
79:                 final Token nextToken = stream.peek();
80:                 if (!(nextToken.isColonToken())) {
81:                         throw NoColonException.create(nextToken);
82:                 }
83:                 stream.removeFirst();
84:         }
85:         
86:         /**
87:          * Throws a {@link NoBracketOpenException} if the next Token of <code>this.getStream()</code> is not a
88:          * BracketOpenToken.
89:          *
90:          * @param stream
91:          * stream
92:          * @throws NoBracketOpenException
93:          * possible Exception
94:          */
95:         static void requireAndRemoveBracketOpenToken(final TokenStream stream) throws NoBracketOpenException {
96:                 final Token token = stream.peek();
97:                 if (!token.isBracketOpenToken()) {
98:                         throw NoBracketOpenException.create(token);
99:                 }
100:                 stream.removeFirst();
101:         }
102:         
103:         /**
104:          * Throws a {@link NoBracketCloseException} if the next Token of <code>this.stream</code> is not a
105:          * BracketCloseToken.
106:          *
107:          * @param stream
108:          * stream
109:          * @return {@link Token}
110:          * @throws NoBracketCloseException
111:          * possible Exception
112:          */
113:         static Token requireAndRemoveBracketCloseToken(final TokenStream stream) throws NoBracketCloseException {
114:                 final Token token = stream.peek();
115:                 if (!token.isBracketCloseToken()) {
116:                         throw NoBracketCloseException.create(token);
117:                 }
118:                 return stream.next();
119:         }
120:         
121:         /**
122:          * Throws a {@link NoSemicolonException} if the next Token of this.stream is a not a SemicolonToken.
123:          *
124:          * @param stream
125:          * stream
126:          * @throws NoSemicolonException
127:          * possible Exception
128:          */
129:         static void requireSemicolonToken(final TokenStream stream) throws NoSemicolonException {
130:                 final Token nextToken = stream.peek();
131:                 if (!(nextToken.isSemicolonToken())) {
132:                         throw NoSemicolonException.create(nextToken);
133:                 }
134:         }
135:         
136:         /**
137:          * If the next element of this.stream is not a ColonToken, the element will be removed and skipToSemicolonToken will
138:          * be called again. Otherwise only the SemicolonToken will be removed.
139:          *
140:          * @param stream
141:          * stream
142:          */
143:         static void skipToSemicolonToken(final TokenStream stream) {
144:                 // First semicolon is the reason for exception.
145:                 // if (stream.hasNext() && stream.peek().isSemicolonToken()) {
146:                 // stream.removeFirst();
147:                 // }
148:                 if (stream.hasNext() && !(stream.peek().isSemicolonToken()) && !(stream.peek().isEndToken())) {
149:                         stream.removeFirst();
150:                         ParserUtils.skipToSemicolonToken(stream);
151:                 }
152:                 if (stream.hasNext() && stream.peek().isSemicolonToken()) {
153:                         stream.removeFirst();
154:                 }
155:         }
156:         
157:         /**
158:          * Throws a NoSquareBracketCloseException if the next Token of this.stream is a not a SquareBracketCloseToken.
159:          *
160:          * @param stream
161:          * stream
162:          * @throws NoSquareBracketCloseException
163:          * possible Exception
164:          */
165:         static void requireAndRemoveSquareBracketCloseToken(final TokenStream stream) throws NoSquareBracketCloseException {
166:                 final Token nextToken = stream.peek();
167:                 if (!nextToken.isSquareBracketCloseToken()) {
168:                         throw NoSquareBracketCloseException.create(nextToken);
169:                 }
170:                 stream.removeFirst();
171:         }
172:         
173:         /**
174:          * Throws a {@link NoDoubleSquareBracketCloseException} if the next Token of this.stream is a not a
175:          * DoubleSquareBracketCloseToken.
176:          *
177:          * @param stream
178:          * stream
179:          * @throws NoDoubleSquareBracketCloseException
180:          * possible Exception
181:          */
182:         static void requireAndRemoveDoubleSquareBracketCloseToken(final TokenStream stream)
183:                         throws NoDoubleSquareBracketCloseException {
184:                 final Token nextToken = stream.peek();
185:                 if (!nextToken.isDoubleSquareBracketCloseToken()) {
186:                         throw NoDoubleSquareBracketCloseException.create(nextToken);
187:                 }
188:                 stream.removeFirst();
189:         }
190:         
191:         /**
192:          * Throws a NoEqualException if the next Token of this.stream is not an EqualToken.
193:          *
194:          * @param stream
195:          * stream
196:          * @throws NoEqualException
197:          * possible Exception
198:          */
199:         static void requireAndRemoveEqualToken(final TokenStream stream) throws NoEqualException {
200:                 final Token nextToken = stream.peek();
201:                 if (!(nextToken.isEqualToken())) {
202:                         throw NoEqualException.create(nextToken);
203:                 }
204:                 stream.removeFirst();
205:         }
206:         
207:         /**
208:          * Throws a {@link NoPlusSymbolException} if the next Token of this.stream is a not a {@link PlusSymbolToken}.
209:          *
210:          * @param stream
211:          * stream
212:          * @return {@link PlusSymbolToken}
213:          * @throws NoPlusSymbolException
214:          * possible Exception
215:          */
216:         static PlusSymbolToken requireAndRemovePlusSymbol(final TokenStream stream) throws NoPlusSymbolException {
217:                 final Token nextToken = stream.peek();
218:•                if (nextToken.isPlusSymbolToken()) {
219:                         stream.removeFirst();
220:                         return (PlusSymbolToken) nextToken;
221:                 } else {
222:                         throw NoPlusSymbolException.create(nextToken);
223:                 }
224:         }
225:         
226:         /**
227:          * Throws a NoCurlyBracketOpenException if the next Token of this.stream is not a CurlyBracketOpenToken.
228:          *
229:          * @param stream
230:          * stream
231:          * @throws NoCurlyBracketOpenException
232:          * possible Exception
233:          */
234:         static void requireAndRemoveCurlyBracketOpenToken(final TokenStream stream) throws NoCurlyBracketOpenException {
235:                 final Token nextToken = stream.peek();
236:                 if (!(nextToken.isCurlyBracketOpenToken())) {
237:                         throw NoCurlyBracketOpenException.create(nextToken);
238:                 }
239:                 stream.removeFirst();
240:         }
241:         
242:         /**
243:          * Throws a NoCurlyBracketCloseException if the next Token of this.stream is not a CurlyBracketCloseToken.
244:          *
245:          * @param stream
246:          * stream
247:          * @throws NoCurlyBracketCloseException
248:          * possible Exception
249:          */
250:         static void requireAndRemoveCurlyBracketCloseToken(final TokenStream stream) throws NoCurlyBracketCloseException {
251:                 final Token nextToken = stream.peek();
252:                 if (!(nextToken.isCurlyBracketCloseToken())) {
253:                         throw NoCurlyBracketCloseException.create(nextToken);
254:                 }
255:                 stream.removeFirst();
256:         }
257:         
258:         /**
259:          * Throws a NoSquareBracketOpenException if the next Token of this.stream is not a SquareBracketOpenToken.
260:          *
261:          * @param stream
262:          * stream
263:          * @throws NoSquareBracketOpenException
264:          * possible Exception
265:          */
266:         static void requireAndRemoveSquareBracketOpenToken(final TokenStream stream) throws NoSquareBracketOpenException {
267:                 final Token nextToken = stream.peek();
268:                 if (!(nextToken.isSquareBracketOpenToken())) {
269:                         throw NoSquareBracketOpenException.create(nextToken);
270:                 }
271:                 stream.removeFirst();
272:         }
273:         
274:         /**
275:          * Throws a NoGroupElementException if the next Token of this.stream is not a GroupToken or a ClassToken.
276:          *
277:          * @param stream
278:          * stream
279:          * @throws NoGroupElementException
280:          * possible Exception
281:          */
282:         static void requireGroupElement(final TokenStream stream) throws NoGroupElementException {
283:                 final Token nextToken = stream.peek();
284:                 if (!(nextToken.isGroupToken() || nextToken.isClassToken() || nextToken.isExceptionToken())) {
285:                         throw NoGroupElementException.create(nextToken);
286:                 }
287:         }
288:         
289:         /**
290:          * Throws a NoGroupException if the next Token of this.stream is not a GroupToken.
291:          *
292:          * @param stream
293:          * stream
294:          * @throws NoGroupException
295:          * possible Exception
296:          */
297:         static void requireAndRemoveGroupToken(final TokenStream stream) throws NoGroupException {
298:                 final Token token = stream.next();
299:                 if (!(token.isGroupToken())) {
300:                         throw NoGroupException.create(token);
301:                 }
302:         }
303: }