Skip to content

Commit f993164

Browse files
committed
feat: parse ranges
1 parent 64f5f6e commit f993164

File tree

4 files changed

+48
-54
lines changed

4 files changed

+48
-54
lines changed

packages/engine/src/lexer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'a> Lexer<'a> {
180180
',' => Comma,
181181
':' => Colon,
182182
_ if double!('.', '.') => {
183-
if self.next() == Some('=') {
183+
if self.next_if_eq(&'=').is_some() {
184184
RangeInclusive
185185
} else {
186186
Range

packages/engine/src/parser/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum Expression {
1212
Arithmetic(Box<(WithCursor<Expression>, WithCursor<ArithmeticOperator>, WithCursor<Expression>)>),
1313
Logical(Box<(WithCursor<Expression>, WithCursor<LogicalOperator>, WithCursor<Expression>)>),
1414
Assignment(Box<(WithCursor<Expression>, WithCursor<AssignmentOperator>, WithCursor<Expression>)>),
15-
Range(Box<(WithCursor<Literal>, WithCursor<Literal>, bool)>),
15+
Range(Box<(WithCursor<Expression>, WithCursor<Expression>, bool)>),
1616
ShellCommand(Box<ShellCommand>),
1717
Identifier(Box<Identifier>),
1818
FunctionCall(Box<(Identifier, Vec<WithCursor<Expression>>)>),

packages/engine/src/parser/mod.rs

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,6 @@ impl<'a> Parser<'a> {
205205
Ok(Statement::Function(Box::from(function)))
206206
}
207207

208-
// MARK: Block Parsing
209-
fn parse_inline_block(&mut self) -> ParserResult<WithCursor<Block>> {
210-
let Some(next) = self.peek().cloned() else {
211-
return Err(ParserErrorKind::ExpectedStatement);
212-
};
213-
214-
let start = self.cursor;
215-
let Some(stmt) = self.parse_statement(next)? else {
216-
return Err(ParserErrorKind::ExpectedStatement);
217-
};
218-
219-
self.next();
220-
Ok(WithCursor::create_with(start, self.cursor, vec![stmt]))
221-
}
222-
223208
fn expression(&mut self) -> ParserResult<Option<WithCursor<Expression>>> {
224209
self.assignment()
225210
}
@@ -470,7 +455,7 @@ impl<'a> Parser<'a> {
470455
// MARK: Primary
471456
/// Highest precedence
472457
fn primary(&mut self) -> ParserResult<Option<WithCursor<Expression>>> {
473-
let_expr!(token = self.peek());
458+
let_expr!(token = self.next());
474459

475460
let expr = match token.kind {
476461
LexerTokenKind::String => Expression::Literal(Box::from(Literal::String(Box::from(
@@ -494,38 +479,48 @@ impl<'a> Parser<'a> {
494479
}
495480

496481
LexerTokenKind::LParen => {
497-
self.next();
498482
return self.group();
499483
}
500484

501485
LexerTokenKind::LBracket => {
502-
self.next();
503486
return self.block_expr();
504487
}
505488

506489
LexerTokenKind::If => {
507-
self.next();
508490
return self.if_expr();
509491
}
510492

511493
LexerTokenKind::Match => {
512-
self.next();
513494
return self.match_expr();
514495
}
515496

516497
LexerTokenKind::EOL | LexerTokenKind::EOF => return Ok(None),
517498

518-
_ => {
519-
let token = token.to_owned();
520-
self.cursor_next(&token);
521-
return Err(ParserErrorKind::UnexpectedToken(token.kind.clone()));
522-
}
499+
_ => return Err(ParserErrorKind::UnexpectedToken(token.kind.clone())),
523500
};
524501

525-
let token = token.to_owned();
526-
self.next();
502+
let_expr!(post = self.range(WithCursor::create_with(token.start, token.end, expr))?);
527503

528-
Ok(Some(WithCursor::create_with(token.start, token.end, expr)))
504+
Ok(Some(post))
505+
}
506+
507+
// MARK: Range
508+
fn range(&mut self, lhs: WithCursor<Expression>) -> ParserResult<Option<WithCursor<Expression>>> {
509+
let_expr!(peek = self.peek());
510+
let inclusive = match peek.kind {
511+
LexerTokenKind::Range => false,
512+
LexerTokenKind::RangeInclusive => true,
513+
_ => return Ok(Some(lhs))
514+
};
515+
516+
self.next();
517+
let_expr!(rhs = self.expression()?);
518+
519+
Ok(Some(WithCursor::create_with(lhs.start, rhs.end, Expression::Range(Box::from((
520+
lhs,
521+
rhs,
522+
inclusive
523+
))))))
529524
}
530525

531526
// MARK: Grouping
@@ -545,7 +540,6 @@ impl<'a> Parser<'a> {
545540

546541
let start = self.cursor;
547542

548-
549543
let truthy_block = if self.next_if_eq(&&LexerTokenKind::LBracket).is_some() {
550544
self.parse_block()?
551545
} else if self.next_if_eq(&&LexerTokenKind::Colon).is_some() {
@@ -554,7 +548,6 @@ impl<'a> Parser<'a> {
554548
return Err(ParserErrorKind::ExpectedExpression);
555549
};
556550

557-
558551
let else_condition = if self.next_if_eq(&&LexerTokenKind::Else).is_some() {
559552
let start = self.cursor;
560553

@@ -601,7 +594,7 @@ impl<'a> Parser<'a> {
601594
self.expect_terminator()?;
602595

603596
let mut hash_map = MatchCase::new();
604-
597+
605598
while let Some(token) = self.peek().cloned() {
606599
if token.kind == LexerTokenKind::RBracket {
607600
// self.next();
@@ -653,6 +646,21 @@ impl<'a> Parser<'a> {
653646
)))
654647
}
655648

649+
// MARK: Block Parsing
650+
fn parse_inline_block(&mut self) -> ParserResult<WithCursor<Block>> {
651+
let Some(next) = self.peek().cloned() else {
652+
return Err(ParserErrorKind::ExpectedStatement);
653+
};
654+
655+
let start = self.cursor;
656+
let Some(stmt) = self.parse_statement(next)? else {
657+
return Err(ParserErrorKind::ExpectedStatement);
658+
};
659+
660+
self.next();
661+
Ok(WithCursor::create_with(start, self.cursor, vec![stmt]))
662+
}
663+
656664
fn block_expr(&mut self) -> ParserResult<Option<WithCursor<Expression>>> {
657665
let block = self.parse_block()?;
658666

@@ -680,7 +688,7 @@ impl<'a> Parser<'a> {
680688
if let Some(statement) = self.parse_statement(token)? {
681689
block.push(statement);
682690
}
683-
691+
684692
self.next();
685693
}
686694

@@ -693,12 +701,13 @@ impl<'a> Parser<'a> {
693701
})
694702
}
695703

696-
fn expect_terminator(&mut self) -> ParserResult<&LexerToken> {
704+
fn expect_terminator(&mut self) -> ParserResult<Option<&LexerToken>> {
697705
match self.expect_any(&[&LexerTokenKind::EOL, &LexerTokenKind::EOF]) {
698-
Ok(found) => Ok(found),
699-
Err(found) => Err(ParserErrorKind::ExpectedToken(
706+
Ok(found) => Ok(Some(found)),
707+
Err(None) => Ok(None),
708+
Err(Some(found)) => Err(ParserErrorKind::ExpectedToken(
700709
vec![LexerTokenKind::EOL, LexerTokenKind::EOF],
701-
found.map(|t| t.kind.clone()),
710+
Some(found.kind.clone()),
702711
)),
703712
}
704713
}

script.tsh

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1 @@
1-
fn test() {
2-
if true {
3-
5
4-
} else {
5-
if true {
6-
10
7-
} else {
8-
match "pattern" {
9-
"not" || "test" => {
10-
10
11-
}
12-
"cool" => 5
13-
}
14-
}
15-
}
16-
}
1+
var test = awesome..=test

0 commit comments

Comments
 (0)