PageRenderTime 264ms CodeModel.GetById 120ms app.highlight 8ms RepoModel.GetById 133ms app.codeStats 0ms

/src/org/ooc/frontend/parser/StatementParser.java

http://github.com/nddrylliog/ooc
Java | 72 lines | 56 code | 16 blank | 0 comment | 22 complexity | 2358aed532d077da4f33eb6deb8a1fa2 MD5 | raw file
 1package org.ooc.frontend.parser;
 2
 3import org.ooc.frontend.model.Block;
 4import org.ooc.frontend.model.Conditional;
 5import org.ooc.frontend.model.Else;
 6import org.ooc.frontend.model.Expression;
 7import org.ooc.frontend.model.FlowControl;
 8import org.ooc.frontend.model.For;
 9import org.ooc.frontend.model.Foreach;
10import org.ooc.frontend.model.Module;
11import org.ooc.frontend.model.Return;
12import org.ooc.frontend.model.Statement;
13import org.ooc.frontend.model.VersionBlock;
14import org.ooc.frontend.model.FlowControl.Mode;
15import org.ooc.frontend.model.tokens.TokenReader;
16import org.ooc.frontend.model.tokens.Token.TokenType;
17import org.ubi.CompilationFailedError;
18import org.ubi.SourceReader;
19
20public class StatementParser {
21
22	public static Statement parse(Module module, SourceReader sReader, TokenReader reader) {
23
24		int mark = reader.mark();
25		
26		Foreach foreach = ForParser.parseForeach(module, sReader, reader);
27		if(foreach != null) return foreach;
28		
29		For for1 = ForParser.parseRegularFor(module, sReader, reader);
30		if(for1 != null) return for1;
31		
32		Conditional conditional = ConditionalParser.parse(module, sReader, reader);
33		if(conditional != null) return conditional;
34		
35		if(reader.peek().type == TokenType.ELSE_KW) {
36			Else else1 = new Else(reader.read());
37			ControlStatementFiller.fill(module, sReader, reader, else1.getBody());
38			return else1;
39		}
40		
41		if(reader.peek().type == TokenType.BREAK_KW) return new FlowControl(Mode.BREAK, reader.read());
42		if(reader.peek().type == TokenType.CONTINUE_KW) return new FlowControl(Mode.CONTINUE, reader.read());
43		
44		VersionBlock verBlock = VersionBlockParser.parse(module, sReader, reader);
45		if(verBlock != null) return verBlock;
46		
47		Return ret = ReturnParser.parse(module, sReader, reader);
48		if(ret != null) return ret;
49		
50		Expression expression = ExpressionParser.parse(module, sReader, reader);
51		if(expression != null) return expression;
52		
53		if(reader.peek().type == TokenType.OPEN_BRACK) {
54			Block block = new Block(reader.peek());
55			reader.skip();
56			while(reader.peek().type != TokenType.CLOS_BRACK) {
57				if(!LineParser.fill(module, sReader, reader, block.getBody())) {
58					throw new CompilationFailedError(sReader.getLocation(reader.peek()),
59							"Expected lines or '}' in block, but got "+reader.peek());
60				}
61				reader.skipWhitespace();
62			}
63			reader.skip(); // skip the closing bracket.
64			return block;
65		}
66		
67		reader.reset(mark);
68		return null;
69		
70	}
71	
72}