Skip to content

Method: create(TokenStream, Collection)

1: package de.fhdw.wtf.parser;
2:
3: import java.util.Collection;
4:
5: import de.fhdw.wtf.common.ast.Model;
6: import de.fhdw.wtf.common.ast.Name;
7: import de.fhdw.wtf.common.ast.UnqualifiedName;
8: import de.fhdw.wtf.common.exception.parser.AbstractParserException;
9: import de.fhdw.wtf.common.stream.TokenStream;
10: import de.fhdw.wtf.common.token.IdentifierToken;
11: import de.fhdw.wtf.common.token.Token;
12:
13: /**
14: * Parser to parse a model from a given TokenStream.
15: */
16: final class ModelParser {
17:         /**
18:          * TokenStream.
19:          */
20:         private final TokenStream stream;
21:         
22:         /**
23:          * Collection of exceptions.
24:          */
25:         private final Collection<AbstractParserException> exceptions;
26:         
27:         /**
28:          * Constructor of {@link ModelParser}.
29:          *
30:          * @param stream
31:          * tokenStream
32:          * @param exceptions
33:          * collection of exceptions
34:          */
35:         private ModelParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
36:                 this.stream = stream;
37:                 this.exceptions = exceptions;
38:         }
39:         
40:         /**
41:          * FactoryMethod for {@link ModelParser}.
42:          *
43:          * @param stream
44:          * tokenStream
45:          * @param exceptions
46:          * collection of exceptions
47:          * @return The {@link ModelParser}-Object.
48:          */
49:         static ModelParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
50:                 return new ModelParser(stream, exceptions);
51:         }
52:         
53:         /**
54:          * Parses Model from a given TokenStream. If the Stream does not contain any Token a empty Model will be returned.
55:          * Any Exceptions ocurring are being put in a Collection, accessible through the Parser-Object.
56:          *
57:          * @return Model
58:          */
59:         Model parse() {
60:                 final Model model = Model.create(this.stream.peek());
61:                 
62:                 while (this.stream.hasNext() && !this.stream.peek().isEndToken()) {
63:                         try {
64:                                 final IdentifierToken identifierToken = ParserUtils.requireAndRemoveIdentifier(this.stream);
65:                                 final Token nextToken = this.stream.peek();
66:                                 ParserUtils.requireAndRemoveColonToken(this.stream);
67:                                 ParserUtils.requireAndRemoveGroupToken(this.stream);
68:                                 final Name name = UnqualifiedName.create(identifierToken, nextToken);
69:                                 model.addGroup(GroupParser.create(this.stream, this.exceptions).parse(name, identifierToken));
70:                         } catch (final AbstractParserException e) {
71:                                 this.exceptions.add(e);
72:                                 ParserUtils.skipToSemicolonToken(this.stream);
73:                                 continue;
74:                         }
75:                 }
76:                 
77:                 model.setLastToken(this.stream.peek());
78:                 
79:                 return model;
80:         }
81:         
82: }