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

Parse attribute syntax #65

Merged
merged 1 commit into from
Aug 28, 2023
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
24 changes: 22 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ module.exports = grammar({
$.external_function,
$.function,
$.type_definition,
$.type_alias
$.type_alias,
$.attribute
),

/* Comments */
module_comment: ($) => token(seq("////", /.*/)),
statement_comment: ($) => token(seq("///", /.*/)),
comment: ($) => token(seq("//", /.*/)),

/* Target groups */
/* Target groups
* DEPRECATED: This syntax was replaced with attributes in v0.30.
*/
target_group: ($) =>
seq(
"if",
Expand All @@ -54,6 +57,23 @@ module.exports = grammar({
),
target: ($) => choice("erlang", "javascript"),

/* Attributes */
attribute: ($) =>
seq(
"@",
field("name", $.identifier),
field("arguments", alias($._attribute_arguments, $.arguments))
),

_attribute_arguments: ($) =>
seq("(", series_of($.attribute_value, ","), ")"),

attribute_value: ($) =>
choice(
$._constant_value,
seq(field("label", $.label), ":", field("value", $._constant_value))
),

/* Import statements */
import: ($) =>
seq(
Expand Down
7 changes: 7 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
(tuple_access
index: (integer) @property)

; Attributes
(attribute
"@" @attribute
name: (identifier) @attribute)

(attribute_value (identifier) @constant)

; Type names
(remote_type_identifier) @type
(type_identifier) @type
Expand Down
48 changes: 48 additions & 0 deletions test/corpus/attributes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
================================================================================
Target attribute
================================================================================

@target(erlang)
pub fn main() { todo }

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

(source_file
(attribute
name: (identifier)
arguments: (arguments
(attribute_value
(identifier))))
(function
(visibility_modifier)
name: (identifier)
parameters: (function_parameters)
body: (function_body
(todo))))

================================================================================
Attribute with multiple values
================================================================================

@deprecated(since: "1.2.0", replacement: wobble)
pub fn wibble() { todo }

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

(source_file
(attribute
name: (identifier)
arguments: (arguments
(attribute_value
label: (label)
value: (string
(quoted_content)))
(attribute_value
label: (label)
value: (identifier))))
(function
(visibility_modifier)
name: (identifier)
parameters: (function_parameters)
body: (function_body
(todo))))
24 changes: 24 additions & 0 deletions test/highlight/modules.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,27 @@ fn make_cat() -> kitty.Cat {
// ^ module
// ^ constructor
}

@target(erlang)
// <- attribute
// ^ attribute
// ^ constant
pub external fn display() -> Bool = "erlang" "display"

@target(erlang)
@external(erlang, "wobble", "main")
// <- attribute
// ^ attribute
// ^ constant
// ^ string
// string
pub fn main() -> Int

@deprecated(since: "1.2.0", replacement: wobble)
// <- attribute
// ^ attribute
// ^ property
// ^ string
// ^ property
// ^constant
pub fn wibble() { todo }
Loading