mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
use the EExpr identifier parser
This commit is contained in:
parent
8ee99fa6be
commit
5e2848d10c
3 changed files with 45 additions and 11 deletions
|
@ -3,7 +3,7 @@ use crate::blankspace::{
|
||||||
line_comment, space0_after_e, space0_around_ee, space0_before_e, space0_e, space1_e,
|
line_comment, space0_after_e, space0_around_ee, space0_before_e, space0_e, space1_e,
|
||||||
spaces_exactly_e,
|
spaces_exactly_e,
|
||||||
};
|
};
|
||||||
use crate::ident::{ident, lowercase_ident, Ident};
|
use crate::ident::{lowercase_ident, parse_ident_help, Ident};
|
||||||
use crate::keyword;
|
use crate::keyword;
|
||||||
use crate::parser::{
|
use crate::parser::{
|
||||||
self, allocated, and_then_with_indent_level, ascii_char, backtrackable, map, newline_char,
|
self, allocated, and_then_with_indent_level, ascii_char, backtrackable, map, newline_char,
|
||||||
|
@ -1967,11 +1967,11 @@ fn ident_then_args<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ident_without_apply_help<'a>() -> impl Parser<'a, Expr<'a>, EExpr<'a>> {
|
fn ident_without_apply_help<'a>() -> impl Parser<'a, Expr<'a>, EExpr<'a>> {
|
||||||
specialize_ref(
|
then(
|
||||||
EExpr::Syntax,
|
loc!(parse_ident_help),
|
||||||
then(loc!(ident()), move |arena, state, progress, loc_ident| {
|
move |arena, state, progress, loc_ident| {
|
||||||
Ok((progress, ident_to_expr(arena, loc_ident.value), state))
|
Ok((progress, ident_to_expr(arena, loc_ident.value), state))
|
||||||
}),
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2163,7 +2163,7 @@ fn record_field_help<'a>(
|
||||||
fn record_updateable_identifier<'a>() -> impl Parser<'a, Expr<'a>, ERecord<'a>> {
|
fn record_updateable_identifier<'a>() -> impl Parser<'a, Expr<'a>, ERecord<'a>> {
|
||||||
specialize(
|
specialize(
|
||||||
|_, r, c| ERecord::Updateable(r, c),
|
|_, r, c| ERecord::Updateable(r, c),
|
||||||
map_with_arena!(ident(), ident_to_expr),
|
map_with_arena!(parse_ident_help, ident_to_expr),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,11 +61,44 @@ impl<'a> Ident<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ident<'a>() -> impl Parser<'a, Ident<'a>, SyntaxError<'a>> {
|
fn chomp_identifier<'a, F>(pred: F, buffer: &[u8]) -> Result<&str, Progress>
|
||||||
crate::parser::specialize(|e, _, _| SyntaxError::Expr(e), parse_ident_help)
|
where
|
||||||
|
F: Fn(char) -> bool,
|
||||||
|
{
|
||||||
|
use encode_unicode::CharExt;
|
||||||
|
|
||||||
|
let mut chomped = 0;
|
||||||
|
|
||||||
|
match char::from_utf8_slice_start(&buffer[chomped..]) {
|
||||||
|
Ok((ch, width)) if pred(ch) => {
|
||||||
|
chomped += width;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// no parse
|
||||||
|
return Err(Progress::NoProgress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while let Ok((ch, width)) = char::from_utf8_slice_start(&buffer[chomped..]) {
|
||||||
|
// After the first character, only these are allowed:
|
||||||
|
//
|
||||||
|
// * Unicode alphabetic chars - you might include `鹏` if that's clear to your readers
|
||||||
|
// * ASCII digits - e.g. `1` but not `¾`, both of which pass .is_numeric()
|
||||||
|
// * A ':' indicating the end of the field
|
||||||
|
if ch.is_alphabetic() || ch.is_ascii_digit() {
|
||||||
|
chomped += width;
|
||||||
|
} else {
|
||||||
|
// we're done
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let name = unsafe { std::str::from_utf8_unchecked(&buffer[..chomped]) };
|
||||||
|
|
||||||
|
Ok(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn global_tag_or_ident<'a, F>(pred: F) -> impl Parser<'a, &'a str, SyntaxError<'a>>
|
fn global_tag_or_ident<'a, F>(pred: F) -> impl Parser<'a, &'a str, SyntaxError<'a>>
|
||||||
where
|
where
|
||||||
F: Fn(char) -> bool,
|
F: Fn(char) -> bool,
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::ast::Pattern;
|
use crate::ast::Pattern;
|
||||||
use crate::blankspace::{space0_around_ee, space0_before_e, space0_e};
|
use crate::blankspace::{space0_around_ee, space0_before_e, space0_e};
|
||||||
use crate::ident::{ident, lowercase_ident, Ident};
|
use crate::ident::{lowercase_ident, parse_ident_help, Ident};
|
||||||
use crate::parser::Progress::{self, *};
|
use crate::parser::Progress::{self, *};
|
||||||
use crate::parser::{
|
use crate::parser::{
|
||||||
backtrackable, optional, specialize, specialize_ref, word1, EPattern, PInParens, PRecord,
|
backtrackable, optional, specialize, specialize_ref, word1, EPattern, PInParens, PRecord,
|
||||||
|
@ -179,7 +179,8 @@ fn loc_ident_pattern_help<'a>(
|
||||||
let original_state = state.clone();
|
let original_state = state.clone();
|
||||||
|
|
||||||
let (_, loc_ident, state) =
|
let (_, loc_ident, state) =
|
||||||
specialize(|_, r, c| EPattern::Start(r, c), loc!(ident())).parse(arena, state)?;
|
specialize(|_, r, c| EPattern::Start(r, c), loc!(parse_ident_help))
|
||||||
|
.parse(arena, state)?;
|
||||||
|
|
||||||
match loc_ident.value {
|
match loc_ident.value {
|
||||||
Ident::GlobalTag(tag) => {
|
Ident::GlobalTag(tag) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue