From b3a90de0205e93218c4bdebbe45445cfbba48ba7 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 3 Jul 2016 18:36:15 +0200 Subject: [PATCH] Syntax error messages: Use Oxford comma In general PEG.js uses American English, where Oxford comma is prevalent. Part of #371. --- lib/compiler/passes/generate-js.js | 17 ++++++++++++----- spec/behavior/generated-parser-behavior.spec.js | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 3e804f0..a03a2cc 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -868,11 +868,18 @@ function generateJS(ast, options) { ' descriptions.length = j;', ' }', '', - ' return descriptions.length > 1', - ' ? descriptions.slice(0, -1).join(", ")', - ' + " or "', - ' + descriptions[descriptions.length - 1]', - ' : descriptions[0];', + ' switch (descriptions.length) {', + ' case 1:', + ' return descriptions[0];', + '', + ' case 2:', + ' return descriptions[0] + " or " + descriptions[1];', + '', + ' default:', + ' return descriptions.slice(0, -1).join(", ")', + ' + ", or "', + ' + descriptions[descriptions.length - 1];', + ' }', ' }', '', ' function describeFound(found) {', diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index eee64c5..0dc9a39 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -1401,7 +1401,7 @@ describe("generated parser behavior", function() { var parser = peg.generate('start = "a" / "b" / "c"', options); expect(parser).toFailToParse("d", { - message: 'Expected "a", "b" or "c" but "d" found.' + message: 'Expected "a", "b", or "c" but "d" found.' }); }); @@ -1433,7 +1433,7 @@ describe("generated parser behavior", function() { var parser = peg.generate('start = "c" / "b" / "a"', options); expect(parser).toFailToParse("d", { - message: 'Expected "a", "b" or "c" but "d" found.' + message: 'Expected "a", "b", or "c" but "d" found.' }); });