Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new tags and support for optional types #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions corpus/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,59 @@ Block tags with no description
(description)
(tag (tag_name))
(tag (tag_name) (description)))

===============================
@typedef tag
================================

/**
* A number, or a string containing a number.
* @typedef {(number|string)} NumberLike
* @typedef {Object[]} NumberLike
* @typedef NumberLike
*/

---

(document
(description)
(tag (tag_name) (type) (identifier))
(tag (tag_name) (type) (identifier))
(tag (tag_name) (identifier))
)

===============================
Various possible denotions of types
================================

/**
* Some description.
* @type {boolean}
* @type {Object[]}
* @type {Array.<MyClass>}
* @type {Object.<string, number>}
* @type {?number}
* @type {!number}
* @param {...number} num
* @param {number} [foo]
* @param {number=} foo - a description of the parameter
* @param {number} [foo=1] - a description of the parameter
* @param {number} [foo=[1, 2]] - a description of the parameter
*/

---

(document
(description)
(tag (tag_name) (type))
(tag (tag_name) (type))
(tag (tag_name) (type))
(tag (tag_name) (type))
(tag (tag_name) (type))
(tag (tag_name) (type))
(tag (tag_name) (type) (identifier))
(tag (tag_name) (type) (identifier))
(tag (tag_name) (type) (identifier) (description))
(tag (tag_name) (type) (identifier) (atomic_value) (description))
(tag (tag_name) (type) (identifier) (atomic_value) (atomic_value) (description))
)
41 changes: 40 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ module.exports = grammar({
'@external',
'@fires',
'@function',
'@member',
'@mixes',
'@name',
'@namespace',
'@param',
'@property'
'@property',
'@typedef',
'@type'
)),

tag_name_with_type: $ => token(choice(
Expand All @@ -82,6 +85,7 @@ module.exports = grammar({
tag_name: $ => /@[a-zA-Z_]+/,

_expression: $ => choice(
$._identifier_of_optional_type,
$.identifier,
$.member_expression,
$.path_expression,
Expand Down Expand Up @@ -115,6 +119,33 @@ module.exports = grammar({

identifier: $ => /[a-zA-Z_$][a-zA-Z_$0-9]*/,

_identifier_of_optional_type: $ =>
seq(
"[",
$.identifier,
optional(
seq(
"=",
$._value
)
),
"]"
),

_list_of_values: $ =>
seq(
"[",
commaSep1($._value),
"]"
),

_value: $ => choice(
$.atomic_value,
$._list_of_values
),

atomic_value: $ => prec.left(/[^,\[\]\n]+/),

type: $ => /[^}\n]+/,

_text: $ => token(prec(-1, /[^*{}@\s][^*{}\n]*([^*/{}\n][^*{}\n]*\*+)*/)),
Expand All @@ -124,3 +155,11 @@ module.exports = grammar({
_end: $ => '/'
}
});

function commaSep1 (rule) {
return sep1(rule, ',')
}

function sep1 (rule, separator) {
return seq(rule, repeat(seq(separator, rule)))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-sitter-jsdoc",
"version": "0.16.0",
"version": "0.16.1",
"description": "JSDoc grammar for tree-sitter",
"main": "index.js",
"keywords": [
Expand Down
113 changes: 113 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@
"type": "STRING",
"value": "@function"
},
{
"type": "STRING",
"value": "@member"
},
{
"type": "STRING",
"value": "@mixes"
Expand All @@ -291,6 +295,14 @@
{
"type": "STRING",
"value": "@property"
},
{
"type": "STRING",
"value": "@typedef"
},
{
"type": "STRING",
"value": "@type"
}
]
}
Expand Down Expand Up @@ -326,6 +338,10 @@
"_expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_identifier_of_optional_type"
},
{
"type": "SYMBOL",
"name": "identifier"
Expand Down Expand Up @@ -432,6 +448,103 @@
"type": "PATTERN",
"value": "[a-zA-Z_$][a-zA-Z_$0-9]*"
},
"_identifier_of_optional_type": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "_value"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "]"
}
]
},
"_list_of_values": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_value"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_value"
}
]
}
}
]
},
{
"type": "STRING",
"value": "]"
}
]
},
"_value": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "atomic_value"
},
{
"type": "SYMBOL",
"name": "_list_of_values"
}
]
},
"atomic_value": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "PATTERN",
"value": "[^,\\[\\]\\n]+"
}
},
"type": {
"type": "PATTERN",
"value": "[^}\\n]+"
Expand Down
33 changes: 33 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"type": "atomic_value",
"named": true,
"fields": {}
},
{
"type": "description",
"named": true,
Expand Down Expand Up @@ -60,6 +65,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "atomic_value",
"named": true
},
{
"type": "identifier",
"named": true
Expand Down Expand Up @@ -102,6 +111,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "atomic_value",
"named": true
},
{
"type": "identifier",
"named": true
Expand Down Expand Up @@ -129,6 +142,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "atomic_value",
"named": true
},
{
"type": "description",
"named": true
Expand Down Expand Up @@ -164,6 +181,10 @@
"type": "#",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": ".",
"named": false
Expand All @@ -176,6 +197,18 @@
"type": ":",
"named": false
},
{
"type": "=",
"named": false
},
{
"type": "[",
"named": false
},
{
"type": "]",
"named": false
},
{
"type": "identifier",
"named": true
Expand Down
Loading