Skip to content

Commit

Permalink
the pipes created at the during parsing instead of lexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarbetu committed Apr 18, 2024
1 parent 599db86 commit eee1a9c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
6 changes: 2 additions & 4 deletions crates/lexer/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub enum Token {
Less,
LessOrEqual,

// Pipe tokens are created at the parsing time tokens
Pipe,

// Keywords
Expand Down Expand Up @@ -701,8 +702,6 @@ impl<'a> TokenLexer<'a> {
check_symbol!("..=", RangeInclusive);
check_symbol!("..", Range);

check_symbol!(">>", Pipe);

check_symbol!("==", Equal);
check_symbol!("!=", NotEqual);
check_symbol!(">=", GreaterOrEqual);
Expand Down Expand Up @@ -1312,14 +1311,13 @@ r#''bar''#

#[test]
fn operators() {
let input = "> >= >> < <=";
let input = "> >= < <=";

check_lexer_output(
input,
&[
(Greater, None, 1),
(GreaterOrEqual, None, 1),
(Pipe, None, 1),
(Less, None, 1),
(LessOrEqual, None, 1),
],
Expand Down
21 changes: 18 additions & 3 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,24 @@ impl<'source> Parser<'source> {
{
return Ok(Some(assignment_expression));
} else if let Some(next) = self.peek_token_with_context(context) {
if let Some((left_priority, right_priority)) = operator_precedence(next.token) {
let maybe_pipe = match next.token {
Token::Greater => {
if matches!(self.peek_token_n(next.peek_count + 1), Some(Token::Greater)) {
Some(Token::Pipe)
} else {
None
}
}
_ => None,
};
if let Some((left_priority, right_priority)) =
operator_precedence(maybe_pipe.unwrap_or(next.token))
{
if left_priority >= min_precedence {
let (op, _) = self.consume_token_with_context(context).unwrap();
if maybe_pipe.is_some() {
self.consume_token();
}
let op_span = self.current_span();

// Move on to the token after the operator
Expand Down Expand Up @@ -602,15 +617,15 @@ impl<'source> Parser<'source> {
Equal => AstBinaryOp::Equal,
NotEqual => AstBinaryOp::NotEqual,

Greater => AstBinaryOp::Greater,
Greater if maybe_pipe.is_none() => AstBinaryOp::Greater,
GreaterOrEqual => AstBinaryOp::GreaterOrEqual,
Less => AstBinaryOp::Less,
LessOrEqual => AstBinaryOp::LessOrEqual,

And => AstBinaryOp::And,
Or => AstBinaryOp::Or,

Pipe => AstBinaryOp::Pipe,
Greater if maybe_pipe.is_some() => AstBinaryOp::Pipe,

_ => unreachable!(), // The list of tokens here matches the operators in
// operator_precedence()
Expand Down

0 comments on commit eee1a9c

Please sign in to comment.