Skip to content

Commit

Permalink
Feat: Doctype + Multiline content with dot
Browse files Browse the repository at this point in the history
  • Loading branch information
zealot128 committed Jul 13, 2021
1 parent 7679f9a commit 5875f9a
Show file tree
Hide file tree
Showing 9 changed files with 1,718 additions and 1,084 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ currently missing:
- [ ] Control Flow (if, each, else, case, include...)
https://pugjs.org/language/conditionals.html
https://pugjs.org/language/includes.html
- [ ] doctype
https://pugjs.org/language/doctype.html
- [ ] inline Javascript code with "- "
- [ ] output Javascript code with "= "
https://pugjs.org/language/code.html
- [ ] inline Javascript code with "#{...}"
- [ ] Filters
- [ ] Piped Content with a dot on the tag


To add tests, check out `./test/corpus/*.txt`
Expand Down
35 changes: 19 additions & 16 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@ module.exports = grammar({
name: "pug",
externals: ($) => [$._newline, $._indent, $._dedent],
rules: {
source_file: ($) => repeat(choice($.comment, $.tag)),
source_file: ($) => repeat(choice($.comment, $.tag, $.doctype)),
doctype: ($) =>
seq("doctype", alias(choice("html", "strict", "xml"), $.doctype_name)),
pipe_content: ($) =>
seq(
"|",
optional($._content_or_javascript),
$._newline,
),
seq("|", optional($._content_or_javascript), $._newline),
tag: ($) =>
seq(
choice($.tag_name, $.id, $.class),
optional(repeat1(choice($.id, $.class))),
optional($.attributes),
choice(
seq(":", $.tag),
$._content_after_dot,
seq(
optional(seq(" ", $._content_or_javascript)),
$._newline,
optional($.children)
)
)
),
_content_after_dot: ($) =>
seq(
".",
$._newline,
$._indent,
alias(
repeat1(seq($._content_or_javascript, $._newline)),
$.children
),
$._dedent
),
attributes: ($) =>
seq(
"(",
Expand All @@ -33,18 +43,11 @@ module.exports = grammar({
attribute: ($) =>
seq(
$.attribute_name,
optional(repeat1(seq(
".",
alias(/[\w@\-:]+/, $.attribute_modifier)
))),
optional(repeat1(seq(".", alias(/[\w@\-:]+/, $.attribute_modifier)))),
optional(seq("=", $.quoted_attribute_value))
),
children: $ => seq($._indent, repeat1($._children_choice), $._dedent),
_children_choice: ($) =>
choice(
$.pipe_content,
$.tag,
),
children: ($) => seq($._indent, repeat1($._children_choice), $._dedent),
_children_choice: ($) => choice($.pipe_content, $.tag),
comment: ($) =>
seq(
"//",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"nan": "^2.14.1"
},
"devDependencies": {
"prettier": "^2.3.2",
"tree-sitter-cli": "^0.20.0"
}
}
81 changes: 81 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,45 @@
{
"type": "SYMBOL",
"name": "tag"
},
{
"type": "SYMBOL",
"name": "doctype"
}
]
}
},
"doctype": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "doctype"
},
{
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "html"
},
{
"type": "STRING",
"value": "strict"
},
{
"type": "STRING",
"value": "xml"
}
]
},
"named": true,
"value": "doctype_name"
}
]
},
"pipe_content": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -114,6 +149,10 @@
}
]
},
{
"type": "SYMBOL",
"name": "_content_after_dot"
},
{
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -160,6 +199,48 @@
}
]
},
"_content_after_dot": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "_newline"
},
{
"type": "SYMBOL",
"name": "_indent"
},
{
"type": "ALIAS",
"content": {
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_content_or_javascript"
},
{
"type": "SYMBOL",
"name": "_newline"
}
]
}
},
"named": true,
"value": "children"
},
{
"type": "SYMBOL",
"name": "_dedent"
}
]
},
"attributes": {
"type": "SEQ",
"members": [
Expand Down
35 changes: 35 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
"multiple": true,
"required": true,
"types": [
{
"type": "content",
"named": true
},
{
"type": "javascript",
"named": true
},
{
"type": "pipe_content",
"named": true
Expand All @@ -66,6 +74,21 @@
"named": true,
"fields": {}
},
{
"type": "doctype",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "doctype_name",
"named": true
}
]
}
},
{
"type": "pipe_content",
"named": true,
Expand Down Expand Up @@ -112,6 +135,10 @@
"type": "comment",
"named": true
},
{
"type": "doctype",
"named": true
},
{
"type": "tag",
"named": true
Expand Down Expand Up @@ -218,6 +245,14 @@
"type": "content",
"named": true
},
{
"type": "doctype",
"named": false
},
{
"type": "doctype_name",
"named": true
},
{
"type": "id",
"named": true
Expand Down
Loading

0 comments on commit 5875f9a

Please sign in to comment.