forked from omertuc/tree-sitter-go-work
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.js
92 lines (75 loc) · 1.91 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
module.exports = grammar({
name: "gowork",
extras: ($) => [$.comment, /\s/],
rules: {
source_file: ($) => repeat($._directive),
_directive: ($) =>
choice(
$.go_directive,
$.use_directive,
$.replace_directive,
),
_string_literal: ($) =>
choice($.raw_string_literal, $.interpreted_string_literal),
raw_string_literal: ($) => token(seq("`", repeat(/[^`]/), "`")),
interpreted_string_literal: ($) =>
seq(
'"',
repeat(
choice(token.immediate(prec(1, /[^"\n\\]+/)), $.escape_sequence)
),
'"'
),
escape_sequence: ($) =>
token.immediate(
seq(
"\\",
choice(
/[^xuU]/,
/\d{2,3}/,
/x[0-9a-fA-F]{2,}/,
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/
)
)
),
_identifier: ($) => token(/[^\s,\[\]]+/),
_string_or_ident: ($) => choice($._string_literal, $._identifier),
module_path: ($) => $._string_or_ident,
go_version: ($) => $._string_or_ident,
version: ($) => $._string_or_ident,
go_directive: ($) => seq("go", $.go_version, "\n"),
replace_directive: ($) =>
seq(
"replace",
choice(
$.replace_spec,
seq("(", "\n", repeat($.replace_spec), ")", "\n")
)
),
replace_spec: ($) =>
choice(
seq($.module_path, optional($.version), "=>", $.file_path, "\n"),
seq(
$.module_path,
optional($.version),
"=>",
$.module_path,
$.version,
"\n"
)
),
use_directive: ($) =>
seq(
"use",
choice(
$.use_spec,
seq("(", "\n", repeat($.use_spec), ")", "\n")
)
),
use_spec: ($) =>
seq($.file_path, "\n"),
file_path: ($) => $._identifier,
comment: ($) => seq("//", /.*/),
},
});