-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
54 lines (42 loc) · 1.25 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
module.exports = grammar({
name: "hledger",
rules: {
source_file: $ => repeat(choice($.transaction, "\n", $.comment)),
transaction: $ => seq($.entry, repeat1(seq(/\s{1,}/, $.posting, "\n"))),
entry: $ =>
seq(
$.date,
optional(seq(/\s*/, $.status)),
optional($.payee),
optional(seq(/\s*/, $.comment))
),
date: $ => {
function createDate(separator) {
return seq(optional(seq($.year, separator)), $.month, separator, $.day);
}
return choice(createDate("-"), createDate("/"), createDate("."));
},
year: $ => /\d{4}/,
month: $ => /\d{2}/,
day: $ => /\d{2}/,
payee: $ => /[^\n;]+/,
posting: $ =>
seq(
optional(seq($.status, " ")),
$.account,
optional(seq(/\s{2,}/, $.amount))
),
status: $ => choice("*", "!"),
account: $ => seq($._accountPart, repeat(seq(":", $._accountPart))),
_accountPart: $ => /\w+/,
amount: $ => seq($.commodity, $.quantity),
// choice(
// $.quantity,
// seq($.commodity, $.quantity), // left commodity
// seq($.quantity, $.commodity) // right commodity
// ),
commodity: $ => /[\w\$]+/,
quantity: $ => /\d+/,
comment: $ => seq(";", /[^\n]*/)
}
});