Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Mar 30, 2024
1 parent 8f5e30d commit 7c55f30
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Diagnostic {
/// associated with.
///
/// Identical to calling `self.span()` when the [`Diagnostic`] has no children.
pub fn merged_span(&self) -> Result<Span, SpanJoinError> {
pub fn merged_span(&self) -> core::result::Result<Span, SpanJoinError> {
let mut merged_span = self.span.clone();
for child in &self.children {
merged_span = merged_span.join(&child.merged_span()?)?;
Expand Down
2 changes: 2 additions & 0 deletions src/parsable.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Contains a menagerie of useful types that implement the [`Parsable`] trait.

use super::*;

mod everything;
Expand Down
4 changes: 2 additions & 2 deletions src/parsable/everything.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Spanned for Everything {
}

impl Parsable for Everything {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let span = Span::new(
stream.source().clone(),
stream.position..(stream.source().len()),
Expand All @@ -21,7 +21,7 @@ impl Parsable for Everything {
Ok(Everything(span))
}

fn parse_value(value: Self, stream: &mut ParseStream) -> ParseResult<Self> {
fn parse_value(value: Self, stream: &mut ParseStream) -> Result<Self> {
let s = value.span();
let text = s.source_text();
if stream.remaining() == text {
Expand Down
4 changes: 2 additions & 2 deletions src/parsable/exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ impl Spanned for Exact {
make_parsable!(Exact);

impl Parsable for Exact {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
Ok(Exact(Span::new(
stream.source().clone(),
stream.position..stream.position,
)))
}

fn parse_value(value: Self, stream: &mut ParseStream) -> ParseResult<Self> {
fn parse_value(value: Self, stream: &mut ParseStream) -> Result<Self> {
let s = value.0;
let text = s.source_text();
if stream.remaining().starts_with(text) {
Expand Down
4 changes: 2 additions & 2 deletions src/parsable/nothing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Spanned for Nothing {
}

impl Parsable for Nothing {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
if stream.position < stream.source().len() {
return Err(Error::new(
stream.current_span(),
Expand All @@ -26,7 +26,7 @@ impl Parsable for Nothing {
Ok(Nothing(stream.current_span()))
}

fn parse_value(_value: Self, stream: &mut ParseStream) -> ParseResult<Self> {
fn parse_value(_value: Self, stream: &mut ParseStream) -> Result<Self> {
stream.parse()
}

Expand Down
12 changes: 6 additions & 6 deletions src/parsable/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Spanned for U64 {
}

impl Parsable for U64 {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let mut digits = Vec::new();
let start_position = stream.position;
while let Ok(_) = stream.next_digit() {
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Spanned for U128 {
}

impl Parsable for U128 {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let mut digits = Vec::new();
let start_position = stream.position;
while let Ok(_) = stream.next_digit() {
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Spanned for I64 {
}

impl Parsable for I64 {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let mut digits = Vec::new();
let start_position = stream.position;
let mut sign = 1;
Expand Down Expand Up @@ -196,7 +196,7 @@ impl Spanned for I128 {
}

impl Parsable for I128 {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let mut digits = Vec::new();
let start_position = stream.position;
let mut sign = 1;
Expand Down Expand Up @@ -279,7 +279,7 @@ impl Spanned for Decimal {
make_parsable!(Decimal);

impl Parsable for Decimal {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let start_position = stream.position;
if stream.next_char()? == '-' {
stream.consume(1)?;
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<const MIN: i64, const MAX: i64> Spanned for BoundedI64<MIN, MAX> {
}

impl<const MIN: i64, const MAX: i64> Parsable for BoundedI64<MIN, MAX> {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let i = stream.parse::<I64>()?;
if i.0 < MIN {
return Err(Error::new(
Expand Down
6 changes: 3 additions & 3 deletions src/parsable/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T: Parsable> Display for Optional<T> {
impl<T: Parsable> FromStr for Optional<T> {
type Err = T::Err;

fn from_str(s: &str) -> Result<Self, Self::Err> {
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
let Ok(val) = s.parse() else {
return Ok(Optional::None);
};
Expand All @@ -67,14 +67,14 @@ impl<T: Parsable> FromStr for Optional<T> {
}

impl<T: Parsable> Parsable for Optional<T> {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
if stream.peek::<T>() {
return Ok(Optional::Some(stream.parse::<T>()?));
}
Ok(Optional::None)
}

fn parse_value(value: Self, stream: &mut ParseStream) -> ParseResult<Self> {
fn parse_value(value: Self, stream: &mut ParseStream) -> Result<Self> {
match value {
Optional::Some(val) => stream.parse_value(val).map(Optional::Some),
Optional::None => Ok(Optional::None),
Expand Down
2 changes: 1 addition & 1 deletion src/parsable/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Spanned for Whitespace {
make_parsable!(Whitespace);

impl Parsable for Whitespace {
fn parse(stream: &mut ParseStream) -> ParseResult<Self> {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let start_position = stream.position;
while let Ok(c) = stream.next_char() {
if !c.is_whitespace() {
Expand Down
53 changes: 27 additions & 26 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use self::parsable::Exact;

use super::*;

/// Represents an error that occurred during parsing.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Error(Diagnostic);

Expand All @@ -35,6 +36,7 @@ impl Debug for Error {
}

impl Error {
/// Creates a new [`Error`] with the given [`Span`] and message.
pub fn new(span: Span, message: impl ToString) -> Error {
Error(Diagnostic::new(
DiagnosticLevel::Error,
Expand All @@ -45,6 +47,7 @@ impl Error {
))
}

/// Creates a new [`Error`] expecting a certain value at the given [`Span`].
pub fn expected(span: Span, expected: impl Display) -> Error {
Error(Diagnostic::new(
DiagnosticLevel::Error,
Expand All @@ -56,7 +59,8 @@ impl Error {
}
}

pub type ParseResult<T> = core::result::Result<T, Error>;
/// Represents the result of a parsing operation.
pub type Result<T> = core::result::Result<T, Error>;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ParseStream {
Expand All @@ -80,16 +84,16 @@ impl ParseStream {
Span::new(self.source.clone(), self.position..self.source.len())
}

pub fn parse<T: Parsable>(&mut self) -> ParseResult<T> {
pub fn parse<T: Parsable>(&mut self) -> Result<T> {
T::parse(self)
}

pub fn parse_value<T: Parsable>(&mut self, value: T) -> ParseResult<T> {
pub fn parse_value<T: Parsable>(&mut self, value: T) -> Result<T> {
T::parse_value(value, self)
}

/// note: panics upon invalid regex syntax
pub fn parse_regex(&mut self, reg: impl Pattern) -> ParseResult<Exact> {
pub fn parse_regex(&mut self, reg: impl Pattern) -> Result<Exact> {
let reg = reg.to_regex();
match reg.find(self.remaining()) {
Some(m) => {
Expand Down Expand Up @@ -117,11 +121,11 @@ impl ParseStream {
self.fork().parse_regex(reg).is_ok()
}

pub fn parse_str(&mut self, value: impl ToString) -> ParseResult<Exact> {
pub fn parse_str(&mut self, value: impl ToString) -> Result<Exact> {
self.parse_value(Exact::from(value))
}

pub fn parse_istr(&mut self, value: impl ToString) -> ParseResult<Exact> {
pub fn parse_istr(&mut self, value: impl ToString) -> Result<Exact> {
let text = value.to_string().to_lowercase();
let remaining_lower = self.remaining().to_lowercase();
if remaining_lower.starts_with(&text) {
Expand All @@ -146,10 +150,7 @@ impl ParseStream {
.starts_with(&s.to_string().to_lowercase())
}

pub fn parse_any_value_of<T: Parsable, const N: usize>(
&mut self,
values: [T; N],
) -> ParseResult<T> {
pub fn parse_any_value_of<T: Parsable, const N: usize>(&mut self, values: [T; N]) -> Result<T> {
for i in 0..N {
if self.peek_value(values[i].clone()) {
return self.parse_value(values[i].clone());
Expand All @@ -171,7 +172,7 @@ impl ParseStream {
pub fn parse_any_str_of<const N: usize>(
&mut self,
values: [impl ToString; N],
) -> ParseResult<(Exact, usize)> {
) -> Result<(Exact, usize)> {
for (i, s) in values.iter().enumerate() {
let s = s.to_string();
if self.peek_str(&s) {
Expand All @@ -194,7 +195,7 @@ impl ParseStream {
pub fn parse_any_istr_of<const N: usize>(
&mut self,
values: [impl ToString; N],
) -> ParseResult<(Exact, usize)> {
) -> Result<(Exact, usize)> {
for (i, s) in values.iter().enumerate() {
let s = s.to_string();
if self.peek_istr(&s) {
Expand Down Expand Up @@ -234,7 +235,7 @@ impl ParseStream {
self.clone()
}

pub fn consume(&mut self, num_chars: usize) -> ParseResult<Span> {
pub fn consume(&mut self, num_chars: usize) -> Result<Span> {
if self.remaining().len() < num_chars {
return Err(Error::new(
self.remaining_span(),
Expand All @@ -255,7 +256,7 @@ impl ParseStream {
span
}

pub fn next_char(&self) -> ParseResult<char> {
pub fn next_char(&self) -> Result<char> {
if self.remaining().is_empty() {
return Err(Error::new(self.current_span(), "unexpected end of input"));
}
Expand All @@ -270,13 +271,13 @@ impl ParseStream {
Ok(c)
}

pub fn parse_char(&mut self) -> ParseResult<char> {
pub fn parse_char(&mut self) -> Result<char> {
let c = self.next_char()?;
self.position += 1;
Ok(c)
}

pub fn next_digit(&self) -> ParseResult<u8> {
pub fn next_digit(&self) -> Result<u8> {
Ok(match self.next_char()? {
'0' => 0,
'1' => 1,
Expand All @@ -292,13 +293,13 @@ impl ParseStream {
})
}

pub fn parse_digit(&mut self) -> ParseResult<u8> {
pub fn parse_digit(&mut self) -> Result<u8> {
let digit = self.next_digit()?;
self.position += 1;
Ok(digit)
}

pub fn next_alpha(&self) -> ParseResult<char> {
pub fn next_alpha(&self) -> Result<char> {
let c = self.next_char()?;
if !c.is_ascii_alphabetic() {
return Err(Error::new(
Expand All @@ -309,7 +310,7 @@ impl ParseStream {
Ok(c)
}

pub fn parse_alpha(&mut self) -> ParseResult<char> {
pub fn parse_alpha(&mut self) -> Result<char> {
let c = self.next_alpha()?;
self.position += 1;
Ok(c)
Expand All @@ -333,7 +334,7 @@ impl<S: Into<Source>> From<S> for ParseStream {
}
}

pub fn parse<T: Parsable>(stream: impl Into<ParseStream>) -> ParseResult<T> {
pub fn parse<T: Parsable>(stream: impl Into<ParseStream>) -> Result<T> {
T::parse(&mut stream.into())
}

Expand All @@ -352,9 +353,9 @@ pub fn common_prefix(s1: &str, s2: &str) -> String {
pub trait Parsable:
Clone + Debug + PartialEq + Eq + Hash + Display + Spanned + FromStr + Peekable
{
fn parse(stream: &mut ParseStream) -> ParseResult<Self>;
fn parse(stream: &mut ParseStream) -> Result<Self>;

fn parse_value(value: Self, stream: &mut ParseStream) -> ParseResult<Self> {
fn parse_value(value: Self, stream: &mut ParseStream) -> Result<Self> {
let s = value.span();
let text = s.source_text();
if stream.remaining().starts_with(text) {
Expand Down Expand Up @@ -449,23 +450,23 @@ pub trait Pattern: Sized {

/// Tries to derive a [`Regex`] from the underlying value, returning a [`regex::Error`] if
/// the value is not a valid [`Regex`].
fn try_to_regex(self) -> Result<Regex, regex::Error>;
fn try_to_regex(self) -> core::result::Result<Regex, regex::Error>;
}

impl Pattern for Regex {
fn try_to_regex(self) -> Result<Regex, regex::Error> {
fn try_to_regex(self) -> core::result::Result<Regex, regex::Error> {
Ok(self)
}
}

impl Pattern for &str {
fn try_to_regex(self) -> Result<Regex, regex::Error> {
fn try_to_regex(self) -> core::result::Result<Regex, regex::Error> {
Regex::new(self)
}
}

impl Pattern for String {
fn try_to_regex(self) -> Result<Regex, regex::Error> {
fn try_to_regex(self) -> core::result::Result<Regex, regex::Error> {
Regex::new(&self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Span {
///
/// If the two spans do not come from the same [`Source`], this method will return an error
/// unless one or more of the spans is [`Span::blank()`].
pub fn join(&self, other: &Span) -> Result<Span, SpanJoinError> {
pub fn join(&self, other: &Span) -> core::result::Result<Span, SpanJoinError> {
if self.source.is_empty() {
return Ok(other.clone());
}
Expand Down

0 comments on commit 7c55f30

Please sign in to comment.