Skip to content

Commit

Permalink
almost
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Apr 3, 2024
1 parent 6c004ba commit 5bb54af
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 87 deletions.
73 changes: 36 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ license = "MIT"
quoth-macros = { path = "quoth-macros", version = "0.1.5" }
regex = "1.10"
rust_decimal = "1"
safe-string = "0.1.5"
safe-string = { git = "https://github.com/sam0x17/safe-string.git", branch = "main" }
2 changes: 1 addition & 1 deletion src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Display for Diagnostic {
for _ in 0..range.start {
write!(f, " ")?;
}
let chars = lin.chars().collect::<Vec<_>>();
let chars = lin.chars();
let mut prev = false;
for i in range {
let Some(char) = chars.get(i) else {
Expand Down
18 changes: 9 additions & 9 deletions src/parsable/everything.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ impl Parsable for Everything {
fn parse(stream: &mut ParseStream) -> Result<Self> {
let span = Span::new(
stream.source().clone(),
stream.position..(stream.source().chars().count()),
stream.position..(stream.source().len()),
);
stream.position = stream.source().chars().count();
stream.position = stream.source().len();
Ok(Everything(span))
}

fn parse_value(value: Self, stream: &mut ParseStream) -> Result<Self> {
let s = value.span();
let text = s.source_text();
if stream.remaining() == text {
return Ok(Everything(stream.consume(text.chars().count())?));
return Ok(Everything(stream.consume(text.len())?));
}
let prefix = common_prefix(text, stream.remaining());
stream.consume(prefix.chars().count())?;
let prefix = common_prefix(&text, stream.remaining());
stream.consume(prefix.len())?;
let missing_span = stream.current_span();
let missing = &text[prefix.chars().count()..];
if missing.chars().count() > 0 {
let missing = text.slice(prefix.len()..);
if missing.len() > 0 {
return Err(Error::expected(missing_span, missing));
}
Err(Error::new(missing_span, "expected end of input"))
Expand Down Expand Up @@ -55,14 +55,14 @@ fn test_parse_everything() {
let mut stream = ParseStream::from("this is a triumph");
let parsed = Everything(Span::new(
Rc::new(Source::from_str("this is b")),
0.."this is b".chars().count(),
0.."this is b".len(),
));
let e = stream.parse_value(parsed).unwrap_err();
assert!(e.message().contains("expected `b`"));
let mut stream = ParseStream::from("this is a triumph");
let parsed = Everything(Span::new(
Rc::new(Source::from_str("this is a")),
0.."this is a".chars().count(),
0.."this is a".len(),
));
let e = stream.parse_value(parsed).unwrap_err();
assert!(e.message().contains("expected end of input"));
Expand Down
12 changes: 6 additions & 6 deletions src/parsable/exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Exact {

pub fn from(source: impl Into<Source>) -> Self {
let source = Rc::new(source.into());
let len = source.chars().count();
let len = source.len();
Exact(Span::new(source, 0..len))
}
}
Expand All @@ -30,18 +30,18 @@ impl Parsable for Exact {
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) {
if stream.remaining().starts_with(&text) {
let start_position = stream.position;
stream.position += text.chars().count();
stream.position += text.len();
return Ok(Exact(Span::new(
stream.source().clone(),
start_position..stream.position,
)));
}
let prefix = common_prefix(text, stream.remaining());
stream.consume(prefix.chars().count())?;
let prefix = common_prefix(&text, stream.remaining());
stream.consume(prefix.len())?;
let missing_span = stream.current_span();
let missing = &text[prefix.chars().count()..];
let missing = text.slice(prefix.len()..);
Err(Error::expected(missing_span, missing))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parsable/nothing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Nothing(Span);

impl Parsable for Nothing {
fn parse(stream: &mut ParseStream) -> Result<Self> {
if stream.position < stream.source().chars().count() {
if stream.position < stream.source().len() {
return Err(Error::new(
stream.current_span(),
format!(
Expand Down
2 changes: 1 addition & 1 deletion src/parsable/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Decimal {
impl From<rust_decimal::Decimal> for Decimal {
fn from(value: rust_decimal::Decimal) -> Self {
let st = value.to_string();
let len = st.chars().count();
let len = st.len();
let span = Span::new(Rc::new(Source::from_str(st)), 0..len);
Decimal(value, span.into())
}
Expand Down
Loading

0 comments on commit 5bb54af

Please sign in to comment.