mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 06:44:46 +00:00
remove Attempting and more
This commit is contained in:
parent
98024b2a21
commit
5e4db62c46
6 changed files with 46 additions and 136 deletions
|
@ -589,33 +589,6 @@ impl<'a> Spaceable<'a> for Def<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// What we're currently attempting to parse, e.g.
|
||||
/// "currently attempting to parse a list." This helps error messages!
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Attempting {
|
||||
LineComment,
|
||||
List,
|
||||
Keyword,
|
||||
StrLiteral,
|
||||
RecordLiteral,
|
||||
RecordFieldLabel,
|
||||
InterpolatedString,
|
||||
NumberLiteral,
|
||||
UnicodeEscape,
|
||||
ClosureParams,
|
||||
ClosureBody,
|
||||
Def,
|
||||
Module,
|
||||
Record,
|
||||
Identifier,
|
||||
HexDigit,
|
||||
ConcreteType,
|
||||
TypeVariable,
|
||||
WhenCondition,
|
||||
WhenBranch,
|
||||
TODO,
|
||||
}
|
||||
|
||||
impl<'a> Expr<'a> {
|
||||
pub fn loc_ref(&'a self, region: Region) -> Loc<&'a Self> {
|
||||
Loc {
|
||||
|
|
|
@ -275,7 +275,8 @@ where
|
|||
debug_assert!(u16::MAX - state.indent_col >= spaces as u16);
|
||||
debug_assert!(spaces <= u16::MAX as usize);
|
||||
|
||||
state.indent_col + spaces as u16
|
||||
// state.indent_col + spaces as u16
|
||||
state.indent_col
|
||||
} else {
|
||||
state.indent_col
|
||||
};
|
||||
|
|
|
@ -7,8 +7,8 @@ use crate::header::{
|
|||
use crate::ident::{lowercase_ident, unqualified_ident, uppercase_ident};
|
||||
use crate::parser::Progress::{self, *};
|
||||
use crate::parser::{
|
||||
backtrackable, end_of_file, specialize, word1, Col, EEffects, EExposes, EHeader, EImports,
|
||||
EPackages, EProvides, ERequires, ETypedIdent, Parser, Row, State, SyntaxError,
|
||||
backtrackable, specialize, word1, Col, EEffects, EExposes, EHeader, EImports, EPackages,
|
||||
EProvides, ERequires, ETypedIdent, Parser, Row, State, SyntaxError,
|
||||
};
|
||||
use crate::string_literal;
|
||||
use crate::type_annotation;
|
||||
|
@ -249,6 +249,16 @@ fn platform_header<'a>() -> impl Parser<'a, PlatformHeader<'a>, EHeader<'a>> {
|
|||
}
|
||||
}
|
||||
|
||||
fn end_of_file<'a>() -> impl Parser<'a, (), SyntaxError<'a>> {
|
||||
|_arena, state: State<'a>| {
|
||||
if state.has_reached_end() {
|
||||
Ok((NoProgress, (), state))
|
||||
} else {
|
||||
Err((NoProgress, SyntaxError::ConditionFailed, state))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn module_defs<'a>() -> impl Parser<'a, Vec<'a, Located<Def<'a>>>, SyntaxError<'a>> {
|
||||
use crate::parser::EExpr;
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
use crate::ast::Base;
|
||||
use crate::parser::{parse_utf8, Number, ParseResult, Parser, Progress, State, SyntaxError};
|
||||
use std::char;
|
||||
use std::str::from_utf8_unchecked;
|
||||
use crate::parser::{Number, ParseResult, Parser, Progress, State};
|
||||
|
||||
pub enum NumLiteral<'a> {
|
||||
Float(&'a str),
|
||||
|
@ -52,29 +50,21 @@ fn chomp_number_base<'a>(
|
|||
) -> ParseResult<'a, NumLiteral<'a>, Number> {
|
||||
let (_is_float, chomped) = chomp_number(bytes);
|
||||
|
||||
match parse_utf8(&bytes[0..chomped]) {
|
||||
Ok(string) => match state.advance_without_indenting(chomped + 2 + is_negative as usize) {
|
||||
Ok(new) => {
|
||||
// all is well
|
||||
Ok((
|
||||
Progress::MadeProgress,
|
||||
NumLiteral::NonBase10Int {
|
||||
is_negative,
|
||||
string,
|
||||
base,
|
||||
},
|
||||
new,
|
||||
))
|
||||
}
|
||||
Err((_, SyntaxError::LineTooLong(_), new)) => {
|
||||
// the only error we care about in this context
|
||||
Err((Progress::MadeProgress, Number::LineTooLong, new))
|
||||
}
|
||||
Err(_) => unreachable!("we know advancing will succeed if there is space on the line"),
|
||||
},
|
||||
let string = unsafe { std::str::from_utf8_unchecked(&bytes[..chomped]) };
|
||||
|
||||
Err(_) => unreachable!("no invalid utf8 could have been chomped"),
|
||||
}
|
||||
let new = state.advance_without_indenting_ee(chomped + 2 + is_negative as usize, |_, _| {
|
||||
Number::LineTooLong
|
||||
})?;
|
||||
|
||||
Ok((
|
||||
Progress::MadeProgress,
|
||||
NumLiteral::NonBase10Int {
|
||||
is_negative,
|
||||
string,
|
||||
base,
|
||||
},
|
||||
new,
|
||||
))
|
||||
}
|
||||
|
||||
fn chomp_number_dec<'a>(
|
||||
|
@ -94,27 +84,21 @@ fn chomp_number_dec<'a>(
|
|||
return Err((Progress::NoProgress, Number::End, state));
|
||||
}
|
||||
|
||||
let string = unsafe { from_utf8_unchecked(&state.bytes[0..chomped + is_negative as usize]) };
|
||||
let string =
|
||||
unsafe { std::str::from_utf8_unchecked(&state.bytes[0..chomped + is_negative as usize]) };
|
||||
|
||||
match state.advance_without_indenting(chomped + is_negative as usize) {
|
||||
Ok(new) => {
|
||||
// all is well
|
||||
Ok((
|
||||
Progress::MadeProgress,
|
||||
if is_float {
|
||||
NumLiteral::Float(string)
|
||||
} else {
|
||||
NumLiteral::Num(string)
|
||||
},
|
||||
new,
|
||||
))
|
||||
}
|
||||
Err((_, SyntaxError::LineTooLong(_), new)) => {
|
||||
// the only error we care about in this context
|
||||
Err((Progress::MadeProgress, Number::LineTooLong, new))
|
||||
}
|
||||
Err(_) => unreachable!("we know advancing will succeed if there is space on the line"),
|
||||
}
|
||||
let new = state
|
||||
.advance_without_indenting_ee(chomped + is_negative as usize, |_, _| Number::LineTooLong)?;
|
||||
|
||||
Ok((
|
||||
Progress::MadeProgress,
|
||||
if is_float {
|
||||
NumLiteral::Float(string)
|
||||
} else {
|
||||
NumLiteral::Num(string)
|
||||
},
|
||||
new,
|
||||
))
|
||||
}
|
||||
|
||||
fn chomp_number(mut bytes: &[u8]) -> (bool, usize) {
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
use crate::ast::Attempting;
|
||||
use bumpalo::collections::vec::Vec;
|
||||
use bumpalo::Bump;
|
||||
use roc_region::all::{Located, Region};
|
||||
use std::fmt;
|
||||
use std::str::from_utf8;
|
||||
use Progress::*;
|
||||
|
||||
/// A position in a source file.
|
||||
|
@ -59,13 +57,6 @@ impl<'a> State<'a> {
|
|||
/// This assumes we are *not* advancing with spaces, or at least that
|
||||
/// any spaces on the line were preceded by non-spaces - which would mean
|
||||
/// they weren't eligible to indent anyway.
|
||||
pub fn advance_without_indenting(
|
||||
self,
|
||||
quantity: usize,
|
||||
) -> Result<Self, (Progress, SyntaxError<'a>, Self)> {
|
||||
self.advance_without_indenting_ee(quantity, |line, _| SyntaxError::LineTooLong(line))
|
||||
}
|
||||
|
||||
pub fn advance_without_indenting_e<TE, E>(
|
||||
self,
|
||||
quantity: usize,
|
||||
|
@ -132,7 +123,7 @@ impl<'a> fmt::Debug for State<'a> {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "State {{")?;
|
||||
|
||||
match from_utf8(self.bytes) {
|
||||
match std::str::from_utf8(self.bytes) {
|
||||
Ok(string) => write!(f, "\n\tbytes: [utf8] {:?}", string)?,
|
||||
Err(_) => write!(f, "\n\tbytes: [invalid utf8] {:?}", self.bytes)?,
|
||||
}
|
||||
|
@ -704,36 +695,6 @@ pub enum TVariable {
|
|||
Space(BadInputError, Row, Col),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ContextStack<'a> {
|
||||
Cons(ContextItem, &'a ContextStack<'a>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
impl<'a> ContextStack<'a> {
|
||||
pub fn uncons(&'a self) -> Option<(ContextItem, &'a Self)> {
|
||||
match self {
|
||||
ContextStack::Cons(item, rest) => Some((*item, rest)),
|
||||
ContextStack::Nil => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ContextItem {
|
||||
pub line: u32,
|
||||
pub column: u16,
|
||||
pub context: Attempting,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DeadEnd<'a, T> {
|
||||
pub line: u32,
|
||||
pub column: u16,
|
||||
pub problem: T,
|
||||
pub context_stack: ContextStack<'a>,
|
||||
}
|
||||
|
||||
/// use std vec to escape the arena's lifetime bound
|
||||
/// since this is only used when there is in fact an error
|
||||
/// I think this is fine
|
||||
|
@ -1862,23 +1823,6 @@ where
|
|||
map_with_arena!(parser, transform)
|
||||
}
|
||||
|
||||
pub fn parse_utf8<'a>(bytes: &[u8]) -> Result<&str, SyntaxError<'a>> {
|
||||
match from_utf8(bytes) {
|
||||
Ok(string) => Ok(string),
|
||||
Err(_) => Err(SyntaxError::BadUtf8),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_of_file<'a>() -> impl Parser<'a, (), SyntaxError<'a>> {
|
||||
|_arena: &'a Bump, state: State<'a>| {
|
||||
if state.has_reached_end() {
|
||||
Ok((NoProgress, (), state))
|
||||
} else {
|
||||
Err((NoProgress, SyntaxError::ConditionFailed, state))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backtrackable<'a, P, Val, Error>(parser: P) -> impl Parser<'a, Val, Error>
|
||||
where
|
||||
P: Parser<'a, Val, Error>,
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
use crate::ast::{EscapedChar, StrLiteral, StrSegment};
|
||||
use crate::expr;
|
||||
use crate::parser::Progress::*;
|
||||
use crate::parser::{
|
||||
allocated, loc, parse_utf8, specialize_ref, word1, BadInputError, EString, Parser, State,
|
||||
};
|
||||
use crate::parser::{allocated, loc, specialize_ref, word1, BadInputError, EString, Parser, State};
|
||||
use bumpalo::collections::vec::Vec;
|
||||
use bumpalo::Bump;
|
||||
|
||||
|
@ -101,7 +99,7 @@ pub fn parse<'a>() -> impl Parser<'a, StrLiteral<'a>, EString<'a>> {
|
|||
// to exclude that char we just parsed.
|
||||
let string_bytes = &state.bytes[0..(segment_parsed_bytes - 1)];
|
||||
|
||||
match parse_utf8(string_bytes) {
|
||||
match std::str::from_utf8(string_bytes) {
|
||||
Ok(string) => {
|
||||
state = advance_state!(state, string.len())?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue