Skip to content

Commit 40a3c8b

Browse files
committed
parser: keep tests
1 parent 7436420 commit 40a3c8b

4 files changed

Lines changed: 20 additions & 103 deletions

File tree

parser/src/diagnostics.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ pub enum ParsingError {
7575
SpecialVariableIndirectCall(Span, String),
7676
#[error("Can't chain non-associative operators.")]
7777
NonAssociativeOperator(Span),
78-
#[error("Using reserved identifier `{1}` as a namespace is not allowed.")]
79-
ReservedNamespace(Span, String),
80-
#[error(
81-
"Using reserved identifier `{1}` as second component of a qualified name is not allowed."
82-
)]
83-
ReservedQualifiedLiteral(Span, String),
8478
}
8579

8680
impl ParsingError {
@@ -127,8 +121,6 @@ impl ParsingError {
127121
Self::SpecialVariableCall(span, _) => Some(span.clone()),
128122
Self::SpecialVariableIndirectCall(span, _) => Some(span.clone()),
129123
Self::NonAssociativeOperator(span) => Some(span.clone()),
130-
Self::ReservedNamespace(span, _) => Some(span.clone()),
131-
Self::ReservedQualifiedLiteral(span, _) => Some(span.clone()),
132124
}
133125
}
134126
fn hint(&self) -> Option<&'static str> {

parser/src/keywords.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

