-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
69 lines (60 loc) · 1.58 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
/**
* @file gosum grammar for tree-sitter
* @author Amaan Qureshi <amaanq12@gmail.com>
* @license MIT
* @see {@link https://go.dev/ref/mod#go-sum-files|official syntax spec}
*/
/* eslint-disable arrow-parens */
/* eslint-disable camelcase */
/* eslint-disable-next-line spaced-comment */
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: 'gosum',
rules: {
checksum_database: $ => repeat1($.checksum),
checksum: $ => seq(
$.module_path,
seq($.version, optional(seq('/', 'go.mod'))),
$.checksum_value,
),
module_path: _ => /[a-zA-Z0-9\-_\./]+/,
version: $ => seq(
alias(seq(
'v',
field('major', $.number),
'.',
field('minor', $.number),
'.',
field('patch', $.number)), $.module_version),
optional(seq(
'-',
field(
'pre_release',
choice(
$.number_with_decimal, // YYYYMMDDHHMMSS
seq(
choice('alpha', 'beta', 'dev', 'pre', 'rc'),
optional('.'),
$.number,
optional(seq('.', $.number_with_decimal)),
),
),
),
optional(seq('-', field('build', $.hex_number))),
)),
optional('+incompatible'),
),
checksum_value: $ => seq(
$.hash_version,
':',
$.hash,
),
// currently only h1 is supported
hash_version: _ => 'h1',
hash: _ => /[a-zA-Z0-9+/]+={0,2}/,
number: _ => /\d+/,
number_with_decimal: _ => /[\d\.]+/,
hex_number: _ => /[0-9a-fA-F]+/,
},
});