mangle_parse/
error.rs

1// Copyright 2024 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use thiserror::Error;
16
17use crate::token::Token;
18
19#[derive(Error, Debug)]
20pub enum ParseError {
21    #[error("{0}: expected `{1}` got `{2}`")]
22    Unexpected(ErrorContext, Token, Token),
23}
24
25#[derive(Error, Debug)]
26pub enum ScanError {
27    #[error("{0}: incomplete UTF8")]
28    IncompleteUtf8(ErrorContext),
29
30    #[error("{0}: unexpected character `{1}`")]
31    Unexpected(ErrorContext, char),
32}
33
34// Represents an error encountered during parsing.
35pub struct ErrorContext {
36    pub path: String,
37    pub line: usize,
38    pub col: usize,
39    pub start_of_line: usize,
40}
41
42impl std::fmt::Display for ErrorContext {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        write!(f, "{}:{}:{}", self.path, self.line, self.col)
45    }
46}
47
48impl std::fmt::Debug for ErrorContext {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "{}:{}:{}", self.path, self.line, self.col)
51    }
52}