diff options
Diffstat (limited to 'compiler/src/ast')
| -rw-r--r-- | compiler/src/ast/parser.rs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/compiler/src/ast/parser.rs b/compiler/src/ast/parser.rs index 171ecfe..2e6d2dd 100644 --- a/compiler/src/ast/parser.rs +++ b/compiler/src/ast/parser.rs | |||
| @@ -67,7 +67,7 @@ where | |||
| 67 | I: Iterator<Item = Spanned<Token<'a>>>, | 67 | I: Iterator<Item = Spanned<Token<'a>>>, |
| 68 | { | 68 | { |
| 69 | tokens: Peekable<I>, | 69 | tokens: Peekable<I>, |
| 70 | cursor: Pos, | 70 | last_token_end: Pos, |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | impl<'a, I> Parser<'a, I> | 73 | impl<'a, I> Parser<'a, I> |
| @@ -77,7 +77,7 @@ where | |||
| 77 | pub fn new(tokens: I) -> Self { | 77 | pub fn new(tokens: I) -> Self { |
| 78 | Self { | 78 | Self { |
| 79 | tokens: tokens.peekable(), | 79 | tokens: tokens.peekable(), |
| 80 | cursor: Pos::new(1, 0, 0), | 80 | last_token_end: Pos::new(1, 0, 0), |
| 81 | } | 81 | } |
| 82 | } | 82 | } |
| 83 | 83 | ||
| @@ -86,7 +86,9 @@ where | |||
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | fn consume(&mut self) -> Option<Spanned<Token<'a>>> { | 88 | fn consume(&mut self) -> Option<Spanned<Token<'a>>> { |
| 89 | self.tokens.next().inspect(|s| self.cursor = s.span.end) | 89 | self.tokens |
| 90 | .next() | ||
| 91 | .inspect(|s| self.last_token_end = s.span.end) | ||
| 90 | } | 92 | } |
| 91 | 93 | ||
| 92 | fn parse_expr(&mut self) -> Result<Spanned<Expr>> { | 94 | fn parse_expr(&mut self) -> Result<Spanned<Expr>> { |
| @@ -105,20 +107,20 @@ where | |||
| 105 | Expr::Atom(Atom::Nil) | 107 | Expr::Atom(Atom::Nil) |
| 106 | }; | 108 | }; |
| 107 | 109 | ||
| 108 | Spanned::new(expr, Span::new(span.start, self.cursor)) | 110 | Spanned::new(expr, Span::new(span.start, self.last_token_end)) |
| 109 | } | 111 | } |
| 110 | Token::RightPar => todo!("unexpected par"), | 112 | Token::RightPar => todo!("unexpected par"), |
| 111 | Token::Quote => { | 113 | Token::Quote => { |
| 112 | self.consume(); | 114 | self.consume(); |
| 113 | let quote = Spanned::new( | 115 | let quote = Spanned::new( |
| 114 | Expr::Atom(Atom::Symbol("quote".into())), | 116 | Expr::Atom(Atom::Symbol("quote".into())), |
| 115 | Span::new(span.start, self.cursor), | 117 | Span::new(span.start, self.last_token_end), |
| 116 | ); | 118 | ); |
| 117 | let expr = self.parse_expr()?; | 119 | let expr = self.parse_expr()?; |
| 118 | 120 | ||
| 119 | Spanned::new( | 121 | Spanned::new( |
| 120 | Expr::List(vec![quote, expr]), | 122 | Expr::List(vec![quote, expr]), |
| 121 | Span::new(span.start, self.cursor), | 123 | Span::new(span.start, self.last_token_end), |
| 122 | ) | 124 | ) |
| 123 | } | 125 | } |
| 124 | Token::Number(number) => { | 126 | Token::Number(number) => { |
| @@ -126,7 +128,7 @@ where | |||
| 126 | 128 | ||
| 127 | Spanned::new( | 129 | Spanned::new( |
| 128 | Expr::Atom(parse_number(number)), | 130 | Expr::Atom(parse_number(number)), |
| 129 | Span::new(span.start, self.cursor), | 131 | Span::new(span.start, self.last_token_end), |
| 130 | ) | 132 | ) |
| 131 | } | 133 | } |
| 132 | Token::String(string) => { | 134 | Token::String(string) => { |
| @@ -134,7 +136,7 @@ where | |||
| 134 | 136 | ||
| 135 | Spanned::new( | 137 | Spanned::new( |
| 136 | Expr::Atom(parse_string(string)), | 138 | Expr::Atom(parse_string(string)), |
| 137 | Span::new(span.start, self.cursor), | 139 | Span::new(span.start, self.last_token_end), |
| 138 | ) | 140 | ) |
| 139 | } | 141 | } |
| 140 | Token::UnclosedString(string) => { | 142 | Token::UnclosedString(string) => { |
| @@ -146,7 +148,7 @@ where | |||
| 146 | 148 | ||
| 147 | Spanned::new( | 149 | Spanned::new( |
| 148 | Expr::Atom(parse_symbol(symbol)), | 150 | Expr::Atom(parse_symbol(symbol)), |
| 149 | Span::new(span.start, self.cursor), | 151 | Span::new(span.start, self.last_token_end), |
| 150 | ) | 152 | ) |
| 151 | } | 153 | } |
| 152 | }; | 154 | }; |
