View Javadoc
1   /*
2    * Copyright 2013-2022 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * This file is part of Sparkle project.
7    *
8    * Sparkle is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License.
11   *
12   * Sparkle is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Sparkle. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package cz.zcu.mre.sparkle.tools.sparqlValidation;
21  
22  import org.antlr.v4.runtime.RecognitionException;
23  import org.antlr.v4.runtime.Recognizer;
24  import org.antlr.v4.runtime.RuleContext;
25  import org.antlr.v4.runtime.Token;
26  import org.antlr.v4.runtime.atn.ATNState;
27  import org.antlr.v4.runtime.misc.IntervalSet;
28  import java.util.Objects;
29  
30  /**
31   * Syntakticka chyba.
32   *
33   * @author Klara Hlavacova
34   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
35   */
36  public class SyntaxError {
37  
38      private final Recognizer<?, ?> recognizer;
39      //private final Object offendingSymbol;
40      private final int line;
41      private final int charPositionInLine;
42      private String message;
43      private final String text;
44      private final int state;
45      private final ATNState atnState;
46  
47      private RuleContext context;
48  
49      private IntervalSet expectedTokens;
50  
51      public SyntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
52              int charPositionInLine, String msg, RecognitionException e) {
53  
54          this.recognizer = recognizer;
55          state = this.recognizer.getState();
56          atnState = recognizer.getATN().states.get(recognizer.getState());
57          context = e.getCtx();
58          expectedTokens = new IntervalSet(e.getExpectedTokens());
59  
60          this.line = line;
61          this.charPositionInLine = charPositionInLine;
62          this.message = "ERROR on line " + line + "; column " + charPositionInLine + ": ";//$NON-NLS-1$
63          this.message += msg;
64          this.message = this.message.replaceAll("\\\\n", "");//$NON-NLS-1$
65  
66  
67          /* for (Integer index :e.getExpectedTokens().toList()  ) {
68              System.out.println(
69                      e.getRecognizer().getVocabulary().getDisplayName(index)
70              );
71          }*/
72          text = ((Token) offendingSymbol).getText();
73      }
74  
75      public IntervalSet getExpectedTokens() {
76          return expectedTokens;
77      }
78  
79      public int getLine() {
80          return line;
81      }
82  
83      public int getCharPositionInLine() {
84          return charPositionInLine;
85      }
86  
87      public String getMessage() {
88          return message;
89      }
90  
91      public String getText() {
92          return text;
93      }
94  
95      public Recognizer<?, ?> getRecognizer() {
96          return recognizer;
97      }
98  
99      public int getState() {
100         return state;
101     }
102 
103     public ATNState getAtnState() {
104         return atnState;
105     }
106 
107     public RuleContext getContext() {
108         return context;
109     }
110 
111     @Override
112     public String toString() {
113         return "SyntaxError{"
114                 +//$NON-NLS-1$
115                 "line=" + line
116                 +//$NON-NLS-1$
117                 ", charPositionInLine=" + charPositionInLine
118                 +//$NON-NLS-1$
119                 ", text='" + text + '\''
120                 +//$NON-NLS-1$
121                 '}';
122     }
123 
124     @Override
125     public boolean equals(Object o) {
126         if (this == o) {
127             return true;
128         }
129         if (o == null || getClass() != o.getClass()) {
130             return false;
131         }
132         SyntaxError that = (SyntaxError) o;
133         return line == that.line
134                 && charPositionInLine == that.charPositionInLine
135                 && Objects.equals(message, that.message)
136                 && Objects.equals(text, that.text);
137     }
138 
139     @Override
140     public int hashCode() {
141 
142         return Objects.hash(line, charPositionInLine, message, text);
143     }
144 
145 }