Skip to content

Commit

Permalink
Allow newlines around delimited attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kolen committed Feb 16, 2024
1 parent 57d2cc8 commit 5262824
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
6 changes: 4 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ const make_attr_delimited = (token_suffix) => {
const make_attrs_delimited = (delim_open, delim_close, token_suffix) => {
return ($) => seq(
delim_open,
optional($._space),
optional($._space_or_newline),
optional(
seq(
alias($[`_attr_delimited_${token_suffix}`], $.attr),
repeat(seq($._space, alias($[`_attr_delimited_${token_suffix}`], $.attr))),
repeat(seq($._space_or_newline, alias($[`_attr_delimited_${token_suffix}`], $.attr))),
)
),
optional($._space_or_newline),
delim_close
)
};
Expand Down Expand Up @@ -295,6 +296,7 @@ module.exports = grammar({
optional($._text_nested)
),

_space_or_newline: $ => /[ \t\n]+/,
_space: $ => token(prec(-1, /[ \t]+/))
},

Expand Down
52 changes: 46 additions & 6 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_space"
"name": "_space_or_newline"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -431,7 +431,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_space"
"name": "_space_or_newline"
},
{
"type": "ALIAS",
Expand All @@ -452,6 +452,18 @@
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ")"
Expand All @@ -470,7 +482,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_space"
"name": "_space_or_newline"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -499,7 +511,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_space"
"name": "_space_or_newline"
},
{
"type": "ALIAS",
Expand All @@ -520,6 +532,18 @@
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "]"
Expand All @@ -538,7 +562,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_space"
"name": "_space_or_newline"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -567,7 +591,7 @@
"members": [
{
"type": "SYMBOL",
"name": "_space"
"name": "_space_or_newline"
},
{
"type": "ALIAS",
Expand All @@ -588,6 +612,18 @@
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "}"
Expand Down Expand Up @@ -1524,6 +1560,10 @@
}
]
},
"_space_or_newline": {
"type": "PATTERN",
"value": "[ \\t\\n]+"
},
"_space": {
"type": "TOKEN",
"content": {
Expand Down
25 changes: 22 additions & 3 deletions test/corpus/attributes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ span[title=make_title]
(attrs
(attr (attr_name) (attr_assignment) (attr_value)))))

==========================
Multiline quoted attribute
==========================
====================================
String attributes - multiline quoted
====================================

span title="example example
example
Expand All @@ -99,3 +99,22 @@ span title="example example
(attr_name)
(attr_assignment)
(attr_value)))))

=======================================
String attributes - delimited multiline
=======================================

a[
href="/"
]

-----

(source_file
(element
(tag_name)
(attrs
(attr
(attr_name)
(attr_assignment)
(attr_value)))))

0 comments on commit 5262824

Please sign in to comment.