microcad_lang/parse/
parse_error.rs1use crate::{parse::*, ty::*};
7use thiserror::Error;
8
9#[derive(Debug, Error)]
11pub enum ParseError {
12 #[error("Error parsing floating point literal: {0}")]
14 ParseFloatError(Refer<std::num::ParseFloatError>),
15
16 #[error("Error parsing integer literal: {0}")]
18 ParseIntError(Refer<std::num::ParseIntError>),
19
20 #[error("Rule not found: {0:?}")]
22 RuleNotFoundError(Box<crate::parser::Rule>),
23
24 #[error("IO Error: {0}")]
26 IoError(Refer<std::io::Error>),
27
28 #[error("Parser error: {0}")]
30 Parser(Box<pest::error::Error<crate::parser::Rule>>),
31
32 #[error("Error parsing color: {0}")]
34 ParseColorError(Refer<microcad_core::ParseColorError>),
35
36 #[error("Unknown color: {0}")]
38 UnknownColorName(Refer<String>),
39
40 #[error("Unknown unit: {0}")]
42 UnknownUnit(Refer<String>),
43
44 #[error("Unexpected token")]
46 UnexpectedToken(SrcRef),
47
48 #[error("Tuple expression contains both named and positional arguments")]
50 MixedTupleArguments(SrcRef),
51
52 #[error("Duplicate named argument: {0}")]
54 DuplicateNamedArgument(Identifier),
55
56 #[error("Positional argument after named argument")]
58 PositionalArgumentAfterNamed(SrcRef),
59
60 #[error("Empty tuple expression")]
62 EmptyTupleExpression(SrcRef),
63
64 #[error("Missing type or value for definition parameter: {0}")]
66 ParameterMissingTypeOrValue(Identifier),
67
68 #[error("Duplicate parameter: {0}")]
70 DuplicateParameter(Identifier),
71
72 #[error("Duplicate argument: {0}")]
74 DuplicateArgument(Identifier),
75
76 #[error("Duplicated type name in map: {0}")]
78 DuplicatedMapType(Identifier),
79
80 #[error("Duplicate id: {0}")]
82 DuplicateIdentifier(Identifier),
83
84 #[error("Duplicate id in tuple: {0}")]
86 DuplicateTupleIdentifier(Identifier),
87
88 #[error("Duplicate unnamed type in tuple: {0}")]
90 DuplicateTupleType(Refer<Type>),
91
92 #[error("Missing format expression")]
94 MissingFormatExpression(SrcRef),
95
96 #[error("Statement between two init statements")]
98 StatementBetweenInit(SrcRef),
99
100 #[error("Loading of source file {0:?} failed")]
102 LoadSource(Refer<std::path::PathBuf>),
103
104 #[error("Grammar rule error {0}")]
106 GrammarRuleError(Refer<String>),
107
108 #[error("Invalid qualified name '{0}'")]
110 InvalidQualifiedName(Refer<String>),
111
112 #[error("Invalid id '{0}'")]
114 InvalidIdentifier(Refer<String>),
115
116 #[error("Qualified name {0} cannot be converted into an Id")]
118 QualifiedNameIsNoId(QualifiedName),
119
120 #[error("Element is not available")]
122 NotAvailable(SrcRef),
123
124 #[error("Unknown type: {0}")]
126 UnknownType(Refer<String>),
127
128 #[error("If expression must return a value in all cases")]
130 IncompleteIfExpression(SrcRef)
131}
132
133pub type ParseResult<T> = Result<T, ParseError>;
135
136impl SrcReferrer for ParseError {
137 fn src_ref(&self) -> SrcRef {
138 match self {
139 ParseError::Parser(error) => SrcRef::new(
140 match error.location {
141 pest::error::InputLocation::Pos(pos) => std::ops::Range {
142 start: pos,
143 end: pos,
144 },
145 pest::error::InputLocation::Span((start, end)) => {
146 std::ops::Range { start, end }
147 }
148 },
149 match error.line_col {
150 pest::error::LineColLocation::Pos(pos) => pos.0,
151 pest::error::LineColLocation::Span(start, _) => start.0,
152 },
153 match error.line_col {
154 pest::error::LineColLocation::Pos(pos) => pos.1,
155 pest::error::LineColLocation::Span(start, _) => start.1,
156 },
157 0,
158 ),
159 ParseError::DuplicateNamedArgument(id)
160 | ParseError::ParameterMissingTypeOrValue(id)
161 | ParseError::DuplicateParameter(id)
162 | ParseError::DuplicateArgument(id)
163 | ParseError::DuplicatedMapType(id)
164 | ParseError::DuplicateIdentifier(id)
165 | ParseError::DuplicateTupleIdentifier(id) => id.src_ref(),
166 ParseError::QualifiedNameIsNoId(name) => name.src_ref(),
167 ParseError::UnexpectedToken(src_ref)
168 | ParseError::MixedTupleArguments(src_ref)
169 | ParseError::PositionalArgumentAfterNamed(src_ref)
170 | ParseError::EmptyTupleExpression(src_ref)
171 | ParseError::MissingFormatExpression(src_ref)
172 | ParseError::StatementBetweenInit(src_ref)
173 | ParseError::NotAvailable(src_ref)
174 | ParseError::IncompleteIfExpression(src_ref) => src_ref.clone(),
175 ParseError::ParseFloatError(parse_float_error) => parse_float_error.src_ref(),
176 ParseError::ParseIntError(parse_int_error) => parse_int_error.src_ref(),
177 ParseError::RuleNotFoundError(_) => SrcRef(None),
178 ParseError::IoError(error) => error.src_ref(),
179 ParseError::ParseColorError(parse_color_error) => parse_color_error.src_ref(),
180 ParseError::UnknownColorName(name) => name.src_ref(),
181 ParseError::UnknownUnit(unit) => unit.src_ref(),
182 ParseError::DuplicateTupleType(ty) => ty.src_ref(),
183 ParseError::LoadSource(path) => path.src_ref(),
184 ParseError::GrammarRuleError(rule) => rule.src_ref(),
185 ParseError::InvalidQualifiedName(name) => name.src_ref(),
186 ParseError::InvalidIdentifier(id) => id.src_ref(),
187 ParseError::UnknownType(ty) => ty.src_ref(),
188 }
189 }
190}