diff --git a/docs/dncl/README.md b/docs/dncl/README.md index 5b57dddd66..76620d9efc 100644 --- a/docs/dncl/README.md +++ b/docs/dncl/README.md @@ -433,6 +433,7 @@ Smalruby は **共通テスト用プログラム表記の形のプログラム | `の間繰り返す` | `a > 0 の間繰り返す:` | `a > 0 の間` | | `増やしながら繰り返す` | `i を ... 増やしながら繰り返す:` | `i を ... 増やしながら` | | `減らしながら繰り返す` | `i を ... 減らしながら繰り返す:` | `i を ... 減らしながら` | +| ブロックの行の読点(`,` / `、` / `,`。前後の空白も) | `a > 0 の間,繰り返す:` / `i を ... 1 ずつ増やしながら、繰り返す:` / `もし a > 0 ならば,` / `そうでなければ,` / `a > 0 の間,`(DNCL 風の形) | 読点の無い形(`a > 0 の間繰り返す:` など)。読点を読み飛ばすのは `の間` / `ならば` / `なら` / `そうでなければ` / `増やしながら` / `減らしながら` の直後で、その後ろが `繰り返す`・`:`・行の終わりのときだけ(`もし a > 0 ならば,b = 1` や `a > 0,の間繰り返す:` は読めない)。見た目だけの揺れなので、書くときは読点を入れない | | 関数の定義(標準の形) | `関数 twice(x):` | `関数 twice(x)`(字下げだけのブロックとして閉じる) | | 戻り値(標準の形) | `x * 2 を返す` | `return x * 2`(`返す x * 2` も読む) | | 要素の追加 | `Data に 3 を追加する` | `@_array_Data_.push(3)`(ブロックは「(3) を (Data) に追加する」。Ruby の `<<` も `を追加する` に戻す) | diff --git a/packages/ja-notation/src/parse.js b/packages/ja-notation/src/parse.js index 5164e2815c..6b1a645e25 100644 --- a/packages/ja-notation/src/parse.js +++ b/packages/ja-notation/src/parse.js @@ -808,6 +808,46 @@ const countingLoopEnd = (tokens) => { return null; }; +/** + * Whether a token is a reading comma: `,` (also `,`, read as `,`) or `、`. + * @param {object} token - The token. + * @returns {boolean} True when it is one. + */ +const isReadingComma = (token) => + Boolean(token) && + ((token.type === 'op' && token.value === ',') || (token.type === 'unknown' && token.text === '、')); + +/** + * Whether a token is a word of a block line after which a reading comma may + * be written: `の間`, `ならば` / `なら`, `そうでなければ` and the end of a + * counting loop (`ずつ増やしながら`, or `増やしながら` after `ずつ`). + * @param {object} token - The token. + * @returns {boolean} True when it is one. + */ +const takesReadingComma = (token) => + Boolean(token) && + token.type === 'name' && + (['の間', 'ならば', 'なら', 'そうでなければ'].includes(token.text) || /(?:増や|減ら)しながら$/.test(token.text)); + +/** + * The tokens without the reading commas of the block words, as in + * `x < 3 の間,繰り返す:`, `ずつ増やしながら、繰り返す:` and `ならば,`: the + * comma is taken only right after such a word and before `繰り返す`, the + * `:` or the end of the line, so a comma of a value (`f(a, b)`) stays. + * Writing gives these lines without the comma. + * @param {Array} tokens - The tokens of the line. + * @returns {Array} The tokens (the same array when there is no such comma). + */ +const withoutReadingCommas = (tokens) => { + const dropped = tokens.filter((t, i) => { + if (!isReadingComma(t) || !takesReadingComma(tokens[i - 1])) return true; + const next = tokens[i + 1]; + const endsHere = !next || (next.type === 'op' && next.value === ':') || isName(next, '繰り返す'); + return !endsHere; + }); + return dropped.length === tokens.length ? tokens : dropped; +}; + /** * Read a line that opens, continues or closes a block, or null when it is not * one. `colon` says the line ends with `:` (the standard form): its block ends @@ -1020,7 +1060,8 @@ const parse = (source) => { currentBody().push({ type: 'Comment', text: entry.comment, line: entry.line, column: 0 }); return; } - const bad = tokenError(entry.tokens); + const tokens = withoutReadingCommas(entry.tokens); + const bad = tokenError(tokens); if (bad) { errors.push(bad); return; @@ -1028,8 +1069,8 @@ const parse = (source) => { let block; let statements = null; try { - block = readBlockLine(entry.tokens, entry.line); - if (!block) statements = readSimple(entry.tokens, entry.line); + block = readBlockLine(tokens, entry.line); + if (!block) statements = readSimple(tokens, entry.line); } catch (e) { if (!(e instanceof LineError)) throw e; errors.push(e.error); diff --git a/packages/ja-notation/test/data/ja-to-ruby.json b/packages/ja-notation/test/data/ja-to-ruby.json index 55e2b71a85..fc24618eb3 100644 --- a/packages/ja-notation/test/data/ja-to-ruby.json +++ b/packages/ja-notation/test/data/ja-to-ruby.json @@ -43,6 +43,13 @@ "ruby": "i = 3\nwhile i >= 1\n puts(i)\n i += -1\nend", "jaLines": [1, 1, 2, 2, 2] }, + { + "title": "reading commas in the block lines are read as without them", + "table": "plain", + "ja": "n = 0\nn < 6 の間,繰り返す:\n もし n % 2 == 0 ならば、:\n 表示する(n)\n そうでなければ,:\n k を 1 から 2 まで 1 ずつ増やしながら,繰り返す:\n 表示する(k)\n n = n + 1", + "ruby": "n = 0\nwhile n < 6\n if n % 2 == 0\n puts(n)\n else\n k = 1\n while k <= 2\n puts(k)\n k += 1\n end\n end\n n = n + 1\nend", + "jaLines": [1, 2, 3, 4, 5, 6, 6, 7, 7, 7, 7, 8, 8] + }, { "title": "a loop on a `not` is an until loop", "table": "none", diff --git a/packages/ja-notation/test/data/parse.json b/packages/ja-notation/test/data/parse.json index 1f0f17ae56..6d791fecbb 100644 --- a/packages/ja-notation/test/data/parse.json +++ b/packages/ja-notation/test/data/parse.json @@ -1222,6 +1222,56 @@ "ja": "i を 1 から 3 まで 1 ずつ増やしながら\n 表示する(i)\nを繰り返す", "as": "i を 1 から 3 まで 1 ずつ増やしながら繰り返す:\n 表示する(i)" }, + { + "title": "a reading comma between の間 and 繰り返す (,)", + "ja": "n < 5 の間,繰り返す:\n n = n + 2", + "as": "n < 5 の間繰り返す:\n n = n + 2" + }, + { + "title": "a reading comma between の間 and 繰り返す (、 and spaces)", + "ja": "n < 5 の間 、 繰り返す:\n n = n + 2", + "as": "n < 5 の間繰り返す:\n n = n + 2" + }, + { + "title": "a reading comma between の間 and 繰り返す (half-width , and a full-width space)", + "ja": "n < 5 の間, 繰り返す:\n n = n + 2", + "as": "n < 5 の間繰り返す:\n n = n + 2" + }, + { + "title": "a reading comma after の間 before the colon", + "ja": "n < 5 の間,:\n n = n + 2", + "as": "n < 5 の間繰り返す:\n n = n + 2" + }, + { + "title": "DNCL-like: a reading comma after の間", + "ja": "n < 5 の間,\n n = n + 2\nを繰り返す", + "as": "n < 5 の間繰り返す:\n n = n + 2" + }, + { + "title": "a reading comma between ずつ増やしながら and 繰り返す", + "ja": "k を 2 から 8 まで 2 ずつ増やしながら,繰り返す:\n 表示する(k)", + "as": "k を 2 から 8 まで 2 ずつ増やしながら繰り返す:\n 表示する(k)" + }, + { + "title": "a reading comma between ずつ 減らしながら and 繰り返す (、)", + "ja": "k を 8 から 2 まで 2 ずつ 減らしながら、繰り返す:\n 表示する(k)", + "as": "k を 8 から 2 まで 2 ずつ減らしながら繰り返す:\n 表示する(k)" + }, + { + "title": "DNCL-like: a reading comma after ずつ増やしながら", + "ja": "k を 2 から 8 まで 2 ずつ増やしながら,\n 表示する(k)\nを繰り返す", + "as": "k を 2 から 8 まで 2 ずつ増やしながら繰り返す:\n 表示する(k)" + }, + { + "title": "a reading comma after ならば, そうでなくもし ... ならば and そうでなければ", + "ja": "もし m > 0 ならば,:\n s = 1\nそうでなくもし m < 0 ならば、:\n s = 2\nそうでなければ,:\n s = 3", + "as": "もし m > 0 ならば:\n s = 1\nそうでなくもし m < 0 ならば:\n s = 2\nそうでなければ:\n s = 3" + }, + { + "title": "DNCL-like: a reading comma after ならば, なら and そうでなければ", + "ja": "もし m > 0 ならば,\n s = 1\nそうでなくもし m < 0 なら,\n s = 2\nそうでなければ,\n s = 3\nを実行する", + "as": "もし m > 0 ならば:\n s = 1\nそうでなくもし m < 0 ならば:\n s = 2\nそうでなければ:\n s = 3" + }, { "title": "DNCL-like: 関数 ... と定義する", "ja": "関数 f(a)\n a + 1 を返す\nと定義する", @@ -1592,6 +1642,16 @@ "column": 0 } ] + }, + { + "title": "a comma after ならば is read only at the end of the line", + "ja": "もし m > 0 ならば,s = 1", + "errors": [{ "code": "syntax", "message": "「もし」の後に「ならば」が要ります", "line": 1, "column": 0 }] + }, + { + "title": "a comma before の間 is not a reading comma of the loop", + "ja": "n < 5,の間繰り返す:\n n = n + 2", + "errors": [{ "code": "syntax", "message": "「,」が読めません", "line": 1, "column": 5 }] } ] } diff --git a/packages/scratch-gui/test/unit/lib/dncl/dncl-validator.test.js b/packages/scratch-gui/test/unit/lib/dncl/dncl-validator.test.js index 3acc371b6f..d45a87eec6 100644 --- a/packages/scratch-gui/test/unit/lib/dncl/dncl-validator.test.js +++ b/packages/scratch-gui/test/unit/lib/dncl/dncl-validator.test.js @@ -33,6 +33,25 @@ describe('block errors of the DNCL-like form', () => { ).toEqual([]); }); + test('a reading comma after the words of a block line is read (#2274)', () => { + expect( + errorsOf([ + 'x < 3 の間,', + ' もし x > 1 ならば、', + ' x ← x + 2', + ' そうでなければ,', + ' x ← x + 1', + ' を実行する', + 'を繰り返す', + 'i を 1 から 3 まで 1 ずつ増やしながら,繰り返す:', + ' 表示する(i)', + ]), + ).toEqual([]); + expect(errorsOf(['x < 3 の間,', ' x ← x + 1'])[0].message).toBe( + '「〜の間」を「を繰り返す」で閉じてください', + ); + }); + test('an if without を実行する is reported on its line', () => { expect(errorsOf(['もし x > 0 ならば', ' 表示する(x)'])).toEqual([ {