Skip to content

Commit

Permalink
Add ruby_expr token for ruby attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kolen committed Feb 23, 2024
1 parent 311f9d2 commit c92181f
Show file tree
Hide file tree
Showing 7 changed files with 2,535 additions and 2,310 deletions.
29 changes: 23 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
// See: https://github.com/slim-template/slim/blob/master/lib/slim/parser.rb
// Also: https://rdoc.info/gems/slim/frames

const make_attr_delimited_value = (token_suffix) => {
return ($) => choice(
$._attr_value_quoted,
alias($[`_attr_value_ruby_${token_suffix}`], $.ruby_expr)
);
}

const make_attr_delimited_splat = (token_suffix) => {
return ($) => seq(
'*',
alias($[`_attr_value_ruby_${token_suffix}`], $.ruby_expr)
)
}

const make_attr_delimited = (token_suffix) => {
return ($) => choice(
seq('*', alias($[`_attr_value_ruby_${token_suffix}`], $.attr_splat)),
alias($[`_attr_delimited_splat_${token_suffix}`], $.attr_splat),
$.attr_boolean,
seq(
field('name', $.attr_name),
field('assignment', choice($.attr_assignment, $.attr_assignment_noescape)),
field('value', choice(
alias($._attr_value_quoted, $.attr_value),
alias($[`_attr_value_ruby_${token_suffix}`], $.attr_value)
))
field('value', alias($[`_attr_delimited_value_${token_suffix}`], $.attr_value))
)
)
}
Expand Down Expand Up @@ -146,6 +157,12 @@ module.exports = grammar({
_attr_delimited_p: make_attr_delimited('p'),
_attr_delimited_s: make_attr_delimited('s'),
_attr_delimited_b: make_attr_delimited('b'),
_attr_delimited_value_p: make_attr_delimited_value('p'),
_attr_delimited_value_s: make_attr_delimited_value('s'),
_attr_delimited_value_b: make_attr_delimited_value('b'),
_attr_delimited_splat_p: make_attr_delimited_splat('p'),
_attr_delimited_splat_s: make_attr_delimited_splat('s'),
_attr_delimited_splat_b: make_attr_delimited_splat('b'),

attr: $ => choice(
seq('*', alias($._attr_value_ruby, $.attr_splat)),
Expand All @@ -161,7 +178,7 @@ module.exports = grammar({
attr_assignment_noescape: $ => /[ \t]*==[ \t]*/,
attr_value: $ => choice(
$._attr_value_quoted,
$._attr_value_ruby
alias($._attr_value_ruby, $.ruby_expr)
// TODO: many more
),
attr_boolean: $ => $.attr_name,
Expand Down
3 changes: 3 additions & 0 deletions queries/injections.scm
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
((ruby) @injection.content
(#set! injection.language "ruby"))

((ruby_expr) @injection.content
(#set! injection.language "ruby"))
270 changes: 157 additions & 113 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -600,22 +600,13 @@
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "*"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_p"
},
"named": true,
"value": "attr_splat"
}
]
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_splat_p"
},
"named": true,
"value": "attr_splat"
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -653,27 +644,13 @@
"type": "FIELD",
"name": "value",
"content": {
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_quoted"
},
"named": true,
"value": "attr_value"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_p"
},
"named": true,
"value": "attr_value"
}
]
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_value_p"
},
"named": true,
"value": "attr_value"
}
}
]
Expand All @@ -684,22 +661,13 @@
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "*"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_s"
},
"named": true,
"value": "attr_splat"
}
]
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_splat_s"
},
"named": true,
"value": "attr_splat"
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -737,27 +705,13 @@
"type": "FIELD",
"name": "value",
"content": {
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_quoted"
},
"named": true,
"value": "attr_value"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_s"
},
"named": true,
"value": "attr_value"
}
]
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_value_s"
},
"named": true,
"value": "attr_value"
}
}
]
Expand All @@ -768,22 +722,13 @@
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "*"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_b"
},
"named": true,
"value": "attr_splat"
}
]
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_splat_b"
},
"named": true,
"value": "attr_splat"
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -821,33 +766,127 @@
"type": "FIELD",
"name": "value",
"content": {
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_quoted"
},
"named": true,
"value": "attr_value"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_b"
},
"named": true,
"value": "attr_value"
}
]
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_value_b"
},
"named": true,
"value": "attr_value"
}
}
]
}
]
},
"_attr_delimited_value_p": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_attr_value_quoted"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_p"
},
"named": true,
"value": "ruby_expr"
}
]
},
"_attr_delimited_value_s": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_attr_value_quoted"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_s"
},
"named": true,
"value": "ruby_expr"
}
]
},
"_attr_delimited_value_b": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_attr_value_quoted"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_b"
},
"named": true,
"value": "ruby_expr"
}
]
},
"_attr_delimited_splat_p": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "*"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_p"
},
"named": true,
"value": "ruby_expr"
}
]
},
"_attr_delimited_splat_s": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "*"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_s"
},
"named": true,
"value": "ruby_expr"
}
]
},
"_attr_delimited_splat_b": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "*"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby_b"
},
"named": true,
"value": "ruby_expr"
}
]
},
"attr": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -940,8 +979,13 @@
"name": "_attr_value_quoted"
},
{
"type": "SYMBOL",
"name": "_attr_value_ruby"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_value_ruby"
},
"named": true,
"value": "ruby_expr"
}
]
},
Expand Down
Loading

0 comments on commit c92181f

Please sign in to comment.