Skip to content

Commit b4121bf

Browse files
committed
Initial commit
1 parent d8ee606 commit b4121bf

13 files changed

+1793
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
log.html
3+
node_modules

.prettierrc

Whitespace-only changes.

binding.gyp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "tree_sitter_hledger_binding",
5+
"include_dirs": [
6+
"<!(node -e \"require('nan')\")",
7+
"src"
8+
],
9+
"sources": [
10+
"src/parser.c",
11+
"src/binding.cc"
12+
],
13+
"cflags_c": [
14+
"-std=c99",
15+
]
16+
}
17+
]
18+
}

corpus/transactions.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
==================
2+
Simple
3+
==================
4+
5+
2019/01/01 Wells Fargo
6+
assets:wells $100
7+
! expenses:beer $100
8+
9+
---
10+
11+
(source_file
12+
(transaction
13+
(date
14+
(year)
15+
(month)
16+
(day))
17+
(payee)
18+
(postings
19+
(posting
20+
(account)
21+
(amount
22+
(commodity)
23+
(quantity)))
24+
(posting
25+
(status)
26+
(account)
27+
(amount
28+
(commodity)
29+
(quantity))))))

grammar.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module.exports = grammar({
2+
name: "hledger",
3+
4+
rules: {
5+
source_file: $ => repeat1($.transaction),
6+
7+
transaction: $ => seq($._entry, "\n", $.postings),
8+
9+
_entry: $ => seq($.date, optional(seq($.status, /\s+/)), optional($.payee)),
10+
11+
date: $ => {
12+
function createDate(separator) {
13+
return seq(optional(seq($.year, separator)), $.month, separator, $.day);
14+
}
15+
return choice(createDate("-"), createDate("/"), createDate("."));
16+
},
17+
year: $ => /\d{4}/,
18+
month: $ => /\d{2}/,
19+
day: $ => /\d{2}/,
20+
21+
payee: $ => /[^\n;]+/,
22+
23+
postings: $ => repeat1(seq(/\s{1,}/, $.posting, "\n")),
24+
posting: $ =>
25+
seq(
26+
optional(seq($.status, " ")),
27+
$.account,
28+
optional(seq(/\s{2,}/, $.amount))
29+
),
30+
31+
status: $ => choice("*", "!"),
32+
33+
account: $ => seq($._part, repeat(seq(":", $._part))),
34+
_part: $ => /\w+/,
35+
36+
amount: $ => seq($.commodity, $.quantity),
37+
38+
// choice(
39+
// $.quantity,
40+
// seq($.commodity, $.quantity), // left commodity
41+
// seq($.quantity, $.commodity) // right commodity
42+
// ),
43+
44+
commodity: $ => /[\w\$]+/,
45+
quantity: $ => "100"
46+
}
47+
});

index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try {
2+
module.exports = require("./build/Release/tree_sitter_hledger_binding");
3+
} catch (error) {
4+
try {
5+
module.exports = require("./build/Debug/tree_sitter_hledger_binding");
6+
} catch (_) {
7+
throw error;
8+
}
9+
}
10+
11+
try {
12+
module.exports.nodeTypeInfo = require("./src/node-types.json");
13+
} catch (_) {}

package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "tree-sitter-ledger",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"nan": "^2.14.0"
8+
},
9+
"devDependencies": {
10+
"tree-sitter-cli": "^0.15.8",
11+
"prettier": "^1.18.2"
12+
}
13+
}

src/binding.cc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "tree_sitter/parser.h"
2+
#include <node.h>
3+
#include "nan.h"
4+
5+
using namespace v8;
6+
7+
extern "C" TSLanguage * tree_sitter_ledger();
8+
9+
namespace {
10+
11+
NAN_METHOD(New) {}
12+
13+
void Init(Local<Object> exports, Local<Object> module) {
14+
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
15+
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
16+
tpl->InstanceTemplate()->SetInternalFieldCount(1);
17+
18+
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
19+
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
20+
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_ledger());
21+
22+
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("ledger").ToLocalChecked());
23+
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
24+
}
25+
26+
NODE_MODULE(tree_sitter_ledger_binding, Init)
27+
28+
} // namespace

0 commit comments

Comments
 (0)