1+ #region BSD 3-Clause License
2+ // <copyright file="TestingErrorListener.cs" company="Edgerunner.org">
3+ // Copyright 2020 Thaddeus Ryker
4+ // </copyright>
5+ //
6+ // BSD 3-Clause License
7+ //
8+ // Copyright (c) 2020, Thaddeus Ryker
9+ // All rights reserved.
10+ //
11+ // Redistribution and use in source and binary forms, with or without
12+ // modification, are permitted provided that the following conditions are met:
13+ //
14+ // 1. Redistributions of source code must retain the above copyright notice, this
15+ // list of conditions and the following disclaimer.
16+ //
17+ // 2. Redistributions in binary form must reproduce the above copyright notice,
18+ // this list of conditions and the following disclaimer in the documentation
19+ // and/or other materials provided with the distribution.
20+ //
21+ // 3. Neither the name of the copyright holder nor the names of its
22+ // contributors may be used to endorse or promote products derived from
23+ // this software without specific prior written permission.
24+ //
25+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28+ // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29+ // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30+ // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31+ // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33+ // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+ #endregion
36+
37+ using System . Collections . Generic ;
38+
39+ using Antlr4 . Runtime ;
40+
41+ namespace Org . Edgerunner . ANTLR4 . Tools . Testing . Grammar . Errors
42+ {
43+ /// <summary>
44+ /// Class for gathering ANTLR4 parsing errors during testing.
45+ /// Implements the <see cref="IToken" />
46+ /// </summary>
47+ /// <seealso cref="IToken" />
48+ public class TestingErrorListener : Antlr4 . Runtime . IAntlrErrorListener < IToken >
49+ {
50+ /// <summary>
51+ /// Initializes a new instance of the <see cref="TestingErrorListener"/> class.
52+ /// </summary>
53+ public TestingErrorListener ( )
54+ {
55+ Errors = new List < ParseError > ( ) ;
56+ }
57+
58+ /// <summary>
59+ /// Gets the parsing errors.
60+ /// </summary>
61+ /// <value>The parsing errors.</value>
62+ public List < ParseError > Errors { get ; }
63+
64+ /// <summary>
65+ /// Upon syntax error, notify any interested parties.
66+ /// </summary>
67+ /// <param name="recognizer">What parser got the error. From this
68+ /// object, you can access the context as well
69+ /// as the input stream.</param>
70+ /// <param name="offendingSymbol">The offending token in the input token
71+ /// stream, unless recognizer is a lexer (then it's null). If
72+ /// no viable alternative error,
73+ /// <paramref name="e" />
74+ /// has token at which we
75+ /// started production for the decision.</param>
76+ /// <param name="line">The line number in the input where the error occurred.</param>
77+ /// <param name="charPositionInLine">The character position within that line where the error occurred.</param>
78+ /// <param name="msg">The message to emit.</param>
79+ /// <param name="e">The exception generated by the parser that led to
80+ /// the reporting of an error. It is null in the case where
81+ /// the parser was able to recover in line without exiting the
82+ /// surrounding rule.</param>
83+ /// <remarks>Upon syntax error, notify any interested parties. This is not how to
84+ /// recover from errors or compute error messages.
85+ /// <see cref="T:Antlr4.Runtime.IAntlrErrorStrategy" />
86+ /// specifies how to recover from syntax errors and how to compute error
87+ /// messages. This listener's job is simply to emit a computed message,
88+ /// though it has enough information to create its own message in many cases.
89+ /// <p>The
90+ /// <see cref="T:Antlr4.Runtime.RecognitionException" />
91+ /// is non-null for all syntax errors except
92+ /// when we discover mismatched token errors that we can recover from
93+ /// in-line, without returning from the surrounding rule (via the single
94+ /// token insertion and deletion mechanism).</p></remarks>
95+ // ReSharper disable once TooManyArguments
96+ public void SyntaxError (
97+ IRecognizer recognizer ,
98+ IToken offendingSymbol ,
99+ int line ,
100+ int charPositionInLine ,
101+ string msg ,
102+ RecognitionException e )
103+ {
104+ Errors . Add ( new ParseError ( line , charPositionInLine , msg ) ) ;
105+ }
106+ }
107+ }
0 commit comments