Skip to content

Commit

Permalink
Error handling: Extract exception building into its own function
Browse files Browse the repository at this point in the history
The exception-creating code will get somewhat hairy soon, so let's make
sure them mess will be contained.

Implements part of #198.
  • Loading branch information
dmajda committed Dec 1, 2013
1 parent d96eb31 commit 3fe6aba
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 53 deletions.
61 changes: 34 additions & 27 deletions lib/compiler/passes/generate-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,26 +909,42 @@ module.exports = function(ast, options) {
' peg$maxFailExpected.push(expected);',
' }',
'',
' function peg$cleanupExpected(expected) {',
' var i = 1;',
'',
' expected.sort(function(a, b) {',
' if (a.description < b.description) {',
' return -1;',
' } else if (a.description > b.description) {',
' return 1;',
' } else {',
' return 0;',
' }',
' });',
' function peg$buildException() {',
' function cleanupExpected(expected) {',
' var i = 1;',
'',
' expected.sort(function(a, b) {',
' if (a.description < b.description) {',
' return -1;',
' } else if (a.description > b.description) {',
' return 1;',
' } else {',
' return 0;',
' }',
' });',
'',
' while (i < expected.length) {',
' if (expected[i - 1].description === expected[i].description) {',
' expected.splice(i, 1);',
' } else {',
' i++;',
' while (i < expected.length) {',
' if (expected[i - 1].description === expected[i].description) {',
' expected.splice(i, 1);',
' } else {',
' i++;',
' }',
' }',
' }',
'',
' var pos = Math.max(peg$currPos, peg$maxFailPos),',
' posDetails = peg$computePosDetails(pos),',
' found = pos < input.length ? input.charAt(pos) : null;',
'',
' cleanupExpected(peg$maxFailExpected);',
'',
' return new SyntaxError(',
' peg$maxFailExpected,',
' found,',
' pos,',
' posDetails.line,',
' posDetails.column',
' );',
' }',
''
].join('\n'));
Expand Down Expand Up @@ -959,16 +975,7 @@ module.exports = function(ast, options) {
' if (peg$result !== peg$FAILED && peg$currPos === input.length) {',
' return peg$result;',
' } else {',
' peg$cleanupExpected(peg$maxFailExpected);',
' peg$reportedPos = Math.max(peg$currPos, peg$maxFailPos);',
'',
' throw new SyntaxError(',
' peg$maxFailExpected,',
' peg$reportedPos < input.length ? input.charAt(peg$reportedPos) : null,',
' peg$reportedPos,',
' peg$computePosDetails(peg$reportedPos).line,',
' peg$computePosDetails(peg$reportedPos).column',
' );',
' throw peg$buildException();',
' }',
' }',
'',
Expand Down
59 changes: 33 additions & 26 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3fe6aba

Please sign in to comment.