Skip to content
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
27 changes: 23 additions & 4 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@
};
} else if (productionRule.config) {
// This isn't a rule, it's an @config.
result.config[productionRule.config] = productionRule.value

// Convert autoUnwrap config to boolean
var value = productionRule.config === 'autoUnwrap'
? productionRule.value === 'true'
: productionRule.value;

result.config[productionRule.config] = value;
} else {
produceRules(productionRule.name, productionRule.rules, {});
if (!result.start) {
Expand Down Expand Up @@ -97,10 +103,20 @@
tokens.push(token);
}
}

var postprocess = rule.postprocess;
var shouldAutoUnwrap = Object.prototype.hasOwnProperty.call(rule, 'autoUnwrap')
? rule.autoUnwrap
: result.config.autoUnwrap;

if (shouldAutoUnwrap && tokens.length === 1) {
postprocess = 'id._auto(' + (postprocess || '') + ')';
}

return new nearley.Rule(
ruleName,
tokens,
rule.postprocess
postprocess
);
}

Expand Down Expand Up @@ -169,7 +185,8 @@
literal: d
};
}),
postprocess: {builtin: "joiner"}
postprocess: {builtin: "joiner"},
autoUnwrap: false,
}
], env);
return newname;
Expand Down Expand Up @@ -210,6 +227,7 @@
produceRules(name,
[{
tokens: [token.ebnf],
autoUnwrap: false,
}, {
tokens: [name, token.ebnf],
postprocess: {builtin: "arrpush"}
Expand Down Expand Up @@ -261,7 +279,8 @@
produceRules(name,
[{
tokens: [token.ebnf],
postprocess: {builtin: "id"}
postprocess: {builtin: "id"},
autoUnwrap: false,
}, {
tokens: [],
postprocess: {builtin: "nuller"}
Expand Down
1 change: 1 addition & 0 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
output += "// http://github.com/Hardmath123/nearley\n";
output += "(function () {\n";
output += "function id(x) { return x[0]; }\n";
output += "id._auto = function(fn) { return fn ? function(d, l, r) { return fn(id(d), l, r); } : id; };";
output += parser.body.join('\n');
output += "var grammar = {\n";
output += " Lexer: " + parser.config.lexer + ",\n";
Expand Down