You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

makeparser.py 4.3KB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # set these first four variables appropriately for your system
  2. eclipseWorkspace = "c:/aspectj/2.1/eclipse/workspace"
  3. workingDir = "c:/apps/jikespg/jdt/tmp"
  4. jikespg = "c:/apps/jikespg/src/jikespg.exe"
  5. makersc = "c:/j2sdk1.4/bin/java -classpath c:/aspectj/2.1/eclipse/workspace/org.eclipse.jdt.core/bin;c:/apps/jikespg/jdt UpdateParserFiles"
  6. # the rest of this should never change
  7. ajCompilerHomeRel = "org.aspectj.ajdt.core/src/"
  8. javaCompilerHomeRel = "org.eclipse.jdt.core/compiler/"
  9. compilerHomeRel = ajCompilerHomeRel
  10. parserHomeRel = ajCompilerHomeRel + "org/aspectj/ajdt/internal/compiler/parser"
  11. parserInfoFileRel = javaCompilerHomeRel + "org/eclipse/jdt/internal/compiler/parser/ParserBasicInformation.java"
  12. symbolsHomeRel = javaCompilerHomeRel + "org/eclipse/jdt/internal/compiler/parser/TerminalTokens.java"
  13. # symbolsHomeRel = "org/aspectj/ajdt/compiler/IAjTerminalSymbols.java"
  14. parserClass = "AjParser.java"
  15. grammarFileRel = javaCompilerHomeRel + "../grammar/java_1_4.g"
  16. import os
  17. from os import path
  18. import re
  19. def readFile(name, mode=''):
  20. f = open(name, 'r'+mode)
  21. text = f.read()
  22. f.close()
  23. return text
  24. def writeFile(name, text, mode=''):
  25. f = open(name, 'w'+mode)
  26. f.write(text)
  27. f.close()
  28. compilerHome = path.join(eclipseWorkspace, compilerHomeRel)
  29. parserHome = path.join(eclipseWorkspace, parserHomeRel)
  30. symbolFile = path.join(eclipseWorkspace, symbolsHomeRel)
  31. parserInfoFile = path.join(eclipseWorkspace, parserInfoFileRel)
  32. parserFile = path.join(parserHome, parserClass)
  33. parserText = readFile(parserFile)
  34. if grammarFileRel == None:
  35. r = re.compile(r"public final static void grammar\(\){.*(--main.*\$end\n)-- need", re.DOTALL)
  36. match = r.search(parserText)
  37. grammar = match.group(1)
  38. else:
  39. grammar = readFile(path.join(eclipseWorkspace, grammarFileRel), 'b')
  40. #print grammar
  41. grammarFile = path.join(workingDir, "java.g")
  42. writeFile(grammarFile, grammar, 'b')
  43. os.chdir(workingDir)
  44. os.system("%s java.g" % jikespg)
  45. #3.1 Copy the contents of the JavaAction.java file into the consumeRule(int) method of the org.eclipse.jdt.internal.compiler.parser.Parser class.
  46. newConsumeRule = readFile(path.join(workingDir, "JavaAction.java"))
  47. #print newConsumeRule
  48. r = re.compile(r"(// This method is part of an automatic generation : do NOT edit-modify\W+protected void consumeRule\(int act\) {.*)protected void consumeSimpleAssertStatement\(\) {", re.DOTALL)
  49. match = r.search(parserText)
  50. parserText = parserText.replace(match.group(1), newConsumeRule)
  51. #3.2 The definition of the Parser needs to be updated with two tables from javadcl.java. Those are rhs[] and name[].
  52. newTables = readFile(path.join(workingDir, "javadcl.java"))
  53. r = re.compile(r"(public final static byte rhs\[\] = \{[^}]*\};)", re.DOTALL)
  54. rhsTable = r.search(newTables).group(0)
  55. parserText = parserText.replace(r.search(parserText).group(0), rhsTable)
  56. r = re.compile(r"(public final static String name\[\] = \{[^}]*\}[^}]*\};)", re.DOTALL)
  57. nameTable = r.search(newTables).group(0)
  58. nameTable = nameTable.replace("\"$eof\"", "UNEXPECTED_EOF")
  59. nameTable = nameTable.replace("$error", "Invalid Character")
  60. parserText = parserText.replace(r.search(parserText).group(0), nameTable)
  61. #we're done w/ Parser.java
  62. writeFile(parserFile, parserText)
  63. #3.3 The class org.eclipse.jdt.internal.compiler.parser.ParserBasicInformation needs to be updated with the content of the file javadef.java.
  64. defs = readFile(path.join(workingDir, "javadef.java"))
  65. r = re.compile(r"(public final static int[^;]*;)", re.DOTALL)
  66. syms = r.search(defs).group(0)
  67. #print syms
  68. text = readFile(parserInfoFile)
  69. text = text.replace(r.search(text).group(0), syms)
  70. writeFile(parserInfoFile, text)
  71. #3.4 This is the contents of the class org.eclipse.jdt.internal.compiler.parser.TerminalSymbols.
  72. defs = readFile(path.join(workingDir, "javasym.java"))
  73. r = re.compile(r"(int\s+TokenNameIdentifier[^;]*;)", re.DOTALL)
  74. syms = r.search(defs).group(0)
  75. syms = syms.replace("$eof", "EOF")
  76. syms = syms.replace("$error", "ERROR")
  77. print syms
  78. text = readFile(symbolFile)
  79. text = text.replace(r.search(text).group(0), syms)
  80. writeFile(symbolFile, text)
  81. #3.5 The last step is to update the resource files:
  82. os.chdir(workingDir)
  83. os.system("%s javadcl.java" % makersc)
  84. for i in range(1,6):
  85. name = "parser%d.rsc" % i
  86. print "moving", name
  87. t = readFile(path.join(workingDir, name), 'b')
  88. writeFile(path.join(parserHome, name), t, 'b')