Skip to content

Method: finish(TokenStream)

1: package de.fhdw.wtf.dsl.scanner.modelScanner.state;
2:
3: import de.fhdw.wtf.common.stream.TokenStream;
4: import de.fhdw.wtf.common.token.InvalidToken;
5: import de.fhdw.wtf.common.token.Position;
6:
7: /**
8: * ScannerState for recognition of any {@link Character} <br>
9: * that hasn't been recognised by any other ScannerState.
10: */
11: public final class InvalidState extends MultiCharState {
12:         
13:         /**
14:          * PRIVATE Constructor.
15:          *
16:          * @param currentInput
17:          * startInput
18:          * @param firstPosition
19:          * of the first char.
20:          */
21:         private InvalidState(final String currentInput, final Position firstPosition) {
22:                 super(currentInput, firstPosition);
23:         }
24:         
25:         /**
26:          * Factory for {@link InvalidState}.
27:          *
28:          * @param position
29:          * of the first char.
30:          * @return {@link InvalidState}
31:          */
32:         public static InvalidState create(final Position position) {
33:                 return new InvalidState("", position);
34:         }
35:         
36:         @Override
37:         public void finish(final TokenStream outputStream) {
38:•                if (!this.getCurrentInput().isEmpty()) {
39:                         outputStream.add(InvalidToken.create(this.getCurrentInput(), this.getFirstPosition()));
40:                 }
41:         }
42:         
43:         /**
44:          * Checks if the given char is invalid for WTF-Code.
45:          *
46:          * @param input
47:          * char
48:          * @return true if invalid
49:          */
50:         public static boolean isInValid(final char input) {
51:                 return !SymbolState.isSymbol(input) && !KeywordIdentifierState.isAllowedCharacter(input)
52:                                 && !Character.isWhitespace(input) && input != '#';
53:         }
54:         
55:         @Override
56:         public boolean accept(final ScannerStateVisitor visitor) {
57:                 return visitor.handleInvalidState();
58:         }
59: }