-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrammar.js
61 lines (46 loc) · 1.46 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
const NEWLINE = /[\r?\n]+/;
// anything but newline
const ANYTHING = /\S[^\n\r]*/;
const ANYTHING_BUT_WHITESPACE = /\S+/;
// white-space token(s) but not the newline character
const WHITE_SPACE = /[\t\f\v ]+/;
const ALPHA_HYPHEN = /[a-zA-Z-]+/;
module.exports = grammar({
name: "git_rebase",
extras: ($) => [WHITE_SPACE, $.comment],
rules: {
source: ($) => surround($.operation, NEWLINE),
operation: ($) =>
choice(
$._label_operation,
$._exec_operation,
$._merge_operation,
$._fixup_operation,
$._nullary_operation
),
_nullary_operation: ($) => $.command,
_label_operation: ($) => seq($.command, $.label, optional($.message)),
_merge_operation: ($) =>
seq($.command, $.option, $.label, $.label, optional($.message)),
_fixup_operation: ($) =>
seq($.command, $.option, $.label, optional($.message)),
_exec_operation: ($) =>
seq(alias(choice("x", "exec"), $.command), $.message),
// maybe this should be /-[a-zA-Z]/?
option: ($) => choice("-c", "-C"),
label: ($) => ANYTHING_BUT_WHITESPACE,
message: ($) => token(prec(-1, ANYTHING)),
command: ($) => ALPHA_HYPHEN,
comment: ($) => token(prec(-1, /#[^\r\n]*/)),
},
});
function surround(rule, separator) {
return seq(
optional(separator),
optional(sep1(rule, repeat1(separator))),
optional(separator)
);
}
function sep1(rule, separator) {
return seq(rule, repeat(seq(separator, rule)));
}