Skip to content

Commit

Permalink
Feature: Add quoted_attribute_value node
Browse files Browse the repository at this point in the history
* Add quoted_attribute_value node for better language injections

* Mention new HEEx grammer in README

* Update versioning to follow semver, add changelog
  • Loading branch information
connorlay authored Aug 15, 2021
1 parent 54082c4 commit 21b7676
Show file tree
Hide file tree
Showing 10 changed files with 542 additions and 476 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

# 0.2.0

- Add `quoted_attribute_value` node to better support language injections, such as injecting JavaScript into AlpineJS directives

# 0.1.0

- Initial release
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

Supports the Surface 0.5+ template syntax.

For HEEx support, see [tree-sitter-heex](https://github.com/connorlay/tree-sitter-heex).

## Features

Parsing support for the following nodes:
Expand Down
11 changes: 7 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ module.exports = grammar({
seq(
'=',
choice(
$.quoted_attribute_value,
$.attribute_value,
$.expression
)
Expand All @@ -242,24 +243,26 @@ module.exports = grammar({
'=',
choice(
$.attribute_value,
$.quoted_attribute_value,
$.expression
)
),

attribute_value: $ => choice(
/[^<>{}"'=\s]+/,
quoted_attribute_value: $ => choice(
seq(
"'",
optional(/[^']+/),
optional(alias(/[^']+/, $.attribute_value)),
"'"
),
seq(
'"',
optional(/[^"]+/),
optional(alias(/[^"]+/, $.attribute_value)),
'"'
)
),

attribute_value: $ => /[^<>{}"'=\s]+/,

tag_name: $ => /[a-z]+[^\-<>{}!"'/=\s]*/,

component_name: $ => /[A-Z#:]+[^\-<>{}!"'/=\s]*/,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "tree-sitter-surface",
"version": "1.0.0",
"version": "0.2.0",
"description": "Tree-sitter grammar for Surface files (sface)",
"main": "bindings/node",
"scripts": {
"test": "npx tree-sitter generate && npx tree-sitter test",
"update_test": "npx tree-sitter generate && npx tree-sitter test -u",
"playground": "npx tree-sitter build-wasm && npx tree-sitter playground"
},
"author": "Clay",
Expand Down
36 changes: 27 additions & 9 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,10 @@
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "quoted_attribute_value"
},
{
"type": "SYMBOL",
"name": "attribute_value"
Expand Down Expand Up @@ -719,6 +723,10 @@
"type": "SYMBOL",
"name": "attribute_value"
},
{
"type": "SYMBOL",
"name": "quoted_attribute_value"
},
{
"type": "SYMBOL",
"name": "expression"
Expand All @@ -727,13 +735,9 @@
}
]
},
"attribute_value": {
"quoted_attribute_value": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^<>{}\"'=\\s]+"
},
{
"type": "SEQ",
"members": [
Expand All @@ -745,8 +749,13 @@
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^']+"
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[^']+"
},
"named": true,
"value": "attribute_value"
},
{
"type": "BLANK"
Expand All @@ -770,8 +779,13 @@
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^\"]+"
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[^\"]+"
},
"named": true,
"value": "attribute_value"
},
{
"type": "BLANK"
Expand All @@ -786,6 +800,10 @@
}
]
},
"attribute_value": {
"type": "PATTERN",
"value": "[^<>{}\"'=\\s]+"
},
"tag_name": {
"type": "PATTERN",
"value": "[a-z]+[^\\-<>{}!\"'/=\\s]*"
Expand Down
32 changes: 27 additions & 5 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
{
"type": "expression",
"named": true
},
{
"type": "quoted_attribute_value",
"named": true
}
]
}
},
{
"type": "attribute_value",
"named": true,
"fields": {}
},
{
"type": "block",
"named": true,
Expand Down Expand Up @@ -158,6 +157,10 @@
{
"type": "expression",
"named": true
},
{
"type": "quoted_attribute_value",
"named": true
}
]
}
Expand Down Expand Up @@ -282,6 +285,21 @@
]
}
},
{
"type": "quoted_attribute_value",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "attribute_value",
"named": true
}
]
}
},
{
"type": "self_closing_component",
"named": true,
Expand Down Expand Up @@ -567,6 +585,10 @@
"type": "attribute_name",
"named": true
},
{
"type": "attribute_value",
"named": true
},
{
"type": "case",
"named": false
Expand Down
Loading

0 comments on commit 21b7676

Please sign in to comment.