Skip to content

Commit 1f6a8d0

Browse files
committed
Rename ConvertError/ConvertResult to Error/Result
1 parent 209b618 commit 1f6a8d0

4 files changed

Lines changed: 31 additions & 34 deletions

File tree

grammar/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
pub mod char_set;
2626
mod grammar;
2727
pub mod parser;
28-
mod spec; // AST-like specification of the TOML format consumed by Sapling
28+
pub mod spec; // AST-like specification of the TOML format consumed by Sapling
2929
pub mod tokenizer;
3030

3131
pub use grammar::*;
32-
pub use spec::{convert::ConvertError, SpecGrammar};
32+
pub use spec::SpecGrammar;

grammar/src/spec/convert.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use regex_syntax::hir;
88
use super::SpecGrammar;
99
use crate::{char_set, grammar, Grammar, TypeId, TypeVec};
1010

11-
pub type ConvertResult<T> = Result<T, ConvertError>;
11+
pub type Result<T> = std::result::Result<T, Error>;
1212

1313
use self::utils::{TokenMap, TypeMap};
1414

1515
/// Convert a [`SpecGrammar`] (likely parsed from a TOML file) into a full [`Grammar`], or fail
1616
/// with a [`ConvertError`]. This is a largely straightforward process, since the 'shapes' of
1717
/// [`SpecGrammar`] and [`Grammar`] are (intentionally) similar.
18-
pub(crate) fn convert(grammar: SpecGrammar) -> ConvertResult<Grammar> {
18+
pub(crate) fn convert(grammar: SpecGrammar) -> self::Result<Grammar> {
1919
let SpecGrammar {
2020
root_type,
2121
whitespace,
@@ -36,7 +36,7 @@ pub(crate) fn convert(grammar: SpecGrammar) -> ConvertResult<Grammar> {
3636

3737
/// The possibly ways that parsing a [`Grammar`] can fail.
3838
#[derive(Debug)]
39-
pub enum ConvertError {
39+
pub enum Error {
4040
/// An error occurred whilst generating a [`Regex`] provided by the user
4141
Regex {
4242
type_name: String,
@@ -67,7 +67,7 @@ pub enum ConvertError {
6767
fn convert_types(
6868
types: HashMap<super::TypeName, super::Type>,
6969
token_map: &mut TokenMap,
70-
) -> ConvertResult<(TypeVec<grammar::Type>, TypeMap)> {
70+
) -> self::Result<(TypeVec<grammar::Type>, TypeMap)> {
7171
// Before generating types, assign all names to type IDs (because types may refer to child
7272
// types which appear after themselves in the HashMap iterator). If we see a `TypeName` which
7373
// is not in the `TypeMap`, then we know it must be invalid and we can generate an error.
@@ -113,7 +113,7 @@ fn convert_types(
113113
&type_map,
114114
)
115115
})
116-
.collect::<Result<_, _>>()?;
116+
.collect::<self::Result<_>>()?;
117117
Ok((types, type_map))
118118
}
119119

@@ -124,7 +124,7 @@ fn convert_type(
124124
parseable_type_ids: &HashSet<TypeId>,
125125
token_map: &mut TokenMap,
126126
type_map: &TypeMap,
127-
) -> ConvertResult<grammar::Type> {
127+
) -> self::Result<grammar::Type> {
128128
let (key, mut keys, inner) = match t {
129129
super::Type::Pattern {
130130
key,
@@ -191,7 +191,7 @@ fn convert_type(
191191
})
192192
}
193193

194-
fn convert_escape_rules(rules: super::EscapeRules) -> ConvertResult<grammar::EscapeRules> {
194+
fn convert_escape_rules(rules: super::EscapeRules) -> self::Result<grammar::EscapeRules> {
195195
let super::EscapeRules {
196196
start_sequence,
197197
rules,
@@ -215,18 +215,18 @@ fn convert_escape_rules(rules: super::EscapeRules) -> ConvertResult<grammar::Esc
215215
fn compute_type_descendants(
216216
types: &IndexSlice<TypeId, [(super::TypeName, super::Type)]>,
217217
type_map: &TypeMap,
218-
) -> ConvertResult<TypeVec<Vec<TypeId>>> {
218+
) -> self::Result<TypeVec<Vec<TypeId>>> {
219219
// For each TypeId, determine the `TypeId`s of its children
220220
let child_type_ids: TypeVec<Vec<TypeId>> = types
221221
.iter()
222222
.map(|(parent_name, ty)| match ty {
223223
super::Type::Pattern { children, .. } => children
224224
.iter()
225225
.map(|child_name| type_map.get(child_name, parent_name))
226-
.collect::<ConvertResult<Vec<TypeId>>>(),
226+
.collect::<self::Result<Vec<TypeId>>>(),
227227
super::Type::Stringy { .. } => Ok(Vec::new()), // Stringy nodes have no children
228228
})
229-
.collect::<Result<_, _>>()?;
229+
.collect::<self::Result<_>>()?;
230230
// For each node flatten its descendant tree, terminating if any cycles are found.
231231
//
232232
// PERF: This does a lot of duplicated work expanding nodes, which could be sped up by
@@ -261,7 +261,7 @@ fn enumerate_type_descendants(
261261
child_type_ids: &IndexSlice<TypeId, [Vec<TypeId>]>,
262262
type_stack: &mut Vec<TypeId>,
263263
out: &mut Vec<TypeId>,
264-
) -> ConvertResult<()> {
264+
) -> self::Result<()> {
265265
// Check for cycles
266266
if let Some(idx) = type_stack.iter().position(|&i| i == id) {
267267
// `type_stack[idx..] + id` forms the cycle (i.e. a cycle which starts and ends with `id`
@@ -270,7 +270,7 @@ fn enumerate_type_descendants(
270270
.chain(std::iter::once(&id))
271271
.map(|&id| types[id].0.to_owned())
272272
.collect_vec();
273-
return Err(ConvertError::TypeCycle(cycle));
273+
return Err(Error::TypeCycle(cycle));
274274
}
275275
// Mark this type as a descendant (if it hasn't been listed already)
276276
if !out.contains(&id) {
@@ -294,19 +294,19 @@ fn compile_pattern(
294294
parent_type_name: &str,
295295
token_map: &mut TokenMap,
296296
type_map: &TypeMap,
297-
) -> ConvertResult<grammar::Pattern> {
297+
) -> self::Result<grammar::Pattern> {
298298
elems
299299
.into_iter()
300300
.map(|e| compile_pattern_element(e, parent_type_name, token_map, type_map))
301-
.collect::<Result<_, _>>()
301+
.collect::<self::Result<_>>()
302302
}
303303

304304
fn compile_pattern_element(
305305
elem: super::PatternElement,
306306
parent_type_name: &str,
307307
token_map: &mut TokenMap,
308308
type_map: &TypeMap,
309-
) -> ConvertResult<grammar::PatternElement> {
309+
) -> self::Result<grammar::PatternElement> {
310310
use super::PatternElement::*;
311311
use grammar::PatternElement as PE;
312312
Ok(match elem {
@@ -323,10 +323,10 @@ fn compile_pattern_element(
323323
// REGEX/WHITESPACE/CHAR SET //
324324
///////////////////////////////
325325

326-
fn convert_validity_regex(regex_str: &str, type_name: &str) -> ConvertResult<grammar::Regexes> {
326+
fn convert_validity_regex(regex_str: &str, type_name: &str) -> self::Result<grammar::Regexes> {
327327
macro_rules! compile_regex {
328328
($string: expr) => {
329-
Regex::new(&$string).map_err(|inner| ConvertError::Regex {
329+
Regex::new(&$string).map_err(|inner| Error::Regex {
330330
type_name: type_name.to_owned(),
331331
regex: $string,
332332
inner,
@@ -345,19 +345,19 @@ fn convert_validity_regex(regex_str: &str, type_name: &str) -> ConvertResult<gra
345345
})
346346
}
347347

348-
fn convert_whitespace(ws_chars: super::CharSet) -> ConvertResult<grammar::Whitespace> {
348+
fn convert_whitespace(ws_chars: super::CharSet) -> self::Result<grammar::Whitespace> {
349349
convert_char_set(ws_chars).map(grammar::Whitespace::from)
350350
}
351351

352-
fn convert_char_set(set: super::CharSet) -> ConvertResult<char_set::CharSet> {
352+
fn convert_char_set(set: super::CharSet) -> self::Result<char_set::CharSet> {
353353
// Convert the source `CharSet` into the string for a regex which matches single `char`s from
354354
// the same set.
355355
let source_str = set.0;
356356
let regex_str = format!("[{}]", source_str);
357357
// Parse that regex
358358
let regex_hir = regex_syntax::Parser::new()
359359
.parse(&regex_str)
360-
.map_err(|inner| ConvertError::CharSet {
360+
.map_err(|inner| Error::CharSet {
361361
source_str,
362362
regex_str,
363363
inner,
@@ -380,7 +380,7 @@ mod utils {
380380

381381
use crate::{spec, Token, TokenId, TypeId, TypeVec};
382382

383-
use super::{ConvertError, ConvertResult};
383+
use super::Error;
384384

385385
/// Maps [`TypeName`]s to [`TypeId`]s, providing `get` methods which generate error messages
386386
#[derive(Debug, Clone)]
@@ -404,21 +404,21 @@ mod utils {
404404
(types, Self { inner: inner_map })
405405
}
406406

407-
pub(super) fn get(&self, name: &str, parent_type_name: &str) -> ConvertResult<TypeId> {
407+
pub(super) fn get(&self, name: &str, parent_type_name: &str) -> super::Result<TypeId> {
408408
self.inner
409409
.get(name)
410410
.copied()
411-
.ok_or_else(|| ConvertError::UnknownChildType {
411+
.ok_or_else(|| Error::UnknownChildType {
412412
name: name.to_owned(),
413413
parent_name: parent_type_name.to_owned(),
414414
})
415415
}
416416

417-
pub(super) fn get_root(&self, name: &str) -> ConvertResult<TypeId> {
417+
pub(super) fn get_root(&self, name: &str) -> super::Result<TypeId> {
418418
self.inner
419419
.get(name)
420420
.copied()
421-
.ok_or_else(|| ConvertError::UnknownRootType(name.to_owned()))
421+
.ok_or_else(|| Error::UnknownRootType(name.to_owned()))
422422
}
423423
}
424424

grammar/src/spec/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//!
99
//! All these stages can generate errors, which are all bubbled up to the caller
1010
11-
pub(crate) mod convert;
11+
pub mod convert;
1212

1313
use std::collections::HashMap;
1414

@@ -17,8 +17,6 @@ use serde::Deserialize;
1717

1818
use crate::Grammar;
1919

20-
use self::convert::ConvertResult;
21-
2220
type TypeName = String;
2321
type TokenText = String;
2422

@@ -40,8 +38,7 @@ pub struct SpecGrammar {
4038
}
4139

4240
impl SpecGrammar {
43-
#[inline]
44-
pub fn into_grammar(self) -> ConvertResult<Grammar> {
41+
pub fn into_grammar(self) -> convert::Result<Grammar> {
4542
convert::convert(self)
4643
}
4744
}

sapling/src/lang.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
rc::Rc,
44
};
55

6-
use sapling_grammar::{parser, tokenizer::Tokenizer, ConvertError, Grammar, SpecGrammar, TypeId};
6+
use sapling_grammar::{parser, tokenizer::Tokenizer, Grammar, SpecGrammar, TypeId};
77
use serde::Deserialize;
88

99
use crate::ast::Tree;
@@ -80,5 +80,5 @@ struct LangFile {
8080
pub enum LoadError {
8181
Io(PathBuf, std::io::Error),
8282
Parse(toml::de::Error),
83-
Convert(ConvertError),
83+
Convert(sapling_grammar::spec::convert::Error),
8484
}

0 commit comments

Comments
 (0)