PageRenderTime 36ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/nddrylliog/ooc
Java | 154 lines | 125 code | 29 blank | 0 comment | 37 complexity | 699ff5222ccfa27a21e69b85aa617822 MD5 | raw file
  1package org.ooc.frontend.parser;
  2
  3import java.io.File;
  4import java.io.IOException;
  5import java.util.HashMap;
  6import java.util.HashSet;
  7import java.util.Map;
  8import java.util.Set;
  9import java.util.StringTokenizer;
 10
 11import org.ooc.frontend.BuildParams;
 12import org.ooc.frontend.model.tokens.Token;
 13import org.ooc.middle.UseDef;
 14import org.ooc.middle.UseDef.Requirement;
 15import org.ubi.CompilationFailedError;
 16import org.ubi.SourceReader;
 17
 18public class UseDefParser {
 19
 20	protected static Map<String, UseDef> cache = new HashMap<String, UseDef>();
 21	
 22	public static UseDef parse(String identifier, SourceReader sReader, Token token, BuildParams params) throws IOException {
 23		
 24		if(params.veryVerbose) System.out.println("Parsing usefile "+identifier);
 25		
 26		UseDef cached = cache.get(identifier);
 27		if(cached != null) {
 28			if(params.veryVerbose) System.out.println("Getting usefile "+identifier+" from cache");
 29			return cached;
 30		}
 31		
 32		File file = findUse(identifier+".use", params);
 33		if(file == null) {
 34			throw new CompilationFailedError(sReader.getLocation(token),
 35					"Use not found in the ooc library path: "+identifier
 36					+"\n\nTo install ooc libraries, copy their directories to /usr/lib/ooc/" +
 37					"\nIf you want to install libraries elsewhere, use the OOC_LIBS environment variable," +
 38					"\nwhich is the path ooc will scan for .use files (in this case, "+identifier+".use)" +
 39					"\nFor more informations, see http://docs.ooc-lang.org/libs.html" +
 40					"\n-------------------");
 41		}
 42		
 43		if(params.veryVerbose) System.out.println("Found usefile "+identifier+" at "+file.getAbsolutePath());
 44		
 45		UseDef def = new UseDef(identifier);
 46		cache.put(identifier, def);
 47		
 48		SourceReader reader = SourceReader.getReaderFromFile(file);
 49		while(reader.hasNext()) {
 50			reader.skipWhitespace();
 51			
 52			if(reader.matches("#", false)) {
 53				reader.skipLine();
 54				continue;
 55			}
 56			
 57			if(reader.matches("=", false)) {
 58				reader.skipLine();
 59				continue;
 60			}
 61			
 62			String id = reader.readUntil(':', false).trim();
 63			reader.read(); // skip the ':'
 64			String value = reader.readLine().trim();
 65			if(id.equals("Name")) {
 66				def.setName(value);
 67			} else if(id.equals("Description")) {
 68				def.setDescription(value);
 69			} else if(id.equals("Pkgs")) {
 70				StringTokenizer st = new StringTokenizer(value, ",");
 71				while(st.hasMoreTokens()) def.getPkgs().add(st.nextToken().trim());
 72			} else if(id.equals("Libs")) {
 73				StringTokenizer st = new StringTokenizer(value, ",");
 74				while(st.hasMoreTokens()) def.getLibs().add(st.nextToken().trim()); 
 75			} else if(id.equals("Includes")) {
 76				StringTokenizer st = new StringTokenizer(value, ",");
 77				while(st.hasMoreTokens()) def.getIncludes().add(st.nextToken().trim()); 
 78			} else if(id.equals("LibPaths")) {
 79				StringTokenizer st = new StringTokenizer(value, ",");
 80				while(st.hasMoreTokens()) {
 81					String libPath = st.nextToken().trim();
 82					File libFile = new File(file.getParent(), libPath);
 83					if(!libFile.isAbsolute()) {
 84						libPath = libFile.getCanonicalPath();
 85					}
 86					def.getLibPaths().add(libPath);
 87				} 
 88			} else if(id.equals("IncludePaths")) {
 89				StringTokenizer st = new StringTokenizer(value, ",");
 90				while(st.hasMoreTokens()) {
 91					String includePath = st.nextToken().trim();
 92					if(!(new File(includePath).isAbsolute())) {
 93						includePath = new File(file.getParent(), includePath).getCanonicalPath();
 94					}
 95					def.getIncludePaths().add(includePath);
 96				} 
 97			} else if(id.equals("Requires")) {
 98				StringTokenizer st = new StringTokenizer(value, ",");
 99				while(st.hasMoreTokens()) {
100					def.getRequirements().add(new Requirement(st.nextToken().trim(), new int[] {0}));
101				}
102			} else if(id.equals("SourcePath")) {
103				File path = new File(value);
104				if(!path.isAbsolute()) {
105					path = new File(file.getParent(), value);
106				}
107				if(params.verbose) {
108					System.out.println("Adding "+path+" to sourcePath from "+def);
109				}
110				params.sourcePath.add(path.getPath());
111				
112				
113			}
114			
115			reader.skipWhitespace();
116		}
117		
118		for(Requirement req: def.getRequirements()) {
119			req.setDef(parse(req.getName(), sReader, token, params));
120		}
121		
122		if(params.veryVerbose) System.out.println("Finished reading usefile "+file.getAbsolutePath());
123		
124		return def;
125		
126	}
127
128	private static File findUse(String fileName, BuildParams params) {
129		
130		Set<File> set = new HashSet<File>();
131		set.add(params.libsPath);
132		set.add(params.sdkLocation);
133		
134		while(!set.isEmpty()) {
135			Set<File> nextSet = new HashSet<File>();
136			for(File candidate: set) {
137				if(candidate.getPath().endsWith(fileName)) {
138					return candidate;
139				} else if(candidate.isDirectory()) {
140					for(File child: candidate.listFiles()) {
141						nextSet.add(child);
142					}
143				}
144			}
145			set = nextSet;
146		}
147		
148		return null;
149		
150	}
151
152	
153	
154}