Skip to content

Method: create(Position)

1: package de.fhdw.wtf.dsl.scanner.modelScanner.state;
2:
3: import java.util.regex.Pattern;
4:
5: import de.fhdw.wtf.common.stream.TokenStream;
6: import de.fhdw.wtf.common.token.IdentifierToken;
7: import de.fhdw.wtf.common.token.Position;
8: import de.fhdw.wtf.common.token.keywords.AbstractToken;
9: import de.fhdw.wtf.common.token.keywords.ClassToken;
10: import de.fhdw.wtf.common.token.keywords.ExceptionToken;
11: import de.fhdw.wtf.common.token.keywords.FindableToken;
12: import de.fhdw.wtf.common.token.keywords.GroupToken;
13: import de.fhdw.wtf.common.token.keywords.MutableToken;
14: import de.fhdw.wtf.common.token.keywords.OperationToken;
15: import de.fhdw.wtf.common.token.keywords.PriorToken;
16: import de.fhdw.wtf.common.token.keywords.ServiceToken;
17: import de.fhdw.wtf.common.token.keywords.SymmetricToken;
18: import de.fhdw.wtf.common.token.keywords.TransientToken;
19: import de.fhdw.wtf.common.token.keywords.VisitableToken;
20:
21: /**
22: * ScannerState for recognition of multi-char Tokens such as KeywordToken or {@link IdentifierToken}.
23: *
24: * @author tajoa
25: */
26: public final class KeywordIdentifierState extends MultiCharState {
27:         
28:         /**
29:          * Specifies that the given class is abstract.
30:          */
31:         private static final String CLASS_MODI_ABSTRACT = "abstract";
32:         /**
33:          * Specifies that the given identifier is typed as class.
34:          */
35:         private static final String CLASS = "class";
36:         /**
37:          * Specifies that the given identifier is typed as operation.
38:          */
39:         private static final String OPERATION = "function";
40:         /**
41:          * Specifies that the given identifier is typed as group.
42:          */
43:         private static final String GROUP = "group";
44:         /**
45:          * Specifies that the given identifier is typed as group.
46:          */
47:         private static final String PRIOR = "prior";
48:         
49:         /**
50:          * Specifies that the given class is an exception.
51:          */
52:         private static final String EXCEPTION = "exception";
53:         
54:         /**
55:          * Specifies that the given Attribute is findable.
56:          */
57:         private static final String FINDABLE = "findable";
58:         
59:         /**
60:          * Specifies that the given Attribute or Class is transient.
61:          */
62:         private static final String TRANSIENT = "transient";
63:         
64:         /**
65:          * Specifies that the given Class is a service.
66:          */
67:         private static final String SERVICE = "service";
68:         
69:         /**
70:          * Specifies that the given attribute is mutable.
71:          */
72:         private static final String MUTABLE = "mutable";
73:         /**
74:          * Specifies that for the given classes a visitor will be created.
75:          */
76:         private static final String VISITABLE = "visitable";
77:         /**
78:          * Specifies that for the given attribute a reverse getter will be created in the target type.
79:          */
80:         private static final String SYMMETRIC = "symmetric";
81:         
82:         /**
83:          * Specifies all allowed char.
84:          */
85:         private static final String ALLOWED_CHARS = "\\w";
86:         
87:         /**
88:          * <b>PRIVATE</b> Constructor.
89:          *
90:          * @param currentInput
91:          * initializes currentInput
92:          * @param firstPosition
93:          * initializes firstPostion
94:          */
95:         private KeywordIdentifierState(final String currentInput, final Position firstPosition) {
96:                 super(currentInput, firstPosition);
97:         }
98:         
99:         /**
100:          * Factory for {@link KeywordIdentifierState}.
101:          *
102:          * @param firstPosition
103:          * initializes firstPostion
104:          * @return {@link KeywordIdentifierState}.
105:          */
106:         public static KeywordIdentifierState create(final Position firstPosition) {
107:                 return new KeywordIdentifierState("", firstPosition);
108:         }
109:         
110:         @Override
111:         public void finish(final TokenStream outputStream) {
112:                 if (this.getCurrentInput().isEmpty()) {
113:                         return;
114:                 }
115:                 if (this.getCurrentInput().equals(KeywordIdentifierState.CLASS)) {
116:                         outputStream.add(ClassToken.create(this.getFirstPosition()));
117:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.CLASS_MODI_ABSTRACT)) {
118:                         outputStream.add(AbstractToken.create(this.getFirstPosition()));
119:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.GROUP)) {
120:                         outputStream.add(GroupToken.create(this.getFirstPosition()));
121:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.PRIOR)) {
122:                         outputStream.add(PriorToken.create(this.getFirstPosition()));
123:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.EXCEPTION)) {
124:                         outputStream.add(ExceptionToken.create(this.getFirstPosition()));
125:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.FINDABLE)) {
126:                         outputStream.add(FindableToken.create(this.getFirstPosition()));
127:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.TRANSIENT)) {
128:                         outputStream.add(TransientToken.create(this.getFirstPosition()));
129:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.SERVICE)) {
130:                         outputStream.add(ServiceToken.create(this.getFirstPosition()));
131:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.OPERATION)) {
132:                         outputStream.add(OperationToken.create(this.getFirstPosition()));
133:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.MUTABLE)) {
134:                         outputStream.add(MutableToken.create(this.getFirstPosition()));
135:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.VISITABLE)) {
136:                         outputStream.add(VisitableToken.create(this.getFirstPosition()));
137:                 } else if (this.getCurrentInput().equals(KeywordIdentifierState.SYMMETRIC)) {
138:                         outputStream.add(SymmetricToken.create(this.getFirstPosition()));
139:                 } else {
140:                         outputStream.add(IdentifierToken.create(this.getCurrentInput(), this.getFirstPosition()));
141:                 }
142:         }
143:         
144:         /**
145:          * Checks whether the input char is a valid identifier/keyword character or not.
146:          *
147:          * @param input
148:          * character to be checked
149:          * @return <code>true</code> if input is a allowed char. <br>
150:          * <code>false</code> if input is not allowed.
151:          */
152:         public static boolean isAllowedCharacter(final char input) {
153:                 return Pattern.matches(KeywordIdentifierState.ALLOWED_CHARS, Character.toString(input));
154:         }
155:         
156:         @Override
157:         public boolean accept(final ScannerStateVisitor visitor) {
158:                 return visitor.handleKeywordIdentifierState();
159:         }
160: }