RParserAdapter.java 3.3 KB

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