/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

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