Skip to content

Method: SumParser(TokenStream, Collection)

1: package de.fhdw.wtf.parser;
2:
3: import java.util.Collection;
4:
5: import de.fhdw.wtf.common.ast.type.SumType;
6: import de.fhdw.wtf.common.ast.type.ThrownType;
7: import de.fhdw.wtf.common.ast.type.Type;
8: import de.fhdw.wtf.common.exception.parser.AbstractParserException;
9: import de.fhdw.wtf.common.exception.parser.NoCurlyBracketCloseException;
10: import de.fhdw.wtf.common.exception.parser.NoTypeException;
11: import de.fhdw.wtf.common.stream.TokenStream;
12: import de.fhdw.wtf.common.token.Token;
13:
14: /**
15: * Parser to parse the given TokenStream to {@link SumType}s.
16: */
17: final class SumParser {
18:         
19:         /**
20:          * TokenStream.
21:          */
22:         private final TokenStream stream;
23:         
24:         /**
25:          * Collection of exceptions.
26:          */
27:         private final Collection<AbstractParserException> exceptions;
28:         
29:         /**
30:          * Constructor of {@link SumParser}.
31:          *
32:          * @param stream
33:          * tokenStream
34:          * @param exceptions
35:          * collection of exceptions
36:          */
37:         private SumParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
38:                 this.stream = stream;
39:                 this.exceptions = exceptions;
40:         }
41:         
42:         /**
43:          * FactoryMethod for {@link SumParser}.
44:          *
45:          * @param stream
46:          * tokenStream
47:          * @param exceptions
48:          * collection of exceptions
49:          * @return The {@link SumParser}-Object.
50:          */
51:         static SumParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
52:                 return new SumParser(stream, exceptions);
53:         }
54:         
55:         /**
56:          * Parses a series of tokens and creates a {@link SumType} from it. If the series of tokens does not match the
57:          * grammar an Exception will be thrown.
58:          *
59:          * @return SumType
60:          * @throws AbstractParserException
61:          * AbstractParserException
62:          */
63:         Type parse() throws AbstractParserException {
64:                 final Token firstToken = this.stream.next();
65:                 final SumType sum = SumType.create(firstToken);
66:                 boolean nextTokenType = true;
67:                 if (this.stream.peek().isCurlyBracketCloseToken()) {
68:                         nextTokenType = false;
69:                 }
70:                 while (!this.stream.peek().isCurlyBracketCloseToken() && this.stream.hasNext() && nextTokenType) {
71:                         
72:                         sum.add(TypeParser.create(this.stream, this.exceptions).parse());
73:                         
74:                         if (this.stream.peek().isCommaToken()) {
75:                                 this.stream.removeFirst();
76:                                 nextTokenType = true;
77:                         } else {
78:                                 nextTokenType = false;
79:                         }
80:                         
81:                 }
82:                 if (nextTokenType) {
83:                         throw NoTypeException.create(this.stream.peek());
84:                 }
85:                 if (this.stream.peek().isCurlyBracketCloseToken()) {
86:                         this.stream.next();
87:                 } else {
88:                         throw NoCurlyBracketCloseException.create(this.stream.peek());
89:                 }
90:                 sum.setLastToken(this.stream.peek());
91:                 if (this.stream.peek().isExclamationToken()) {
92:                         final ThrownType thrownType = ThrownType.create(sum.getFirstToken(), sum, sum.getLastToken());
93:                         this.stream.next();
94:                         
95:                         return thrownType;
96:                 }
97:                 return sum;
98:         }
99: }