microcad_lang/parse/
parse_error.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Parser errors
5
6use crate::{parse::*, ty::*};
7use thiserror::Error;
8
9/// Parsing errors
10#[derive(Debug, Error)]
11pub enum ParseError {
12    /// Expected identifier
13    #[error("Expected identifier")]
14    ExpectedIdentifier,
15
16    /// Error parsing floating point literal
17    #[error("Error parsing floating point literal: {0}")]
18    ParseFloatError(#[from] std::num::ParseFloatError),
19
20    /// Error parsing integer literal
21    #[error("Error parsing integer literal: {0}")]
22    ParseIntError(#[from] std::num::ParseIntError),
23
24    /// Parser rule error
25    #[error("Cannot parse rule: {0:?}")]
26    RuleError(Box<crate::parser::Rule>),
27
28    /// IO Error
29    #[error("IO Error: {0}")]
30    IoError(#[from] std::io::Error),
31
32    /// Error in pest parser
33    #[error("Parser error: {0}")]
34    Parser(#[from] Box<pest::error::Error<crate::parser::Rule>>),
35
36    /// Error parsing color literal
37    #[error("Error parsing color: {0}")]
38    ParseColorError(#[from] microcad_core::ParseColorError),
39
40    /// Unknown color name
41    #[error("Unknown color: {0}")]
42    UnknownColorName(String),
43
44    /// Unknown unit
45    #[error("Unknown unit: {0}")]
46    UnknownUnit(String),
47
48    /// Unexpected token
49    #[error("Unexpected token")]
50    UnexpectedToken,
51
52    /// Tuple expression contains both named and positional arguments
53    #[error("Tuple expression contains both named and positional arguments")]
54    MixedTupleArguments,
55
56    /// Duplicate named argument
57    #[error("Duplicate named argument: {0}")]
58    DuplicateNamedArgument(Identifier),
59
60    /// Positional argument after named argument
61    #[error("Positional argument after named argument")]
62    PositionalArgumentAfterNamed,
63
64    /// Empty tuple expression
65    #[error("Empty tuple expression")]
66    EmptyTupleExpression,
67
68    /// Missing type or value for definition parameter
69    #[error("Missing type or value for definition parameter: {0}")]
70    ParameterMissingTypeOrValue(Identifier),
71
72    /// Duplicate parameter
73    #[error("Duplicate parameter: {0}")]
74    DuplicateParameter(Identifier),
75
76    /// Duplicate argument
77    #[error("Duplicate argument: {0}")]
78    DuplicateArgument(Identifier),
79
80    /// Invalid map key type
81    #[error("Invalid map key type: {0}")]
82    InvalidMapKeyType(String),
83
84    /// Duplicated type name in map
85    #[error("Duplicated type name in map: {0}")]
86    DuplicatedMapType(Identifier),
87
88    /// Duplicate identifier
89    #[error("Duplicate identifier: {0}")]
90    DuplicateIdentifier(Identifier),
91
92    /// Duplicate identifier in tuple
93    #[error("Duplicate identifier in tuple: {0}")]
94    DuplicateTupleIdentifier(Identifier),
95
96    /// Duplicate unnamed type in tuple
97    #[error("Duplicate unnamed type in tuple: {0}")]
98    DuplicateTupleType(Type),
99
100    /// Missing format expression
101    #[error("Missing format expression")]
102    MissingFormatExpression,
103
104    /// Statement between two init statements
105    #[error("Statement between two init statements")]
106    StatementBetweenInit,
107
108    /// Loading of a source file failed
109    #[error("Loading of source file {0:?} failed")]
110    LoadSource(std::path::PathBuf),
111
112    /// Grammar rule error
113    #[error("Grammar rule error {0}")]
114    GrammarRuleError(String),
115
116    /// Grammar rule error
117    #[error("Invalid qualified name '{0}'")]
118    InvalidQualifiedName(String),
119
120    /// Grammar rule error
121    #[error("Invalid identifier '{0}'")]
122    InvalidIdentifier(String),
123
124    /// Qualified name cannot be converted into an Id
125    #[error("Qualified name {0} cannot be converted into an Id")]
126    QualifiedNameIsNoId(QualifiedName),
127
128    /// Statement not allowed within workbenches
129    #[error("Statement not allowed within workbenches ({0})")]
130    IllegalWorkbenchStatement(SrcRef),
131
132    /// Code Between initializers
133    #[error("Code between initializers is not allowed ({0})")]
134    CodeBetweenInitializers(SrcRef),
135
136    /// Statement not allowed prior initializers
137    #[error("Statement not allowed prior initializers ({0})")]
138    StatementNotAllowedPriorInitializers(SrcRef),
139
140    /// Element is not available
141    #[error("Element is not available")]
142    NotAvailable,
143
144    /// Unknown type
145    #[error("Unknown type: {0}")]
146    UnknownType(String),
147}
148
149/// Result with parse error
150pub type ParseResult<T> = Result<T, ParseError>;