Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ unexpected_cfgs = { level = "warn", check-cfg = [
unused_qualifications = "warn"

[workspace.lints.clippy]
collapsible_if = { level = "allow", priority = 127 } # remove me
# The counts were generated with this command:
# cargo clippy --all-targets --workspace --message-format=json --quiet \
# | jq -r '.message.code.code | select(. != null and startswith("clippy::"))' \
Expand Down
15 changes: 9 additions & 6 deletions lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ pub enum Token<'a> {
RlengthVariable,
#[token("ENVIRON", accept_expression)]
EnvironVariable,
#[regex("(?&identifier)", Identifier::without_namespace)]
#[regex(r"(?&identifier)::(?&identifier)", Identifier::with_namespace)]
#[regex("(?&identifier)", |lex| Identifier::without_namespace::<0>(lex))]
#[regex(r"(?&identifier)::(?&identifier)", |lex| Identifier::with_namespace::<0>(lex))]
Identifier(Identifier<'a>),
#[regex(r"(?&identifier)\(", |lex| Identifier::without_namespace::<1>(lex))]
#[regex(r"(?&identifier)::(?&identifier)\(", |lex| Identifier::with_namespace::<1>(lex))]
FunctionCall(Identifier<'a>),
#[token("+", accept_expression)]
Plus,
#[token("-", accept_expression)]
Expand Down Expand Up @@ -376,19 +379,19 @@ fn parse_float(lex: &mut Lexer<'_>) -> f64 {
}

impl<'a> Identifier<'a> {
fn without_namespace(lex: &mut Lexer<'a>) -> Self {
fn without_namespace<const TRIM: usize>(lex: &mut Lexer<'a>) -> Self {
Self {
namespace: None,
literal: parse_ident(lex, ..),
literal: parse_ident(lex, ..lex.slice().len() - TRIM),
}
}

fn with_namespace(lex: &mut Lexer<'a>) -> Self {
fn with_namespace<const TRIM: usize>(lex: &mut Lexer<'a>) -> Self {
// SAFETY: The regex matching ensures it is present and well-formed.
let separator = unsafe { memchr(b':', lex.slice()).unwrap_unchecked() };
Self {
namespace: Some(parse_ident(lex, ..separator)),
literal: parse_ident(lex, separator + 2..),
literal: parse_ident(lex, separator + 2..lex.slice().len() - TRIM),
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions parser/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ impl TokenExt for Token<'_> {
fn is_expr_start(&self) -> bool {
self.is_atom()
|| self.is_prefix_op()
|| self == &Token::OpenParent
|| self == &Token::Getline
|| matches!(
self,
Token::OpenParent | Token::FunctionCall(_) | Token::Getline
)
}
fn is_place(&self) -> bool {
matches!(
Expand Down
28 changes: 13 additions & 15 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,17 @@ impl<'a> Parser<'a> {

#[tracing::instrument]
fn parse_function(&mut self, lex: &mut Lexer<'a>) -> Result<()> {
let name = lex.expect_identifier()?.qualify(self.namespace);
let name = match lex.expect_next()? {
Token::FunctionCall(ident) => ident,
Token::Identifier(ident) => {
lex.expect(&Token::OpenParent, |span| {
ParsingError::NoFunctionSignature(span, ident.literal.to_string())
})?;
ident
}
_ => return Err(ParsingError::ExpectedIdentifier(lex.span())),
}
.qualify(self.namespace);
let args = self.parse_signature(lex, &name)?;
lex.consume(&Token::Newline);
let body = self.parse_body(lex)?;
Expand All @@ -481,9 +491,6 @@ impl<'a> Parser<'a> {
name: &Identifier<'a>,
) -> Result<Vec<'a, Identifier<'a>>> {
let mut args = Vec::new_in(self.arena);
lex.expect(&Token::OpenParent, |s| {
ParsingError::NoFunctionSignature(s, name.to_string())
})?;

if lex.consume(&Token::ClosedParent) {
return Ok(args);
Expand Down Expand Up @@ -567,10 +574,7 @@ impl<'a> Parser<'a> {
}
} else {
let next = lex.expect_next()?;
if let Token::Identifier(name) = next
&& lex.peek_is(&Token::OpenParent)
{
// TODO: use spans to check there is no space between ident, (.
if let Token::FunctionCall(name) = next {
self.parse_function_call(lex, name.qualify(self.namespace), lex.span())?
} else {
Expr::leaf(self.parse_atom(lex, next)?)
Expand Down Expand Up @@ -691,10 +695,6 @@ impl<'a> Parser<'a> {
name: Identifier<'a>,
span: Span,
) -> Result<Expr<'a>> {
lex.expect(&Token::OpenParent, ParsingError::ExpectedOpeningParenthesis)?;
if lex.span().start != span.end {
return Err(ParsingError::FunctionCallSeparatedIdent(span));
}
let expr = ExprNode::FunctionCall(
name,
self.parse_arguments(lex, |t| t == &Token::ClosedParent)?,
Expand Down Expand Up @@ -724,9 +724,7 @@ impl<'a> Parser<'a> {
#[tracing::instrument]
fn get_place(&self, lex: &mut Lexer<'a>, token: Token<'a>) -> Option<Variable<'a>> {
match token {
Token::Identifier(a) if !lex.peek_is(&Token::OpenParent) => {
Some(a.qualify(self.namespace).into())
}
Token::Identifier(a) => Some(a.qualify(self.namespace).into()),
Token::NrVariable => Some(Variable::Nr),
Token::NfVariable => Some(Variable::Nf),
Token::FsVariable => Some(Variable::Fs),
Expand Down
Loading