RustParserAdapter.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package de.jplag.rust;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import org.antlr.v4.runtime.CharStreams;
  6. import org.antlr.v4.runtime.CommonTokenStream;
  7. import org.antlr.v4.runtime.ParserRuleContext;
  8. import org.antlr.v4.runtime.tree.ParseTree;
  9. import org.antlr.v4.runtime.tree.ParseTreeWalker;
  10. import de.jplag.AbstractParser;
  11. import de.jplag.TokenConstants;
  12. import de.jplag.TokenList;
  13. import de.jplag.rust.grammar.RustLexer;
  14. import de.jplag.rust.grammar.RustParser;
  15. public class RustParserAdapter extends AbstractParser {
  16. private static final int NOT_SET = -1;
  17. private String currentFile;
  18. private TokenList tokens;
  19. /**
  20. * Parsers a list of files into a single {@link TokenList}.
  21. * @param directory the directory of the files.
  22. * @param fileNames the file names of the files.
  23. * @return a {@link TokenList} containing all tokens of all files.
  24. */
  25. public TokenList parse(File directory, String[] fileNames) {
  26. tokens = new TokenList();
  27. errors = 0;
  28. for (String fileName : fileNames) {
  29. if (!parseFile(directory, fileName)) {
  30. errors++;
  31. }
  32. tokens.addToken(new RustToken(TokenConstants.FILE_END, fileName, NOT_SET, NOT_SET, NOT_SET));
  33. }
  34. return tokens;
  35. }
  36. private boolean parseFile(File directory, String fileName) {
  37. File file = new File(directory, fileName);
  38. try (FileInputStream inputStream = new FileInputStream(file)) {
  39. currentFile = fileName;
  40. // create a lexer, a parser and a buffer between them.
  41. RustLexer lexer = new RustLexer(CharStreams.fromStream(inputStream));
  42. CommonTokenStream tokenStream = new CommonTokenStream(lexer);
  43. RustParser parser = new RustParser(tokenStream);
  44. // Create a tree walker and the entry context defined by the parser grammar
  45. ParserRuleContext entryContext = parser.crate();
  46. ParseTreeWalker treeWalker = new ParseTreeWalker();
  47. // Walk over the parse tree:
  48. for (int i = 0; i < entryContext.getChildCount(); i++) {
  49. ParseTree parseTree = entryContext.getChild(i);
  50. treeWalker.walk(new JplagRustListener(this), parseTree);
  51. }
  52. } catch (IOException exception) {
  53. logger.error("Parsing Error in '" + fileName + "':" + File.separator, exception);
  54. return false;
  55. }
  56. return true;
  57. }
  58. /**
  59. * Adds a new {@link de.jplag.Token} to the current {@link TokenList}.
  60. * @param type the type of the new {@link de.jplag.Token}
  61. * @param line the line of the Token in the current file
  62. * @param start the start column of the Token in the line
  63. * @param length the length of the Token
  64. */
  65. /* package-private */ void addToken(int type, int line, int start, int length) {
  66. tokens.addToken(new RustToken(type, currentFile, line, start, length));
  67. }
  68. }