Skip to content

Commit

Permalink
Try another approach
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Dec 29, 2023
1 parent aefcc28 commit f2318ba
Show file tree
Hide file tree
Showing 15 changed files with 647 additions and 790 deletions.
10 changes: 2 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ serve: build
npm run wasm
npm run web

release: build format
release: build
npm run wasm
# GitHub pages doesn't like symbolic links
cp tree-sitter-comment.wasm docs/js/tree-sitter-comment.wasm

format:
clang-format -i \
--style="{BasedOnStyle: webkit, IndentWidth: 2}" \
src/scanner.c \
src/tree_sitter_comment/*

.PHONY: build test serve release format
.PHONY: build test serve release
1 change: 0 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"sources": [
"src/parser.c",
"bindings/node/binding.cc",
"src/scanner.c"
],
"cflags_c": [
"-std=c99",
Expand Down
Binary file modified docs/js/tree-sitter-comment.wasm
Binary file not shown.
33 changes: 20 additions & 13 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,41 @@ const STOP_CHARS = [
module.exports = grammar({
name: "comment",

externals: ($) => [
$.name,
$.invalid_token
conflicts: ($) => [
[$._simple_tag, $._tag_with_annotation],
],

rules: {
source: ($) => repeat(
choice(
$.tag,
$._full_uri,
alias($._text, "text"),
$.uri,
$._text,
),
),

tag: ($) => seq(
$.name,
optional($._user),
":",
tag: ($) => choice(
$._simple_tag,
$._normal_tag,
$._tag_with_annotation,
),

_user: ($) => seq(
_simple_tag: ($) => alias($._tag_name, $.name),

_normal_tag: ($) => seq(
alias($._tag_name, $.name),
token.immediate(":"),
),

_tag_with_annotation: ($) => seq(
alias($._tag_name, $.name),
"(",
alias(/[^()]+/, $.user),
optional(alias(/[^()]+/, $.annotation)),
")",
token.immediate(":"),
),

// This token is split into two parts so the end character isn't included in the URI itself.
_full_uri: ($) => seq($.uri, choice(alias($._end_char, "text"), /\s/)),
_tag_name: ($) => /[A-Z]([A-Z0-9_-]*[A-Z0-9])?/,

// This token needs to be single regex, otherwise a partial match will result in an error.
uri: ($) => get_uri_regex(),
Expand Down
118 changes: 66 additions & 52 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,94 +12,112 @@
},
{
"type": "SYMBOL",
"name": "_full_uri"
"name": "uri"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_text"
},
"named": false,
"value": "text"
"type": "SYMBOL",
"name": "_text"
}
]
}
},
"tag": {
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "name"
"name": "_simple_tag"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_user"
},
{
"type": "BLANK"
}
]
"type": "SYMBOL",
"name": "_normal_tag"
},
{
"type": "STRING",
"value": ":"
"type": "SYMBOL",
"name": "_tag_with_annotation"
}
]
},
"_user": {
"_simple_tag": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_tag_name"
},
"named": true,
"value": "name"
},
"_normal_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[^()]+"
"type": "SYMBOL",
"name": "_tag_name"
},
"named": true,
"value": "user"
"value": "name"
},
{
"type": "STRING",
"value": ")"
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": ":"
}
}
]
},
"_full_uri": {
"_tag_with_annotation": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "uri"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_tag_name"
},
"named": true,
"value": "name"
},
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_end_char"
"type": "PATTERN",
"value": "[^()]+"
},
"named": false,
"value": "text"
"named": true,
"value": "annotation"
},
{
"type": "PATTERN",
"value": "\\s"
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ")"
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": ":"
}
}
]
},
"_tag_name": {
"type": "PATTERN",
"value": "[A-Z]([A-Z0-9_-]*[A-Z0-9])?"
},
"uri": {
"type": "PATTERN",
"value": "https?:\\/\\/([^\\s\\.,:;!\\?\\\\'\"\\}\\]\\)>]|[\\.,:;!\\?\\\\'\"\\}\\]\\)>][a-zA-Z0-9])+"
Expand Down Expand Up @@ -262,18 +280,14 @@
"value": "\\s"
}
],
"conflicts": [],
"precedences": [],
"externals": [
{
"type": "SYMBOL",
"name": "name"
},
{
"type": "SYMBOL",
"name": "invalid_token"
}
"conflicts": [
[
"_simple_tag",
"_tag_with_annotation"
]
],
"precedences": [],
"externals": [],
"inline": [],
"supertypes": []
}
Expand Down
15 changes: 5 additions & 10 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,16 @@
"required": true,
"types": [
{
"type": "name",
"type": "annotation",
"named": true
},
{
"type": "user",
"type": "name",
"named": true
}
]
}
},
{
"type": "text",
"named": false,
"fields": {}
},
{
"type": "!",
"named": false
Expand Down Expand Up @@ -111,15 +106,15 @@
"named": false
},
{
"type": "name",
"type": "annotation",
"named": true
},
{
"type": "uri",
"type": "name",
"named": true
},
{
"type": "user",
"type": "uri",
"named": true
},
{
Expand Down
Loading

0 comments on commit f2318ba

Please sign in to comment.