Skip to content

Commit c52acc2

Browse files
committed
#748 Fix signed numbers not parsing correctly after parens
1 parent ff0812e commit c52acc2

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/fun/parser.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,21 +590,56 @@ impl<'a> FunParser<'a> {
590590
// App, Tup, Num Op
591591
if self.starts_with("(") {
592592
self.advance_one();
593-
594-
// Opr but maybe a tup
595593
self.skip_trivia();
594+
595+
// Opr but maybe something else
596+
// ( +/-n , -> Tup with Int/Float
597+
// ( +/-n ) -> Int/Float
598+
// ( +/-n term -> App with Int/Float
599+
// ( * , -> Tup with Era
600+
// ( * ) -> Era
601+
// ( opr -> Num Op
596602
if let Some(opr) = self.try_parse_oper() {
603+
if (opr == Op::ADD || opr == Op::SUB) && self.peek_one().map_or(false, |c| "0123456789".contains(c))
604+
{
605+
unexpected_tag(self)?;
606+
*self.index() -= 1;
607+
let num = self.parse_number()?;
608+
let head = Term::Num { val: num };
609+
self.skip_trivia();
610+
611+
if self.starts_with(",") {
612+
self.consume_exactly(",")?;
613+
let tail = self.list_like(|p| p.parse_term(), "", ")", ",", true, 1)?;
614+
let els = [head].into_iter().chain(tail).collect();
615+
return Ok(Term::Fan { fan: FanKind::Tup, tag: tag.unwrap_or(Tag::Static), els });
616+
}
617+
618+
if self.starts_with(")") {
619+
self.consume_exactly(")")?;
620+
return Ok(head);
621+
}
622+
623+
let els = self.list_like(|p| p.parse_term(), "", ")", "", false, 0)?;
624+
let term = els.into_iter().fold(head, |fun, arg| Term::App {
625+
tag: tag.clone().unwrap_or(Tag::Static),
626+
fun: Box::new(fun),
627+
arg: Box::new(arg),
628+
});
629+
return Ok(term);
630+
}
631+
597632
self.skip_trivia();
598633

599-
// jk, actually a tuple
600-
if self.starts_with(",") && opr == Op::MUL {
634+
if opr == Op::MUL && self.starts_with(",") {
601635
self.consume_exactly(",")?;
602636
let tail = self.list_like(|p| p.parse_term(), "", ")", ",", true, 1)?;
603637
let els = [Term::Era].into_iter().chain(tail).collect();
604638
return Ok(Term::Fan { fan: FanKind::Tup, tag: tag.unwrap_or(Tag::Static), els });
605639
}
606640

607-
if opr == Op::MUL && self.try_consume(")") {
641+
if opr == Op::MUL && self.starts_with(")") {
642+
self.consume_exactly(")")?;
608643
return Ok(Term::Era);
609644
}
610645

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Test that we can parse tuples and apps beginning with signed numbers
2+
main =
3+
let a = (+1, +1)
4+
let b = (+1.1324)
5+
let c = (-6.234, -1)
6+
let d = (-1)
7+
let e = (+6 * λx x)
8+
let f = ((*) λx x)
9+
(+ a (+ b (+ c (+ d (- e f)))))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: tests/golden_tests.rs
3+
input_file: tests/golden_tests/parse_file/tup_with_signed.bend
4+
---
5+
unchecked main: Any
6+
(main) = let a = (+1, +1); let b = 1.132; let c = (-6.234, -1); let d = -1; let e = (+6 * λx x); let f = (* λx x); (+ a (+ b (+ c (+ d (- e f)))))

0 commit comments

Comments
 (0)