-
-
Notifications
You must be signed in to change notification settings - Fork 217
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
Issue #43 - Customizable operation parse error #174
base: main
Are you sure you want to change the base?
Changes from 3 commits
8eb972d
32f9c28
a08e75c
973d30f
f312eb5
7ae7e2a
03a529f
4697e24
2a6d401
e8f8d07
aa62353
a1d2b91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,31 @@ MatchFailure.prototype.getRightmostFailures = function() { | |
return this._failures; | ||
}; | ||
|
||
MatchFailure.prototype.getMessage = function(config) { | ||
var source = this.state.inputStream.source; | ||
if (typeof source !== 'string') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need this |
||
return 'match failed at position ' + this.getRightmostFailurePosition(); | ||
} | ||
var detail = 'Expected ' + this.getExpectedText(); | ||
|
||
// use default values if not defined | ||
var includeSource = typeof config !== 'undefined' && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be a cleaner to do this slightly differently. First, rename the argument
Then, define
|
||
typeof config.includeSource !== 'undefined' ? | ||
config.includeSource : true; | ||
var includeLineNumbers = typeof config !== 'undefined' && | ||
typeof config.includeLineNumbers !== 'undefined' ? | ||
config.includeLineNumbers : true; | ||
|
||
if (!includeSource) { | ||
return getShortMatchErrorMessage( | ||
this.getRightmostFailurePosition(), | ||
this.state.inputStream.source, | ||
detail); | ||
} | ||
return util.getOptionalLineAndColumnMessage( | ||
source, this.getRightmostFailurePosition(), includeLineNumbers) + detail; | ||
}; | ||
|
||
// Return a string summarizing the expected contents of the input stream when | ||
// the match failure occurred. | ||
MatchFailure.prototype.getExpectedText = function() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -340,7 +340,7 @@ function parseSignature(signature, type) { | |
signature, | ||
type === 'operation' ? 'OperationSignature' : 'AttributeSignature'); | ||
if (r.failed()) { | ||
throw new Error(r.message); | ||
throw new Error(r.getMessage({includeSource: true, includeLineNumbers: true})); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want |
||
} | ||
|
||
return prototypeGrammarSemantics(r).parse(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,7 +93,10 @@ exports.getLineAndColumn = function(str, offset) { | |
|
||
// Return a nicely-formatted string describing the line and column for the | ||
// given offset in `str`. | ||
exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | ||
|
||
exports.getOptionalLineAndColumnMessage = function(str, offset, includeLineNumbers /* ranges */) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that the line numbers are optional, I think we could pick a better name for this. Maybe 'describeSourceLocation'? |
||
|
||
var _includeLineNumbers = typeof includeLineNumbers !== 'undefined' ? includeLineNumbers : true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: it's unusual for local variables to start with |
||
var repeatStr = common.repeatStr; | ||
|
||
var lineAndCol = exports.getLineAndColumn(str, offset); | ||
|
@@ -109,7 +112,11 @@ exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | |
|
||
// Helper for appending formatting input lines to the buffer. | ||
function appendLine(num, content, prefix) { | ||
sb.append(prefix + lineNumbers[num] + ' | ' + content + '\n'); | ||
if (_includeLineNumbers) { | ||
sb.append(prefix + lineNumbers[num] + ' | ' + content + '\n'); | ||
} else { | ||
sb.append(' ' + content + '\n'); | ||
} | ||
} | ||
|
||
// Include the previous line for context if possible. | ||
|
@@ -123,7 +130,7 @@ exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | |
// Start with a blank line, and indicate each range by overlaying a string of `~` chars. | ||
var lineLen = lineAndCol.line.length; | ||
var indicationLine = repeatStr(' ', lineLen + 1); | ||
var ranges = Array.prototype.slice.call(arguments, 2); | ||
var ranges = Array.prototype.slice.call(arguments, 3); | ||
for (var i = 0; i < ranges.length; ++i) { | ||
var startIdx = ranges[i][0]; | ||
var endIdx = ranges[i][1]; | ||
|
@@ -135,7 +142,7 @@ exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | |
|
||
indicationLine = strcpy(indicationLine, repeatStr('~', endIdx - startIdx), startIdx); | ||
} | ||
var gutterWidth = 2 + lineNumbers[1].length + 3; | ||
var gutterWidth = _includeLineNumbers ? 2 + lineNumbers[1].length + 3 : 2; | ||
sb.append(repeatStr(' ', gutterWidth)); | ||
indicationLine = strcpy(indicationLine, '^', lineAndCol.colNum - 1); | ||
sb.append(indicationLine.replace(/ +$/, '') + '\n'); | ||
|
@@ -146,3 +153,9 @@ exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | |
} | ||
return sb.contents(); | ||
}; | ||
|
||
exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd get rid of the optional |
||
var args = Array.prototype.slice.call(arguments); | ||
args.splice(2, 0, true); | ||
return exports.getOptionalLineAndColumnMessage.apply(this, args); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add a short comment above this function explaining what options can be passed in the
config
arg.