1 package de.fhdw.wtf.parser;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6
7 import de.fhdw.wtf.common.ast.type.ClassModifier;
8 import de.fhdw.wtf.common.ast.type.ClassModifierAbstract;
9 import de.fhdw.wtf.common.ast.type.ClassModifierService;
10 import de.fhdw.wtf.common.ast.type.ClassModifierTransient;
11 import de.fhdw.wtf.common.ast.type.ClassModifierVisitable;
12 import de.fhdw.wtf.common.ast.type.ClassType;
13 import de.fhdw.wtf.common.ast.visitor.ClassModifierVisitor;
14 import de.fhdw.wtf.common.exception.parser.AbstractParserException;
15 import de.fhdw.wtf.common.exception.parser.ClassModifierAlreadyAddedException;
16 import de.fhdw.wtf.common.exception.parser.NoSemicolonException;
17 import de.fhdw.wtf.common.stream.TokenStream;
18 import de.fhdw.wtf.common.token.Token;
19
20
21
22
23
24 public final class ClassModifierParser {
25
26
27
28 private final TokenStream stream;
29
30
31
32 private final Collection<AbstractParserException> exceptions;
33
34
35
36
37
38
39
40
41
42 private ClassModifierParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
43 this.stream = stream;
44 this.exceptions = exceptions;
45 }
46
47
48
49
50
51
52
53
54
55
56 static ClassModifierParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
57 return new ClassModifierParser(stream, exceptions);
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71 Collection<ClassModifier> parse(final ClassType currentClass) throws AbstractParserException {
72 final Collection<ClassModifier> result = new ArrayList<>();
73 while (this.stream.hasNext()) {
74 if (this.stream.peek().isSemicolonToken()) {
75 this.stream.next();
76 currentClass.setLastToken(this.stream.peek());
77 break;
78 } else {
79 try {
80 result.add(this.parseClassModifier(result));
81 } catch (final ClassModifierAlreadyAddedException e3) {
82 this.stream.removeFirst();
83 this.exceptions.add(e3);
84 }
85 }
86 }
87 return result;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 private ClassModifier parseClassModifier(final Collection<ClassModifier> modifiers) throws NoSemicolonException,
103 ClassModifierAlreadyAddedException {
104 final Token nextToken = this.stream.peek();
105 if (nextToken.isClassModifierToken()) {
106 if (containsClassModifier(modifiers, nextToken)) {
107 throw ClassModifierAlreadyAddedException.create(nextToken);
108 }
109 if (nextToken.isAbstractModifierToken()) {
110 this.stream.removeFirst();
111 return ClassModifierAbstract.create(nextToken, this.stream.peek());
112 }
113 if (nextToken.isServiceToken()) {
114 this.stream.removeFirst();
115 return ClassModifierService.create(nextToken, this.stream.peek());
116 }
117 if (nextToken.isTransientToken()) {
118 this.stream.removeFirst();
119 return ClassModifierTransient.create(nextToken, this.stream.peek());
120 }
121 if (nextToken.isVisitableToken()) {
122 this.stream.removeFirst();
123 return ClassModifierVisitable.create(nextToken, this.stream.peek());
124 }
125 }
126 throw NoSemicolonException.create(nextToken);
127 }
128
129
130
131
132
133
134
135
136
137
138
139 private static boolean containsClassModifier(final Collection<ClassModifier> modifiers, final Token modifier) {
140 final Iterator<ClassModifier> i = modifiers.iterator();
141 while (i.hasNext()) {
142 final ClassModifier current = i.next();
143 if (current.accept(new ClassModifierVisitor() {
144 @Override
145 public boolean handle(final ClassModifierAbstract abstract1) {
146 return modifier.isAbstractModifierToken();
147 }
148
149 @Override
150 public boolean handle(final ClassModifierTransient tranzient) {
151 return modifier.isTransientToken();
152 }
153
154 @Override
155 public boolean handle(final ClassModifierService service) {
156 return modifier.isServiceToken();
157 }
158
159 @Override
160 public boolean handle(final ClassModifierVisitable visitable) {
161 return modifier.isVisitableToken();
162 }
163 })) {
164 return true;
165 }
166 }
167 return false;
168 }
169 }