SyntaxError.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package common;
  7. import common.ErrorReport;
  8. import java.io.PrintWriter;
  9. import java.io.StringWriter;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import org.antlr.v4.runtime.BaseErrorListener;
  13. import org.antlr.v4.runtime.RecognitionException;
  14. import org.antlr.v4.runtime.Recognizer;
  15. /**
  16. *
  17. * @author Eugenio
  18. */
  19. public class SyntaxError extends BaseErrorListener implements ErrorReport {
  20. protected List<SyntaxErrorItem> items;
  21. public SyntaxError() {
  22. this.items = new ArrayList<SyntaxErrorItem>();
  23. }
  24. @Override
  25. public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
  26. items.add(new SyntaxErrorItem(line, charPositionInLine, msg, offendingSymbol, e));
  27. }
  28. // public boolean hasErrors() {
  29. // return this.items.size() > 0;
  30. // }
  31. @Override
  32. public String toString() {
  33. if (!HasError()) {
  34. return "0 errors";
  35. }
  36. StringBuilder builder = new StringBuilder();
  37. for (SyntaxErrorItem s : items) {
  38. builder.append(String.format("%s\n", s));
  39. }
  40. return builder.toString();
  41. }
  42. public void PrintErros() {
  43. System.out.println(toString());
  44. }
  45. @Override
  46. public boolean HasError() {
  47. return !items.isEmpty();
  48. }
  49. protected class SyntaxErrorItem {
  50. private int line;
  51. private Object offendingSymbol;
  52. private int column;
  53. private String msg;
  54. private RecognitionException oops;
  55. protected String file;
  56. SyntaxErrorItem(int line, int column, String msg, Object symbol, RecognitionException oops) {
  57. this.line = line;
  58. this.column = column;
  59. this.offendingSymbol = symbol;
  60. this.oops = oops;
  61. if (oops != null) {
  62. String path[] = oops.getInputStream().getSourceName().split("\\\\");
  63. file = path[path.length - 1];
  64. // System.out.println(oops.getClass().getCanonicalName());
  65. }
  66. if (msg.contains("extraneous input")) {
  67. msg = msg.replace("extraneous input", "Entrada invalida");
  68. msg = msg.replace("expecting", "esperando");
  69. } else if (msg.contains("recognition")) {
  70. msg = msg.replace("token recognition", "Erro de reconhecimento do simbólo");
  71. } else if (msg.contains("no viable")) {
  72. msg = msg.replace("no viable alternative at input", "Não encontrou alternativa para a entrada");
  73. } else if (msg.contains("missing")) {
  74. msg = msg.replace("missing", "Está faltando");
  75. msg = msg.replace("at", "antes");
  76. } else if (msg.contains("mismatched")) {
  77. msg = msg.replace("mismatched input", "Entrada incompativel");
  78. msg = msg.replace("expecting", "esperando");
  79. }
  80. this.msg = msg;
  81. }
  82. @Override
  83. public String toString() {
  84. String format;
  85. format = "[%d:%d|%s\t] SyntaxError: %s";
  86. if (oops == null) {
  87. // return String.format("[%d:%d] %s", line, column, msg);
  88. return String.format(format, line, column, file, msg);
  89. // return String.format("[%d:%d] %s", line, column, msg);
  90. } else {
  91. StringWriter sw = new StringWriter();
  92. PrintWriter pw = new PrintWriter(sw);
  93. oops.printStackTrace(pw);
  94. pw.close();
  95. // return String.format("[%d:%d] %s\n%s", line, column, msg, sw.toString());
  96. return String.format(format, line, column, file, msg);
  97. }
  98. }
  99. }
  100. }