RParserAdapter.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package de.jplag.rlang;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.antlr.v4.runtime.CharStreams;
  8. import org.antlr.v4.runtime.CommonTokenStream;
  9. import org.antlr.v4.runtime.ParserRuleContext;
  10. import org.antlr.v4.runtime.tree.ParseTree;
  11. import org.antlr.v4.runtime.tree.ParseTreeWalker;
  12. import de.jplag.AbstractParser;
  13. import de.jplag.Token;
  14. import de.jplag.rlang.grammar.RFilter;
  15. import de.jplag.rlang.grammar.RLexer;
  16. import de.jplag.rlang.grammar.RParser;
  17. /**
  18. * This class sets up the lexer and parser generated by ANTLR4, feeds the submissions through them and passes the
  19. * selected tokens on to the main program.
  20. */
  21. public class RParserAdapter extends AbstractParser implements RTokenConstants {
  22. private String currentFile;
  23. private List<Token> tokens;
  24. /**
  25. * Creates the RParserAdapter
  26. */
  27. public RParserAdapter() {
  28. super();
  29. }
  30. /**
  31. * Parsers a list of files into a single token list of {@link Token}s.
  32. * @param directory the directory of the files.
  33. * @param fileNames the file names of the files.
  34. * @return a list containing all tokens of all files.
  35. */
  36. public List<Token> parse(File directory, String[] fileNames) {
  37. tokens = new ArrayList<>();
  38. errors = 0;
  39. for (String fileName : fileNames) {
  40. if (!parseFile(directory, fileName)) {
  41. errors++;
  42. }
  43. tokens.add(new RToken(FILE_END, fileName, -1, -1, -1));
  44. }
  45. return tokens;
  46. }
  47. private boolean parseFile(File directory, String fileName) {
  48. File file = new File(directory, fileName);
  49. try (FileInputStream inputStream = new FileInputStream(file)) {
  50. currentFile = fileName;
  51. // create a lexer, a parser and a buffer between them.
  52. RLexer lexer = new RLexer(CharStreams.fromStream(inputStream));
  53. CommonTokenStream tokens = new CommonTokenStream(lexer);
  54. RFilter filter = new RFilter(tokens);
  55. filter.stream();
  56. tokens.seek(0);
  57. RParser parser = new RParser(tokens);
  58. // Create a tree walker and the entry context defined by the parser grammar
  59. ParserRuleContext entryContext = parser.prog();
  60. ParseTreeWalker treeWalker = new ParseTreeWalker();
  61. // Walk over the parse tree:
  62. for (int i = 0; i < entryContext.getChildCount(); i++) {
  63. ParseTree parseTree = entryContext.getChild(i);
  64. treeWalker.walk(new JplagRListener(this), parseTree);
  65. }
  66. } catch (IOException exception) {
  67. logger.error("Parsing Error in '" + fileName + "': " + File.separator + exception.getMessage(), exception);
  68. return false;
  69. }
  70. return true;
  71. }
  72. /**
  73. * Adds a new {@link Token} to the current token list.
  74. * @param type the type of the new {@link Token}
  75. * @param line the line of the Token in the current file
  76. * @param start the start column of the Token in the line
  77. * @param length the length of the Token
  78. */
  79. /* package-private */ void addToken(int type, int line, int start, int length) {
  80. tokens.add(new RToken(type, currentFile, line, start, length));
  81. }
  82. }