1 package de.fhdw.wtf.parser;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import de.fhdw.wtf.common.ast.Operation;
7 import de.fhdw.wtf.common.ast.OperationModifier;
8 import de.fhdw.wtf.common.ast.type.ClassType;
9 import de.fhdw.wtf.common.ast.type.ProductType;
10 import de.fhdw.wtf.common.ast.type.Type;
11 import de.fhdw.wtf.common.exception.parser.AbstractParserException;
12 import de.fhdw.wtf.common.stream.TokenStream;
13 import de.fhdw.wtf.common.token.IdentifierToken;
14 import de.fhdw.wtf.common.token.Token;
15
16
17
18
19 final class OperationParser {
20
21
22
23 private final TokenStream stream;
24
25
26
27
28 private final Collection<AbstractParserException> exceptions;
29
30
31
32
33
34
35
36
37
38 private OperationParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
39 this.stream = stream;
40 this.exceptions = exceptions;
41 }
42
43
44
45
46
47
48
49
50
51
52 static OperationParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
53 return new OperationParser(stream, exceptions);
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67 Operation parse(final String name, final ClassType containingType, final IdentifierToken firstToken)
68 throws AbstractParserException {
69 final Collection<OperationModifier> modifiers = new ArrayList<>();
70 final Token firstTokenProduct = this.stream.peek();
71 final ProductType params = ProductType.create(firstTokenProduct);
72 ParserUtils.requireAndRemoveBracketOpenToken(this.stream);
73 while (this.stream.hasNext() && this.stream.peek().isIdentifierToken()) {
74 params.addElement(ParameterParser.create(this.stream, this.exceptions).parse());
75 if (this.stream.peek().isCommaToken()) {
76 this.stream.removeFirst();
77 }
78 }
79 ParserUtils.requireAndRemoveBracketCloseToken(this.stream);
80 params.setLastToken(this.stream.peek());
81 ParserUtils.requireAndRemoveArrowToken(this.stream);
82 final Type type = TypeParser.create(this.stream, this.exceptions).parse();
83
84 final Operation result =
85 Operation.create(name, modifiers, params, containingType, type, firstToken, type.getLastToken());
86 ParserUtils.requireAndRemoveDoubleSquareBracketCloseToken(this.stream);
87 modifiers.addAll(OperationModifierParser.create(this.stream, this.exceptions).parse(result));
88 return result;
89 }
90 }