Skip to content

Commit

Permalink
fix(parser): issue in function declaration.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 4, 2024
1 parent 98342a7 commit 7a1ea1a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 20 deletions.
4 changes: 0 additions & 4 deletions crates/fuse-parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ impl<'a> Parser<'a> {
self.cur_token().kind
}

pub(crate) fn peek_token(&self) -> Option<&TokenReference> {
self.lexer.peek()
}

pub(crate) fn consume(&mut self) -> TokenReference {
let token = self.lexer.consume();
self.prev_token = token.clone();
Expand Down
6 changes: 0 additions & 6 deletions crates/fuse-parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ impl<'a> Lexer<'a> {
&self.current_token
}

pub fn peek(&self) -> Option<&TokenReference> {
self.lookahead.front().map(|next| &next.token)
}

pub fn lookahead(&mut self, n: u8) -> &TokenReference {
// Cache the new lookahead if it dosn't exists.
self.ensure_lookahead(n);
Expand Down Expand Up @@ -108,14 +104,12 @@ impl<'a> Lexer<'a> {
unsafe { self.source.set_position(lookahead.position) };
}

let mut n = 0;
for _ in self.lookahead.len()..n {
let next = self.next_with_trivia();
self.lookahead.push_back(Lookahead {
position: self.source.position(),
token: next,
});
n += 1;
}

// SAFETY: Position is created at the begining of the function,
Expand Down
21 changes: 11 additions & 10 deletions crates/fuse-parser/src/parsers/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ impl<'a> Parser<'a> {
.parse_expression()
.map(|expr| self.ast.expression_statement(expr)),

TokenKind::Function | TokenKind::Fn => match self.peek_token() {
// function declaration
Some(peek) if peek.kind() == TokenKind::Identifier => self
.parse_function_declaration()
.map(|func| self.ast.function_declaration_statement(func)),
// function expression
_ => self
.parse_function_expression()
.map(|expr| self.ast.expression_statement(expr)),
},
TokenKind::Function | TokenKind::Fn => {
if self.nth_kind(1) == TokenKind::Identifier {
// function declaration
self.parse_function_declaration()
.map(|func| self.ast.function_declaration_statement(func))
} else {
// function expression
self.parse_function_expression()
.map(|expr| self.ast.expression_statement(expr))
}
}

kind if kind.is_trivial() => {
unreachable!("All trivial tokens should be eaten by a `TokenReference`.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: crates/fuse-parser/tests/cases/mod.rs
expression: parsed.chunk
input_file: crates/fuse-parser/tests/cases/pass/function-declaration-01/case.fuse
---
Some(Chunk(
span: Span(
start: 0,
end: 20,
),
body: Block(
statements: [
FunctionDeclaration(Function(
span: Span(
start: 0,
end: 15,
),
identifier: Some(Identifier(
span: Span(
start: 9,
end: 13,
),
name: Atom("test"),
)),
params: FunctionParameters(
span: Span(
start: 13,
end: 15,
),
items: [],
rest: None,
),
return_type: None,
body: Block(Block(
statements: [],
)),
)),
],
),
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function test()
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
source: crates/fuse-parser/tests/cases/mod.rs
expression: tokens
input_file: crates/fuse-parser/tests/cases/pass/function-declaration-01/case.fuse
---
[
TokenReference(
token: Token(
span: Span(
start: 0,
end: 8,
),
kind: Function,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 8,
end: 9,
),
kind: Whitespace,
),
],
),
TokenReference(
token: Token(
span: Span(
start: 9,
end: 13,
),
kind: Identifier,
),
leading_trivia: [],
trailing_trivia: [],
),
TokenReference(
token: Token(
span: Span(
start: 13,
end: 14,
),
kind: LParen,
),
leading_trivia: [],
trailing_trivia: [],
),
TokenReference(
token: Token(
span: Span(
start: 14,
end: 15,
),
kind: RParen,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 15,
end: 16,
),
kind: Whitespace,
),
],
),
TokenReference(
token: Token(
span: Span(
start: 16,
end: 19,
),
kind: End,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 19,
end: 20,
),
kind: Whitespace,
),
],
),
]

0 comments on commit 7a1ea1a

Please sign in to comment.