Parser.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package jplag.csharp;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import jplag.Structure;
  5. import jplag.UnicodeReader;
  6. import jplag.csharp.grammar.CSharpLexer;
  7. import jplag.csharp.grammar.CSharpParser;
  8. public class Parser extends jplag.Parser implements CSharpTokenConstants {
  9. private Structure struct;
  10. private String currentFile;
  11. public jplag.Structure parse(File dir, String files[]) {
  12. struct = new Structure();
  13. errors = 0;
  14. for (int i = 0; i < files.length; i++) {
  15. // getProgram().print(null, "Parsing file " + files[i] + "\n");
  16. if (!parseFile(dir, files[i]))
  17. errors++;
  18. struct.addToken(new CSharpToken(FILE_END, files[i], -1, -1, -1));
  19. }
  20. this.parseEnd();
  21. return struct;
  22. }
  23. private boolean parseFile(File dir, String file) {
  24. try {
  25. FileInputStream fis = new FileInputStream(new File(dir, file));
  26. currentFile = file;
  27. // Create a scanner that reads from the input stream passed to us
  28. CSharpLexer lexer = new CSharpLexer(new UnicodeReader(fis, "UTF-8"));
  29. lexer.setFilename(file);
  30. lexer.setTabSize(1);
  31. // Create a parser that reads from the scanner
  32. CSharpParser parser = new CSharpParser(lexer);
  33. parser.setFilename(file);
  34. parser.parser = this;//Added by emeric 22.01.05
  35. // start parsing at the compilationUnit rule
  36. parser.compilation_unit();
  37. // close file
  38. fis.close();
  39. } catch (Exception e) {
  40. getProgram().addError(" Parsing Error in '" + file + "':\n " + e.toString() + "\n");
  41. return false;
  42. }
  43. return true;
  44. }
  45. public void add(int type, antlr.Token tok) {
  46. if (tok == null) {
  47. System.out.println("tok == null ERROR!");
  48. return;
  49. }
  50. struct.addToken(new CSharpToken(type, currentFile, tok.getLine(), tok.getColumn(), tok.getText().length()));
  51. // System.out.println("type: " + CSharpToken.type2string(type) +
  52. // " text: '"+tok.getText()+"'");
  53. }
  54. public void add(int type, CSharpParser p) {
  55. add(type, p.getLastConsumedToken());
  56. }
  57. }