Skip to content

Commit

Permalink
grammar improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wrapperup committed Mar 7, 2024
1 parent 74f21ab commit 1ba0962
Show file tree
Hide file tree
Showing 5 changed files with 613 additions and 203 deletions.
7 changes: 5 additions & 2 deletions corpus/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ Comments
(comment))
(content)
(tag
(keyword)
(code))
(content)
(tag
Expand Down Expand Up @@ -154,7 +153,6 @@ Expressions
(code))
(content)
(tag
(keyword)
(code))
(content)
(tag
Expand Down Expand Up @@ -197,6 +195,8 @@ Expressions 2
{{-> javascriptCode() }}
{{> javascriptCode() -}}

{{ JSON.parse({ hello: "world" }) }}

---

(template
Expand All @@ -220,4 +220,7 @@ Expressions 2
(tag
(keyword)
(code))
(content)
(tag
(code))
(content))
13 changes: 8 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module.exports = grammar({
name: 'vento',
externals: $ => [
$.code,
$._code,
],
extras: $ => [/\s/],
rules: {
template: $ => repeat(choice(
$.tag,
Expand All @@ -20,24 +21,26 @@ module.exports = grammar({

_expression: $ => choice(
// "Solo keywords" are just code blocks
alias($.keyword2, $.code),
alias($.keyword, $.code),
alias($.close_keyword, $.keyword),
seq(
$.keyword,
$.code
alias($._code, $.code),
),
$.comment,
),

// General rule for keyword tags
// It just tries to match the first word in a tag block,
// plus any other special characters that might be present
keyword: $ => /[a-zA-Z>]+/,
close_keyword: $ => /\/[a-zA-Z]+/,
keyword: $ => /[a-z>][a-zA-Z]*? |if|for|include|set|import|export|layout|function/,
keyword2: $ => seq(/[a-zA-Z>\.\(\)\!_\?]/, $._code),
close_keyword: $ => /\/([a-zA-Z]+|if|for|include|set|import|export|layout|function)/,

filter: $ => repeat1(seq(
"|>",
$.code
alias($._code, $.code)
)),

comment: $ => /#[^#]+#/,
Expand Down
46 changes: 39 additions & 7 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
"_expression": {
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "keyword2"
},
"named": true,
"value": "code"
},
{
"type": "ALIAS",
"content": {
Expand All @@ -109,8 +118,13 @@
"name": "keyword"
},
{
"type": "SYMBOL",
"name": "code"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_code"
},
"named": true,
"value": "code"
}
]
},
Expand All @@ -122,11 +136,24 @@
},
"keyword": {
"type": "PATTERN",
"value": "[a-zA-Z>]+"
"value": "[a-z>][a-zA-Z]*? |if|for|include|set|import|export|layout|function"
},
"keyword2": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[a-zA-Z>\\.\\(\\)\\!_\\?]"
},
{
"type": "SYMBOL",
"name": "_code"
}
]
},
"close_keyword": {
"type": "PATTERN",
"value": "\\/[a-zA-Z]+"
"value": "\\/([a-zA-Z]+|if|for|include|set|import|export|layout|function)"
},
"filter": {
"type": "REPEAT1",
Expand All @@ -138,8 +165,13 @@
"value": "|>"
},
{
"type": "SYMBOL",
"name": "code"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_code"
},
"named": true,
"value": "code"
}
]
}
Expand All @@ -160,7 +192,7 @@
"externals": [
{
"type": "SYMBOL",
"name": "code"
"name": "_code"
}
],
"inline": [],
Expand Down
9 changes: 5 additions & 4 deletions src/node-types.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"type": "code",
"named": true,
"fields": {}
},
{
"type": "content",
"named": true,
Expand Down Expand Up @@ -69,10 +74,6 @@
"type": "-}}",
"named": false
},
{
"type": "code",
"named": true
},
{
"type": "comment",
"named": true
Expand Down
Loading

0 comments on commit 1ba0962

Please sign in to comment.