PageRenderTime 353ms CodeModel.GetById 161ms app.highlight 5ms RepoModel.GetById 186ms app.codeStats 0ms

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

http://github.com/nddrylliog/ooc
Java | 56 lines | 42 code | 13 blank | 1 comment | 5 complexity | f64f71e855a561084018c87884b1cf0b MD5 | raw file
 1package org.ooc.frontend.parser;
 2
 3import java.io.File;
 4import java.io.IOException;
 5import java.util.List;
 6
 7import org.ooc.frontend.BuildParams;
 8import org.ooc.frontend.model.Import;
 9import org.ooc.frontend.model.Module;
10import org.ooc.frontend.model.Tokenizer;
11import org.ooc.frontend.model.tokens.Token;
12import org.ooc.frontend.model.tokens.TokenReader;
13import org.ubi.CompilationFailedError;
14import org.ubi.SourceReader;
15
16public class Parser {
17	
18	protected BuildParams params;
19	protected Module mainModule;
20	
21	public Parser(BuildParams params) {
22		this.params = params;
23	}
24	
25	public Module parse(final String path) throws IOException {
26		final File file = params.sourcePath.getElement(path);
27		if(file == null) {
28			throw new CompilationFailedError(null, "File "+path+" not found in sourcePath."
29				+" sourcePath = "+params.sourcePath);
30		}
31		return parse(path, file, null);
32	}
33
34	public Module parse(final String path, final File element, Import imp) throws IOException {
35		if(params.verbose)
36			System.out.println("Parsing "+path);
37		
38		File file = new File(element, path);
39		
40		final SourceReader sReader = SourceReader.getReaderFromFile(file);
41		final List<Token> tokens = new Tokenizer().parse(sReader);
42		
43		final String fullName = path.substring(0, path.lastIndexOf('.'))
44			.replace(File.separatorChar, '.').replace('/', '.');
45		
46		final Module module = new Module(fullName, element, sReader);
47		ModuleParser.cache.put(path, module);
48		if(imp != null) imp.setModule(module);
49		
50		ModuleParser.parse(module, fullName, file,
51				sReader, new TokenReader(tokens), Parser.this);
52		//new XStream().toXML(module, new FileWriter(file.getName()+".xml"));
53		return module;
54	}
55		
56}