Skip to content

Commit

Permalink
feat: rewrite parser to make it more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Feb 22, 2022
1 parent b67f8e9 commit 35d9f25
Show file tree
Hide file tree
Showing 7 changed files with 833 additions and 1,043 deletions.
96 changes: 37 additions & 59 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,58 @@ module.exports = grammar({
name: 'norg_meta',

rules: {
metadata: $ => repeat(
choice(
$.pair,
$.delimiter
)
),

pair: $ => seq(
$._key,
token.immediate(/[ \t\v]*/),
choice(
$.value,
$.object,
$.array,
token.immediate('\n'),
),
),
metadata: $ => repeat(choice(
$.pair,
$.delimiter,
)),

key: $ => /[^\s:]+/,

_key: $ => seq(
$.key,
$._separator
),
key: _ => /[^\-\s:][^\s:]*/,

value: $ => seq(
/[^\s\[\]\{\}][^\n\[\]\{\}(\~\n)]*/,
optional(
repeat($._text_with_trailing_modifier)
),
$._text_till_eol
),

object: $ => seq(
"{",
repeat(
$.pair,
),
"}",
seq(
"~\n",
alias($.value, "_value")
)
)
),

array: $ => seq(
"[",
'[',
repeat(
field("item", $.value),
choice(
$.array,
$.value,
$.object,
),
),
"]",
']',
),

delimiter: $ => choice(
$._delimiter,
$.named_delimiter
object: $ => seq(
'{',
repeat($.pair),
'}',
),

_delimiter: $ => /---+\n/,

named_delimiter: $ => prec(3, seq(
/---+\s*/,
prec.left(2,
seq(
field("title", /[^-]+/),
/\s*/
)
pair: $ => seq(
$.key,
token.immediate(':'),
/[\t\v ]*/,
seq(
choice(
$.array,
$.value,
$.object,
'\n',
),
),
$._delimiter,
)),

_separator: $ => ":",

_text_till_eol: $ => /[^(\~\n)]+/,
),

_text_with_trailing_modifier: $ => seq(
$._text_till_eol,
"\~\n"
delimiter: _ => seq(
"-",
/[^\n]*/,
)

}
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
},
"tree-sitter": [
{
"scope": "document.norg",
"scope": "document.norg_meta",
"file-types": [
"norg"
"norg_meta"
]
}
]
Expand Down
204 changes: 77 additions & 127 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,84 +17,75 @@
]
}
},
"pair": {
"key": {
"type": "PATTERN",
"value": "[^\\-\\s:][^\\s:]*"
},
"value": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_key"
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[ \\t\\v]*"
}
"type": "PATTERN",
"value": "[^\\s\\[\\]\\{\\}][^\\n\\[\\]\\{\\}(\\~\\n)]*"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "value"
},
{
"type": "SYMBOL",
"name": "object"
},
{
"type": "SYMBOL",
"name": "array"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "~\n"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "value"
},
"named": false,
"value": "_value"
}
]
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "\n"
}
"type": "BLANK"
}
]
}
]
},
"key": {
"type": "PATTERN",
"value": "[^\\s:]+"
},
"_key": {
"array": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "key"
"type": "STRING",
"value": "["
},
{
"type": "SYMBOL",
"name": "_separator"
}
]
},
"value": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_text_with_trailing_modifier"
"name": "array"
},
{
"type": "SYMBOL",
"name": "value"
},
{
"type": "SYMBOL",
"name": "object"
}
},
{
"type": "BLANK"
}
]
]
}
},
{
"type": "SYMBOL",
"name": "_text_till_eol"
"type": "STRING",
"value": "]"
}
]
},
Expand All @@ -118,103 +109,62 @@
}
]
},
"array": {
"pair": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
"type": "SYMBOL",
"name": "key"
},
{
"type": "REPEAT",
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "FIELD",
"name": "item",
"content": {
"type": "SYMBOL",
"name": "value"
}
"type": "STRING",
"value": ":"
}
},
{
"type": "STRING",
"value": "]"
}
]
},
"delimiter": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_delimiter"
"type": "PATTERN",
"value": "[\\t\\v ]*"
},
{
"type": "SYMBOL",
"name": "named_delimiter"
}
]
},
"_delimiter": {
"type": "PATTERN",
"value": "---+\\n"
},
"named_delimiter": {
"type": "PREC",
"value": 3,
"content": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "---+\\s*"
},
{
"type": "PREC_LEFT",
"value": 2,
"content": {
"type": "SEQ",
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "title",
"content": {
"type": "PATTERN",
"value": "[^-]+"
}
"type": "SYMBOL",
"name": "array"
},
{
"type": "PATTERN",
"value": "\\s*"
"type": "SYMBOL",
"name": "value"
},
{
"type": "SYMBOL",
"name": "object"
},
{
"type": "STRING",
"value": "\n"
}
]
}
},
{
"type": "SYMBOL",
"name": "_delimiter"
}
]
}
},
"_separator": {
"type": "STRING",
"value": ":"
},
"_text_till_eol": {
"type": "PATTERN",
"value": "[^(\\~\\n)]+"
]
}
]
},
"_text_with_trailing_modifier": {
"delimiter": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_text_till_eol"
"type": "STRING",
"value": "-"
},
{
"type": "STRING",
"value": "~\n"
"type": "PATTERN",
"value": "[^\\n]*"
}
]
}
Expand Down
Loading

0 comments on commit 35d9f25

Please sign in to comment.