-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpep508.js
95 lines (72 loc) · 2.84 KB
/
pep508.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
/**
* @file Python's PEP-508 Dependency Specification
* @author Dan Sully
* @license MIT
*/
const URI = require("./uri");
module.exports = {
_requirement: ($) => seq($._dquote, choice($._url_req, $._name_req), $._dquote),
_name_req: ($) => seq($._package, optional($.extras), optional($._version_spec), optional($._quoted_marker)),
_url_req: ($) => seq($._package, optional($.extras), $._urlspec, optional($._quoted_marker)),
_package: ($) => field("package", alias(/[A-Za-z0-9][A-Za-z0-9\-\_\.]*[A-Za-z0-9]*/, $.string)),
_constraint: ($) => field("constraint", alias(choice("<=", "<", "!=", "==", ">=", ">", "~=", "==="), $.string)),
// PEP-440 version as per: https://packaging.python.org/en/latest/specifications/version-specifiers/
_version: ($) =>
field(
"version",
alias(
/v?(?:(?:[0-9]+!)?(?:[0-9]+(?:\.[0-9]+)*)(?:[-_\.]?(?:a|b|c|rc|alpha|beta|pre|preview)[-_\.]?[0-9]*)?(?:(?:-(?:[0-9]+))|(?:[-_\.]?(?:post|rev|r)[-_\.]?[0-9]*)?)?(?:[-_\.]?dev[-_\.]?[0-9]*)?)(?:\+[a-z0-9]+(?:[-_\.][a-z0-9]+)*)?/i,
$.string,
),
),
_version_one: ($) => seq($._constraint, $._version),
_version_many: ($) => seq($._version_one, repeat(seq(",", $._version_one))),
_version_spec: ($) => choice(seq("(", $._version_many, ")"), $._version_many),
...URI,
_urlspec: ($) => seq("@", $.uri),
marker_op: ($) => choice($._constraint, "in", seq("not", "in")),
_escape_sequence: () =>
token(
choice(
"\\\\", // backslash
'\\"', // escaped double quote
"\\'", // escaped single quote
"\\n", // newline
"\\r", // carriage return
"\\t", // tab
),
),
_python_str: ($) =>
choice(
seq($._squote, optional(repeat1(choice(/[^'\\]+/, $._escape_sequence))), $._squote),
seq($._dquote, optional(repeat1(choice(/[^"\\]+/, $._escape_sequence))), $._dquote),
),
_squote: () => "'",
_dquote: () => '"',
env_var: ($) =>
alias(
choice(
"python_version",
"python_full_version",
"os_name",
"sys_platform",
"platform_release",
"platform_system",
"platform_version",
"platform_machine",
"platform_python_implementation",
"implementation_name",
"implementation_version",
"extra",
),
$.string,
),
_marker_var: ($) => seq(choice($.env_var, $._python_str)),
_marker_expr: ($) => choice(seq($._marker_var, $.marker_op, $._marker_var), seq("(", $.marker, ")")),
marker_and: ($) => choice(seq($._marker_expr, "and", $._marker_expr), $._marker_expr),
marker_or: ($) => choice(seq($.marker_and, "or", $.marker_and), $.marker_and),
marker: ($) => $.marker_or,
_quoted_marker: ($) => seq(";", $.marker),
_extras_list: ($) => seq($._package, repeat(seq(",", $._package))),
extras: ($) => seq("[", optional($._extras_list), "]"),
};