Skip to content

Commit

Permalink
Allow attributes not separated by space
Browse files Browse the repository at this point in the history
  • Loading branch information
kolen committed Feb 16, 2024
1 parent 4a8180f commit e20d092
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 179 deletions.
15 changes: 5 additions & 10 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ const make_attr_delimited = (token_suffix) => {
const make_attrs_delimited = (delim_open, delim_close, token_suffix) => {
return ($) => seq(
delim_open,
optional($._space_or_newline),
optional(
seq(
alias($[`_attr_delimited_${token_suffix}`], $.attr),
repeat(seq($._space_or_newline, alias($[`_attr_delimited_${token_suffix}`], $.attr))),
)
),
repeat(seq(
optional($._space_or_newline), alias($[`_attr_delimited_${token_suffix}`], $.attr)
)),
optional($._space_or_newline),
delim_close
)
Expand Down Expand Up @@ -134,7 +130,7 @@ module.exports = grammar({
$._attrs_plain,
$._attrs_delimited
),
_attrs_plain: $ => repeat1($._attr_with_space),
_attrs_plain: $ => seq($._space, repeat1(seq(optional($._space), $.attr))),
_attrs_delimited: $ => choice(
$._attrs_delimited_p,
$._attrs_delimited_s,
Expand All @@ -152,7 +148,6 @@ module.exports = grammar({
field('assignment', choice($.attr_assignment, $.attr_assignment_noescape)),
field('value', $.attr_value),
),
_attr_with_space: $ => seq($._space, $.attr),
attr_name: $ => $._attr_name,
_attr_name: $ => /[a-zA-Z0-9_-]+/, // TODO: very wrong
attr_assignment: $ => /[ \t]*=[ \t]*/,
Expand Down Expand Up @@ -305,7 +300,7 @@ module.exports = grammar({
},

conflicts: $ => [
[$.attrs],
[$._attrs_plain],
],

extras: $ => []
Expand Down
273 changes: 104 additions & 169 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,37 @@
]
},
"_attrs_plain": {
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_attr_with_space"
}
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_space"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "attr"
}
]
}
}
]
},
"_attrs_delimited": {
"type": "CHOICE",
Expand All @@ -413,59 +439,33 @@
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_attr_delimited_p"
"name": "_space_or_newline"
},
"named": true,
"value": "attr"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_p"
},
"named": true,
"value": "attr"
}
]
{
"type": "BLANK"
}
}
]
},
{
"type": "BLANK"
}
]
]
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_p"
},
"named": true,
"value": "attr"
}
]
}
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -493,59 +493,33 @@
"value": "["
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_attr_delimited_s"
"name": "_space_or_newline"
},
"named": true,
"value": "attr"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_s"
},
"named": true,
"value": "attr"
}
]
{
"type": "BLANK"
}
}
]
},
{
"type": "BLANK"
}
]
]
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_s"
},
"named": true,
"value": "attr"
}
]
}
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -573,59 +547,33 @@
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_attr_delimited_b"
"name": "_space_or_newline"
},
"named": true,
"value": "attr"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_space_or_newline"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_b"
},
"named": true,
"value": "attr"
}
]
{
"type": "BLANK"
}
}
]
},
{
"type": "BLANK"
}
]
]
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attr_delimited_b"
},
"named": true,
"value": "attr"
}
]
}
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -854,19 +802,6 @@
}
]
},
"_attr_with_space": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_space"
},
{
"type": "SYMBOL",
"name": "attr"
}
]
},
"attr_name": {
"type": "SYMBOL",
"name": "_attr_name"
Expand Down Expand Up @@ -1594,7 +1529,7 @@
"extras": [],
"conflicts": [
[
"attrs"
"_attrs_plain"
]
],
"precedences": [],
Expand Down
21 changes: 21 additions & 0 deletions test/corpus/attributes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ span title="example example

---

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

=================================
String attributes - without space
=================================

a href="/"title="root"

-----

(source_file
(element
(tag_name)
Expand Down

0 comments on commit e20d092

Please sign in to comment.