1use crate::loc::Loc;
2use crate::token::TokenType;
3use core::fmt;
4use core::fmt::Debug;
5use core::fmt::Formatter;
6use std::error::Error;
7use std::fmt::Display;
8
9#[derive(Copy, Clone, Eq, PartialEq, Debug)]
10pub enum SyntaxErrorType {
11 ExpectedNotFound,
12 ExpectedSyntax(&'static str),
13 ForLoopHeaderHasInvalidLhs,
14 ForLoopHeaderHasMultipleDeclarators,
15 ForLoopHeaderHasNoLhs,
16 InvalidAssigmentTarget,
17 InvalidCharacterEscape,
18 JsxClosingTagMismatch,
19 LineTerminatorAfterArrowFunctionParameters,
20 LineTerminatorAfterThrow,
21 LineTerminatorAfterYield,
22 LineTerminatorInRegex,
23 LineTerminatorInString,
24 MalformedLiteralBigInt,
25 MalformedLiteralNumber,
26 RequiredTokenNotFound(TokenType),
27 TryStatementHasNoCatchOrFinally,
28 UnexpectedEnd,
29}
30
31#[derive(Clone)]
32pub struct SyntaxError {
33 pub typ: SyntaxErrorType,
34 pub loc: Loc,
35 pub actual_token: Option<TokenType>,
36}
37
38impl SyntaxError {
39 pub fn new(typ: SyntaxErrorType, loc: Loc, actual_token: Option<TokenType>) -> SyntaxError {
40 SyntaxError {
41 typ,
42 loc,
43 actual_token,
44 }
45 }
46}
47
48impl Debug for SyntaxError {
49 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
50 write!(f, "{} around loc [{}:{}]", self, self.loc.0, self.loc.1)
51 }
52}
53
54impl Display for SyntaxError {
55 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
56 write!(f, "{:?} [token={:?}]", self.typ, self.actual_token)
57 }
58}
59
60impl Error for SyntaxError {}
61
62impl PartialEq for SyntaxError {
63 fn eq(&self, other: &Self) -> bool {
64 self.typ == other.typ
65 }
66}
67
68impl Eq for SyntaxError {}
69
70pub type SyntaxResult<T> = Result<T, SyntaxError>;