Commit
Author: Michael Davis [mcarsondavis@gmail.com]
Hash: 62f49eace1b9b01c2bacf7d599120be05df40beb
Timestamp: Fri, 24 Dec 2021 01:35:39 +0000 (2 years ago)

+19 -25 +/-1 browse
reorganize 'seq's with 'iseq' helper
1diff --git a/grammar.js b/grammar.js
2index 3979ce3..20d076d 100644
3--- a/grammar.js
4+++ b/grammar.js
5 @@ -2,17 +2,6 @@ const NEWLINE = /\r?\n/;
6 const WHITE_SPACE = /[\t\f\v ]+/;
7 const ANYTHING = /[^\r\n]+/;
8
9- const COMMAND_PRELUDE = token.immediate("diff");
10- const FILE_CHANGE_PRELUDE = token.immediate(
11- field("kind", choice("new", "deleted", "rename"))
12- );
13- const INDEX_PRELUDE = token.immediate("index");
14- const SIMILARITY_PRELUDE = token.immediate("similarity");
15- const FILE_PRELUDE = token.immediate(field("kind", choice("---", "+++")));
16- const LOCATION_PRELUDE = token.immediate("@@");
17- const ADDITION_PRELUDE = token.immediate("+");
18- const DELETION_PRELUDE = token.immediate("-");
19-
20 module.exports = grammar({
21 name: "gitdiff",
22
23 @@ -34,34 +23,39 @@ module.exports = grammar({
24 $.context
25 ),
26
27- command: ($) => seq(COMMAND_PRELUDE, "--git", $.filename),
28+ command: ($) => iseq("diff", "--git", $.filename),
29
30 file_change: ($) =>
31- choice(
32- seq(FILE_CHANGE_PRELUDE, "file", "mode", $.mode),
33- seq(FILE_CHANGE_PRELUDE, choice("from", "to"), $.filename)
34+ iseq(
35+ field("kind", choice("new", "deleted", "rename")),
36+ choice(
37+ seq("file", "mode", $.mode),
38+ seq(choice("from", "to"), $.filename)
39+ )
40 ),
41
42- index: ($) =>
43- seq(INDEX_PRELUDE, $.commit, "..", $.commit, optional($.mode)),
44
45- similarity: ($) =>
46- seq(SIMILARITY_PRELUDE, "index", field("score", /\d+/), "%"),
47+ index: ($) => iseq("index", $.commit, "..", $.commit, optional($.mode)),
48
49- file: ($) => seq(FILE_PRELUDE, $.filename),
50+ similarity: ($) => iseq("similarity", "index", field("score", /\d+/), "%"),
51
52- location: ($) =>
53- seq(LOCATION_PRELUDE, $.linerange, $.linerange, "@@", ANYTHING),
54+ file: ($) => iseq(field("kind", choice("---", "+++")), $.filename),
55
56- addition: ($) => seq(ADDITION_PRELUDE, optional(ANYTHING)),
57+ location: ($) => iseq("@@", $.linerange, $.linerange, "@@", ANYTHING),
58
59- deletion: ($) => seq(DELETION_PRELUDE, optional(ANYTHING)),
60+ addition: ($) => iseq("+", optional(ANYTHING)),
61+
62+ deletion: ($) => iseq("-", optional(ANYTHING)),
63
64 context: ($) => token(prec(-1, ANYTHING)),
65
66 linerange: ($) => /[-\+]\d+,\d+/,
67- filename: ($) => ANYTHING,
68+ filename: ($) => repeat1(/\S+/),
69 commit: ($) => /[a-f0-9]{7,40}/,
70 mode: ($) => /\d+/,
71 },
72 });
73+
74+ function iseq(start_token, ...tokens) {
75+ return seq(token.immediate(start_token), ...tokens);
76+ }