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

Experimental support for #21 #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

njjewers
Copy link
Contributor

A quick implementation of #21. This is accomplished by extending GrammarSyntaxError to provide more relevant messages than the default "expected" messages that Ohm gives. This is, in my opinion, the most appropriate place to put it, as this exception is only used for failure to create user grammars, and this keeps all ohm-specific parse failure messages separate from the rest of Ohm's logic.

I won't lie, I'm not entirely satisfied with it quite yet, especially the changes I made to the visualizer to support taking this from the exception. Perhaps it might be better to integrate this elsewhere.

Copy link
Contributor

@pdubroy pdubroy left a comment

Choose a reason for hiding this comment

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

Thanks! Generally I think this looks good. I suggested a few small changes that would improve the code.

After you address those, would you mind resubmitting this request without including the stuff that has nothing to do with #21? I think you've accidentally included some stuff that was submitted in another PR.

Also, would you mind adding a test for this fix? After that I think we can merge this.

var reason = null;

var failures = matchFailure.getRightmostFailures();
for(var i = 0; i < failures.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Missing space before (.

var failure = failures[i];
if (failure.getPExpr().ruleName === 'escapeChar'){
// Failure to match an escape sequence, therefore the grammar contains an invalid escape sequence.
var escapeSequence = source.substr(matchFailure.getRightmostFailurePosition(),2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Missing space after ,.

if (failure.getPExpr().ruleName === 'escapeChar'){
// Failure to match an escape sequence, therefore the grammar contains an invalid escape sequence.
var escapeSequence = source.substr(matchFailure.getRightmostFailurePosition(),2);
var reason = "Invalid escape sequence \"" + escapeSequence + "\"";
Copy link
Contributor

Choose a reason for hiding this comment

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

You shouldn't be re-declaring reason here -- should just be reason = ...

}

if (reason) {
// Use the more relevant reason an the error message
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: 'as', not 'an'

var failure = failures[i];
if (failure.getPExpr().ruleName === 'escapeChar'){
// Failure to match an escape sequence, therefore the grammar contains an invalid escape sequence.
var escapeSequence = source.substr(matchFailure.getRightmostFailurePosition(),2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you call matchFailure.getRightmostFailurePosition() several times here, why not declare var rightmostFailurePos = matchFailure.getRightmostFailurePosition(); above the for loop?

@pdubroy pdubroy force-pushed the main branch 4 times, most recently from ac91299 to fdddc68 Compare March 4, 2023 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants