-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
179 lines (152 loc) · 5.26 KB
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @file Pyproject TOML Parser
* @author Dan Sully <[email protected]>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
// grammar.js for pyproject-toml
const TOML = require("./tree-sitter-toml/grammar");
const PEP508 = require("./pep508");
const newline = /\r?\n/;
module.exports = grammar(TOML, {
name: "pyproject",
extras: ($) => [$.comment, /\s/],
rules: {
document: ($) => repeat1(choice($.build_system, $.project, $.tool)),
// Import.
...PEP508,
// --- Build System Table ---
build_system: ($) =>
seq(
"[build-system]",
newline,
repeat(choice($._build_system_requires, pair("build-backend", $.string), $._build_system_backend_path)),
),
_build_system_requires: ($) => pair("requires", array_field($._requirement)),
_build_system_backend_path: ($) =>
choice(
// Allow exactly `backend-path = "."`, as used in `flit_core==2.3.0`.
pair("backend-path", "."),
pair("backend-path", array_field($.string)),
),
// --- Project Table ---
project: ($) => alias($._project, "project"),
_project: ($) =>
choice(
seq("[project]", newline, $._project_metadata, repeat($._project_metadata)),
$.dependencies,
$.optional_dependencies,
$.scripts,
$.gui_scripts,
$.entry_points,
$.urls,
),
_project_metadata: ($) =>
choice(
pair("name", $.string),
pair("version", seq($._dquote, $._version, $._dquote)),
pair("description", $.string),
pair("requires-python", $.requires_python),
pair("readme", $._readme),
pair("license", choice(inline_field(seq("text", "=", $.string)), $.string)),
pair("license-files", array_field($._inline_value)),
pair("authors", $._authors),
pair("maintainers", $._maintainers),
pair("keywords", $.keywords),
pair("classifiers", $.classifiers),
pair("dynamic", array_field($.string)),
pair("dependencies", array_field($._requirement)),
pair("optional-dependencies", array_field($._requirement)),
),
requires_python: ($) => seq($._dquote, $._version_many, $._dquote),
classifiers: ($) => array_field(field("classifier", $.string)),
keywords: ($) => array_field(field("keyword", $.string)),
_readme: ($) =>
choice(
$.string,
inline_field(choice(pair("file", $.string), pair("text", $.string), pair("content-type", $.string))),
),
// Authors & Maintainers
_authors: ($) => array_field(alias($._name_email, $.author)),
_maintainers: ($) => array_field(alias($._name_email, $.maintainer)),
_name_email: ($) => inline_field(choice(pair("email", $.string), pair("name", $.string))),
// Dependencies in standalone table form.
dependencies: ($) => seq("[project.dependencies]", newline, repeat1($._array_dependency)),
optional_dependencies: ($) => seq("[project.optional-dependencies]", newline, repeat1($._array_dependency)),
_array_dependency: ($) => seq($._package, "=", array_field($._requirement)),
// URLs section
urls: ($) => seq("[project.urls]", newline, repeat1($.url_entry)),
url_entry: ($) => seq(alias($._maybe_quoted_key, $.url_key), "=", seq($._dquote, $.uri, $._dquote)),
// Scripts section
scripts: ($) => seq("[project.scripts]", newline, repeat1($.script_entry)),
gui_scripts: ($) => seq("[project.gui-scripts]", newline, repeat1($.script_entry)),
// [project.entry-points.console_scripts], etc.
entry_points: ($) =>
seq(
choice("[project.entry-points]", seq("[project.entry-points.", alias($._maybe_quoted_key, $.name), "]")),
newline,
repeat1($.script_entry),
),
script_entry: ($) => seq(field("script_entry", $.bare_key), "=", field("script", $.string)),
// --- Tool Table(s) ---
tool: ($) =>
seq(
alias(
choice(
"[",
"tool",
"]",
seq("[", "tool.", field("name", $._dotted_key), "]"),
seq("[[", "tool.", field("name", $._dotted_key), "]]"),
),
$.string,
),
newline,
repeat($.pair),
),
// Slightly different from tree-sitter-toml's $.dotted_key, in that I want to include the dots.
_dotted_key: ($) => alias(/[A-Za-z0-9_\.-]+/, $.string),
// Quoted keys can have a space in them.
_maybe_quoted_key: ($) => alias(choice(/[A-Za-z0-9_-]+/, seq($._dquote, /[A-Za-z0-9_\. -]+/, $._dquote)), $.string),
_dquote: ($) => alias('"', $.string),
},
});
/**
* @param {RuleOrLiteral} rule
*
* @returns {SeqRule}
*/
function array_field(rule) {
return seq(
"[",
repeat(newline),
optional(
seq(
rule,
repeat(newline),
repeat(seq(",", repeat(newline), rule, repeat(newline))),
optional(seq(",", repeat(newline))),
),
),
"]",
);
}
/**
* @param {string} name
* @param {RuleOrLiteral} rule
*
* @returns {SeqRule}
*/
function pair(name, rule) {
// return seq(name, "=", rule);
return seq(name, "=", field(name.replace(/-/g, "_"), rule));
}
/**
* @param {RuleOrLiteral} rule
*
* @returns {SeqRule}
*/
function inline_field(rule) {
return seq("{", optional(rule), repeat(seq(",", rule)), "}");
}