Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issue #56. #175

Open
wants to merge 1 commit into
base: main
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
15 changes: 15 additions & 0 deletions src/GrammarDecl.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ GrammarDecl.prototype.build = function() {
// the part of the source that caused it.
var grammarErrors = [];
var grammarHasInvalidApplications = false;
var parents = {};
Object.keys(grammar.rules).forEach(function(ruleName) {
var body = grammar.rules[ruleName].body;
try {
Expand All @@ -118,6 +119,11 @@ GrammarDecl.prototype.build = function() {
grammarErrors.push(e);
grammarHasInvalidApplications = true;
}
if (body instanceof pexprs.Alt) {
for (var idx = 0; idx < body.terms.length; idx++) {
parents[body.terms[idx].ruleName] = ruleName;
}
}
});
if (!grammarHasInvalidApplications) {
// The following check can only be done if the grammar has no invalid applications.
Expand All @@ -130,6 +136,15 @@ GrammarDecl.prototype.build = function() {
}
});
}

Object.keys(grammar.rules).forEach(function(ruleName) {
var body = grammar.rules[ruleName].body;
try {
body.assertProperAssociativity(ruleName, parents[ruleName], body.source);
} catch (e) {
grammarErrors.push(e);
}
});
if (grammarErrors.length > 0) {
errors.throwErrors(grammarErrors);
}
Expand Down
11 changes: 11 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ function missingSemanticAction(ctorName, name, type) {
return e;
}

// --------- left-right associativity ambiguousness -----------

function associativityAmbiguousness(parentName, expr) {
return createError(
'Left or Right associativity is unclear. Ensure ' + parentName +
' is only on either the right or left.',
expr
);
}

// --------------------------------------------------------------------
// Exports
// --------------------------------------------------------------------
Expand All @@ -209,6 +219,7 @@ module.exports = {
undeclaredRule: undeclaredRule,
wrongNumberOfArguments: wrongNumberOfArguments,
wrongNumberOfParameters: wrongNumberOfParameters,
associativityAmbiguousness: associativityAmbiguousness,

throwErrors: function(errors) {
if (errors.length === 1) {
Expand Down
60 changes: 60 additions & 0 deletions src/pexprs-assertProperAssociativity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

// --------------------------------------------------------------------
// Imports
// --------------------------------------------------------------------

var common = require('./common');
var errors = require('./errors');
var pexprs = require('./pexprs');

// --------------------------------------------------------------------
// Operations
// --------------------------------------------------------------------

pexprs.PExpr.prototype.assertProperAssociativity = common.abstract(
'assertProperAssociativity'
);

pexprs.any.assertProperAssociativity =
pexprs.end.assertProperAssociativity =
pexprs.Terminal.prototype.assertProperAssociativity =
pexprs.Range.prototype.assertProperAssociativity =
pexprs.Param.prototype.assertProperAssociativity =
pexprs.Lex.prototype.assertProperAssociativity =
pexprs.UnicodeChar.prototype.assertProperAssociativity = function(ruleName) {
// no-op
};

pexprs.Alt.prototype.assertProperAssociativity = function(ruleName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might as well just group all of the no-op versions together.

// no-op
};

pexprs.Seq.prototype.assertProperAssociativity = function(ruleName, trueParentName, src) {
if (this.factors.length < 2) {
return;
}
if (this.factors[0] instanceof pexprs.Apply &&
this.factors[this.factors.length - 1] instanceof pexprs.Apply) {
if (this.factors[0].ruleName === this.factors[this.factors.length - 1].ruleName &&
this.factors[0].ruleName === trueParentName) {
throw errors.associativityAmbiguousness(trueParentName, src);
}
}
};

pexprs.Iter.prototype.assertProperAssociativity = function(ruleName) {
this.expr.assertProperAssociativity(ruleName);
};

pexprs.Not.prototype.assertProperAssociativity = function(ruleName) {
// no-op
};

pexprs.Lookahead.prototype.assertProperAssociativity = function(ruleName) {
this.expr.assertProperAssociativity(ruleName);
};

pexprs.Apply.prototype.assertProperAssociativity = function(ruleName) {
// no-op
};
1 change: 1 addition & 0 deletions src/pexprs.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,4 @@ require('./pexprs-toDisplayString');
require('./pexprs-toArgumentNameList');
require('./pexprs-toFailure');
require('./pexprs-toString');
require('./pexprs-assertProperAssociativity');
25 changes: 25 additions & 0 deletions test/test-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,28 @@ test('errors for Not-of-<PExpr>', function(t) {

t.end();
});

test('Error where left-right associativity is unclear', function(t) {
try {
ohm.grammar(
'G {\n' +
' A = A "+" A -- add \n' +
' | B\n' +
' B = "117"\n' +
'}');
t.fail('Expected an exception to be thrown');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more idiomatic way to ensure an exception is thrown is to use t.throws(): https://github.com/substack/tape#tthrowsfn-expected-msg

} catch (e) {
t.equal(e.message, 'Line 2, col 7:\n' +
' 1 | G {\n> 2 | A = A "+" A -- add \n' +
' ^~~~~~~\n 3 | | B\n' +
'Left or Right associativity is unclear. Ensure A is only on either the right or left.');
}
ohm.grammar(
'G {\n' +
' A = A "+" B -- add \n' +
' | B\n' +
' B = "117"\n' +
'}');
t.end();

});