Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect unary precedence #297

Merged
merged 10 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Punctuated<T>` now implements `Default` for all `T`, rather than if `T: Default`.

### Removed
- Removed `UnOp::precedence`, as unary operators do not traditionally use precedence in the same way binary operators do.
- Removed `TokenizerErrorType::UnexpectedShebang`.
- Removed `stacker` feature flag, as rewrites to the parser should make it unnecessary.

Expand Down
9 changes: 8 additions & 1 deletion full-moon/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,8 @@ macro_rules! make_bin_op {
}

impl BinOp {
/// The precedence of the operator, from a scale of 1 to 10. The larger the number, the higher the precedence.
/// The precedence of non-unary operator. The larger the number, the higher the precedence.
/// Shares the same precedence table as unary operators.
pub fn precedence_of_token(token: &TokenReference) -> Option<u8> {
match token.token_type() {
TokenType::Symbol { symbol } => match symbol {
Expand Down Expand Up @@ -2203,6 +2204,12 @@ impl UnOp {
UnOp::Tilde(token) => token,
}
}

/// The precedence of unary operator. The larger the number, the higher the precedence.
/// Shares the same precedence table as binary operators.
pub fn precedence() -> u8 {
11
}
}

/// An error that occurs when creating the AST.
Expand Down
21 changes: 20 additions & 1 deletion full-moon/src/ast/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,26 @@ fn parse_unary_expression(
_ => unreachable!(),
};

let expression = match parse_expression(state) {
let primary_expression = match parse_primary_expression(state) {
ParserResult::Value(expression) => expression,
ParserResult::NotFound => {
state.token_error(
unary_operator.token().clone(),
format!(
"expected an expression after {}",
unary_operator.token().token()
),
);
return ParserResult::NotFound;
}
ParserResult::LexerMoved => return ParserResult::LexerMoved,
};

let expression = match parse_expression_with_precedence(
state,
primary_expression,
ast::UnOp::precedence(),
) {
ParserResult::Value(expression) => expression,
ParserResult::LexerMoved => return ParserResult::LexerMoved,
ParserResult::NotFound => {
Expand Down
155 changes: 153 additions & 2 deletions full-moon/tests/cases/pass/negative-numbers/ast.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: full-moon/tests/pass_cases.rs
assertion_line: 52
expression: ast.nodes()
input_file: full-moon/tests/cases/pass/negative-numbers
---
Expand Down Expand Up @@ -441,4 +440,156 @@ stmts:
text: "3"
trailing_trivia: []
- ~

- - LocalAssignment:
local_token:
leading_trivia: []
token:
start_position:
bytes: 45
line: 4
character: 1
end_position:
bytes: 50
line: 4
character: 6
token_type:
type: Symbol
symbol: local
trailing_trivia:
- start_position:
bytes: 50
line: 4
character: 6
end_position:
bytes: 51
line: 4
character: 7
token_type:
type: Whitespace
characters: " "
name_list:
pairs:
- End:
leading_trivia: []
token:
start_position:
bytes: 51
line: 4
character: 7
end_position:
bytes: 54
line: 4
character: 10
token_type:
type: Identifier
identifier: foo
trailing_trivia:
- start_position:
bytes: 54
line: 4
character: 10
end_position:
bytes: 55
line: 4
character: 11
token_type:
type: Whitespace
characters: " "
equal_token:
leading_trivia: []
token:
start_position:
bytes: 55
line: 4
character: 11
end_position:
bytes: 56
line: 4
character: 12
token_type:
type: Symbol
symbol: "="
trailing_trivia:
- start_position:
bytes: 56
line: 4
character: 12
end_position:
bytes: 57
line: 4
character: 13
token_type:
type: Whitespace
characters: " "
expr_list:
pairs:
- End:
BinaryOperator:
lhs:
UnaryOperator:
unop:
Minus:
leading_trivia: []
token:
start_position:
bytes: 57
line: 4
character: 13
end_position:
bytes: 58
line: 4
character: 14
token_type:
type: Symbol
symbol: "-"
trailing_trivia: []
expression:
Var:
Name:
leading_trivia: []
token:
start_position:
bytes: 58
line: 4
character: 14
end_position:
bytes: 59
line: 4
character: 15
token_type:
type: Identifier
identifier: x
trailing_trivia: []
binop:
Plus:
leading_trivia: []
token:
start_position:
bytes: 59
line: 4
character: 15
end_position:
bytes: 60
line: 4
character: 16
token_type:
type: Symbol
symbol: +
trailing_trivia: []
rhs:
Number:
leading_trivia: []
token:
start_position:
bytes: 60
line: 4
character: 16
end_position:
bytes: 61
line: 4
character: 17
token_type:
type: Number
text: "1"
trailing_trivia: []
- ~
1 change: 1 addition & 0 deletions full-moon/tests/cases/pass/negative-numbers/source.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local foo = x-1
local foo = x -1
print(1+-3)
local foo = -x+1
116 changes: 112 additions & 4 deletions full-moon/tests/cases/pass/negative-numbers/tokens.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: full-moon/tests/pass_cases.rs
expression: tokens
input_file: full-moon/tests/cases/pass/negative-numbers

---
- start_position:
bytes: 0
Expand Down Expand Up @@ -328,9 +327,118 @@ input_file: full-moon/tests/cases/pass/negative-numbers
line: 4
character: 1
end_position:
bytes: 45
bytes: 50
line: 4
character: 1
character: 6
token_type:
type: Symbol
symbol: local
- start_position:
bytes: 50
line: 4
character: 6
end_position:
bytes: 51
line: 4
character: 7
token_type:
type: Whitespace
characters: " "
- start_position:
bytes: 51
line: 4
character: 7
end_position:
bytes: 54
line: 4
character: 10
token_type:
type: Identifier
identifier: foo
- start_position:
bytes: 54
line: 4
character: 10
end_position:
bytes: 55
line: 4
character: 11
token_type:
type: Whitespace
characters: " "
- start_position:
bytes: 55
line: 4
character: 11
end_position:
bytes: 56
line: 4
character: 12
token_type:
type: Symbol
symbol: "="
- start_position:
bytes: 56
line: 4
character: 12
end_position:
bytes: 57
line: 4
character: 13
token_type:
type: Whitespace
characters: " "
- start_position:
bytes: 57
line: 4
character: 13
end_position:
bytes: 58
line: 4
character: 14
token_type:
type: Symbol
symbol: "-"
- start_position:
bytes: 58
line: 4
character: 14
end_position:
bytes: 59
line: 4
character: 15
token_type:
type: Identifier
identifier: x
- start_position:
bytes: 59
line: 4
character: 15
end_position:
bytes: 60
line: 4
character: 16
token_type:
type: Symbol
symbol: +
- start_position:
bytes: 60
line: 4
character: 16
end_position:
bytes: 61
line: 4
character: 17
token_type:
type: Number
text: "1"
- start_position:
bytes: 61
line: 4
character: 17
end_position:
bytes: 61
line: 4
character: 17
token_type:
type: Eof

Loading
Loading