78 lines
2.6 KiB
TeX
78 lines
2.6 KiB
TeX
\section{Error handling}
|
|
|
|
All syntactic and semantic errors cause parser exceptions to be thrown. In
|
|
particular, the methods used to match tokens in the parser base class (match et
|
|
al) throw §EdpgMismatchedToken§. The methods in the lexer base class used to
|
|
match characters (match et al) throw analogous exceptions.
|
|
|
|
\subsection{DPG exception hierarchy}
|
|
|
|
DPG-generated parsers throw exceptions to signal recognition errors or other
|
|
stream problems. All exceptions derive from EdpgException. The hierarchy is the
|
|
following:
|
|
|
|
\begin{verbatim}
|
|
EdpgException
|
|
EdpgMismatchedChar
|
|
EdpgMismatchedToken
|
|
EdpgSemantic
|
|
\end{verbatim}
|
|
|
|
\subsubsection{EdpgException} The EdpgException exception class is the base of
|
|
all DPG generated exceptions. User defined exceptions must derive from this
|
|
class.
|
|
|
|
\subsubsection{EdpgMismatchedChar} This exception is thrown by the lexer when it
|
|
is looking for a character, but finds a different one on the input stream.
|
|
|
|
\subsubsection{EdpgMismatchedToken} This exception is thrown by the parser when
|
|
it is looking for a token, but finds a different one on the input token stream.
|
|
|
|
\subsubsection{EdpgSemantic} This exception is thrown by a validating semantic
|
|
predicate.
|
|
|
|
\subsection{Specifying exception handlers}
|
|
|
|
DPG allows to specify specific exception handler to a given rule or
|
|
alternative. The general form of an exception handler specification is:
|
|
|
|
\begin{verbatim}
|
|
... except { code to handle exception }
|
|
... finally { code to handle exception }
|
|
\end{verbatim}
|
|
|
|
\subsubsection{Exception handler for a rule}
|
|
|
|
The exception handler for a rule must be placed after the terminating
|
|
semicolon. The handler can be either an §except§ block or a §finally§ block.
|
|
The implementation of rule will be surrounded by a try block.
|
|
|
|
\begin{verbatim}
|
|
r : ...
|
|
;
|
|
except { handler code }
|
|
\end{verbatim}
|
|
|
|
\subsubsection{Exception handler for an alternative}
|
|
|
|
The exception handler of an alternative must be the last element of the
|
|
alternative. Both exception handler blocks can be used. Every alternative that
|
|
have exception block specified, will be surrounded by a §try...except/finally§
|
|
block.
|
|
|
|
\begin{verbatim}
|
|
r : alternative_1 ... except { handler code }
|
|
| alternative_2 ... finally { handler code }
|
|
...
|
|
| alternative_n
|
|
;
|
|
\end{verbatim}
|
|
|
|
\paragraph{Note:} It is not necessary to define exception handler for each alternative.
|
|
|
|
\subsubsection{Default error handler in lexer}
|
|
|
|
To skip every character that isn't recognized by any public lexer rule, specify
|
|
§filter=true§ option for a lexer. That way, the parser doesn't have to deal
|
|
with lexical errors and ask for another token.
|