Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Expose the new EXCLUDE rule from the JS grammar API #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/api/dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function alias(rule, value) {
result.named = true;
result.value = value.symbol.name;
return result;
case SymbolRule:
case Object:
if (typeof value.type === 'string' && value.type === 'SYMBOL') {
result.named = true;
Expand Down Expand Up @@ -129,11 +130,25 @@ function string(value) {
};
}

class SymbolRule {
constructor (name) {
this.type = 'SYMBOL';
this.name = name;
}

exclude (...tokens) {
return choice(
this,
...tokens.map(token => ({
type: 'EXCLUDE',
content: normalize(token)
}))
);
}
}

function sym(name) {
return {
type: "SYMBOL",
name: name
};
return new SymbolRule(name);
}

function token(value) {
Expand Down Expand Up @@ -174,7 +189,7 @@ function normalize(value) {
if (typeof value.type === 'string') {
return value;
} else {
throw new TypeError("Invalid rule: " + value.toString());
throw new TypeError("Invalid rule: " + JSON.stringify(value))
}
}
}
Expand All @@ -198,7 +213,7 @@ function grammar(baseGrammar, options) {
throw new Error("Grammar's 'externals' property must be a function.");
}

const externalsRuleBuilder = new RuleBuilder()
const externalsRuleBuilder = new RuleBuilder(sym, null)
const externalRules = options.externals.call(externalsRuleBuilder, externalsRuleBuilder, baseGrammar.externals);

if (!Array.isArray(externalRules)) {
Expand All @@ -221,7 +236,7 @@ function grammar(baseGrammar, options) {
}
}

const ruleBuilder = new RuleBuilder(rulesSet);
const ruleBuilder = new RuleBuilder(sym, rulesSet);

const name = options.name;
if (typeof name !== "string") {
Expand Down
26 changes: 12 additions & 14 deletions src/rule_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ using namespace v8;

Nan::Persistent<v8::Function> constructor;

Local<Object> build_symbol(Local<String> name) {
auto result = Nan::New<Object>();
result->Set(Nan::New("type").ToLocalChecked(), Nan::New("SYMBOL").ToLocalChecked());
result->Set(Nan::New("name").ToLocalChecked(), name);
return result;
}

static void GetProperty(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
Local<Value> rules = info.This()->GetInternalField(0);
Local<Object> symbol = build_symbol(property);
Local<Value> symbol_constructor = info.This()->GetInternalField(0);
Local<Value> rules = info.This()->GetInternalField(1);

Local<Value> argv = {property};
Local<Value> symbol = Local<Function>::Cast(symbol_constructor)->Call(Nan::Null(), 1, &argv);

if (rules->IsObject()) {
Local<Object> rules_object = Local<Object>::Cast(rules);
Expand All @@ -39,18 +35,20 @@ static void GetProperty(Local<String> property, const Nan::PropertyCallbackInfo<
}

static void construct(const Nan::FunctionCallbackInfo<Value> &info) {
Local<Value> data = Nan::Null();
if (info.Length() == 1 && info[0]->IsObject()) {
data = info[0];
if (info.Length() < 2) {
Nan::ThrowTypeError("Must pass RuleBuilder two arguments");
return;
}
info.This()->SetInternalField(0, data);

info.This()->SetInternalField(0, info[0]);
info.This()->SetInternalField(1, info[1]);
}

void Init(Handle<Object> exports) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(construct);
tpl->SetClassName(Nan::New("RuleBuilder").ToLocalChecked());
Nan::SetNamedPropertyHandler(tpl->InstanceTemplate(), GetProperty);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->InstanceTemplate()->SetInternalFieldCount(2);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("RuleBuilder").ToLocalChecked(), Nan::New(constructor));
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/tree-sitter