parser/src/lib.rs

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
mod ast;
99
mod diagnostics;
1010
mod idempotency;
11-
mod keywords;
1211
mod lex;
1312
mod pratt;
1413
mod sexpr;
@@ -135,14 +134,7 @@ impl<'a> Parser<'a> {
135134
}
136135
Token::NamespaceDirective => {
137136
let namespace = lex.expect_string()?;
138-
let namespace = lex.lex_ident(namespace.as_ref(), self.arena)?;
139-
if keywords::is_reserved_keyword(namespace) {
140-
return Err(ParsingError::ReservedNamespace(
141-
lex.span(),
142-
namespace.to_string(),
143-
));
144-
}
145-
self.namespace = namespace;
137+
self.namespace = lex.lex_ident(namespace.as_ref(), self.arena)?;
146138
lex.expect_with(Token::is_stmnt_end, "expected statement end.".into())?;
147139
}
148140
Token::ConcurrentDirective => {
@@ -552,13 +544,8 @@ impl<'a> Parser<'a> {
552544

553545
fn parse_delete(&mut self, lex: &mut Lexer<'a>) -> Result<SimpleStatement<'a>> {
554546
let next = lex.expect_next()?;
555-
let var = match self.get_place(lex, next) {
556-
Ok(var) => var,
557-
Err(
558-
err @ (ParsingError::ReservedNamespace(..)
559-
| ParsingError::ReservedQualifiedLiteral(..)),
560-
) => return Err(err),
561-
Err(_) => return Err(ParsingError::OperatorExpectsVariable(lex.span())),
547+
let Ok(var) = self.get_place(lex, next) else {
548+
return Err(ParsingError::OperatorExpectsVariable(lex.span()));
562549
};
563550
let index = if lex.consume(&Token::OpenBracket) {
564551
let mut pratt = Pratt::new(self, false);
@@ -574,9 +561,7 @@ impl<'a> Parser<'a> {
574561

575562
#[tracing::instrument]
576563
fn parse_function(&mut self, lex: &mut Lexer<'a>) -> Result<()> {
577-
let name = lex
578-
.expect_identifier()?
579-
.try_qualify(self.namespace, &lex.span())?;
564+
let name = lex.expect_identifier()?.qualify(self.namespace);
580565
let args = self.parse_signature(lex, &name)?;
581566
lex.consume(&Token::Newline);
582567
let body = self.parse_body(lex)?;
@@ -601,9 +586,7 @@ impl<'a> Parser<'a> {
601586
}
602587

603588
loop {
604-
let name = lex
605-
.expect_identifier()?
606-
.try_qualify(self.namespace, &lex.span())?;
589+
let name = lex.expect_identifier()?.qualify(self.namespace);
607590
// Linear search is fine for the numbers we are working with.
608591
if let Some(arg) = args.iter().find(|&a| a == &name) {
609592
return Err(ParsingError::DuplicatedArgument(
@@ -675,10 +658,6 @@ impl<'a> Parser<'a> {
675658
Token::TypedRegex(_) => Err(ParsingError::UnexpectedTypedRegex(lex.span())),
676659
token => match self.get_place(lex, token) {
677660
Ok(var) => Ok(Atom::Variable(var)),
678-
Err(
679-
err @ (ParsingError::ReservedNamespace(..)
680-
| ParsingError::ReservedQualifiedLiteral(..)),
681-
) => Err(err),
682661
Err(_) => Err(ParsingError::UnexpectedToken(
683662
lex.span(),
684663
"is not valid data.".into(),
@@ -688,10 +667,10 @@ impl<'a> Parser<'a> {
688667
}
689668

690669
#[tracing::instrument]
691-
fn get_place(&self, lex: &mut Lexer<'a>, token: Token<'a>) -> Result<Variable<'a>> {
670+
fn get_place(&self, lex: &mut Lexer<'a>, token: Token<'a>) -> Result<Variable<'a>, Token<'a>> {
692671
match token {
693672
Token::Identifier(a) if !(lex.peek_is(&Token::OpenParent) && lex.is_yuxtaposed()) => {
694-
Ok(a.try_qualify(self.namespace, &lex.span())?.into())
673+
Ok(a.qualify(self.namespace).into())
695674
}
696675
Token::NrVariable => Ok(Variable::Nr),
697676
Token::NfVariable => Ok(Variable::Nf),
@@ -708,10 +687,7 @@ impl<'a> Parser<'a> {
708687
Token::RstartVariable => Ok(Variable::Rstart),
709688
Token::RlengthVariable => Ok(Variable::Rlength),
710689
Token::EnvironVariable => Ok(Variable::Environ),
711-
tok => Err(ParsingError::UnexpectedToken(
712-
lex.span(),
713-
format!("{tok:?}"),
714-
)),
690+
tok => Err(tok),
715691
}
716692
}
717693
}
@@ -741,35 +717,22 @@ impl Preprocessor {
741717
}
742718

743719
trait IdentifierExt<'a> {
744-
fn try_qualify(self, namespace: &'a str, span: &Span) -> Result<Identifier<'a>>
720+
fn qualify(self, namespace: &'a str) -> Identifier<'a>
745721
where
746722
Self: 'a;
747723
}
748724

749725
impl<'a> IdentifierExt<'a> for lexer::Identifier<'_> {
750-
fn try_qualify(self, namespace: &'a str, span: &Span) -> Result<Identifier<'a>>
726+
fn qualify(self, namespace: &'a str) -> Identifier<'a>
751727
where
752728
Self: 'a,
753729
{
754730
let literal = self.literal;
755-
let namespace = if let Some(ns) = self.namespace {
756-
if keywords::is_reserved_keyword(ns) {
757-
return Err(ParsingError::ReservedNamespace(
758-
span.clone(),
759-
ns.to_string(),
760-
));
761-
}
762-
if keywords::is_reserved_keyword(literal) {
763-
return Err(ParsingError::ReservedQualifiedLiteral(
764-
span.clone(),
765-
literal.to_string(),
766-
));
767-
}
768-
ns
731+
if let Some(namespace) = self.namespace {
732+
Identifier { namespace, literal }
769733
} else {
770-
namespace
771-
};
772-
Ok(Identifier { namespace, literal })
734+
Identifier { namespace, literal }
735+
}
773736
}
774737
}
775738

parser/src/pratt.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,10 @@ impl<'a, 'b> Pratt<'a, 'b> {
269269
name.literal.to_string(),
270270
));
271271
}
272-
let span = lex.span();
273-
let qualified = name.try_qualify(self.parser.namespace, &span)?;
274272
self.parser.parse_function_call(
275273
lex,
276-
move |args| ExprNode::FunctionCall(qualified, args),
277-
span,
274+
|args| ExprNode::FunctionCall(name.qualify(self.parser.namespace), args),
275+
lex.span(),
278276
)
279277
} else if let Some(builtin) = next.maps_to_builtin() {
280278
self.parser.parse_function_call(
@@ -291,17 +289,16 @@ impl<'a, 'b> Pratt<'a, 'b> {
291289
name.literal.to_string(),
292290
));
293291
}
294-
let span = lex.span();
295-
let name = Variable::User(name.try_qualify(self.parser.namespace, &span)?);
292+
let name = Variable::User(name.qualify(self.parser.namespace));
296293
self.parser.parse_function_call(
297294
lex,
298-
move |args| ExprNode::IndirectCall(name, args),
299-
span,
295+
|args| ExprNode::IndirectCall(name, args),
296+
lex.span(),
300297
)
301298
} else if next.is_place() && lex.peek_is(&Token::OpenParent) && lex.is_yuxtaposed() {
302299
let name = match self.parser.get_place(lex, next) {
303300
Ok(var) => var.to_string(),
304-
Err(err) => err.to_string(),
301+
Err(tok) => format!("{tok:?}"),
305302
};
306303
Err(ParsingError::SpecialVariableCall(lex.span(), name))
307304
} else {

0 commit comments

Comments
 (0)