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

Highlight echo #103

Merged
merged 3 commits into from
Oct 30, 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
12 changes: 11 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = grammar({
[$.source_file],
[$._constant_value, $._case_clause_guard_unit],
[$.integer],
[$.echo],
],
rules: {
/* General rules */
Expand Down Expand Up @@ -298,7 +299,13 @@ module.exports = grammar({
binaryExpr(prec.left, 4, ">=", $._expression),
binaryExpr(prec.left, 4, ">.", $._expression),
binaryExpr(prec.left, 4, ">=.", $._expression),
binaryExpr(prec.left, 5, "|>", $._expression),
binaryExpr(
prec.left,
5,
"|>",
$._expression,
choice($.pipeline_echo, $._expression)
),
binaryExpr(prec.left, 6, "+", $._expression),
binaryExpr(prec.left, 6, "+.", $._expression),
binaryExpr(prec.left, 6, "-", $._expression),
Expand Down Expand Up @@ -330,6 +337,7 @@ module.exports = grammar({
$.todo,
$.panic,
$.tuple,
$.echo,
$.list,
alias($._expression_bit_string, $.bit_string),
$.anonymous_function,
Expand Down Expand Up @@ -369,6 +377,8 @@ module.exports = grammar({
)
)
),
pipeline_echo: (_$) => prec.left("echo"),
echo: ($) => seq("echo", $._expression),
tuple: ($) => seq("#", "(", optional(series_of($._expression, ",")), ")"),
list: ($) =>
seq(
Expand Down
3 changes: 2 additions & 1 deletion queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
; TODO: when tree-sitter supports `#any-of?` in the Rust bindings,
; refactor this to use `#any-of?` rather than `#match?`
((identifier) @error
(#match? @error "^(auto|delegate|derive|else|implement|macro|test|echo)$"))
(#match? @error "^(auto|delegate|derive|else|implement|macro|test)$"))

; Variables
(identifier) @variable
Expand All @@ -87,6 +87,7 @@
"assert"
"case"
"const"
"echo"
; DEPRECATED: 'external' was removed in v0.30.
"external"
"fn"
Expand Down
112 changes: 112 additions & 0 deletions test/corpus/echo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
================================================================================
Echo with expression
================================================================================

pub fn main() {
echo 1
}

--------------------------------------------------------------------------------

(source_file
(function
(visibility_modifier)
(identifier)
(function_parameters)
(function_body
(echo
(integer)))))

================================================================================
Echo in pipeline
================================================================================

pub fn main() {
[]
|> echo
|> panic
}

--------------------------------------------------------------------------------

(source_file
(function
(visibility_modifier)
(identifier)
(function_parameters)
(function_body
(binary_expression
(binary_expression
(list)
(pipeline_echo))
(panic)))))

================================================================================
Echo last in pipeline
================================================================================

pub fn main() {
[]
|> echo

1
}

--------------------------------------------------------------------------------

(source_file
(function
(visibility_modifier)
(identifier)
(function_parameters)
(function_body
(binary_expression
(list)
(pipeline_echo))
(integer))))

================================================================================
Echo precedence with pipes
================================================================================

pub fn main() {
echo 1 |> 2
3
}

--------------------------------------------------------------------------------

(source_file
(function
(visibility_modifier)
(identifier)
(function_parameters)
(function_body
(echo
(binary_expression
(integer)
(integer)))
(integer))))

================================================================================
Echo precedence with binop
================================================================================

pub fn main() {
echo 1 + 2
3
}

--------------------------------------------------------------------------------

(source_file
(function
(visibility_modifier)
(identifier)
(function_parameters)
(function_body
(echo
(binary_expression
(integer)
(integer)))
(integer))))
2 changes: 0 additions & 2 deletions test/highlight/reserved.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ macro
// <- error
test
// <- error
echo
// <- error
Loading