PageRenderTime 455ms CodeModel.GetById 130ms app.highlight 9ms RepoModel.GetById 122ms app.codeStats 0ms

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

http://github.com/nddrylliog/ooc
Java | 91 lines | 73 code | 18 blank | 0 comment | 19 complexity | ac714c151d2eb9db4508d24033c1d774 MD5 | raw file
 1package org.ooc.frontend.parser;
 2
 3import java.util.ArrayList;
 4import java.util.List;
 5
 6import org.ooc.frontend.model.Include;
 7import org.ooc.frontend.model.NodeList;
 8import org.ooc.frontend.model.Include.Define;
 9import org.ooc.frontend.model.Include.Mode;
10import org.ooc.frontend.model.tokens.Token;
11import org.ooc.frontend.model.tokens.TokenReader;
12import org.ooc.frontend.model.tokens.Token.TokenType;
13import org.ubi.CompilationFailedError;
14import org.ubi.SourceReader;
15
16public class IncludeParser {
17
18	public static boolean fill(SourceReader sReader, TokenReader reader, NodeList<Include> includes) throws CompilationFailedError {
19
20		Token startToken = reader.peek();
21		if(startToken.type != TokenType.INCLUDE_KW) return false;
22		reader.skip();
23		
24		StringBuilder sb = new StringBuilder();
25		List<Define> defines = new ArrayList<Define>();
26		
27		while(true) {
28		
29			Token token = reader.read();
30			if(token.type == TokenType.LINESEP) {
31				addInclude(includes, sb.toString(), defines, startToken);
32				break;
33			}
34			if(token.type == TokenType.COMMA) {
35				addInclude(includes, sb.toString(), defines, startToken);
36				sb.setLength(0);
37				defines.clear();
38			} else if(token.type == TokenType.PIPE) {
39				readDefines(sReader, reader, defines);
40			} else {
41				sb.append(token.get(sReader));
42			}
43			
44		}
45		
46		return true;
47		
48	}
49
50	protected static void readDefines(SourceReader sReader, TokenReader reader,
51			List<Define> defines) throws CompilationFailedError {
52		if(reader.read().type != TokenType.OPEN_PAREN) {
53			throw new CompilationFailedError(null, "Expected opening parenthesis to begin include defines, but got "
54					+reader.prev().type);
55		}
56		
57		while(true){
58			Define define = new Define();
59			define.name = reader.read().get(sReader);
60			if(reader.peek().type == TokenType.ASSIGN) {
61				reader.skip();
62				define.value = reader.read().get(sReader);
63			}
64			defines.add(define);
65			
66			if(reader.peek().type != TokenType.COMMA) {
67				break;
68			}
69			reader.skip();
70		}
71		
72		if(reader.read().type != TokenType.CLOS_PAREN) {
73			throw new CompilationFailedError(null, "Expected closing parenthesis to end include defines but got "
74					+reader.prev().type);
75		}
76	}
77
78	protected static void addInclude(NodeList<Include> includes, String contentParam, List<Define> defines, Token startToken) {
79		String content = contentParam;
80		Mode mode = Mode.PATHY;
81		if(content.startsWith("./")) {
82			content = content.substring(2);
83			mode = Mode.LOCAL;
84		}
85
86		Include include = new Include(content, mode, startToken);
87		include.getDefines().addAll(defines);
88		includes.add(include);
89	}
90	
91}