nadesiko3 3.3.49 → 3.3.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (343) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +143 -143
  3. package/batch/.DS_Store +0 -0
  4. package/batch/browsers.template.md +3 -3
  5. package/batch/build_browsers.nako3 +72 -72
  6. package/batch/build_command.nako3 +44 -44
  7. package/batch/build_nako_version.nako3 +42 -42
  8. package/batch/calc_hash.nako3 +29 -29
  9. package/batch/cmd_txt2json.nako3 +74 -74
  10. package/batch/command.txt +162 -83
  11. package/batch/command_nakopad.txt +69 -0
  12. package/batch/download-extlib.nako3 +43 -43
  13. package/batch/gen_command_nakopad.nako3 +57 -57
  14. package/batch/jsplugin2text.nako3 +285 -285
  15. package/batch/pickup_command.nako3 +113 -110
  16. package/batch/pickup_reserved_words.nako3 +11 -11
  17. package/batch/publish_version.nako3 +46 -46
  18. package/batch/show_agents.js +14 -14
  19. package/batch/turtle2js.nako3 +21 -21
  20. package/bin/cnako3 +10 -10
  21. package/core/LICENSE +21 -21
  22. package/core/README.md +66 -66
  23. package/core/batch/build_nako_version.nako3 +42 -42
  24. package/core/command/snako.mjs +105 -105
  25. package/core/command/snako.mts +116 -116
  26. package/core/index.mjs +21 -21
  27. package/core/package.json +47 -47
  28. package/core/sample/hello.nako3 +7 -7
  29. package/core/sample/hoge.mjs +4 -4
  30. package/core/sample/hoge.mts +6 -6
  31. package/core/src/nako3.mjs +870 -858
  32. package/core/src/nako3.mts +983 -967
  33. package/core/src/nako_colors.mjs +78 -78
  34. package/core/src/nako_colors.mts +86 -86
  35. package/core/src/nako_core_version.mjs +8 -8
  36. package/core/src/nako_core_version.mts +19 -19
  37. package/core/src/nako_csv.mjs +185 -185
  38. package/core/src/nako_csv.mts +188 -188
  39. package/core/src/nako_errors.mjs +173 -173
  40. package/core/src/nako_errors.mts +197 -197
  41. package/core/src/nako_from_dncl.mjs +255 -255
  42. package/core/src/nako_from_dncl.mts +250 -250
  43. package/core/src/nako_gen.mjs +1647 -1648
  44. package/core/src/nako_gen.mts +1718 -1719
  45. package/core/src/nako_gen_async.mjs +1659 -1659
  46. package/core/src/nako_gen_async.mts +1732 -1732
  47. package/core/src/nako_global.mjs +109 -107
  48. package/core/src/nako_global.mts +141 -138
  49. package/core/src/nako_indent.mjs +445 -445
  50. package/core/src/nako_indent.mts +492 -492
  51. package/core/src/nako_josi_list.mjs +38 -38
  52. package/core/src/nako_josi_list.mts +45 -45
  53. package/core/src/nako_lex_rules.mjs +253 -253
  54. package/core/src/nako_lex_rules.mts +260 -260
  55. package/core/src/nako_lexer.mjs +609 -609
  56. package/core/src/nako_lexer.mts +612 -612
  57. package/core/src/nako_logger.mjs +199 -199
  58. package/core/src/nako_logger.mts +232 -232
  59. package/core/src/nako_parser3.mjs +2441 -2439
  60. package/core/src/nako_parser3.mts +2197 -2195
  61. package/core/src/nako_parser_base.mjs +371 -370
  62. package/core/src/nako_parser_base.mts +372 -370
  63. package/core/src/nako_parser_const.mjs +37 -37
  64. package/core/src/nako_parser_const.mts +37 -37
  65. package/core/src/nako_prepare.mjs +304 -304
  66. package/core/src/nako_prepare.mts +315 -315
  67. package/core/src/nako_reserved_words.mjs +38 -38
  68. package/core/src/nako_reserved_words.mts +38 -38
  69. package/core/src/nako_source_mapping.mjs +207 -207
  70. package/core/src/nako_source_mapping.mts +262 -262
  71. package/core/src/nako_test.mjs +37 -37
  72. package/core/src/nako_types.mjs +13 -25
  73. package/core/src/nako_types.mts +144 -151
  74. package/core/src/plugin_csv.mjs +49 -49
  75. package/core/src/plugin_csv.mts +50 -50
  76. package/core/src/plugin_math.mjs +328 -328
  77. package/core/src/plugin_math.mts +326 -326
  78. package/core/src/plugin_promise.mjs +91 -91
  79. package/core/src/plugin_promise.mts +91 -91
  80. package/core/src/plugin_system.mjs +2832 -2832
  81. package/core/src/plugin_system.mts +2690 -2690
  82. package/core/src/plugin_test.mjs +34 -34
  83. package/core/src/plugin_test.mts +34 -34
  84. package/demo/.DS_Store +0 -0
  85. package/demo/ace_editor.html +89 -89
  86. package/demo/ace_editor_tabs.html +161 -161
  87. package/demo/basic.html +71 -71
  88. package/demo/browsers.html +10 -9
  89. package/demo/css/basic.css +3 -3
  90. package/demo/css/common.css +157 -157
  91. package/demo/css/editor.css +8 -8
  92. package/demo/css/flow.css +3 -3
  93. package/demo/css/index.css +3 -3
  94. package/demo/extlib/.DS_Store +0 -0
  95. package/demo/flow.html +98 -98
  96. package/demo/graph.html +53 -53
  97. package/demo/image/nakopad-icon256.png +0 -0
  98. package/demo/index.html +133 -133
  99. package/demo/js/common.js +17 -17
  100. package/demo/js/turtle3d_test.js +44 -44
  101. package/demo/js/turtle_test.js +45 -45
  102. package/demo/nako3/calc.nako3 +4 -4
  103. package/demo/runscript.html +47 -47
  104. package/demo/runscript2.html +33 -33
  105. package/demo/runscript3.html +35 -35
  106. package/demo/runscript4.html +33 -33
  107. package/demo/turtle.html +58 -58
  108. package/demo/turtle2.html +141 -141
  109. package/demo/turtle3.html +279 -279
  110. package/demo/turtle3d.html +58 -58
  111. package/demo/turtle3d2.html +107 -107
  112. package/demo/version.html +24 -24
  113. package/doc/SETUP.md +157 -157
  114. package/doc/about.md +17 -17
  115. package/doc/browsers.md +26 -26
  116. package/doc/docgen.md +21 -21
  117. package/doc/editor.md +44 -44
  118. package/doc/files.md +39 -39
  119. package/doc/plugins.md +234 -234
  120. package/doc/release.md +79 -79
  121. package/doc/textlint.md +43 -43
  122. package/doc/win32.md +57 -57
  123. package/package.json +195 -195
  124. package/release/_hash.txt +28 -28
  125. package/release/_script-tags.txt +14 -14
  126. package/release/command.json +1 -1
  127. package/release/command.json.js +1 -1
  128. package/release/command_cnako3.json +1 -1
  129. package/release/command_list.json +1 -1
  130. package/release/editor.js +1 -1
  131. package/release/nako_gen_async.js +2 -1
  132. package/release/nako_gen_async.js.LICENSE.txt +35 -0
  133. package/release/plugin_caniuse.js.LICENSE.txt +11 -0
  134. package/release/plugin_csv.js +1 -1
  135. package/release/plugin_csv.js.LICENSE.txt +15 -0
  136. package/release/plugin_datetime.js.LICENSE.txt +15 -0
  137. package/release/plugin_kansuji.js.LICENSE.txt +3 -0
  138. package/release/plugin_markup.js.LICENSE.txt +11 -0
  139. package/release/plugin_turtle.js.LICENSE.txt +15 -0
  140. package/release/plugin_webworker.js.LICENSE.txt +3 -0
  141. package/release/plugin_weykturtle3d.js.LICENSE.txt +3 -0
  142. package/release/stats.json +1 -1
  143. package/release/version.js +1 -1
  144. package/release/wnako3.js +1 -1
  145. package/release/wnako3.js.LICENSE.txt +203 -1
  146. package/release/wnako3webworker.js +2 -1
  147. package/release/wnako3webworker.js.LICENSE.txt +131 -0
  148. package/src/.DS_Store +0 -0
  149. package/src/browsers.txt +12 -11
  150. package/src/browsers_agents.json +2 -2
  151. package/src/browsers_agents.mjs +1 -1
  152. package/src/cnako3.mjs +17 -17
  153. package/src/cnako3.mts +18 -18
  154. package/src/cnako3mod.mjs +682 -707
  155. package/src/cnako3mod.mts +688 -712
  156. package/src/commander_ja.mjs +164 -164
  157. package/src/commander_ja.mts +161 -161
  158. package/src/enako3.mjs +68 -68
  159. package/src/era.mjs +22 -22
  160. package/src/image_turtle-elephant.mjs +2 -2
  161. package/src/image_turtle-panda.mjs +2 -2
  162. package/src/image_turtle64.mjs +2 -2
  163. package/src/index.mjs +9 -9
  164. package/src/index.mts +10 -10
  165. package/src/nako3editorfix.sfd +106 -106
  166. package/src/nako_version.mjs +8 -8
  167. package/src/nako_version.mts +2 -2
  168. package/src/plugin_browser.mjs +213 -213
  169. package/src/plugin_browser.mts +206 -206
  170. package/src/plugin_browser_ajax.mjs +399 -399
  171. package/src/plugin_browser_audio.mjs +109 -109
  172. package/src/plugin_browser_canvas.mjs +449 -449
  173. package/src/plugin_browser_chart.mjs +294 -294
  174. package/src/plugin_browser_color.mjs +49 -49
  175. package/src/plugin_browser_crypto.mjs +26 -26
  176. package/src/plugin_browser_dialog.mjs +53 -53
  177. package/src/plugin_browser_dom_basic.mjs +336 -336
  178. package/src/plugin_browser_dom_event.mjs +193 -193
  179. package/src/plugin_browser_dom_parts.mjs +396 -396
  180. package/src/plugin_browser_geolocation.mjs +51 -51
  181. package/src/plugin_browser_hotkey.mjs +25 -25
  182. package/src/plugin_browser_html.mjs +59 -59
  183. package/src/plugin_browser_in_worker.mjs +45 -45
  184. package/src/plugin_browser_location.mjs +21 -21
  185. package/src/plugin_browser_speech.mjs +111 -111
  186. package/src/plugin_browser_storage.mjs +121 -121
  187. package/src/plugin_browser_system.mjs +31 -31
  188. package/src/plugin_browser_websocket.mjs +73 -73
  189. package/src/plugin_caniuse.mjs +29 -29
  190. package/src/plugin_datetime.mjs +394 -394
  191. package/src/plugin_httpserver.mjs +277 -277
  192. package/src/plugin_httpserver.mts +286 -286
  193. package/src/plugin_kansuji.mjs +224 -224
  194. package/src/plugin_keigo.mjs +55 -55
  195. package/src/plugin_markup.mjs +32 -32
  196. package/src/plugin_node.mjs +1047 -1047
  197. package/src/plugin_node.mts +980 -980
  198. package/src/plugin_turtle.mjs +647 -647
  199. package/src/plugin_webworker.mjs +334 -334
  200. package/src/plugin_weykturtle3d.mjs +1214 -1214
  201. package/src/plugin_worker.mjs +95 -95
  202. package/src/repl.nako3 +63 -63
  203. package/src/wnako3.mjs +12 -12
  204. package/src/wnako3.mts +11 -11
  205. package/src/wnako3_editor.css +215 -215
  206. package/src/wnako3_editor.mjs +1542 -1542
  207. package/src/wnako3_editor.mts +1658 -1657
  208. package/src/wnako3mod.mjs +213 -213
  209. package/src/wnako3mod.mts +214 -214
  210. package/src/wnako3webworker.mjs +69 -69
  211. package/test/.DS_Store +0 -0
  212. package/test/ace_editor/karma.config.js +94 -94
  213. package/test/ace_editor/test/.babelrc.json +3 -3
  214. package/test/ace_editor/test/ace_editor_test.js +178 -178
  215. package/test/ace_editor/test/html/custom_context.html +139 -139
  216. package/test/async/async_basic_test.mjs +122 -122
  217. package/test/browser/.DS_Store +0 -0
  218. package/test/browser/karma.config.js +221 -221
  219. package/test/browser/test/.babelrc.json +3 -3
  220. package/test/browser/test/compare_util.js +50 -50
  221. package/test/browser/test/html/div_basic.html +2 -2
  222. package/test/browser/test/html/event_dom_form.html +4 -4
  223. package/test/browser/test/html/event_dom_scrolldiv.html +5 -5
  224. package/test/browser/test/import_plugin_checker.js +24 -24
  225. package/test/browser/test/plugin_browser_test.js +51 -51
  226. package/test/browser/test/plugin_browser_test_ajax.js +123 -123
  227. package/test/browser/test/plugin_browser_test_color.js +18 -18
  228. package/test/browser/test/plugin_browser_test_dialog.js +72 -72
  229. package/test/browser/test/plugin_browser_test_dom_event.js +598 -598
  230. package/test/browser/test/plugin_browser_test_dom_parts.js +125 -125
  231. package/test/browser/test/plugin_browser_test_system.js +9 -9
  232. package/test/browser/test/plugin_turtle_test.js +817 -817
  233. package/test/browser/test/plugin_webworker_test.js +86 -86
  234. package/test/browser/test/require_test.js +68 -68
  235. package/test/bundled/.DS_Store +0 -0
  236. package/test/bundled/karma.config.base.js +117 -117
  237. package/test/bundled/karma.config.js +86 -86
  238. package/test/bundled/test/.babelrc.json +3 -3
  239. package/test/bundled/test/bundled_test.js +69 -69
  240. package/test/bundled/test/html/custom_context.html +65 -65
  241. package/test/bundled/test/html/custom_debug.html +66 -66
  242. package/test/bundled/test4b.cmd +52 -52
  243. package/test/bundled/test_base/.DS_Store +0 -0
  244. package/test/bundled/test_base/.babelrc.json +3 -3
  245. package/test/bundled/test_base/_checktool_test.js +25 -25
  246. package/test/bundled/test_base/basic_ajax_test.js +56 -56
  247. package/test/bundled/test_base/basic_async_test.js +18 -18
  248. package/test/bundled/test_base/basic_test.js +153 -153
  249. package/test/bundled/test_base/calc_test.js +132 -132
  250. package/test/bundled/test_base/css/browsers_box.css +114 -114
  251. package/test/bundled/test_base/html/custom_context.html +69 -69
  252. package/test/bundled/test_base/html/custom_debug.html +71 -71
  253. package/test/bundled/test_base/js/browsers_box.js +72 -72
  254. package/test/bundled/test_base/plugin_csv_test.js +37 -37
  255. package/test/bundled/test_base/plugin_datetime_test.js +115 -115
  256. package/test/bundled/test_base/plugin_kansuji_test.js +49 -49
  257. package/test/bundled/test_base/plugin_system_test.js +410 -410
  258. package/test/bundled/test_base/plugin_webworker_test.js +53 -53
  259. package/test/bundled/test_base/test_utils.js +191 -191
  260. package/test/common/.DS_Store +0 -0
  261. package/test/common/plugin_browser_test.mjs +22 -22
  262. package/test/common/plugin_browser_ut_audio_test.mjs +108 -108
  263. package/test/common/plugin_browser_ut_color_test.mjs +21 -21
  264. package/test/common/plugin_browser_ut_dialog_test.mjs +100 -100
  265. package/test/common/plugin_browser_ut_html_test.mjs +13 -13
  266. package/test/common/plugin_browser_ut_system_test.mjs +10 -10
  267. package/test/common/plugin_markup_test.mjs +23 -23
  268. package/test/jsconfig.json +19 -19
  269. package/test/karma.config.js +91 -91
  270. package/test/node/.DS_Store +0 -0
  271. package/test/node/async_test.mjs +96 -96
  272. package/test/node/commander_ja_test.mjs +89 -89
  273. package/test/node/error_message_test.mjs +243 -243
  274. package/test/node/kai_test.nako3 +6 -6
  275. package/test/node/node_test.mjs +60 -60
  276. package/test/node/plugin_broken.js.txt +3 -3
  277. package/test/node/plugin_browser_ut_ajax_test.mjs.todo +357 -357
  278. package/test/node/plugin_browser_ut_location_test.mjs +42 -42
  279. package/test/node/plugin_markup_test.mjs +47 -47
  280. package/test/node/plugin_math_test.mjs +45 -45
  281. package/test/node/plugin_node_test.mjs +98 -98
  282. package/test/node/plugin_test.mjs +44 -44
  283. package/test/node/relative_import_test_1.nako3 +1 -1
  284. package/test/node/relative_import_test_2.nako3 +2 -2
  285. package/test/node/require_nako3_test.mjs +67 -67
  286. package/test/node/requiretest.nako3 +4 -4
  287. package/test/node/requiretest_indirect.nako3 +1 -1
  288. package/test/node/requiretest_name.nako3 +5 -5
  289. package/test/node/runtime_error.nako3 +2 -2
  290. package/test/node/scope1.nako3 +10 -10
  291. package/test/node/scope2.nako3 +12 -12
  292. package/test/node/side_effects_test.mjs +39 -39
  293. package/test/node/sjis.txt +5 -5
  294. package/test/node/syntax_error.nako3 +1 -1
  295. package/test/node/wnako3_editor_test.mjs +384 -384
  296. package/tools/.DS_Store +0 -0
  297. package/tools/README.md +12 -12
  298. package/tools/check_new_version.nako3 +25 -25
  299. package/tools/nako3edit/.DS_Store +0 -0
  300. package/tools/nako3edit/a.sqlite3 +0 -0
  301. package/tools/nako3edit/html/.DS_Store +0 -0
  302. package/tools/nako3edit/html/daisyui/LICENSE +22 -22
  303. package/tools/nako3edit/html/daisyui/version_2.14.1 +1 -1
  304. package/tools/nako3edit/html/edit.html +170 -170
  305. package/tools/nako3edit/html/edit_plugin.js +6 -6
  306. package/tools/nako3edit/html/files.html +125 -125
  307. package/tools/nako3edit/html/nako3edit.css +65 -65
  308. package/tools/nako3edit/index.mjs +248 -248
  309. package/tools/nako3server/index.html +10 -10
  310. package/tools/nako3server/index.mjs +116 -116
  311. package/tools/nako3server/index.nako3 +34 -34
  312. package/core/.editorconfig +0 -6
  313. package/core/.eslintrc.cjs +0 -33
  314. package/core/.github/dependabot.yml +0 -7
  315. package/core/.github/workflows/nodejs.yml +0 -37
  316. package/core/.github/workflows/super-linter.yml +0 -61
  317. package/core/.github/workflows/textlint.yml +0 -199
  318. package/core/index.mts +0 -21
  319. package/core/test/array_test.mjs +0 -34
  320. package/core/test/basic_test.mjs +0 -344
  321. package/core/test/calc_test.mjs +0 -140
  322. package/core/test/core_module_test.mjs +0 -23
  323. package/core/test/debug_test.mjs +0 -16
  324. package/core/test/dncl_test.mjs +0 -94
  325. package/core/test/error_message_test.mjs +0 -210
  326. package/core/test/error_test.mjs +0 -16
  327. package/core/test/flow_test.mjs +0 -373
  328. package/core/test/func_call.mjs +0 -160
  329. package/core/test/func_test.mjs +0 -149
  330. package/core/test/indent_test.mjs +0 -364
  331. package/core/test/lex_test.mjs +0 -168
  332. package/core/test/literal_test.mjs +0 -73
  333. package/core/test/nako_lexer_test.mjs +0 -35
  334. package/core/test/nako_logger_test.mjs +0 -76
  335. package/core/test/nako_logger_test.mts +0 -78
  336. package/core/test/plugin_csv_test.mjs +0 -38
  337. package/core/test/plugin_promise_test.mjs +0 -18
  338. package/core/test/plugin_system_test.mjs +0 -630
  339. package/core/test/prepare_test.mjs +0 -96
  340. package/core/test/re_test.mjs +0 -22
  341. package/core/test/side_effects_test.mjs +0 -92
  342. package/core/test/variable_scope_test.mjs +0 -149
  343. package/core/tsconfig.json +0 -101
@@ -1,2439 +1,2441 @@
1
- /**
2
- * nadesiko v3 parser
3
- */
4
- import { opPriority, keizokuJosi, operatorList } from './nako_parser_const.mjs';
5
- import { NakoParserBase } from './nako_parser_base.mjs';
6
- import { NakoSyntaxError } from './nako_errors.mjs';
7
- import { NakoLexer } from './nako_lexer.mjs';
8
- import { NewEmptyToken } from './nako_types.mjs';
9
- /**
10
- * 構文解析を行うクラス
11
- */
12
- export class NakoParser extends NakoParserBase {
13
- /**
14
- * 構文解析を実行する
15
- * @param {TokenWithSourceMap[]} tokens 字句解析済みのトークンの配列
16
- * @param {string} filename 解析対象のモジュール名
17
- * @return {Ast} AST(構文木)
18
- */
19
- parse(tokens, filename) {
20
- this.reset();
21
- this.tokens = tokens;
22
- this.modName = NakoLexer.filenameToModName(filename);
23
- this.modList.push(this.modName);
24
- // 解析開始
25
- return this.startParser();
26
- }
27
- /** パーサーの一番最初に呼び出す構文規則 */
28
- startParser() {
29
- const b = this.ySentenceList();
30
- const c = this.get();
31
- if (c && c.type !== 'eof') {
32
- this.logger.debug(`構文解析でエラー。${this.nodeToStr(c, { depth: 1 }, true)}の使い方が間違っています。`, c);
33
- throw NakoSyntaxError.fromNode(`構文解析でエラー。${this.nodeToStr(c, { depth: 1 }, false)}の使い方が間違っています。`, c);
34
- }
35
- return b;
36
- }
37
- /** 複数文を返す */
38
- ySentenceList() {
39
- const blocks = [];
40
- let line = -1;
41
- const map = this.peekSourceMap();
42
- while (!this.isEOF()) {
43
- const n = this.ySentence();
44
- if (!n) {
45
- break;
46
- }
47
- blocks.push(n);
48
- if (line < 0) {
49
- line = n.line;
50
- }
51
- }
52
- if (blocks.length === 0) {
53
- const token = this.peek() || this.tokens[0];
54
- this.logger.debug('構文解析に失敗:' + this.nodeToStr(this.peek(), { depth: 1 }, true), token);
55
- throw NakoSyntaxError.fromNode('構文解析に失敗:' + this.nodeToStr(this.peek(), { depth: 1 }, false), token);
56
- }
57
- return { type: 'block', block: blocks, ...map, end: this.peekSourceMap(), genMode: this.genMode };
58
- }
59
- yEOL() {
60
- // 行末のチェック #1009
61
- const eol = this.get();
62
- if (!eol) {
63
- return null;
64
- }
65
- // 余剰スタックの確認
66
- if (this.stack.length > 0) {
67
- /** 余剰スタックのレポートを作る */
68
- const words = [];
69
- this.stack.forEach((t) => {
70
- let w = this.nodeToStr(t, { depth: 1 }, false);
71
- if (t.josi) {
72
- w += t.josi;
73
- }
74
- words.push(w);
75
- });
76
- const desc = words.join(',');
77
- // 最近使った関数の使い方レポートを作る #1093
78
- let descFunc = '';
79
- const chA = 'A'.charCodeAt(0);
80
- for (const f of this.recentlyCalledFunc) {
81
- descFunc += ' - ';
82
- let no = 0;
83
- const josiA = f.josi;
84
- if (josiA) {
85
- for (const arg of josiA) {
86
- const ch = String.fromCharCode(chA + no);
87
- descFunc += ch;
88
- if (arg.length === 1) {
89
- descFunc += arg[0];
90
- }
91
- else {
92
- descFunc += `(${arg.join('|')})`;
93
- }
94
- no++;
95
- }
96
- }
97
- descFunc += f.name + '\n';
98
- }
99
- throw NakoSyntaxError.fromNode(`未解決の単語があります: [${desc}]\n次の命令の可能性があります:\n${descFunc}`, eol);
100
- }
101
- this.recentlyCalledFunc = [];
102
- return eol;
103
- }
104
- /** @returns {Ast | null} */
105
- ySentence() {
106
- const map = this.peekSourceMap();
107
- // 最初の語句が決まっている構文
108
- if (this.check('eol')) {
109
- return this.yEOL();
110
- }
111
- if (this.check('もし')) {
112
- return this.yIF();
113
- }
114
- if (this.check('後判定')) {
115
- return this.yAtohantei();
116
- }
117
- if (this.check('エラー監視')) {
118
- return this.yTryExcept();
119
- }
120
- if (this.check('逐次実行')) {
121
- return this.yTikuji();
122
- }
123
- if (this.accept(['抜ける'])) {
124
- return { type: 'break', josi: '', ...map, end: this.peekSourceMap() };
125
- }
126
- if (this.accept(['続ける'])) {
127
- return { type: 'continue', josi: '', ...map, end: this.peekSourceMap() };
128
- }
129
- if (this.accept(['require', 'string', '取込'])) {
130
- return this.yRequire();
131
- }
132
- if (this.accept(['not', '非同期モード'])) {
133
- return this.yASyncMode();
134
- }
135
- if (this.accept(['not', 'DNCLモード'])) {
136
- return this.yDNCLMode();
137
- }
138
- if (this.accept(['not', 'string', 'モード設定'])) {
139
- return this.ySetGenMode(this.y[1].value);
140
- }
141
- // 関数呼び出し演算子
142
- if (this.check2(['func', ''])) {
143
- return this.yCallOp();
144
- }
145
- if (this.check2(['func', 'eq'])) {
146
- const word = this.get() || NewEmptyToken('?', '?', map.line, map.file || '');
147
- throw NakoSyntaxError.fromNode(`関数『${word.value}』に代入できません。『←』を使ってください。`, word);
148
- }
149
- // 先読みして初めて確定する構文
150
- if (this.accept([this.ySpeedMode])) {
151
- return this.y[0];
152
- }
153
- if (this.accept([this.yPerformanceMonitor])) {
154
- return this.y[0];
155
- }
156
- if (this.accept([this.yLet])) {
157
- return this.y[0];
158
- }
159
- if (this.accept([this.yDefTest])) {
160
- return this.y[0];
161
- }
162
- if (this.accept([this.yDefFunc])) {
163
- return this.y[0];
164
- }
165
- // 関数呼び出しの他、各種構文の実装
166
- if (this.accept([this.yCall])) {
167
- const c1 = this.y[0];
168
- if (c1.josi === 'して') { // 連文をblockとして接続する(もし構文、逐次実行構文などのため)
169
- const c2 = this.ySentence();
170
- if (c2 !== null) {
171
- return {
172
- type: 'block',
173
- block: [c1, c2],
174
- josi: c2.josi,
175
- ...map,
176
- end: this.peekSourceMap()
177
- };
178
- }
179
- }
180
- return c1;
181
- }
182
- return null;
183
- }
184
- /** @returns {Ast} */
185
- yASyncMode() {
186
- const map = this.peekSourceMap();
187
- this.genMode = '非同期モード';
188
- return { type: 'eol', ...map, end: this.peekSourceMap() };
189
- }
190
- /** @returns {Ast} */
191
- yDNCLMode() {
192
- const map = this.peekSourceMap();
193
- // 配列インデックスは1から
194
- this.arrayIndexFrom = 1;
195
- // 配列アクセスをJSと逆順で指定する
196
- this.flagReverseArrayIndex = true;
197
- // 配列代入時自動で初期化チェックする
198
- this.flagCheckArrayInit = true;
199
- return { type: 'eol', ...map, end: this.peekSourceMap() };
200
- }
201
- /** @returns {Ast} */
202
- ySetGenMode(mode) {
203
- const map = this.peekSourceMap();
204
- this.genMode = mode;
205
- return { type: 'eol', ...map, end: this.peekSourceMap() };
206
- }
207
- /** @returns {Ast} */
208
- yRequire() {
209
- const nameToken = this.y[1];
210
- const filename = nameToken.value;
211
- const modName = NakoLexer.filenameToModName(filename);
212
- if (this.modList.indexOf(modName) < 0) {
213
- // 優先度が最も高いのは modList[0]
214
- // [memo] モジュールの検索優先度は、下に書くほど高くなる
215
- const modSelf = this.modList.shift();
216
- if (modSelf) {
217
- this.modList.unshift(modName);
218
- this.modList.unshift(modSelf);
219
- }
220
- }
221
- return {
222
- type: 'require',
223
- value: filename,
224
- josi: '',
225
- ...this.peekSourceMap(),
226
- end: this.peekSourceMap()
227
- };
228
- }
229
- /** @returns {Ast} */
230
- yBlock() {
231
- const map = this.peekSourceMap();
232
- const blocks = [];
233
- if (this.check('ここから')) {
234
- this.get();
235
- }
236
- while (!this.isEOF()) {
237
- if (this.checkTypes(['違えば', 'ここまで', 'エラー'])) {
238
- break;
239
- }
240
- if (!this.accept([this.ySentence])) {
241
- break;
242
- }
243
- blocks.push(this.y[0]);
244
- }
245
- return { type: 'block', block: blocks, ...map, end: this.peekSourceMap() };
246
- }
247
- yDefFuncReadArgs() {
248
- if (!this.check('(')) {
249
- return null;
250
- }
251
- const a = [];
252
- this.get(); // skip '('
253
- while (!this.isEOF()) {
254
- if (this.check(')')) {
255
- this.get(); // skip ''
256
- break;
257
- }
258
- const t = this.get();
259
- if (t) {
260
- a.push(t);
261
- }
262
- if (this.check('comma')) {
263
- this.get();
264
- }
265
- }
266
- return a;
267
- }
268
- /** @returns {Ast | null} */
269
- yDefTest() {
270
- return this.yDef('def_test');
271
- }
272
- /** @returns {Ast | null} */
273
- yDefFunc() {
274
- return this.yDef('def_func');
275
- }
276
- /**
277
- * @param {string} type
278
- * @returns {Ast | null}
279
- */
280
- yDef(type) {
281
- if (!this.check(type)) {
282
- return null;
283
- }
284
- const map = this.peekSourceMap();
285
- const def = this.get(); // ●
286
- if (!def) {
287
- return null;
288
- }
289
- let defArgs = [];
290
- if (this.check('(')) {
291
- defArgs = this.yDefFuncReadArgs() || [];
292
- } // // lexerでも解析しているが再度詳しく
293
- const funcName = this.get();
294
- if (!funcName || funcName.type !== 'func') {
295
- this.logger.debug(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, true) + 'の宣言でエラー。', funcName);
296
- throw NakoSyntaxError.fromNode(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, false) + 'の宣言でエラー。', def);
297
- }
298
- if (this.check('(')) {
299
- // 関数引数の二重定義
300
- if (defArgs.length > 0) {
301
- this.logger.debug(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, true) + 'の宣言で、引数定義は名前の前か後に一度だけ可能です。', funcName);
302
- throw NakoSyntaxError.fromNode(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, false) + 'の宣言で、引数定義は名前の前か後に一度だけ可能です。', funcName);
303
- }
304
- defArgs = this.yDefFuncReadArgs() || [];
305
- }
306
- if (this.check('とは')) {
307
- this.get();
308
- }
309
- let block = null;
310
- let multiline = false;
311
- let asyncFn = false;
312
- if (this.check('ここから')) {
313
- multiline = true;
314
- }
315
- if (this.check('eol')) {
316
- multiline = true;
317
- }
318
- try {
319
- this.funcLevel++;
320
- this.usedAsyncFn = false;
321
- // ローカル変数を生成
322
- const backupLocalvars = this.localvars;
323
- this.localvars = { 'それ': { type: 'var', value: '' } };
324
- if (multiline) {
325
- this.saveStack();
326
- // 関数の引数をローカル変数として登録する
327
- for (const arg of defArgs) {
328
- const fnName = (arg.value) ? arg.value + '' : '';
329
- this.localvars[fnName] = { 'type': 'var', 'value': '' };
330
- }
331
- block = this.yBlock();
332
- if (this.check('ここまで')) {
333
- this.get();
334
- }
335
- else {
336
- throw NakoSyntaxError.fromNode('『ここまで』がありません。関数定義の末尾に必要です。', def);
337
- }
338
- this.loadStack();
339
- }
340
- else {
341
- this.saveStack();
342
- block = this.ySentence();
343
- this.loadStack();
344
- }
345
- this.funcLevel--;
346
- asyncFn = this.usedAsyncFn;
347
- this.localvars = backupLocalvars;
348
- }
349
- catch (err) {
350
- this.logger.debug(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, true) +
351
- 'の定義で以下のエラーがありました。\n' + err.message, def);
352
- throw NakoSyntaxError.fromNode(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, false) +
353
- 'の定義で以下のエラーがありました。\n' + err.message, def);
354
- }
355
- return {
356
- type,
357
- name: funcName,
358
- args: defArgs,
359
- block: block || [],
360
- asyncFn,
361
- josi: '',
362
- ...map,
363
- end: this.peekSourceMap()
364
- };
365
- }
366
- /** @returns {Ast | null} */
367
- yIFCond() {
368
- const map = this.peekSourceMap();
369
- let a = this.yGetArg();
370
- if (!a) {
371
- return null;
372
- }
373
- // console.log('yIFCond=', a, this.peek())
374
- // チェック : AがBならば
375
- if (a.josi === 'が') {
376
- const tmpI = this.index;
377
- const b = this.yGetArg();
378
- const naraba = this.get();
379
- if ((b && b.type !== 'func') && (naraba && naraba.type === 'ならば')) {
380
- return {
381
- type: 'op',
382
- operator: (naraba.value === 'でなければ') ? 'noteq' : 'eq',
383
- left: a,
384
- right: b,
385
- josi: '',
386
- ...map,
387
- end: this.peekSourceMap()
388
- };
389
- }
390
- this.index = tmpI;
391
- }
392
- if (a.josi !== '') {
393
- // もし文で関数呼び出しがある場合
394
- this.stack.push(a);
395
- a = this.yCall();
396
- }
397
- // (ならば|でなければ)を確認
398
- if (!this.check('ならば')) {
399
- const smap = a || { type: '?', ...map };
400
- this.logger.debug('もし文で『ならば』がないか、条件が複雑過ぎます。' + this.nodeToStr(this.peek(), { depth: 1 }, false) + 'の直前に『ならば』を書いてください。', smap);
401
- throw NakoSyntaxError.fromNode('もし文で『ならば』がないか、条件が複雑過ぎます。' + this.nodeToStr(this.peek(), { depth: 1 }, false) + 'の直前に『ならば』を書いてください。', smap);
402
- }
403
- const naraba = this.get();
404
- if (naraba && naraba.value === 'でなければ') {
405
- a = {
406
- type: 'not',
407
- value: a,
408
- josi: '',
409
- ...map,
410
- end: this.peekSourceMap()
411
- };
412
- }
413
- return a;
414
- }
415
- /** @returns {Ast | null} */
416
- yIF() {
417
- const map = this.peekSourceMap();
418
- if (!this.check('もし')) {
419
- return null;
420
- }
421
- const mosi = this.get(); // skip もし
422
- if (mosi == null) {
423
- return null;
424
- }
425
- while (this.check('comma')) {
426
- this.get();
427
- } // skip comma
428
- let cond = null;
429
- try {
430
- cond = this.yIFCond();
431
- }
432
- catch (err) {
433
- throw NakoSyntaxError.fromNode('『もし』文の条件で次のエラーがあります。\n' + err.message, mosi);
434
- }
435
- if (cond === null) {
436
- throw NakoSyntaxError.fromNode('『もし』文で条件の指定が空です。', mosi);
437
- }
438
- let trueBlock = null;
439
- let falseBlock = null;
440
- let tanbun = false;
441
- // True Block
442
- if (this.check('eol')) {
443
- trueBlock = this.yBlock();
444
- }
445
- else {
446
- trueBlock = this.ySentence();
447
- tanbun = true;
448
- }
449
- // skip EOL
450
- while (this.check('eol')) {
451
- this.get();
452
- }
453
- // Flase Block
454
- if (this.check('違えば')) {
455
- this.get(); // skip 違えば
456
- while (this.check('comma')) {
457
- this.get();
458
- }
459
- if (this.check('eol')) {
460
- falseBlock = this.yBlock();
461
- }
462
- else {
463
- falseBlock = this.ySentence();
464
- tanbun = true;
465
- }
466
- }
467
- if (tanbun === false) {
468
- if (this.check('ここまで')) {
469
- this.get();
470
- }
471
- else {
472
- throw NakoSyntaxError.fromNode('『もし』文で『ここまで』がありません。', mosi);
473
- }
474
- }
475
- return {
476
- type: 'if',
477
- expr: cond || [],
478
- block: trueBlock || [],
479
- false_block: falseBlock || [],
480
- josi: '',
481
- ...map,
482
- end: this.peekSourceMap()
483
- };
484
- }
485
- ySpeedMode() {
486
- const map = this.peekSourceMap();
487
- if (!this.check2(['string', '実行速度優先'])) {
488
- return null;
489
- }
490
- const optionNode = this.get();
491
- this.get();
492
- let val = '';
493
- if (optionNode && optionNode.value) {
494
- val = optionNode.value;
495
- }
496
- else {
497
- return null;
498
- }
499
- const options = { 行番号無し: false, 暗黙の型変換無し: false, 強制ピュア: false, それ無効: false };
500
- for (const name of val.split('/')) {
501
- // 全て有効化
502
- if (name === '全て') {
503
- for (const k of Object.keys(options)) {
504
- options[k] = true;
505
- }
506
- break;
507
- }
508
- // 個別に有効化
509
- if (Object.keys(options).includes(name)) {
510
- options[name] = true;
511
- }
512
- else {
513
- // 互換性を考えて、警告に留める。
514
- this.logger.warn(`実行速度優先文のオプション『${name}』は存在しません。`, optionNode);
515
- }
516
- }
517
- let multiline = false;
518
- if (this.check('ここから')) {
519
- this.get();
520
- multiline = true;
521
- }
522
- else if (this.check('eol')) {
523
- multiline = true;
524
- }
525
- let block = null;
526
- if (multiline) {
527
- block = this.yBlock();
528
- if (this.check('ここまで')) {
529
- this.get();
530
- }
531
- }
532
- else {
533
- block = this.ySentence();
534
- }
535
- return {
536
- type: 'speed_mode',
537
- options,
538
- block: block || [],
539
- josi: '',
540
- ...map
541
- };
542
- }
543
- yPerformanceMonitor() {
544
- const map = this.peekSourceMap();
545
- if (!this.check2(['string', 'パフォーマンスモニタ適用'])) {
546
- return null;
547
- }
548
- const optionNode = this.get();
549
- if (!optionNode) {
550
- return null;
551
- }
552
- this.get();
553
- const options = { ユーザ関数: false, システム関数本体: false, システム関数: false };
554
- for (const name of optionNode.value.split('/')) {
555
- // 全て有効化
556
- if (name === '全て') {
557
- for (const k of Object.keys(options)) {
558
- options[k] = true;
559
- }
560
- break;
561
- }
562
- // 個別に有効化
563
- if (Object.keys(options).includes(name)) {
564
- options[name] = true;
565
- }
566
- else {
567
- // 互換性を考えて、警告に留める。
568
- this.logger.warn(`パフォーマンスモニタ適用文のオプション『${name}』は存在しません。`, optionNode);
569
- }
570
- }
571
- let multiline = false;
572
- if (this.check('ここから')) {
573
- this.get();
574
- multiline = true;
575
- }
576
- else if (this.check('eol')) {
577
- multiline = true;
578
- }
579
- let block = null;
580
- if (multiline) {
581
- block = this.yBlock();
582
- if (this.check('ここまで')) {
583
- this.get();
584
- }
585
- }
586
- else {
587
- block = this.ySentence();
588
- }
589
- return {
590
- type: 'performance_monitor',
591
- options,
592
- block: block || [],
593
- josi: '',
594
- ...map
595
- };
596
- }
597
- /** (非推奨) 「逐次実行」構文 @returns {Ast | null} */
598
- yTikuji() {
599
- const map = this.peekSourceMap();
600
- if (!this.check('逐次実行')) {
601
- return null;
602
- }
603
- const tikuji = this.getCur(); // skip 逐次実行
604
- this.logger.warn('『逐次実行』構文の使用は非推奨になりました(https://nadesi.com/v3/doc/go.php?944)。', tikuji);
605
- const blocks = [];
606
- let errorBlock = null;
607
- if (!tikuji || !this.check('eol')) {
608
- throw NakoSyntaxError.fromNode('『逐次実行』の直後は改行が必要です。', tikuji);
609
- }
610
- // ブロックを読む
611
- for (;;) {
612
- if (this.check('ここまで')) {
613
- break;
614
- }
615
- if (this.check('eol')) {
616
- this.get(); // skip EOL
617
- continue;
618
- }
619
- if (this.check2(['エラー', 'ならば'])) {
620
- this.get(); // skip エラー
621
- this.get(); // skip ならば
622
- errorBlock = this.yBlock();
623
- break;
624
- }
625
- let block = null;
626
- // 「先に」「次に」句はブロック宣言 #717 (ただしブロック以外も可能)
627
- if (this.check('先に') || this.check('次に')) {
628
- const tugini = this.get(); // skip 先に | 次に
629
- if (this.check('comma')) {
630
- this.get();
631
- }
632
- if (this.check('eol')) { // block
633
- block = this.yBlock();
634
- if (!this.check('ここまで')) {
635
- let tuginiType = '次に';
636
- if (tugini != null) {
637
- tuginiType = tugini.type;
638
- }
639
- throw NakoSyntaxError.fromNode(`『${tuginiType}』...『ここまで』を対応させてください。`, map);
640
- }
641
- this.get(); // skip 'ここまで'
642
- }
643
- else { // line
644
- block = this.ySentence();
645
- }
646
- }
647
- else {
648
- block = this.ySentence();
649
- }
650
- // add block
651
- if (block != null) {
652
- blocks.push(block);
653
- }
654
- }
655
- if (!this.check('ここまで')) {
656
- console.log(blocks, this.peek());
657
- throw NakoSyntaxError.fromNode('『逐次実行』...『ここまで』を対応させてください。', tikuji);
658
- }
659
- this.get(); // skip 'ここまで'
660
- return {
661
- type: 'tikuji',
662
- blocks: blocks || [],
663
- errorBlock: errorBlock || [],
664
- josi: '',
665
- ...map,
666
- end: this.peekSourceMap()
667
- };
668
- }
669
- /**
670
- * 1つ目の値を与え、その後に続く計算式を取得し、優先規則に沿って並び替えして戻す
671
- * @param {Ast} firstValue
672
- */
673
- yGetArgOperator(firstValue) {
674
- const args = [firstValue];
675
- while (!this.isEOF()) {
676
- // 演算子がある?
677
- let op = this.peek();
678
- if (op && opPriority[op.type]) {
679
- op = this.getCur();
680
- args.push(op);
681
- // 演算子後の値を取得
682
- const v = this.yValue();
683
- if (v === null) {
684
- throw NakoSyntaxError.fromNode(`計算式で演算子『${op.value}』後に値がありません`, firstValue);
685
- }
686
- args.push(v);
687
- continue;
688
- }
689
- break;
690
- }
691
- if (args.length === 0) {
692
- return null;
693
- }
694
- if (args.length === 1) {
695
- return args[0];
696
- }
697
- return this.infixToAST(args);
698
- }
699
- yGetArg() {
700
- // 値を一つ読む
701
- const value1 = this.yValue();
702
- if (value1 === null) {
703
- return null;
704
- }
705
- // 計算式がある場合を考慮
706
- return this.yGetArgOperator(value1);
707
- }
708
- infixToPolish(list) {
709
- // 中間記法から逆ポーランドに変換
710
- const priority = (t) => {
711
- if (opPriority[t.type]) {
712
- return opPriority[t.type];
713
- }
714
- return 10;
715
- };
716
- const stack = [];
717
- const polish = [];
718
- while (list.length > 0) {
719
- const t = list.shift();
720
- if (!t) {
721
- break;
722
- }
723
- while (stack.length > 0) { // 優先順位を見て移動する
724
- const sTop = stack[stack.length - 1];
725
- if (priority(t) > priority(sTop)) {
726
- break;
727
- }
728
- const tpop = stack.pop();
729
- if (!tpop) {
730
- this.logger.error('計算式に間違いがあります。', t);
731
- break;
732
- }
733
- polish.push(tpop);
734
- }
735
- stack.push(t);
736
- }
737
- // 残った要素を積み替える
738
- while (stack.length > 0) {
739
- const t = stack.pop();
740
- if (t) {
741
- polish.push(t);
742
- }
743
- }
744
- return polish;
745
- }
746
- /** @returns {Ast | null} */
747
- infixToAST(list) {
748
- if (list.length === 0) {
749
- return null;
750
- }
751
- // 逆ポーランドを構文木に
752
- const josi = list[list.length - 1].josi;
753
- const node = list[list.length - 1];
754
- const polish = this.infixToPolish(list);
755
- /** @type {Ast[]} */
756
- const stack = [];
757
- for (const t of polish) {
758
- if (!opPriority[t.type]) { // 演算子ではない
759
- stack.push(t);
760
- continue;
761
- }
762
- const b = stack.pop();
763
- const a = stack.pop();
764
- if (a === undefined || b === undefined) {
765
- this.logger.debug('--- 計算式(逆ポーランド) ---\n' + JSON.stringify(polish));
766
- throw NakoSyntaxError.fromNode('計算式でエラー', node);
767
- }
768
- /** @type {Ast} */
769
- const op = {
770
- type: 'op',
771
- operator: t.type,
772
- left: a,
773
- right: b,
774
- josi,
775
- startOffset: a.startOffset,
776
- endOffset: a.endOffset,
777
- line: a.line,
778
- column: a.column,
779
- file: a.file
780
- };
781
- stack.push(op);
782
- }
783
- const ans = stack.pop();
784
- if (!ans) {
785
- return null;
786
- }
787
- return ans;
788
- }
789
- yGetArgParen(y) {
790
- let isClose = false;
791
- const si = this.stack.length;
792
- while (!this.isEOF()) {
793
- if (this.check(')')) {
794
- isClose = true;
795
- break;
796
- }
797
- const v = this.yGetArg();
798
- if (v) {
799
- this.pushStack(v);
800
- if (this.check('comma')) {
801
- this.get();
802
- }
803
- continue;
804
- }
805
- break;
806
- }
807
- if (!isClose) {
808
- throw NakoSyntaxError.fromNode(`C風関数『${y[0].value}』でカッコが閉じていません`, y[0]);
809
- }
810
- const a = [];
811
- while (si < this.stack.length) {
812
- const v = this.popStack();
813
- if (v) {
814
- a.unshift(v);
815
- }
816
- }
817
- return a;
818
- }
819
- /** @returns {Ast | null} */
820
- yRepeatTime() {
821
- const map = this.peekSourceMap();
822
- if (!this.check('回')) {
823
- return null;
824
- }
825
- this.get(); // skip '回'
826
- if (this.check('comma')) {
827
- this.get();
828
- } // skip comma
829
- if (this.check('繰返')) {
830
- this.get();
831
- } // skip 'N回、繰り返す' (#924)
832
- let num = this.popStack([]);
833
- let multiline = false;
834
- let block = null;
835
- if (num === null) {
836
- num = { type: 'word', value: 'それ', josi: '', ...map, end: this.peekSourceMap() };
837
- }
838
- if (this.check('comma')) {
839
- this.get();
840
- }
841
- if (this.check('ここから')) {
842
- this.get();
843
- multiline = true;
844
- }
845
- else if (this.check('eol')) {
846
- multiline = true;
847
- }
848
- if (multiline) { // multiline
849
- block = this.yBlock();
850
- if (this.check('ここまで')) {
851
- this.get();
852
- }
853
- else {
854
- throw NakoSyntaxError.fromNode('『ここまで』がありません。『回』...『ここまで』を対応させてください。', map);
855
- }
856
- }
857
- else {
858
- // singleline
859
- block = this.ySentence();
860
- }
861
- return {
862
- type: 'repeat_times',
863
- value: num,
864
- block: block || [],
865
- josi: '',
866
- ...map,
867
- end: this.peekSourceMap()
868
- };
869
- }
870
- /** @returns {Ast | null} */
871
- yWhile() {
872
- const map = this.peekSourceMap();
873
- if (!this.check('間')) {
874
- return null;
875
- }
876
- this.get(); // skip ''
877
- while (this.check('comma')) {
878
- this.get();
879
- } // skip ','
880
- if (this.check('繰返')) {
881
- this.get();
882
- } // skip '繰り返す' #927
883
- const cond = this.popStack();
884
- if (cond === null) {
885
- throw NakoSyntaxError.fromNode('『間』で条件がありません。', map);
886
- }
887
- if (this.check('comma')) {
888
- this.get();
889
- }
890
- if (!this.checkTypes(['ここから', 'eol'])) {
891
- throw NakoSyntaxError.fromNode('『間』の直後は改行が必要です', map);
892
- }
893
- const block = this.yBlock();
894
- if (this.check('ここまで')) {
895
- this.get();
896
- }
897
- return {
898
- type: 'while',
899
- cond,
900
- block,
901
- josi: '',
902
- ...map,
903
- end: this.peekSourceMap()
904
- };
905
- }
906
- /** @returns {Ast | null} */
907
- yAtohantei() {
908
- const map = this.peekSourceMap();
909
- if (this.check('後判定')) {
910
- this.get();
911
- } // skip 後判定
912
- if (this.check('繰返')) {
913
- this.get();
914
- } // skip 繰り返す
915
- if (this.check('ここから')) {
916
- this.get();
917
- }
918
- const block = this.yBlock();
919
- if (this.check('ここまで')) {
920
- this.get();
921
- }
922
- if (this.check('comma')) {
923
- this.get();
924
- }
925
- let cond = this.yGetArg(); // 条件
926
- let bUntil = false;
927
- const t = this.peek();
928
- if (t && t.value === 'なる' && (t.josi === 'まで' || t.josi === 'までの')) {
929
- this.get(); // skip なるまで
930
- bUntil = true;
931
- }
932
- if (this.check('間')) {
933
- this.get();
934
- } // skip 間
935
- if (bUntil) { // 条件を反転する
936
- cond = {
937
- type: 'not',
938
- value: cond,
939
- josi: '',
940
- ...map,
941
- end: this.peekSourceMap()
942
- };
943
- }
944
- return {
945
- type: 'atohantei',
946
- cond: cond || [],
947
- block,
948
- josi: '',
949
- ...map,
950
- end: this.peekSourceMap()
951
- };
952
- }
953
- /** @returns {Ast | null} */
954
- yFor() {
955
- const map = this.peekSourceMap();
956
- if (this.check('繰返') || this.check('増繰返') || this.check('減繰返')) {
957
- // pass
958
- }
959
- else {
960
- return null;
961
- }
962
- const kurikaesu = this.getCur(); // skip 繰り返す
963
- // スタックに(増や|減ら)してがある?
964
- const incdec = this.stack.pop();
965
- if (incdec) {
966
- if (incdec.type === 'word' && (incdec.value === '増' || incdec.value === '減')) {
967
- kurikaesu.type = incdec.value + kurikaesu.type;
968
- // ↑ typeを増繰返 | 減繰返 に変換
969
- }
970
- else {
971
- // 普通の繰り返しの場合
972
- this.stack.push(incdec); // 違ったので改めて追加
973
- }
974
- }
975
- let vInc = null;
976
- if (kurikaesu.type === '増繰返' || kurikaesu.type === '減繰返') {
977
- vInc = this.popStack(['ずつ']);
978
- }
979
- const vTo = this.popStack(['まで']);
980
- const vFrom = this.popStack(['から']);
981
- const word = this.popStack(['を', 'で']);
982
- if (vFrom === null || vTo === null) {
983
- throw NakoSyntaxError.fromNode('『繰り返す』文でAからBまでの指定がありません。', kurikaesu);
984
- }
985
- if (this.check('comma')) {
986
- this.get();
987
- } // skip comma
988
- let multiline = false;
989
- if (this.check('ここから')) {
990
- multiline = true;
991
- this.get();
992
- }
993
- else if (this.check('eol')) {
994
- multiline = true;
995
- this.get();
996
- }
997
- let block = null;
998
- if (multiline) {
999
- block = this.yBlock();
1000
- if (this.check('ここまで')) {
1001
- this.get();
1002
- }
1003
- else {
1004
- throw NakoSyntaxError.fromNode('『ここまで』がありません。『繰り返す』...『ここまで』を対応させてください。', map);
1005
- }
1006
- }
1007
- else {
1008
- block = this.ySentence();
1009
- }
1010
- return {
1011
- type: 'for',
1012
- from: vFrom,
1013
- to: vTo,
1014
- inc: vInc,
1015
- word,
1016
- block: block || [],
1017
- josi: '',
1018
- ...map,
1019
- end: this.peekSourceMap()
1020
- };
1021
- }
1022
- /** @returns {Ast | null} */
1023
- yReturn() {
1024
- const map = this.peekSourceMap();
1025
- if (!this.check('戻る')) {
1026
- return null;
1027
- }
1028
- this.get(); // skip '戻る'
1029
- const v = this.popStack(['で', 'を']);
1030
- if (this.stack.length > 0) {
1031
- throw NakoSyntaxError.fromNode('『戻』文の直前に未解決の引数があります。『(式)を戻す』のように式をカッコで括ってください。', map);
1032
- }
1033
- return {
1034
- type: 'return',
1035
- value: v,
1036
- josi: '',
1037
- ...map,
1038
- end: this.peekSourceMap()
1039
- };
1040
- }
1041
- /** @returns {Ast | null} */
1042
- yForEach() {
1043
- const map = this.peekSourceMap();
1044
- if (!this.check('反復')) {
1045
- return null;
1046
- }
1047
- this.get(); // skip '反復'
1048
- while (this.check('comma')) {
1049
- this.get();
1050
- } // skip ','
1051
- const target = this.popStack(['を']);
1052
- const name = this.popStack(['']);
1053
- let block = null;
1054
- let multiline = false;
1055
- if (this.check('ここから')) {
1056
- multiline = true;
1057
- this.get();
1058
- }
1059
- else if (this.check('eol')) {
1060
- multiline = true;
1061
- }
1062
- if (multiline) {
1063
- block = this.yBlock();
1064
- if (this.check('ここまで')) {
1065
- this.get();
1066
- }
1067
- }
1068
- else {
1069
- block = this.ySentence();
1070
- }
1071
- return {
1072
- type: 'foreach',
1073
- name,
1074
- target,
1075
- block: block || [],
1076
- josi: '',
1077
- ...map,
1078
- end: this.peekSourceMap()
1079
- };
1080
- }
1081
- /** 条件分岐構文 */
1082
- ySwitch() {
1083
- const map = this.peekSourceMap();
1084
- if (!this.check('条件分岐')) {
1085
- return null;
1086
- }
1087
- const joukenbunki = this.get(); // skip '条件分岐'
1088
- if (!joukenbunki) {
1089
- return null;
1090
- }
1091
- const eol = this.get(); // skip 'eol'
1092
- if (!eol) {
1093
- return null;
1094
- }
1095
- const value = this.popStack(['で']);
1096
- if (!value) {
1097
- throw NakoSyntaxError.fromNode('『(値)で条件分岐』のように記述してください。', joukenbunki);
1098
- }
1099
- if (eol.type !== 'eol') {
1100
- throw NakoSyntaxError.fromNode('『条件分岐』の直後は改行してください。', joukenbunki);
1101
- }
1102
- let isDefaultClause = false; // 「違えば」内かどうか
1103
- let skippedKokomade = false;
1104
- const cases = [];
1105
- while (!this.isEOF()) {
1106
- if (this.check('ここまで')) {
1107
- if (skippedKokomade) {
1108
- throw NakoSyntaxError.fromNode('『条件分岐』は『(条件)ならば〜ここまで』と記述してください。', joukenbunki);
1109
- }
1110
- this.get(); // skip ここまで
1111
- break;
1112
- }
1113
- if (this.check('eol')) {
1114
- this.get();
1115
- continue;
1116
- }
1117
- if (isDefaultClause) {
1118
- throw NakoSyntaxError.fromNode('『条件分岐』で『違えば〜ここまで』の後に処理を続けることは出来ません。', joukenbunki);
1119
- }
1120
- // 違えば?
1121
- let cond = null;
1122
- const condToken = this.peek();
1123
- if (condToken && condToken.type === '違えば') {
1124
- // 違えば
1125
- skippedKokomade = false;
1126
- isDefaultClause = true;
1127
- cond = this.get(); // skip 違えば
1128
- if (this.check('comma')) {
1129
- this.get();
1130
- } // skip ','
1131
- }
1132
- else {
1133
- // ***ならば
1134
- if (skippedKokomade) {
1135
- throw NakoSyntaxError.fromNode('『条件分岐』は『(条件)ならば〜ここまで』と記述してください。', joukenbunki);
1136
- }
1137
- // 「**ならば」を得る
1138
- cond = this.yValue();
1139
- if (!cond) {
1140
- throw NakoSyntaxError.fromNode('『条件分岐』は『(条件)ならば〜ここまで』と記述してください。', joukenbunki);
1141
- }
1142
- const naraba = this.get(); // skip ならば
1143
- if (!naraba || naraba.type !== 'ならば') {
1144
- throw NakoSyntaxError.fromNode('『条件分岐』で条件は**ならばと記述してください。', joukenbunki);
1145
- }
1146
- if (this.check('comma')) {
1147
- this.get();
1148
- } // skip ','
1149
- }
1150
- // 条件にあったときに実行すること
1151
- const condBlock = this.yBlock();
1152
- const kokomade = this.peek();
1153
- if (kokomade && kokomade.type === 'ここまで') {
1154
- this.get(); // skip ここまで
1155
- }
1156
- else {
1157
- if (isDefaultClause) {
1158
- throw NakoSyntaxError.fromNode('『条件分岐』は『違えば〜ここまで』と記述してください。', joukenbunki);
1159
- }
1160
- // 次が「違えば」の場合に限り、「もし〜ここまで」の「ここまで」を省略できる
1161
- skippedKokomade = true;
1162
- }
1163
- cases.push([cond, condBlock]);
1164
- }
1165
- return {
1166
- type: 'switch',
1167
- value,
1168
- cases: cases || [],
1169
- josi: '',
1170
- ...map,
1171
- end: this.peekSourceMap()
1172
- };
1173
- }
1174
- /** 無名関数 */
1175
- yMumeiFunc() {
1176
- const map = this.peekSourceMap();
1177
- if (!this.check('def_func')) {
1178
- return null;
1179
- }
1180
- const def = this.get();
1181
- if (!def) {
1182
- return null;
1183
- }
1184
- let args = [];
1185
- // 「,」を飛ばす
1186
- if (this.check('comma')) {
1187
- this.get();
1188
- }
1189
- // 関数の引数定義は省略できる
1190
- if (this.check('(')) {
1191
- args = this.yDefFuncReadArgs() || [];
1192
- }
1193
- // 「,」を飛ばす
1194
- if (this.check('comma')) {
1195
- this.get();
1196
- }
1197
- // ブロックを読む
1198
- this.funcLevel++;
1199
- this.saveStack();
1200
- const block = this.yBlock();
1201
- // 末尾の「ここまで」をチェック - もしなければエラーにする #1045
1202
- if (!this.check('ここまで')) {
1203
- throw NakoSyntaxError.fromNode('『ここまで』がありません。『には』構文か無名関数の末尾に『ここまで』が必要です。', map);
1204
- }
1205
- this.get(); // skip ここまで
1206
- this.loadStack();
1207
- this.funcLevel--;
1208
- return {
1209
- type: 'func_obj',
1210
- args,
1211
- block,
1212
- meta: def.meta,
1213
- josi: '',
1214
- ...map,
1215
- end: this.peekSourceMap()
1216
- };
1217
- }
1218
- /** 代入構文 */
1219
- yDainyu() {
1220
- const map = this.peekSourceMap();
1221
- const dainyu = this.get(); // 代入
1222
- if (dainyu === null) {
1223
- return null;
1224
- }
1225
- const value = this.popStack(['']);
1226
- const word = this.popStack(['へ', 'に']);
1227
- if (!word || (word.type !== 'word' && word.type !== 'func' && word.type !== '配列参照')) {
1228
- throw NakoSyntaxError.fromNode('代入文で代入先の変数が見当たりません。『(変数名)に(値)を代入』のように使います。', dainyu);
1229
- }
1230
- // 配列への代入
1231
- if (word.type === '配列参照') {
1232
- return {
1233
- type: 'let_array',
1234
- name: word.name,
1235
- index: word.index,
1236
- value,
1237
- josi: '',
1238
- checkInit: this.flagCheckArrayInit,
1239
- ...map,
1240
- end: this.peekSourceMap()
1241
- };
1242
- }
1243
- // 一般的な変数への代入
1244
- const word2 = this.getVarName(word);
1245
- return {
1246
- type: 'let',
1247
- name: word2,
1248
- value,
1249
- josi: '',
1250
- ...map,
1251
- end: this.peekSourceMap()
1252
- };
1253
- }
1254
- /** 定める構文 */
1255
- ySadameru() {
1256
- const map = this.peekSourceMap();
1257
- const sadameru = this.get(); // 定める
1258
- if (sadameru === null) {
1259
- return null;
1260
- }
1261
- const word = this.popStack(['を']);
1262
- const value = this.popStack(['へ', 'に']);
1263
- if (!word || (word.type !== 'word' && word.type !== 'func' && word.type !== '配列参照')) {
1264
- throw NakoSyntaxError.fromNode('『定める』文で定数が見当たりません。『(定数名)を(値)に定める』のように使います。', sadameru);
1265
- }
1266
- // 変数を生成する
1267
- const nameToken = this.getVarName(word);
1268
- return {
1269
- type: 'def_local_var',
1270
- name: nameToken,
1271
- vartype: '定数',
1272
- value,
1273
- josi: '',
1274
- ...map,
1275
- end: this.peekSourceMap()
1276
- };
1277
- }
1278
- yIncDec() {
1279
- const map = this.peekSourceMap();
1280
- const action = this.get(); // (増やす|減らす)
1281
- if (action === null) {
1282
- return null;
1283
- }
1284
- // 『Nずつ増やして繰り返す』文か?
1285
- if (this.check('繰返')) {
1286
- this.pushStack({ type: 'word', value: action.value, josi: action.josi, ...map, end: this.peekSourceMap() });
1287
- return this.yFor();
1288
- }
1289
- // スタックから引数をポップ
1290
- let value = this.popStack(['だけ', '']);
1291
- if (!value) {
1292
- value = { type: 'number', value: 1, josi: 'だけ', ...map, end: this.peekSourceMap() };
1293
- }
1294
- const word = this.popStack(['を']);
1295
- if (!word || (word.type !== 'word' && word.type !== '配列参照')) {
1296
- throw NakoSyntaxError.fromNode(`『${action.type}』文で定数が見当たりません。『(変数名)を(値)だけ${action.type}』のように使います。`, action);
1297
- }
1298
- // 減らすなら-1かける
1299
- if (action.value === '減') {
1300
- value = { type: 'op', operator: '*', left: value, right: { type: 'number', value: -1, line: action.line }, josi: '', ...map };
1301
- }
1302
- return {
1303
- type: 'inc',
1304
- name: word,
1305
- value,
1306
- josi: action.josi,
1307
- ...map,
1308
- end: this.peekSourceMap()
1309
- };
1310
- }
1311
- yCall() {
1312
- if (this.isEOF()) {
1313
- return null;
1314
- }
1315
- // スタックに積んでいく
1316
- while (!this.isEOF()) {
1317
- if (this.check('ここから')) {
1318
- this.get();
1319
- }
1320
- // 代入
1321
- if (this.check('代入')) {
1322
- return this.yDainyu();
1323
- }
1324
- if (this.check('定める')) {
1325
- return this.ySadameru();
1326
- }
1327
- // 制御構文
1328
- if (this.check('')) {
1329
- return this.yRepeatTime();
1330
- }
1331
- if (this.check('')) {
1332
- return this.yWhile();
1333
- }
1334
- if (this.check('繰返') || this.check('増繰返') || this.check('減繰返')) {
1335
- return this.yFor();
1336
- }
1337
- if (this.check('反復')) {
1338
- return this.yForEach();
1339
- }
1340
- if (this.check('条件分岐')) {
1341
- return this.ySwitch();
1342
- }
1343
- if (this.check('戻る')) {
1344
- return this.yReturn();
1345
- }
1346
- if (this.check('増') || this.check('減')) {
1347
- return this.yIncDec();
1348
- }
1349
- // C言語風関数
1350
- if (this.check2([['func', 'word'], '('])) { // C言語風
1351
- const cur = this.peek();
1352
- if (cur && cur.josi === '') {
1353
- const t = this.yValue();
1354
- if (t) {
1355
- const josi = t.josi || '';
1356
- if (t.type === 'func' && (t.josi === '' || keizokuJosi.indexOf(josi) >= 0)) {
1357
- t.josi = '';
1358
- return t; // 関数なら値とする
1359
- }
1360
- this.pushStack(t);
1361
- }
1362
- if (this.check('comma')) {
1363
- this.get();
1364
- }
1365
- continue;
1366
- }
1367
- }
1368
- // なでしこ式関数
1369
- if (this.check('func')) {
1370
- const r = this.yCallFunc();
1371
- if (r === null) {
1372
- continue;
1373
- }
1374
- // 「〜する間」の形ならスタックに積む。
1375
- if (this.check('間')) {
1376
- this.pushStack(r);
1377
- continue;
1378
- }
1379
- // 関数呼び出しの直後に、四則演算があるか?
1380
- if (!this.checkTypes(operatorList)) {
1381
- return r;
1382
- } // なければ関数呼び出しを戻す
1383
- // 四則演算があった場合、計算してスタックに載せる
1384
- this.pushStack(this.yGetArgOperator(r));
1385
- continue;
1386
- }
1387
- // 値のとき → スタックに載せる
1388
- const t = this.yGetArg();
1389
- if (t) {
1390
- this.pushStack(t);
1391
- continue;
1392
- }
1393
- break;
1394
- } // end of while
1395
- // 助詞が余ってしまった場合
1396
- if (this.stack.length > 0) {
1397
- this.logger.debug('--- stack dump ---\n' + JSON.stringify(this.stack, null, 2) + '\npeek: ' + JSON.stringify(this.peek(), null, 2));
1398
- let msgDebug = `不完全な文です。${this.stack.map((n) => this.nodeToStr(n, { depth: 0 }, true)).join('、')}が解決していません。`;
1399
- let msg = `不完全な文です。${this.stack.map((n) => this.nodeToStr(n, { depth: 0 }, false)).join('、')}が解決していません。`;
1400
- // 各ノードについて、更に詳細な情報があるなら表示
1401
- for (const n of this.stack) {
1402
- const d0 = this.nodeToStr(n, { depth: 0 }, false);
1403
- const d1 = this.nodeToStr(n, { depth: 1 }, false);
1404
- if (d0 !== d1) {
1405
- msgDebug += `${this.nodeToStr(n, { depth: 0 }, true)}は${this.nodeToStr(n, { depth: 1 }, true)}として使われています。`;
1406
- msg += `${d0}は${d1}として使われています。`;
1407
- }
1408
- }
1409
- const first = this.stack[0];
1410
- const last = this.stack[this.stack.length - 1];
1411
- this.logger.debug(msgDebug, first);
1412
- throw NakoSyntaxError.fromNode(msg, first, last);
1413
- }
1414
- return this.popStack([]);
1415
- }
1416
- /** @returns {Ast | null} */
1417
- yCallFunc() {
1418
- const map = this.peekSourceMap();
1419
- const t = this.get();
1420
- if (!t) {
1421
- return null;
1422
- }
1423
- const f = t.meta;
1424
- const funcName = t.value;
1425
- // (関数)には ... 構文 ... https://github.com/kujirahand/nadesiko3/issues/66
1426
- let funcObj = null;
1427
- if (t.josi === 'には') {
1428
- try {
1429
- funcObj = this.yMumeiFunc();
1430
- }
1431
- catch (err) {
1432
- throw NakoSyntaxError.fromNode(`『${t.value}には...』で無名関数の定義で以下の間違いがあります。\n${err.message}`, t);
1433
- }
1434
- if (funcObj === null) {
1435
- throw NakoSyntaxError.fromNode('『Fには』構文がありましたが、関数定義が見当たりません。', t);
1436
- }
1437
- }
1438
- if (!f || typeof f.josi === 'undefined') {
1439
- throw NakoSyntaxError.fromNode('関数の定義でエラー。', t);
1440
- }
1441
- // 最近使った関数を記録
1442
- this.recentlyCalledFunc.push({ name: funcName, ...f });
1443
- // 呼び出す関数が非同期呼び出しが必要(asyncFn)ならマーク
1444
- if (f && f.asyncFn) {
1445
- this.usedAsyncFn = true;
1446
- }
1447
- // 関数の引数を取り出す処理
1448
- const args = [];
1449
- let nullCount = 0;
1450
- let valueCount = 0;
1451
- for (let i = 0; i < f.josi.length; i++) {
1452
- while (true) {
1453
- // スタックから任意の助詞を持つ値を一つ取り出す、助詞がなければ末尾から得る
1454
- let popArg = this.popStack(f.josi[i]);
1455
- if (popArg !== null) {
1456
- valueCount++;
1457
- }
1458
- else if (i < f.josi.length - 1 || !f.isVariableJosi) {
1459
- nullCount++;
1460
- popArg = funcObj;
1461
- }
1462
- else {
1463
- break;
1464
- }
1465
- if (popArg !== null && f.funcPointers !== undefined && f.funcPointers[i] !== null) {
1466
- if (popArg.type === 'func') { // 引数が関数の参照渡しに該当する場合、typeを『func_pointer』に変更
1467
- popArg.type = 'func_pointer';
1468
- }
1469
- else {
1470
- const varname = (f.varnames) ? f.varnames[i] : `${i + 1}番目の引数`;
1471
- throw NakoSyntaxError.fromNode(`関数『${t.value}』の引数『${varname}』には関数オブジェクトが必要です。`, t);
1472
- }
1473
- }
1474
- args.push(popArg);
1475
- if (i < f.josi.length - 1 || !f.isVariableJosi) {
1476
- break;
1477
- }
1478
- }
1479
- }
1480
- // 1つだけなら、変数「それ」で補完される
1481
- if (nullCount >= 2 && (valueCount > 0 || t.josi === '' || keizokuJosi.indexOf(t.josi) >= 0)) {
1482
- throw NakoSyntaxError.fromNode(`関数『${t.value}』の引数が不足しています。`, t);
1483
- }
1484
- // 関数呼び出しのAstを構築
1485
- const funcNode = {
1486
- type: 'func',
1487
- name: t.value,
1488
- args,
1489
- josi: t.josi,
1490
- ...map,
1491
- end: this.peekSourceMap()
1492
- };
1493
- // 「プラグイン名設定」ならば、そこでスコープを変更することを意味する
1494
- if (funcNode.name === 'プラグイン名設定') {
1495
- if (args.length > 0 && args[0]) {
1496
- let fname = '' + args[0].value;
1497
- if (fname === 'メイン') {
1498
- fname = '' + args[0].file;
1499
- }
1500
- this.modName = NakoLexer.filenameToModName(fname);
1501
- }
1502
- }
1503
- // 言い切りならそこで一度切る
1504
- if (t.josi === '') {
1505
- return funcNode;
1506
- }
1507
- // 「**して、**」の場合も一度切る
1508
- if (keizokuJosi.indexOf(t.josi) >= 0) {
1509
- funcNode.josi = 'して';
1510
- return funcNode;
1511
- }
1512
- // 続き
1513
- funcNode.meta = f;
1514
- this.pushStack(funcNode);
1515
- return null;
1516
- }
1517
- /** 関数呼び出し演算子 #891
1518
- * @returns {Ast | null} */
1519
- yCallOp() {
1520
- if (!this.check2(['func', '←'])) {
1521
- return null;
1522
- }
1523
- const map = this.peekSourceMap();
1524
- // 関数名を得る
1525
- const word = this.get();
1526
- if (word == null) {
1527
- throw new Error('関数が取得できません。');
1528
- }
1529
- try {
1530
- const op = this.get();
1531
- if (op == null) {
1532
- throw new Error('関数呼び出し演算子が取得できません。');
1533
- }
1534
- const funcName = word.value;
1535
- // 関数の引数なしをチェック
1536
- if (!word.meta) {
1537
- throw new Error('関数本体を取得できません。');
1538
- }
1539
- if (!word.meta.josi) {
1540
- throw new Error('関数の引数情報を取得できません。');
1541
- }
1542
- const argCount = word.meta.josi.length;
1543
- if (argCount === 0) {
1544
- throw NakoSyntaxError.fromNode(`引数がない関数『${funcName}』を関数呼び出し演算子で呼び出すことはできません。`, word);
1545
- }
1546
- // 引数を順に取得
1547
- const curStackPos = this.stack.length;
1548
- while (!this.isEOF()) {
1549
- const t = this.yGetArg();
1550
- if (t) {
1551
- this.pushStack(t);
1552
- if ((this.stack.length - curStackPos) === argCount) {
1553
- break;
1554
- }
1555
- continue;
1556
- }
1557
- break;
1558
- }
1559
- // この場合第一引数の省略は認めない
1560
- const realArgCount = this.stack.length - curStackPos;
1561
- if (realArgCount !== argCount) {
1562
- throw NakoSyntaxError.fromNode(`関数『${funcName}』呼び出しで引数の数(${realArgCount})が定義(${argCount})と違います。`, word);
1563
- }
1564
- // 引数を取り出す
1565
- const tmpList = this.stack.splice(curStackPos, argCount);
1566
- // 引数が1つなら助詞は省略が可能。ただし、引数が2つ以上の時、正しく助詞の順序を入れ替える
1567
- let argList = tmpList;
1568
- if (argCount >= 2) {
1569
- argList = [];
1570
- const defList = word.meta.josi;
1571
- defList.forEach((josiList, i) => {
1572
- for (let j = 0; j < tmpList.length; j++) {
1573
- const t = tmpList[j];
1574
- if (josiList.indexOf(t.josi) >= 0) {
1575
- argList[i] = t;
1576
- return;
1577
- }
1578
- }
1579
- const josiStr = josiList.join(',');
1580
- throw new Error(`助詞『${josiStr}』が見当たりません。`);
1581
- });
1582
- }
1583
- // funcノードを返す
1584
- return {
1585
- type: 'func',
1586
- name: funcName,
1587
- args: argList,
1588
- setter: true,
1589
- josi: '',
1590
- ...map,
1591
- end: this.peekSourceMap()
1592
- };
1593
- }
1594
- catch (err) {
1595
- this.logger.debug(`${this.nodeToStr(word, { depth: 0 }, true)}の関数呼び出しで引数(『←』以降)が読み取れません。`, word);
1596
- throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 0 }, false)}の関数呼び出しでエラーがあります。\n${err.message}`, word);
1597
- }
1598
- }
1599
- /** @returns {Ast | null} */
1600
- yLet() {
1601
- const map = this.peekSourceMap();
1602
- // 通常の変数
1603
- if (this.check2(['word', 'eq'])) {
1604
- const word = this.peek();
1605
- let threw = false;
1606
- try {
1607
- if (this.accept(['word', 'eq', this.yCalc]) || this.accept(['word', 'eq', this.ySentence])) {
1608
- if (this.y[2].type === 'eol') {
1609
- throw new Error('値が空です。');
1610
- }
1611
- if (this.check('comma')) {
1612
- this.get();
1613
- } // skip comma (ex) name1=val1, name2=val2
1614
- const nameToken = this.getVarName(this.y[0]);
1615
- const valueToken = this.y[2];
1616
- return {
1617
- type: 'let',
1618
- name: nameToken,
1619
- value: valueToken,
1620
- ...map,
1621
- end: this.peekSourceMap()
1622
- };
1623
- }
1624
- else {
1625
- threw = true;
1626
- this.logger.debug(`${this.nodeToStr(word, { depth: 1 }, true)}への代入文で計算式に書き間違いがあります。`, word);
1627
- throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文で計算式に書き間違いがあります。`, map);
1628
- }
1629
- }
1630
- catch (err) {
1631
- if (threw) {
1632
- throw err;
1633
- }
1634
- this.logger.debug(`${this.nodeToStr(word, { depth: 1 }, true)}への代入文で計算式に以下の書き間違いがあります。\n${err.message}`, word);
1635
- throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文で計算式に以下の書き間違いがあります。\n${err.message}`, map);
1636
- }
1637
- }
1638
- // let_array ?
1639
- if (this.check2(['word', '@'])) {
1640
- const la = this.yLetArrayAt(map);
1641
- if (this.check('comma')) {
1642
- this.get();
1643
- } // skip comma (ex) name1=val1, name2=val2
1644
- if (la) {
1645
- la.checkInit = this.flagCheckArrayInit;
1646
- return la;
1647
- }
1648
- }
1649
- if (this.check2(['word', '['])) {
1650
- const lb = this.yLetArrayBracket(map);
1651
- if (this.check('comma')) {
1652
- this.get();
1653
- } // skip comma (ex) name1=val1, name2=val2
1654
- if (lb) {
1655
- lb.checkInit = this.flagCheckArrayInit;
1656
- return lb;
1657
- }
1658
- }
1659
- // ローカル変数定義
1660
- if (this.accept(['word', 'とは'])) {
1661
- const word = this.getVarName(this.y[0]);
1662
- if (!this.checkTypes(['変数', '定数'])) {
1663
- throw NakoSyntaxError.fromNode('ローカル変数『' + word.value + '』の定義エラー', word);
1664
- }
1665
- const vtype = this.getCur(); // 変数
1666
- // 初期値がある?
1667
- let value = null;
1668
- if (this.check('eq')) {
1669
- this.get();
1670
- value = this.yCalc();
1671
- }
1672
- if (this.check('comma')) {
1673
- this.get();
1674
- } // skip comma (ex) name1=val1, name2=val2
1675
- return {
1676
- type: 'def_local_var',
1677
- name: word,
1678
- vartype: vtype.type,
1679
- value,
1680
- ...map,
1681
- end: this.peekSourceMap()
1682
- };
1683
- }
1684
- // ローカル変数定義(その2)
1685
- if (this.accept(['変数', 'word', 'eq', this.yCalc])) {
1686
- const word = this.getVarName(this.y[1]);
1687
- return {
1688
- type: 'def_local_var',
1689
- name: word,
1690
- vartype: '変数',
1691
- value: this.y[3],
1692
- ...map,
1693
- end: this.peekSourceMap()
1694
- };
1695
- }
1696
- if (this.accept(['定数', 'word', 'eq', this.yCalc])) {
1697
- const word = this.getVarName(this.y[1]);
1698
- return {
1699
- type: 'def_local_var',
1700
- name: word,
1701
- vartype: '定数',
1702
- value: this.y[3],
1703
- ...map,
1704
- end: this.peekSourceMap()
1705
- };
1706
- }
1707
- // 複数定数への代入 #563
1708
- if (this.accept(['定数', this.yJSONArray, 'eq', this.yCalc])) {
1709
- const names = this.y[1];
1710
- // check array
1711
- if (names && names.value instanceof Array) {
1712
- for (const i in names.value) {
1713
- if (names.value[i].type !== 'word') {
1714
- throw NakoSyntaxError.fromNode(`複数定数の代入文${i + 1}番目でエラー。『定数[A,B,C]=[1,2,3]』の書式で記述してください。`, this.y[0]);
1715
- }
1716
- }
1717
- }
1718
- else {
1719
- throw NakoSyntaxError.fromNode('複数定数の代入文でエラー。『定数[A,B,C]=[1,2,3]』の書式で記述してください。', this.y[0]);
1720
- }
1721
- names.value = this.getVarNameList(names.value);
1722
- return {
1723
- type: 'def_local_varlist',
1724
- names: names.value,
1725
- vartype: '定数',
1726
- value: this.y[3],
1727
- ...map,
1728
- end: this.peekSourceMap()
1729
- };
1730
- }
1731
- // 複数変数への代入 #563
1732
- if (this.accept(['変数', this.yJSONArray, 'eq', this.yCalc])) {
1733
- const names = this.y[1];
1734
- // check array
1735
- if (names && names.value instanceof Array) {
1736
- for (const i in names.value) {
1737
- if (names.value[i].type !== 'word') {
1738
- throw NakoSyntaxError.fromNode(`複数変数の代入文${i + 1}番目でエラー。『変数[A,B,C]=[1,2,3]』の書式で記述してください。`, this.y[0]);
1739
- }
1740
- }
1741
- }
1742
- else {
1743
- throw NakoSyntaxError.fromNode('複数変数の代入文でエラー。『変数[A,B,C]=[1,2,3]』の書式で記述してください。', this.y[0]);
1744
- }
1745
- names.value = this.getVarNameList(names.value);
1746
- return {
1747
- type: 'def_local_varlist',
1748
- names: names.value,
1749
- vartype: '変数',
1750
- value: this.y[3],
1751
- ...map,
1752
- end: this.peekSourceMap()
1753
- };
1754
- }
1755
- // 複数変数への代入 #563
1756
- if (this.check2(['word', 'comma', 'word'])) {
1757
- // 2 word
1758
- if (this.accept(['word', 'comma', 'word', 'eq', this.yCalc])) {
1759
- let names = [this.y[0], this.y[2]];
1760
- names = this.getVarNameList(names);
1761
- return {
1762
- type: 'def_local_varlist',
1763
- names,
1764
- vartype: '変数',
1765
- value: this.y[4],
1766
- ...map,
1767
- end: this.peekSourceMap()
1768
- };
1769
- }
1770
- // 3 word
1771
- if (this.accept(['word', 'comma', 'word', 'comma', 'word', 'eq', this.yCalc])) {
1772
- let names = [this.y[0], this.y[2], this.y[4]];
1773
- names = this.getVarNameList(names);
1774
- return {
1775
- type: 'def_local_varlist',
1776
- names,
1777
- vartype: '変数',
1778
- value: this.y[6],
1779
- ...map,
1780
- end: this.peekSourceMap()
1781
- };
1782
- }
1783
- // 4 word
1784
- if (this.accept(['word', 'comma', 'word', 'comma', 'word', 'comma', 'word', 'eq', this.yCalc])) {
1785
- let names = [this.y[0], this.y[2], this.y[4], this.y[6]];
1786
- names = this.getVarNameList(names);
1787
- return {
1788
- type: 'def_local_varlist',
1789
- names,
1790
- vartype: '変数',
1791
- value: this.y[8],
1792
- ...map,
1793
- end: this.peekSourceMap()
1794
- };
1795
- }
1796
- // 5 word
1797
- if (this.accept(['word', 'comma', 'word', 'comma', 'word', 'comma', 'word', 'comma', 'word', 'eq', this.yCalc])) {
1798
- let names = [this.y[0], this.y[2], this.y[4], this.y[6], this.y[8]];
1799
- names = this.getVarNameList(names);
1800
- return {
1801
- type: 'def_local_varlist',
1802
- names,
1803
- vartype: '変数',
1804
- value: this.y[10],
1805
- ...map,
1806
- end: this.peekSourceMap()
1807
- };
1808
- }
1809
- }
1810
- return null;
1811
- }
1812
- /**
1813
- * 配列のインデックスが1から始まる場合を考慮するか
1814
- * @param {Ast} node
1815
- * @returns
1816
- */
1817
- checkArrayIndex(node) {
1818
- // 配列が0から始まるのであればそのまま返す
1819
- if (this.arrayIndexFrom === 0) {
1820
- return node;
1821
- }
1822
- // 配列が1から始まるのであれば演算を加えて返す
1823
- return {
1824
- ...node,
1825
- 'type': 'op',
1826
- 'operator': '-',
1827
- 'left': node,
1828
- 'right': {
1829
- ...node,
1830
- 'type': 'number',
1831
- 'value': this.arrayIndexFrom
1832
- }
1833
- };
1834
- }
1835
- /**
1836
- * 配列のインデックスを逆順にするのを考慮するか
1837
- * @param {Ast[]| null} ary
1838
- */
1839
- checkArrayReverse(ary) {
1840
- if (!ary) {
1841
- return [];
1842
- }
1843
- if (!this.flagReverseArrayIndex) {
1844
- return ary;
1845
- }
1846
- // 二次元以上の配列変数のアクセスを[y][x]ではなく[x][y]と順序を変更する
1847
- if (ary.length <= 1) {
1848
- return ary;
1849
- }
1850
- return ary.reverse();
1851
- }
1852
- /** @returns {Ast | null} */
1853
- yLetArrayAt(map) {
1854
- // 一次元配列
1855
- if (this.accept(['word', '@', this.yValue, 'eq', this.yCalc])) {
1856
- return {
1857
- type: 'let_array',
1858
- name: this.getVarName(this.y[0]),
1859
- index: [this.checkArrayIndex(this.y[2])],
1860
- value: this.y[4],
1861
- ...map,
1862
- end: this.peekSourceMap()
1863
- };
1864
- }
1865
- // 二次元配列
1866
- if (this.accept(['word', '@', this.yValue, '@', this.yValue, 'eq', this.yCalc])) {
1867
- return {
1868
- type: 'let_array',
1869
- name: this.getVarName(this.y[0]),
1870
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4])]),
1871
- value: this.y[6],
1872
- ...map,
1873
- end: this.peekSourceMap()
1874
- };
1875
- }
1876
- // 三次元配列
1877
- if (this.accept(['word', '@', this.yValue, '@', this.yValue, '@', this.yValue, 'eq', this.yCalc])) {
1878
- return {
1879
- type: 'let_array',
1880
- name: this.getVarName(this.y[0]),
1881
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4]), this.checkArrayIndex(this.y[6])]),
1882
- value: this.y[8],
1883
- ...map,
1884
- end: this.peekSourceMap()
1885
- };
1886
- }
1887
- // 二次元配列(カンマ指定)
1888
- if (this.accept(['word', '@', this.yValue, 'comma', this.yValue, 'eq', this.yCalc])) {
1889
- return {
1890
- type: 'let_array',
1891
- name: this.getVarName(this.y[0]),
1892
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4])]),
1893
- value: this.y[6],
1894
- ...map,
1895
- end: this.peekSourceMap()
1896
- };
1897
- }
1898
- // 三次元配列(カンマ指定)
1899
- if (this.accept(['word', '@', this.yValue, 'comma', this.yValue, 'comma', this.yValue, 'eq', this.yCalc])) {
1900
- return {
1901
- type: 'let_array',
1902
- name: this.getVarName(this.y[0]),
1903
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4]), this.checkArrayIndex(this.y[6])]),
1904
- value: this.y[8],
1905
- ...map,
1906
- end: this.peekSourceMap()
1907
- };
1908
- }
1909
- return null;
1910
- }
1911
- /** @returns {Ast | null} */
1912
- yLetArrayBracket(map) {
1913
- // 一次元配列
1914
- if (this.accept(['word', '[', this.yCalc, ']', 'eq', this.yCalc])) {
1915
- return {
1916
- type: 'let_array',
1917
- name: this.getVarName(this.y[0]),
1918
- index: [this.checkArrayIndex(this.y[2])],
1919
- value: this.y[5],
1920
- ...map,
1921
- end: this.peekSourceMap()
1922
- };
1923
- }
1924
- // 二次元配列
1925
- if (this.accept(['word', '[', this.yCalc, ']', '[', this.yCalc, ']', 'eq', this.yCalc])) {
1926
- return {
1927
- type: 'let_array',
1928
- name: this.getVarName(this.y[0]),
1929
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[5])]),
1930
- value: this.y[8],
1931
- tag: '2',
1932
- ...map,
1933
- end: this.peekSourceMap()
1934
- };
1935
- }
1936
- if (this.accept(['word', '[', this.yCalc, 'comma', this.yCalc, ']', 'eq', this.yCalc])) {
1937
- return {
1938
- type: 'let_array',
1939
- name: this.getVarName(this.y[0]),
1940
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4])]),
1941
- value: this.y[7],
1942
- tag: '2',
1943
- ...map,
1944
- end: this.peekSourceMap()
1945
- };
1946
- }
1947
- // 三次元配列
1948
- if (this.accept(['word', '[', this.yCalc, ']', '[', this.yCalc, ']', '[', this.yCalc, ']', 'eq', this.yCalc])) {
1949
- return {
1950
- type: 'let_array',
1951
- name: this.getVarName(this.y[0]),
1952
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[5]), this.checkArrayIndex(this.y[8])]),
1953
- value: this.y[11],
1954
- ...map,
1955
- end: this.peekSourceMap()
1956
- };
1957
- }
1958
- if (this.accept(['word', '[', this.yCalc, 'comma', this.yCalc, 'comma', this.yCalc, ']', 'eq', this.yCalc])) {
1959
- return {
1960
- type: 'let_array',
1961
- name: this.getVarName(this.y[0]),
1962
- index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4]), this.checkArrayIndex(this.y[6])]),
1963
- value: this.y[9],
1964
- ...map,
1965
- end: this.peekSourceMap()
1966
- };
1967
- }
1968
- return null;
1969
- }
1970
- /** @returns {Ast | null} */
1971
- yCalc() {
1972
- const map = this.peekSourceMap();
1973
- if (this.check('eol')) {
1974
- return null;
1975
- }
1976
- // 値を一つ読む
1977
- const t = this.yGetArg();
1978
- if (!t) {
1979
- return null;
1980
- }
1981
- // 助詞がある? つまり、関数呼び出しがある?
1982
- if (t.josi === '') {
1983
- return t;
1984
- } // 値だけの場合
1985
- // 関数の呼び出しがあるなら、スタックに載せて関数読み出しを呼ぶ
1986
- this.pushStack(t);
1987
- const t1 = this.yCall();
1988
- if (!t1) {
1989
- return this.popStack();
1990
- }
1991
- // それが連文か確認
1992
- if (t1.josi !== 'して') {
1993
- return t1;
1994
- } // 連文ではない
1995
- // 連文なら右側を読んで左側とくっつける
1996
- const t2 = this.yCalc();
1997
- if (!t2) {
1998
- return t1;
1999
- }
2000
- return {
2001
- type: 'renbun',
2002
- left: t1,
2003
- right: t2,
2004
- josi: t2.josi,
2005
- ...map,
2006
- end: this.peekSourceMap()
2007
- };
2008
- }
2009
- /** @returns {Ast | null} */
2010
- yValueKakko() {
2011
- if (!this.check('(')) {
2012
- return null;
2013
- }
2014
- const t = this.get(); // skip '('
2015
- if (!t) {
2016
- throw new Error('[System Error] check したのに get できない');
2017
- }
2018
- this.saveStack();
2019
- const v = this.yCalc() || this.ySentence();
2020
- if (v === null) {
2021
- const v2 = this.get();
2022
- this.logger.debug('(...)の解析エラー。' + this.nodeToStr(v2, { depth: 1 }, true) + 'の近く', t);
2023
- throw NakoSyntaxError.fromNode('(...)の解析エラー。' + this.nodeToStr(v2, { depth: 1 }, false) + 'の近く', t);
2024
- }
2025
- if (!this.check(')')) {
2026
- this.logger.debug('(...)の解析エラー。' + this.nodeToStr(v, { depth: 1 }, true) + 'の近く', t);
2027
- throw NakoSyntaxError.fromNode('(...)の解析エラー。' + this.nodeToStr(v, { depth: 1 }, false) + 'の近く', t);
2028
- }
2029
- const closeParent = this.get(); // skip ')'
2030
- this.loadStack();
2031
- if (closeParent) {
2032
- v.josi = closeParent.josi;
2033
- }
2034
- return v;
2035
- }
2036
- /** @returns {Ast | null} */
2037
- yValue() {
2038
- const map = this.peekSourceMap();
2039
- // カンマなら飛ばす #877
2040
- if (this.check('comma')) {
2041
- this.get();
2042
- }
2043
- // プリミティブな値
2044
- if (this.checkTypes(['number', 'string'])) {
2045
- return this.getCur();
2046
- }
2047
- // 丸括弧
2048
- if (this.check('(')) {
2049
- return this.yValueKakko();
2050
- }
2051
- // マイナス記号
2052
- if (this.check2(['-', 'number']) || this.check2(['-', 'word']) || this.check2(['-', 'func'])) {
2053
- const m = this.get(); // skip '-'
2054
- const v = this.yValue();
2055
- const josi = (v && v.josi) ? v.josi : '';
2056
- const line = (m && m.line) ? m.line : 0;
2057
- return {
2058
- type: 'op',
2059
- operator: '*',
2060
- left: { type: 'number', value: -1, line },
2061
- right: v || [],
2062
- josi,
2063
- ...map,
2064
- end: this.peekSourceMap()
2065
- };
2066
- }
2067
- // NOT
2068
- if (this.check('not')) {
2069
- this.get(); // skip '!'
2070
- const v = this.yValue();
2071
- const josi = (v && v.josi) ? v.josi : '';
2072
- return {
2073
- type: 'not',
2074
- value: v,
2075
- josi,
2076
- ...map,
2077
- end: this.peekSourceMap()
2078
- };
2079
- }
2080
- // JSON object
2081
- const a = this.yJSONArray();
2082
- if (a) {
2083
- return a;
2084
- }
2085
- const o = this.yJSONObject();
2086
- if (o) {
2087
- return o;
2088
- }
2089
- // 一語関数
2090
- const splitType = operatorList.concat(['eol', ')', ']', 'ならば', '回', '間', '反復', '条件分岐']);
2091
- if (this.check2(['func', splitType])) {
2092
- const tt = this.get();
2093
- if (!tt) {
2094
- throw new Error('[System Error] 正しく値が取れませんでした。');
2095
- }
2096
- const f = this.getVarNameRef(tt);
2097
- return {
2098
- type: 'func',
2099
- name: f.value,
2100
- args: [],
2101
- josi: f.josi,
2102
- ...map,
2103
- end: this.peekSourceMap()
2104
- };
2105
- }
2106
- // C風関数呼び出し FUNC(...)
2107
- if (this.check2([['func', 'word'], '(']) && this.peekDef().josi === '') {
2108
- const f = this.peek();
2109
- if (this.accept([['func', 'word'], '(', this.yGetArgParen, ')'])) {
2110
- return {
2111
- type: 'func',
2112
- name: this.getVarNameRef(this.y[0]).value,
2113
- args: this.y[2],
2114
- josi: this.y[3].josi,
2115
- ...map,
2116
- end: this.peekSourceMap()
2117
- };
2118
- }
2119
- else {
2120
- throw NakoSyntaxError.fromNode('C風関数呼び出しのエラー', f || NewEmptyToken());
2121
- }
2122
- }
2123
- // 関数呼び出し演算子
2124
- if (this.check2(['func', '←'])) {
2125
- return this.yCallOp();
2126
- }
2127
- // 無名関数(関数オブジェクト)
2128
- if (this.check('def_func')) {
2129
- return this.yMumeiFunc();
2130
- }
2131
- // 変数
2132
- const word = this.yValueWord();
2133
- if (word) {
2134
- return word;
2135
- }
2136
- // その他
2137
- return null;
2138
- }
2139
- yValueWordGetIndex(ast) {
2140
- if (!ast.index) {
2141
- ast.index = [];
2142
- }
2143
- // word @ a, b, c
2144
- if (this.check('@')) {
2145
- if (this.accept(['@', this.yValue, 'comma', this.yValue, 'comma', this.yValue])) {
2146
- ast.index.push(this.checkArrayIndex(this.y[1]));
2147
- ast.index.push(this.checkArrayIndex(this.y[3]));
2148
- ast.index.push(this.checkArrayIndex(this.y[5]));
2149
- ast.index = this.checkArrayReverse(ast.index);
2150
- ast.josi = this.y[5].josi;
2151
- return true;
2152
- }
2153
- if (this.accept(['@', this.yValue, 'comma', this.yValue])) {
2154
- ast.index.push(this.checkArrayIndex(this.y[1]));
2155
- ast.index.push(this.checkArrayIndex(this.y[3]));
2156
- ast.index = this.checkArrayReverse(ast.index);
2157
- ast.josi = this.y[3].josi;
2158
- return true;
2159
- }
2160
- if (this.accept(['@', this.yValue])) {
2161
- ast.index.push(this.checkArrayIndex(this.y[1]));
2162
- ast.josi = this.y[1].josi;
2163
- return true;
2164
- }
2165
- throw NakoSyntaxError.fromNode('変数の後ろの『@要素』の指定が不正です。', ast);
2166
- }
2167
- if (this.check('[')) {
2168
- if (this.accept(['[', this.yCalc, ']'])) {
2169
- ast.index.push(this.checkArrayIndex(this.y[1]));
2170
- ast.josi = this.y[2].josi;
2171
- return true;
2172
- }
2173
- }
2174
- if (this.check('[')) {
2175
- if (this.accept(['[', this.yCalc, 'comma', this.yCalc, ']'])) {
2176
- const index = [
2177
- this.checkArrayIndex(this.y[1]),
2178
- this.checkArrayIndex(this.y[3])
2179
- ];
2180
- ast.index = this.checkArrayReverse(index);
2181
- ast.josi = this.y[4].josi;
2182
- return true;
2183
- }
2184
- }
2185
- if (this.check('[')) {
2186
- if (this.accept(['[', this.yCalc, 'comma', this.yCalc, 'comma', this.yCalc, ']'])) {
2187
- const index = [
2188
- this.checkArrayIndex(this.y[1]),
2189
- this.checkArrayIndex(this.y[3]),
2190
- this.checkArrayIndex(this.y[5])
2191
- ];
2192
- ast.index = this.checkArrayReverse(index);
2193
- ast.josi = this.y[6].josi;
2194
- return true;
2195
- }
2196
- }
2197
- return false;
2198
- }
2199
- /** @returns {Ast | null} */
2200
- yValueWord() {
2201
- const map = this.peekSourceMap();
2202
- if (this.check('word')) {
2203
- const t = this.getCur();
2204
- const word = this.getVarNameRef(t);
2205
- // word[n] || word@n
2206
- if (word.josi === '' && this.checkTypes(['[', '@'])) {
2207
- const ast = {
2208
- type: '配列参照',
2209
- name: word,
2210
- index: [],
2211
- josi: '',
2212
- ...map,
2213
- end: this.peekSourceMap()
2214
- };
2215
- while (!this.isEOF()) {
2216
- if (!this.yValueWordGetIndex(ast)) {
2217
- break;
2218
- }
2219
- }
2220
- if (ast.index && ast.index.length === 0) {
2221
- throw NakoSyntaxError.fromNode(`配列『${word.value}』アクセスで指定ミス`, word);
2222
- }
2223
- return ast;
2224
- }
2225
- return word;
2226
- }
2227
- return null;
2228
- }
2229
- /** 変数名を検索して解決する
2230
- * @param {Ast|Token} word
2231
- * @return {Ast|Token}
2232
- */
2233
- getVarName(word) {
2234
- // check word name
2235
- const f = this.findVar(word.value);
2236
- if (!f) { // 変数が見つからない
2237
- if (this.funcLevel === 0) { // global
2238
- let gname = word.value;
2239
- if (gname.indexOf('__') < 0) {
2240
- gname = this.modName + '__' + word.value;
2241
- }
2242
- this.funclist[gname] = { type: 'var', value: '' };
2243
- word.value = gname;
2244
- }
2245
- else { // local
2246
- this.localvars[word.value] = { type: 'var', value: '' };
2247
- }
2248
- }
2249
- else if (f && f.scope === 'global') {
2250
- word.value = f.name;
2251
- }
2252
- return word;
2253
- }
2254
- /** 変数名を検索して解決する */
2255
- getVarNameRef(word) {
2256
- // check word name
2257
- const f = this.findVar(word.value);
2258
- if (!f) { // 変数が見つからない
2259
- if (this.funcLevel === 0 && word.value.indexOf('__') < 0) {
2260
- word.value = this.modName + '__' + word.value;
2261
- }
2262
- }
2263
- else if (f && f.scope === 'global') {
2264
- word.value = f.name;
2265
- }
2266
- return word;
2267
- }
2268
- /** 複数の変数名を検索して解決する */
2269
- getVarNameList(words) {
2270
- for (let i = 0; i < words.length; i++) {
2271
- words[i] = this.getVarName(words[i]);
2272
- }
2273
- return words;
2274
- }
2275
- yJSONObjectValue() {
2276
- const a = [];
2277
- const firstToken = this.peek();
2278
- if (!firstToken) {
2279
- return null;
2280
- }
2281
- while (!this.isEOF()) {
2282
- while (this.check('eol')) {
2283
- this.get();
2284
- }
2285
- if (this.check('}')) {
2286
- break;
2287
- }
2288
- if (this.accept(['word', ':', this.yCalc])) {
2289
- this.y[0].type = 'string'; // キー名の文字列記号省略の場合
2290
- a.push({
2291
- key: this.y[0],
2292
- value: this.y[2]
2293
- });
2294
- }
2295
- else if (this.accept(['string', ':', this.yCalc])) {
2296
- a.push({
2297
- key: this.y[0],
2298
- value: this.y[2]
2299
- });
2300
- }
2301
- else if (this.check('word')) {
2302
- const w = this.getCur();
2303
- w.type = 'string';
2304
- a.push({
2305
- key: w,
2306
- value: w
2307
- });
2308
- }
2309
- else if (this.checkTypes(['string', 'number'])) {
2310
- const w = this.getCur();
2311
- a.push({
2312
- key: w,
2313
- value: w
2314
- });
2315
- }
2316
- else {
2317
- throw NakoSyntaxError.fromNode('辞書オブジェクトの宣言で末尾の『}』がありません。', firstToken);
2318
- }
2319
- if (this.check('comma')) {
2320
- this.get();
2321
- }
2322
- }
2323
- return a;
2324
- }
2325
- /** @returns {Ast | null} */
2326
- yJSONObject() {
2327
- const map = this.peekSourceMap();
2328
- if (this.accept(['{', '}'])) {
2329
- return {
2330
- type: 'json_obj',
2331
- value: [],
2332
- josi: this.y[1].josi,
2333
- ...map,
2334
- end: this.peekSourceMap()
2335
- };
2336
- }
2337
- if (this.accept(['{', this.yJSONObjectValue, '}'])) {
2338
- return {
2339
- type: 'json_obj',
2340
- value: this.y[1],
2341
- josi: this.y[2].josi,
2342
- ...map,
2343
- end: this.peekSourceMap()
2344
- };
2345
- }
2346
- // 辞書初期化に終わりがなかった場合 (エラーチェックのため) #958
2347
- if (this.accept(['{', this.yJSONObjectValue])) {
2348
- throw NakoSyntaxError.fromNode('辞書型変数の初期化が『}』で閉じられていません。', this.y[1]);
2349
- }
2350
- return null;
2351
- }
2352
- yJSONArrayValue() {
2353
- if (this.check('eol')) {
2354
- this.get();
2355
- }
2356
- const v1 = this.yCalc();
2357
- if (v1 === null) {
2358
- return null;
2359
- }
2360
- if (this.check('comma')) {
2361
- this.get();
2362
- }
2363
- const a = [v1];
2364
- while (!this.isEOF()) {
2365
- if (this.check('eol')) {
2366
- this.get();
2367
- }
2368
- if (this.check(']')) {
2369
- break;
2370
- }
2371
- const v2 = this.yCalc();
2372
- if (v2 === null) {
2373
- break;
2374
- }
2375
- if (this.check('comma')) {
2376
- this.get();
2377
- }
2378
- a.push(v2);
2379
- }
2380
- return a;
2381
- }
2382
- /** @returns {Ast | null} */
2383
- yJSONArray() {
2384
- const map = this.peekSourceMap();
2385
- if (this.accept(['[', ']'])) {
2386
- return {
2387
- type: 'json_array',
2388
- value: [],
2389
- josi: this.y[1].josi,
2390
- ...map,
2391
- end: this.peekSourceMap()
2392
- };
2393
- }
2394
- if (this.accept(['[', this.yJSONArrayValue, ']'])) {
2395
- return {
2396
- type: 'json_array',
2397
- value: this.y[1],
2398
- josi: this.y[2].josi,
2399
- ...map,
2400
- end: this.peekSourceMap()
2401
- };
2402
- }
2403
- // 配列に終わりがなかった場合 (エラーチェックのため) #958
2404
- if (this.accept(['[', this.yJSONArrayValue])) {
2405
- throw NakoSyntaxError.fromNode('配列変数の初期化が『]』で閉じられていません。', this.y[1]);
2406
- }
2407
- return null;
2408
- }
2409
- /** エラー監視構文 */
2410
- yTryExcept() {
2411
- const map = this.peekSourceMap();
2412
- if (!this.check('エラー監視')) {
2413
- return null;
2414
- }
2415
- const kansi = this.getCur(); // skip エラー監視
2416
- const block = this.yBlock();
2417
- if (!this.check2(['エラー', 'ならば'])) {
2418
- throw NakoSyntaxError.fromNode('エラー構文で『エラーならば』がありません。' +
2419
- '『エラー監視..エラーならば..ここまで』を対で記述します。', kansi);
2420
- }
2421
- this.get(); // skip エラー
2422
- this.get(); // skip ならば
2423
- const errBlock = this.yBlock();
2424
- if (this.check('ここまで')) {
2425
- this.get();
2426
- }
2427
- else {
2428
- throw NakoSyntaxError.fromNode('『ここまで』がありません。『エラー監視』...『エラーならば』...『ここまで』を対応させてください。', map);
2429
- }
2430
- return {
2431
- type: 'try_except',
2432
- block,
2433
- errBlock: errBlock || [],
2434
- josi: '',
2435
- ...map,
2436
- end: this.peekSourceMap()
2437
- };
2438
- }
2439
- }
1
+ /**
2
+ * nadesiko v3 parser
3
+ */
4
+ import { opPriority, keizokuJosi, operatorList } from './nako_parser_const.mjs';
5
+ import { NakoParserBase } from './nako_parser_base.mjs';
6
+ import { NakoSyntaxError } from './nako_errors.mjs';
7
+ import { NakoLexer } from './nako_lexer.mjs';
8
+ import { NewEmptyToken } from './nako_types.mjs';
9
+ /**
10
+ * 構文解析を行うクラス
11
+ */
12
+ export class NakoParser extends NakoParserBase {
13
+ /**
14
+ * 構文解析を実行する
15
+ */
16
+ parse(tokens, filename) {
17
+ this.reset();
18
+ this.tokens = tokens;
19
+ this.modName = NakoLexer.filenameToModName(filename);
20
+ this.modList.push(this.modName);
21
+ // 解析開始
22
+ return this.startParser();
23
+ }
24
+ /** パーサーの一番最初に呼び出す構文規則 */
25
+ startParser() {
26
+ const b = this.ySentenceList();
27
+ const c = this.get();
28
+ if (c && c.type !== 'eof') {
29
+ this.logger.debug(`構文解析でエラー。${this.nodeToStr(c, { depth: 1 }, true)}の使い方が間違っています。`, c);
30
+ throw NakoSyntaxError.fromNode(`構文解析でエラー。${this.nodeToStr(c, { depth: 1 }, false)}の使い方が間違っています。`, c);
31
+ }
32
+ return b;
33
+ }
34
+ /** 複数文を返す */
35
+ ySentenceList() {
36
+ const blocks = [];
37
+ let line = -1;
38
+ const map = this.peekSourceMap();
39
+ while (!this.isEOF()) {
40
+ const n = this.ySentence();
41
+ if (!n) {
42
+ break;
43
+ }
44
+ blocks.push(n);
45
+ if (line < 0) {
46
+ line = n.line;
47
+ }
48
+ }
49
+ if (blocks.length === 0) {
50
+ const token = this.peek() || this.tokens[0];
51
+ this.logger.debug('構文解析に失敗:' + this.nodeToStr(this.peek(), { depth: 1 }, true), token);
52
+ throw NakoSyntaxError.fromNode('構文解析に失敗:' + this.nodeToStr(this.peek(), { depth: 1 }, false), token);
53
+ }
54
+ return { type: 'block', block: blocks, ...map, end: this.peekSourceMap(), genMode: this.genMode };
55
+ }
56
+ yEOL() {
57
+ // 行末のチェック #1009
58
+ const eol = this.get();
59
+ if (!eol) {
60
+ return null;
61
+ }
62
+ // 余剰スタックの確認
63
+ if (this.stack.length > 0) {
64
+ /** 余剰スタックのレポートを作る */
65
+ const words = [];
66
+ this.stack.forEach((t) => {
67
+ let w = this.nodeToStr(t, { depth: 1 }, false);
68
+ if (t.josi) {
69
+ w += t.josi;
70
+ }
71
+ words.push(w);
72
+ });
73
+ const desc = words.join(',');
74
+ // 最近使った関数の使い方レポートを作る #1093
75
+ let descFunc = '';
76
+ const chA = 'A'.charCodeAt(0);
77
+ for (const f of this.recentlyCalledFunc) {
78
+ descFunc += ' - ';
79
+ let no = 0;
80
+ const josiA = f.josi;
81
+ if (josiA) {
82
+ for (const arg of josiA) {
83
+ const ch = String.fromCharCode(chA + no);
84
+ descFunc += ch;
85
+ if (arg.length === 1) {
86
+ descFunc += arg[0];
87
+ }
88
+ else {
89
+ descFunc += `(${arg.join('|')})`;
90
+ }
91
+ no++;
92
+ }
93
+ }
94
+ descFunc += f.name + '\n';
95
+ }
96
+ throw NakoSyntaxError.fromNode(`未解決の単語があります: [${desc}]\n次の命令の可能性があります:\n${descFunc}`, eol);
97
+ }
98
+ this.recentlyCalledFunc = [];
99
+ return eol;
100
+ }
101
+ /** @returns {Ast | null} */
102
+ ySentence() {
103
+ const map = this.peekSourceMap();
104
+ // 最初の語句が決まっている構文
105
+ if (this.check('eol')) {
106
+ return this.yEOL();
107
+ }
108
+ if (this.check('もし')) {
109
+ return this.yIF();
110
+ }
111
+ if (this.check('後判定')) {
112
+ return this.yAtohantei();
113
+ }
114
+ if (this.check('エラー監視')) {
115
+ return this.yTryExcept();
116
+ }
117
+ if (this.check('逐次実行')) {
118
+ return this.yTikuji();
119
+ }
120
+ if (this.accept(['抜ける'])) {
121
+ return { type: 'break', josi: '', ...map, end: this.peekSourceMap() };
122
+ }
123
+ if (this.accept(['続ける'])) {
124
+ return { type: 'continue', josi: '', ...map, end: this.peekSourceMap() };
125
+ }
126
+ if (this.accept(['require', 'string', '取込'])) {
127
+ return this.yRequire();
128
+ }
129
+ if (this.accept(['not', '非同期モード'])) {
130
+ return this.yASyncMode();
131
+ }
132
+ if (this.accept(['not', 'DNCLモード'])) {
133
+ return this.yDNCLMode();
134
+ }
135
+ if (this.accept(['not', 'string', 'モード設定'])) {
136
+ return this.ySetGenMode(this.y[1].value);
137
+ }
138
+ // 関数呼び出し演算子
139
+ if (this.check2(['func', '←'])) {
140
+ return this.yCallOp();
141
+ }
142
+ if (this.check2(['func', 'eq'])) {
143
+ const word = this.get() || NewEmptyToken('?', '?', map.line, map.file || '');
144
+ throw NakoSyntaxError.fromNode(`関数『${word.value}』に代入できません。『←』を使ってください。`, word);
145
+ }
146
+ // 先読みして初めて確定する構文
147
+ if (this.accept([this.ySpeedMode])) {
148
+ return this.y[0];
149
+ }
150
+ if (this.accept([this.yPerformanceMonitor])) {
151
+ return this.y[0];
152
+ }
153
+ if (this.accept([this.yLet])) {
154
+ return this.y[0];
155
+ }
156
+ if (this.accept([this.yDefTest])) {
157
+ return this.y[0];
158
+ }
159
+ if (this.accept([this.yDefFunc])) {
160
+ return this.y[0];
161
+ }
162
+ // 関数呼び出しの他、各種構文の実装
163
+ if (this.accept([this.yCall])) {
164
+ const c1 = this.y[0];
165
+ if (c1.josi === 'して') { // 連文をblockとして接続する(もし構文、逐次実行構文などのため)
166
+ const c2 = this.ySentence();
167
+ if (c2 !== null) {
168
+ return {
169
+ type: 'block',
170
+ block: [c1, c2],
171
+ josi: c2.josi,
172
+ ...map,
173
+ end: this.peekSourceMap()
174
+ };
175
+ }
176
+ }
177
+ return c1;
178
+ }
179
+ return null;
180
+ }
181
+ /** @returns {Ast} */
182
+ yASyncMode() {
183
+ const map = this.peekSourceMap();
184
+ this.genMode = '非同期モード';
185
+ return { type: 'eol', ...map, end: this.peekSourceMap() };
186
+ }
187
+ /** @returns {Ast} */
188
+ yDNCLMode() {
189
+ const map = this.peekSourceMap();
190
+ // 配列インデックスは1から
191
+ this.arrayIndexFrom = 1;
192
+ // 配列アクセスをJSと逆順で指定する
193
+ this.flagReverseArrayIndex = true;
194
+ // 配列代入時自動で初期化チェックする
195
+ this.flagCheckArrayInit = true;
196
+ return { type: 'eol', ...map, end: this.peekSourceMap() };
197
+ }
198
+ /** @returns {Ast} */
199
+ ySetGenMode(mode) {
200
+ const map = this.peekSourceMap();
201
+ this.genMode = mode;
202
+ return { type: 'eol', ...map, end: this.peekSourceMap() };
203
+ }
204
+ /** @returns {Ast} */
205
+ yRequire() {
206
+ const nameToken = this.y[1];
207
+ const filename = nameToken.value;
208
+ const modName = NakoLexer.filenameToModName(filename);
209
+ if (this.modList.indexOf(modName) < 0) {
210
+ // 優先度が最も高いのは modList[0]
211
+ // [memo] モジュールの検索優先度は、下に書くほど高くなる
212
+ const modSelf = this.modList.shift();
213
+ if (modSelf) {
214
+ this.modList.unshift(modName);
215
+ this.modList.unshift(modSelf);
216
+ }
217
+ }
218
+ return {
219
+ type: 'require',
220
+ value: filename,
221
+ josi: '',
222
+ ...this.peekSourceMap(),
223
+ end: this.peekSourceMap()
224
+ };
225
+ }
226
+ /** @returns {Ast} */
227
+ yBlock() {
228
+ const map = this.peekSourceMap();
229
+ const blocks = [];
230
+ if (this.check('ここから')) {
231
+ this.get();
232
+ }
233
+ while (!this.isEOF()) {
234
+ if (this.checkTypes(['違えば', 'ここまで', 'エラー'])) {
235
+ break;
236
+ }
237
+ if (!this.accept([this.ySentence])) {
238
+ break;
239
+ }
240
+ blocks.push(this.y[0]);
241
+ }
242
+ return { type: 'block', block: blocks, ...map, end: this.peekSourceMap() };
243
+ }
244
+ yDefFuncReadArgs() {
245
+ if (!this.check('(')) {
246
+ return null;
247
+ }
248
+ const a = [];
249
+ this.get(); // skip '('
250
+ while (!this.isEOF()) {
251
+ if (this.check(')')) {
252
+ this.get(); // skip ''
253
+ break;
254
+ }
255
+ const t = this.get();
256
+ if (t) {
257
+ a.push(t);
258
+ }
259
+ if (this.check('comma')) {
260
+ this.get();
261
+ }
262
+ }
263
+ return a;
264
+ }
265
+ /** @returns {Ast | null} */
266
+ yDefTest() {
267
+ return this.yDef('def_test');
268
+ }
269
+ /** @returns {Ast | null} */
270
+ yDefFunc() {
271
+ return this.yDef('def_func');
272
+ }
273
+ /**
274
+ * @param {string} type
275
+ * @returns {Ast | null}
276
+ */
277
+ yDef(type) {
278
+ if (!this.check(type)) {
279
+ return null;
280
+ }
281
+ const map = this.peekSourceMap();
282
+ const def = this.get(); // ●
283
+ if (!def) {
284
+ return null;
285
+ }
286
+ let defArgs = [];
287
+ if (this.check('(')) {
288
+ defArgs = this.yDefFuncReadArgs() || [];
289
+ } // // lexerでも解析しているが再度詳しく
290
+ const funcName = this.get();
291
+ if (!funcName || funcName.type !== 'func') {
292
+ this.logger.debug(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, true) + 'の宣言でエラー。', funcName);
293
+ throw NakoSyntaxError.fromNode(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, false) + 'の宣言でエラー。', def);
294
+ }
295
+ if (this.check('(')) {
296
+ // 関数引数の二重定義
297
+ if (defArgs.length > 0) {
298
+ this.logger.debug(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, true) + 'の宣言で、引数定義は名前の前か後に一度だけ可能です。', funcName);
299
+ throw NakoSyntaxError.fromNode(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, false) + 'の宣言で、引数定義は名前の前か後に一度だけ可能です。', funcName);
300
+ }
301
+ defArgs = this.yDefFuncReadArgs() || [];
302
+ }
303
+ if (this.check('とは')) {
304
+ this.get();
305
+ }
306
+ let block = null;
307
+ let multiline = false;
308
+ let asyncFn = false;
309
+ if (this.check('ここから')) {
310
+ multiline = true;
311
+ }
312
+ if (this.check('eol')) {
313
+ multiline = true;
314
+ }
315
+ try {
316
+ this.funcLevel++;
317
+ this.usedAsyncFn = false;
318
+ // ローカル変数を生成
319
+ const backupLocalvars = this.localvars;
320
+ this.localvars = { 'それ': { type: 'var', value: '' } };
321
+ if (multiline) {
322
+ this.saveStack();
323
+ // 関数の引数をローカル変数として登録する
324
+ for (const arg of defArgs) {
325
+ const fnName = (arg.value) ? arg.value + '' : '';
326
+ this.localvars[fnName] = { 'type': 'var', 'value': '' };
327
+ }
328
+ block = this.yBlock();
329
+ if (this.check('ここまで')) {
330
+ this.get();
331
+ }
332
+ else {
333
+ throw NakoSyntaxError.fromNode('『ここまで』がありません。関数定義の末尾に必要です。', def);
334
+ }
335
+ this.loadStack();
336
+ }
337
+ else {
338
+ this.saveStack();
339
+ block = this.ySentence();
340
+ this.loadStack();
341
+ }
342
+ this.funcLevel--;
343
+ asyncFn = this.usedAsyncFn;
344
+ this.localvars = backupLocalvars;
345
+ }
346
+ catch (err) {
347
+ this.logger.debug(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, true) +
348
+ 'の定義で以下のエラーがありました。\n' + err.message, def);
349
+ throw NakoSyntaxError.fromNode(this.nodeToStr(funcName, { depth: 0, typeName: '関数' }, false) +
350
+ 'の定義で以下のエラーがありました。\n' + err.message, def);
351
+ }
352
+ return {
353
+ type,
354
+ name: funcName,
355
+ args: defArgs,
356
+ block: block || [],
357
+ asyncFn,
358
+ josi: '',
359
+ ...map,
360
+ end: this.peekSourceMap()
361
+ };
362
+ }
363
+ /** @returns {Ast | null} */
364
+ yIFCond() {
365
+ const map = this.peekSourceMap();
366
+ let a = this.yGetArg();
367
+ if (!a) {
368
+ return null;
369
+ }
370
+ // console.log('yIFCond=', a, this.peek())
371
+ // チェック : AがBならば
372
+ if (a.josi === 'が') {
373
+ const tmpI = this.index;
374
+ const b = this.yGetArg();
375
+ const naraba = this.get();
376
+ if ((b && b.type !== 'func') && (naraba && naraba.type === 'ならば')) {
377
+ return {
378
+ type: 'op',
379
+ operator: (naraba.value === 'でなければ') ? 'noteq' : 'eq',
380
+ left: a,
381
+ right: b,
382
+ josi: '',
383
+ ...map,
384
+ end: this.peekSourceMap()
385
+ };
386
+ }
387
+ this.index = tmpI;
388
+ }
389
+ if (a.josi !== '') {
390
+ // もし文で関数呼び出しがある場合
391
+ this.stack.push(a);
392
+ a = this.yCall();
393
+ }
394
+ // (ならば|でなければ)を確認
395
+ if (!this.check('ならば')) {
396
+ const smap = a || { type: '?', ...map };
397
+ this.logger.debug('もし文で『ならば』がないか、条件が複雑過ぎます。' + this.nodeToStr(this.peek(), { depth: 1 }, false) + 'の直前に『ならば』を書いてください。', smap);
398
+ throw NakoSyntaxError.fromNode('もし文で『ならば』がないか、条件が複雑過ぎます。' + this.nodeToStr(this.peek(), { depth: 1 }, false) + 'の直前に『ならば』を書いてください。', smap);
399
+ }
400
+ const naraba = this.get();
401
+ if (naraba && naraba.value === 'でなければ') {
402
+ a = {
403
+ type: 'not',
404
+ value: a,
405
+ josi: '',
406
+ ...map,
407
+ end: this.peekSourceMap()
408
+ };
409
+ }
410
+ return a;
411
+ }
412
+ /** @returns {Ast | null} */
413
+ yIF() {
414
+ const map = this.peekSourceMap();
415
+ if (!this.check('もし')) {
416
+ return null;
417
+ }
418
+ const mosi = this.get(); // skip もし
419
+ if (mosi == null) {
420
+ return null;
421
+ }
422
+ while (this.check('comma')) {
423
+ this.get();
424
+ } // skip comma
425
+ let cond = null;
426
+ try {
427
+ cond = this.yIFCond();
428
+ }
429
+ catch (err) {
430
+ throw NakoSyntaxError.fromNode('『もし』文の条件で次のエラーがあります。\n' + err.message, mosi);
431
+ }
432
+ if (cond === null) {
433
+ throw NakoSyntaxError.fromNode('『もし』文で条件の指定が空です。', mosi);
434
+ }
435
+ let trueBlock = null;
436
+ let falseBlock = null;
437
+ let tanbun = false;
438
+ // True Block
439
+ if (this.check('eol')) {
440
+ trueBlock = this.yBlock();
441
+ }
442
+ else {
443
+ trueBlock = this.ySentence();
444
+ tanbun = true;
445
+ }
446
+ // skip EOL
447
+ while (this.check('eol')) {
448
+ this.get();
449
+ }
450
+ // Flase Block
451
+ if (this.check('違えば')) {
452
+ this.get(); // skip 違えば
453
+ while (this.check('comma')) {
454
+ this.get();
455
+ }
456
+ if (this.check('eol')) {
457
+ falseBlock = this.yBlock();
458
+ }
459
+ else {
460
+ falseBlock = this.ySentence();
461
+ tanbun = true;
462
+ }
463
+ }
464
+ if (tanbun === false) {
465
+ if (this.check('ここまで')) {
466
+ this.get();
467
+ }
468
+ else {
469
+ throw NakoSyntaxError.fromNode('『もし』文で『ここまで』がありません。', mosi);
470
+ }
471
+ }
472
+ return {
473
+ type: 'if',
474
+ expr: cond || [],
475
+ block: trueBlock || [],
476
+ false_block: falseBlock || [],
477
+ josi: '',
478
+ ...map,
479
+ end: this.peekSourceMap()
480
+ };
481
+ }
482
+ ySpeedMode() {
483
+ const map = this.peekSourceMap();
484
+ if (!this.check2(['string', '実行速度優先'])) {
485
+ return null;
486
+ }
487
+ const optionNode = this.get();
488
+ this.get();
489
+ let val = '';
490
+ if (optionNode && optionNode.value) {
491
+ val = optionNode.value;
492
+ }
493
+ else {
494
+ return null;
495
+ }
496
+ const options = { 行番号無し: false, 暗黙の型変換無し: false, 強制ピュア: false, それ無効: false };
497
+ for (const name of val.split('/')) {
498
+ // 全て有効化
499
+ if (name === '全て') {
500
+ for (const k of Object.keys(options)) {
501
+ options[k] = true;
502
+ }
503
+ break;
504
+ }
505
+ // 個別に有効化
506
+ if (Object.keys(options).includes(name)) {
507
+ options[name] = true;
508
+ }
509
+ else {
510
+ // 互換性を考えて、警告に留める。
511
+ this.logger.warn(`実行速度優先文のオプション『${name}』は存在しません。`, optionNode);
512
+ }
513
+ }
514
+ let multiline = false;
515
+ if (this.check('ここから')) {
516
+ this.get();
517
+ multiline = true;
518
+ }
519
+ else if (this.check('eol')) {
520
+ multiline = true;
521
+ }
522
+ let block = null;
523
+ if (multiline) {
524
+ block = this.yBlock();
525
+ if (this.check('ここまで')) {
526
+ this.get();
527
+ }
528
+ }
529
+ else {
530
+ block = this.ySentence();
531
+ }
532
+ return {
533
+ type: 'speed_mode',
534
+ options,
535
+ block: block || [],
536
+ josi: '',
537
+ ...map
538
+ };
539
+ }
540
+ yPerformanceMonitor() {
541
+ const map = this.peekSourceMap();
542
+ if (!this.check2(['string', 'パフォーマンスモニタ適用'])) {
543
+ return null;
544
+ }
545
+ const optionNode = this.get();
546
+ if (!optionNode) {
547
+ return null;
548
+ }
549
+ this.get();
550
+ const options = { ユーザ関数: false, システム関数本体: false, システム関数: false };
551
+ for (const name of optionNode.value.split('/')) {
552
+ // 全て有効化
553
+ if (name === '全て') {
554
+ for (const k of Object.keys(options)) {
555
+ options[k] = true;
556
+ }
557
+ break;
558
+ }
559
+ // 個別に有効化
560
+ if (Object.keys(options).includes(name)) {
561
+ options[name] = true;
562
+ }
563
+ else {
564
+ // 互換性を考えて、警告に留める。
565
+ this.logger.warn(`パフォーマンスモニタ適用文のオプション『${name}』は存在しません。`, optionNode);
566
+ }
567
+ }
568
+ let multiline = false;
569
+ if (this.check('ここから')) {
570
+ this.get();
571
+ multiline = true;
572
+ }
573
+ else if (this.check('eol')) {
574
+ multiline = true;
575
+ }
576
+ let block = null;
577
+ if (multiline) {
578
+ block = this.yBlock();
579
+ if (this.check('ここまで')) {
580
+ this.get();
581
+ }
582
+ }
583
+ else {
584
+ block = this.ySentence();
585
+ }
586
+ return {
587
+ type: 'performance_monitor',
588
+ options,
589
+ block: block || [],
590
+ josi: '',
591
+ ...map
592
+ };
593
+ }
594
+ /** (非推奨) 「逐次実行」構文 @returns {Ast | null} */
595
+ yTikuji() {
596
+ const map = this.peekSourceMap();
597
+ if (!this.check('逐次実行')) {
598
+ return null;
599
+ }
600
+ const tikuji = this.getCur(); // skip 逐次実行
601
+ this.logger.warn('『逐次実行』構文の使用は非推奨になりました(https://nadesi.com/v3/doc/go.php?944)。', tikuji);
602
+ const blocks = [];
603
+ let errorBlock = null;
604
+ if (!tikuji || !this.check('eol')) {
605
+ throw NakoSyntaxError.fromNode('『逐次実行』の直後は改行が必要です。', tikuji);
606
+ }
607
+ // ブロックを読む
608
+ for (;;) {
609
+ if (this.check('ここまで')) {
610
+ break;
611
+ }
612
+ if (this.check('eol')) {
613
+ this.get(); // skip EOL
614
+ continue;
615
+ }
616
+ if (this.check2(['エラー', 'ならば'])) {
617
+ this.get(); // skip エラー
618
+ this.get(); // skip ならば
619
+ errorBlock = this.yBlock();
620
+ break;
621
+ }
622
+ let block = null;
623
+ // 「先に」「次に」句はブロック宣言 #717 (ただしブロック以外も可能)
624
+ if (this.check('先に') || this.check('次に')) {
625
+ const tugini = this.get(); // skip 先に | 次に
626
+ if (this.check('comma')) {
627
+ this.get();
628
+ }
629
+ if (this.check('eol')) { // block
630
+ block = this.yBlock();
631
+ if (!this.check('ここまで')) {
632
+ let tuginiType = '次に';
633
+ if (tugini != null) {
634
+ tuginiType = tugini.type;
635
+ }
636
+ throw NakoSyntaxError.fromNode(`『${tuginiType}』...『ここまで』を対応させてください。`, map);
637
+ }
638
+ this.get(); // skip 'ここまで'
639
+ }
640
+ else { // line
641
+ block = this.ySentence();
642
+ }
643
+ }
644
+ else {
645
+ block = this.ySentence();
646
+ }
647
+ // add block
648
+ if (block != null) {
649
+ blocks.push(block);
650
+ }
651
+ }
652
+ if (!this.check('ここまで')) {
653
+ console.log(blocks, this.peek());
654
+ throw NakoSyntaxError.fromNode('『逐次実行』...『ここまで』を対応させてください。', tikuji);
655
+ }
656
+ this.get(); // skip 'ここまで'
657
+ return {
658
+ type: 'tikuji',
659
+ blocks: blocks || [],
660
+ errorBlock: errorBlock || [],
661
+ josi: '',
662
+ ...map,
663
+ end: this.peekSourceMap()
664
+ };
665
+ }
666
+ /**
667
+ * 1つ目の値を与え、その後に続く計算式を取得し、優先規則に沿って並び替えして戻す
668
+ * @param {Ast} firstValue
669
+ */
670
+ yGetArgOperator(firstValue) {
671
+ const args = [firstValue];
672
+ while (!this.isEOF()) {
673
+ // 演算子がある?
674
+ let op = this.peek();
675
+ if (op && opPriority[op.type]) {
676
+ op = this.getCur();
677
+ args.push(op);
678
+ // 演算子後の値を取得
679
+ const v = this.yValue();
680
+ if (v === null) {
681
+ throw NakoSyntaxError.fromNode(`計算式で演算子『${op.value}』後に値がありません`, firstValue);
682
+ }
683
+ args.push(v);
684
+ continue;
685
+ }
686
+ break;
687
+ }
688
+ if (args.length === 0) {
689
+ return null;
690
+ }
691
+ if (args.length === 1) {
692
+ return args[0];
693
+ }
694
+ return this.infixToAST(args);
695
+ }
696
+ yGetArg() {
697
+ // 値を一つ読む
698
+ const value1 = this.yValue();
699
+ if (value1 === null) {
700
+ return null;
701
+ }
702
+ // 計算式がある場合を考慮
703
+ return this.yGetArgOperator(value1);
704
+ }
705
+ infixToPolish(list) {
706
+ // 中間記法から逆ポーランドに変換
707
+ const priority = (t) => {
708
+ if (opPriority[t.type]) {
709
+ return opPriority[t.type];
710
+ }
711
+ return 10;
712
+ };
713
+ const stack = [];
714
+ const polish = [];
715
+ while (list.length > 0) {
716
+ const t = list.shift();
717
+ if (!t) {
718
+ break;
719
+ }
720
+ while (stack.length > 0) { // 優先順位を見て移動する
721
+ const sTop = stack[stack.length - 1];
722
+ if (priority(t) > priority(sTop)) {
723
+ break;
724
+ }
725
+ const tpop = stack.pop();
726
+ if (!tpop) {
727
+ this.logger.error('計算式に間違いがあります。', t);
728
+ break;
729
+ }
730
+ polish.push(tpop);
731
+ }
732
+ stack.push(t);
733
+ }
734
+ // 残った要素を積み替える
735
+ while (stack.length > 0) {
736
+ const t = stack.pop();
737
+ if (t) {
738
+ polish.push(t);
739
+ }
740
+ }
741
+ return polish;
742
+ }
743
+ /** @returns {Ast | null} */
744
+ infixToAST(list) {
745
+ if (list.length === 0) {
746
+ return null;
747
+ }
748
+ // 逆ポーランドを構文木に
749
+ const josi = list[list.length - 1].josi;
750
+ const node = list[list.length - 1];
751
+ const polish = this.infixToPolish(list);
752
+ /** @type {Ast[]} */
753
+ const stack = [];
754
+ for (const t of polish) {
755
+ if (!opPriority[t.type]) { // 演算子ではない
756
+ stack.push(t);
757
+ continue;
758
+ }
759
+ const b = stack.pop();
760
+ const a = stack.pop();
761
+ if (a === undefined || b === undefined) {
762
+ this.logger.debug('--- 計算式(逆ポーランド) ---\n' + JSON.stringify(polish));
763
+ throw NakoSyntaxError.fromNode('計算式でエラー', node);
764
+ }
765
+ /** @type {Ast} */
766
+ const op = {
767
+ type: 'op',
768
+ operator: t.type,
769
+ left: a,
770
+ right: b,
771
+ josi,
772
+ startOffset: a.startOffset,
773
+ endOffset: a.endOffset,
774
+ line: a.line,
775
+ column: a.column,
776
+ file: a.file
777
+ };
778
+ stack.push(op);
779
+ }
780
+ const ans = stack.pop();
781
+ if (!ans) {
782
+ return null;
783
+ }
784
+ return ans;
785
+ }
786
+ yGetArgParen(y) {
787
+ let isClose = false;
788
+ const si = this.stack.length;
789
+ while (!this.isEOF()) {
790
+ if (this.check(')')) {
791
+ isClose = true;
792
+ break;
793
+ }
794
+ const v = this.yGetArg();
795
+ if (v) {
796
+ this.pushStack(v);
797
+ if (this.check('comma')) {
798
+ this.get();
799
+ }
800
+ continue;
801
+ }
802
+ break;
803
+ }
804
+ if (!isClose) {
805
+ throw NakoSyntaxError.fromNode(`C風関数『${y[0].value}』でカッコが閉じていません`, y[0]);
806
+ }
807
+ const a = [];
808
+ while (si < this.stack.length) {
809
+ const v = this.popStack();
810
+ if (v) {
811
+ a.unshift(v);
812
+ }
813
+ }
814
+ return a;
815
+ }
816
+ /** @returns {Ast | null} */
817
+ yRepeatTime() {
818
+ const map = this.peekSourceMap();
819
+ if (!this.check('回')) {
820
+ return null;
821
+ }
822
+ this.get(); // skip '回'
823
+ if (this.check('comma')) {
824
+ this.get();
825
+ } // skip comma
826
+ if (this.check('繰返')) {
827
+ this.get();
828
+ } // skip 'N回、繰り返す' (#924)
829
+ let num = this.popStack([]);
830
+ let multiline = false;
831
+ let block = null;
832
+ if (num === null) {
833
+ num = { type: 'word', value: 'それ', josi: '', ...map, end: this.peekSourceMap() };
834
+ }
835
+ if (this.check('comma')) {
836
+ this.get();
837
+ }
838
+ if (this.check('ここから')) {
839
+ this.get();
840
+ multiline = true;
841
+ }
842
+ else if (this.check('eol')) {
843
+ multiline = true;
844
+ }
845
+ if (multiline) { // multiline
846
+ block = this.yBlock();
847
+ if (this.check('ここまで')) {
848
+ this.get();
849
+ }
850
+ else {
851
+ throw NakoSyntaxError.fromNode('『ここまで』がありません。『回』...『ここまで』を対応させてください。', map);
852
+ }
853
+ }
854
+ else {
855
+ // singleline
856
+ block = this.ySentence();
857
+ }
858
+ return {
859
+ type: 'repeat_times',
860
+ value: num,
861
+ block: block || [],
862
+ josi: '',
863
+ ...map,
864
+ end: this.peekSourceMap()
865
+ };
866
+ }
867
+ /** @returns {Ast | null} */
868
+ yWhile() {
869
+ const map = this.peekSourceMap();
870
+ if (!this.check('間')) {
871
+ return null;
872
+ }
873
+ this.get(); // skip '間'
874
+ while (this.check('comma')) {
875
+ this.get();
876
+ } // skip ','
877
+ if (this.check('繰返')) {
878
+ this.get();
879
+ } // skip '繰り返す' #927
880
+ const cond = this.popStack();
881
+ if (cond === null) {
882
+ throw NakoSyntaxError.fromNode('『間』で条件がありません。', map);
883
+ }
884
+ if (this.check('comma')) {
885
+ this.get();
886
+ }
887
+ if (!this.checkTypes(['ここから', 'eol'])) {
888
+ throw NakoSyntaxError.fromNode('『間』の直後は改行が必要です', map);
889
+ }
890
+ const block = this.yBlock();
891
+ if (this.check('ここまで')) {
892
+ this.get();
893
+ }
894
+ return {
895
+ type: 'while',
896
+ cond,
897
+ block,
898
+ josi: '',
899
+ ...map,
900
+ end: this.peekSourceMap()
901
+ };
902
+ }
903
+ /** @returns {Ast | null} */
904
+ yAtohantei() {
905
+ const map = this.peekSourceMap();
906
+ if (this.check('後判定')) {
907
+ this.get();
908
+ } // skip 後判定
909
+ if (this.check('繰返')) {
910
+ this.get();
911
+ } // skip 繰り返す
912
+ if (this.check('ここから')) {
913
+ this.get();
914
+ }
915
+ const block = this.yBlock();
916
+ if (this.check('ここまで')) {
917
+ this.get();
918
+ }
919
+ if (this.check('comma')) {
920
+ this.get();
921
+ }
922
+ let cond = this.yGetArg(); // 条件
923
+ let bUntil = false;
924
+ const t = this.peek();
925
+ if (t && t.value === 'なる' && (t.josi === 'まで' || t.josi === 'までの')) {
926
+ this.get(); // skip なるまで
927
+ bUntil = true;
928
+ }
929
+ if (this.check('間')) {
930
+ this.get();
931
+ } // skip 間
932
+ if (bUntil) { // 条件を反転する
933
+ cond = {
934
+ type: 'not',
935
+ value: cond,
936
+ josi: '',
937
+ ...map,
938
+ end: this.peekSourceMap()
939
+ };
940
+ }
941
+ return {
942
+ type: 'atohantei',
943
+ cond: cond || [],
944
+ block,
945
+ josi: '',
946
+ ...map,
947
+ end: this.peekSourceMap()
948
+ };
949
+ }
950
+ /** @returns {Ast | null} */
951
+ yFor() {
952
+ const map = this.peekSourceMap();
953
+ if (this.check('繰返') || this.check('増繰返') || this.check('減繰返')) {
954
+ // pass
955
+ }
956
+ else {
957
+ return null;
958
+ }
959
+ const kurikaesu = this.getCur(); // skip 繰り返す
960
+ // スタックに(増や|減ら)してがある?
961
+ const incdec = this.stack.pop();
962
+ if (incdec) {
963
+ if (incdec.type === 'word' && (incdec.value === '増' || incdec.value === '減')) {
964
+ kurikaesu.type = incdec.value + kurikaesu.type;
965
+ // typeを増繰返 | 減繰返 に変換
966
+ }
967
+ else {
968
+ // 普通の繰り返しの場合
969
+ this.stack.push(incdec); // 違ったので改めて追加
970
+ }
971
+ }
972
+ let vInc = null;
973
+ if (kurikaesu.type === '増繰返' || kurikaesu.type === '減繰返') {
974
+ vInc = this.popStack(['ずつ']);
975
+ }
976
+ const vTo = this.popStack(['まで']);
977
+ const vFrom = this.popStack(['から']);
978
+ const word = this.popStack(['を', 'で']);
979
+ if (vFrom === null || vTo === null) {
980
+ throw NakoSyntaxError.fromNode('『繰り返す』文でAからBまでの指定がありません。', kurikaesu);
981
+ }
982
+ if (this.check('comma')) {
983
+ this.get();
984
+ } // skip comma
985
+ let multiline = false;
986
+ if (this.check('ここから')) {
987
+ multiline = true;
988
+ this.get();
989
+ }
990
+ else if (this.check('eol')) {
991
+ multiline = true;
992
+ this.get();
993
+ }
994
+ let block = null;
995
+ if (multiline) {
996
+ block = this.yBlock();
997
+ if (this.check('ここまで')) {
998
+ this.get();
999
+ }
1000
+ else {
1001
+ throw NakoSyntaxError.fromNode('『ここまで』がありません。『繰り返す』...『ここまで』を対応させてください。', map);
1002
+ }
1003
+ }
1004
+ else {
1005
+ block = this.ySentence();
1006
+ }
1007
+ return {
1008
+ type: 'for',
1009
+ from: vFrom,
1010
+ to: vTo,
1011
+ inc: vInc,
1012
+ word,
1013
+ block: block || [],
1014
+ josi: '',
1015
+ ...map,
1016
+ end: this.peekSourceMap()
1017
+ };
1018
+ }
1019
+ /** @returns {Ast | null} */
1020
+ yReturn() {
1021
+ const map = this.peekSourceMap();
1022
+ if (!this.check('戻る')) {
1023
+ return null;
1024
+ }
1025
+ this.get(); // skip '戻る'
1026
+ const v = this.popStack(['で', 'を']);
1027
+ if (this.stack.length > 0) {
1028
+ throw NakoSyntaxError.fromNode('『戻』文の直前に未解決の引数があります。『(式)を戻す』のように式をカッコで括ってください。', map);
1029
+ }
1030
+ return {
1031
+ type: 'return',
1032
+ value: v,
1033
+ josi: '',
1034
+ ...map,
1035
+ end: this.peekSourceMap()
1036
+ };
1037
+ }
1038
+ /** @returns {Ast | null} */
1039
+ yForEach() {
1040
+ const map = this.peekSourceMap();
1041
+ if (!this.check('反復')) {
1042
+ return null;
1043
+ }
1044
+ this.get(); // skip '反復'
1045
+ while (this.check('comma')) {
1046
+ this.get();
1047
+ } // skip ','
1048
+ const target = this.popStack(['']);
1049
+ const name = this.popStack(['で']);
1050
+ let block = null;
1051
+ let multiline = false;
1052
+ if (this.check('ここから')) {
1053
+ multiline = true;
1054
+ this.get();
1055
+ }
1056
+ else if (this.check('eol')) {
1057
+ multiline = true;
1058
+ }
1059
+ if (multiline) {
1060
+ block = this.yBlock();
1061
+ if (this.check('ここまで')) {
1062
+ this.get();
1063
+ }
1064
+ }
1065
+ else {
1066
+ block = this.ySentence();
1067
+ }
1068
+ return {
1069
+ type: 'foreach',
1070
+ name,
1071
+ target,
1072
+ block: block || [],
1073
+ josi: '',
1074
+ ...map,
1075
+ end: this.peekSourceMap()
1076
+ };
1077
+ }
1078
+ /** 条件分岐構文 */
1079
+ ySwitch() {
1080
+ const map = this.peekSourceMap();
1081
+ if (!this.check('条件分岐')) {
1082
+ return null;
1083
+ }
1084
+ const joukenbunki = this.get(); // skip '条件分岐'
1085
+ if (!joukenbunki) {
1086
+ return null;
1087
+ }
1088
+ const eol = this.get(); // skip 'eol'
1089
+ if (!eol) {
1090
+ return null;
1091
+ }
1092
+ const value = this.popStack(['で']);
1093
+ if (!value) {
1094
+ throw NakoSyntaxError.fromNode('『(値)で条件分岐』のように記述してください。', joukenbunki);
1095
+ }
1096
+ if (eol.type !== 'eol') {
1097
+ throw NakoSyntaxError.fromNode('『条件分岐』の直後は改行してください。', joukenbunki);
1098
+ }
1099
+ let isDefaultClause = false; // 「違えば」内かどうか
1100
+ let skippedKokomade = false;
1101
+ const cases = [];
1102
+ while (!this.isEOF()) {
1103
+ if (this.check('ここまで')) {
1104
+ if (skippedKokomade) {
1105
+ throw NakoSyntaxError.fromNode('『条件分岐』は『(条件)ならば〜ここまで』と記述してください。', joukenbunki);
1106
+ }
1107
+ this.get(); // skip ここまで
1108
+ break;
1109
+ }
1110
+ if (this.check('eol')) {
1111
+ this.get();
1112
+ continue;
1113
+ }
1114
+ if (isDefaultClause) {
1115
+ throw NakoSyntaxError.fromNode('『条件分岐』で『違えば〜ここまで』の後に処理を続けることは出来ません。', joukenbunki);
1116
+ }
1117
+ // 違えば?
1118
+ let cond = null;
1119
+ const condToken = this.peek();
1120
+ if (condToken && condToken.type === '違えば') {
1121
+ // 違えば
1122
+ skippedKokomade = false;
1123
+ isDefaultClause = true;
1124
+ cond = this.get(); // skip 違えば
1125
+ if (this.check('comma')) {
1126
+ this.get();
1127
+ } // skip ','
1128
+ }
1129
+ else {
1130
+ // ***ならば
1131
+ if (skippedKokomade) {
1132
+ throw NakoSyntaxError.fromNode('『条件分岐』は『(条件)ならば〜ここまで』と記述してください。', joukenbunki);
1133
+ }
1134
+ // 「**ならば」を得る
1135
+ cond = this.yValue();
1136
+ if (!cond) {
1137
+ throw NakoSyntaxError.fromNode('『条件分岐』は『(条件)ならば〜ここまで』と記述してください。', joukenbunki);
1138
+ }
1139
+ const naraba = this.get(); // skip ならば
1140
+ if (!naraba || naraba.type !== 'ならば') {
1141
+ throw NakoSyntaxError.fromNode('『条件分岐』で条件は**ならばと記述してください。', joukenbunki);
1142
+ }
1143
+ if (this.check('comma')) {
1144
+ this.get();
1145
+ } // skip ','
1146
+ }
1147
+ // 条件にあったときに実行すること
1148
+ const condBlock = this.yBlock();
1149
+ const kokomade = this.peek();
1150
+ if (kokomade && kokomade.type === 'ここまで') {
1151
+ this.get(); // skip ここまで
1152
+ }
1153
+ else {
1154
+ if (isDefaultClause) {
1155
+ throw NakoSyntaxError.fromNode('『条件分岐』は『違えば〜ここまで』と記述してください。', joukenbunki);
1156
+ }
1157
+ // 次が「違えば」の場合に限り、「もし〜ここまで」の「ここまで」を省略できる
1158
+ skippedKokomade = true;
1159
+ }
1160
+ cases.push([cond, condBlock]);
1161
+ }
1162
+ return {
1163
+ type: 'switch',
1164
+ value,
1165
+ cases: cases || [],
1166
+ josi: '',
1167
+ ...map,
1168
+ end: this.peekSourceMap()
1169
+ };
1170
+ }
1171
+ /** 無名関数 */
1172
+ yMumeiFunc() {
1173
+ const map = this.peekSourceMap();
1174
+ if (!this.check('def_func')) {
1175
+ return null;
1176
+ }
1177
+ const def = this.get();
1178
+ if (!def) {
1179
+ return null;
1180
+ }
1181
+ let args = [];
1182
+ // 「,」を飛ばす
1183
+ if (this.check('comma')) {
1184
+ this.get();
1185
+ }
1186
+ // 関数の引数定義は省略できる
1187
+ if (this.check('(')) {
1188
+ args = this.yDefFuncReadArgs() || [];
1189
+ }
1190
+ // 「,」を飛ばす
1191
+ if (this.check('comma')) {
1192
+ this.get();
1193
+ }
1194
+ // ブロックを読む
1195
+ this.funcLevel++;
1196
+ this.saveStack();
1197
+ const block = this.yBlock();
1198
+ // 末尾の「ここまで」をチェック - もしなければエラーにする #1045
1199
+ if (!this.check('ここまで')) {
1200
+ throw NakoSyntaxError.fromNode('『ここまで』がありません。『には』構文か無名関数の末尾に『ここまで』が必要です。', map);
1201
+ }
1202
+ this.get(); // skip ここまで
1203
+ this.loadStack();
1204
+ this.funcLevel--;
1205
+ return {
1206
+ type: 'func_obj',
1207
+ args,
1208
+ block,
1209
+ meta: def.meta,
1210
+ josi: '',
1211
+ ...map,
1212
+ end: this.peekSourceMap()
1213
+ };
1214
+ }
1215
+ /** 代入構文 */
1216
+ yDainyu() {
1217
+ const map = this.peekSourceMap();
1218
+ const dainyu = this.get(); // 代入
1219
+ if (dainyu === null) {
1220
+ return null;
1221
+ }
1222
+ const value = this.popStack(['を']);
1223
+ const word = this.popStack(['へ', 'に']);
1224
+ if (!word || (word.type !== 'word' && word.type !== 'func' && word.type !== '配列参照')) {
1225
+ throw NakoSyntaxError.fromNode('代入文で代入先の変数が見当たりません。『(変数名)に(値)を代入』のように使います。', dainyu);
1226
+ }
1227
+ // 配列への代入
1228
+ if (word.type === '配列参照') {
1229
+ return {
1230
+ type: 'let_array',
1231
+ name: word.name,
1232
+ index: word.index,
1233
+ value,
1234
+ josi: '',
1235
+ checkInit: this.flagCheckArrayInit,
1236
+ ...map,
1237
+ end: this.peekSourceMap()
1238
+ };
1239
+ }
1240
+ // 一般的な変数への代入
1241
+ const word2 = this.getVarName(word);
1242
+ return {
1243
+ type: 'let',
1244
+ name: word2,
1245
+ value,
1246
+ josi: '',
1247
+ ...map,
1248
+ end: this.peekSourceMap()
1249
+ };
1250
+ }
1251
+ /** 定める構文 */
1252
+ ySadameru() {
1253
+ const map = this.peekSourceMap();
1254
+ const sadameru = this.get(); // 定める
1255
+ if (sadameru === null) {
1256
+ return null;
1257
+ }
1258
+ const word = this.popStack(['を']);
1259
+ const value = this.popStack(['へ', 'に']);
1260
+ if (!word || (word.type !== 'word' && word.type !== 'func' && word.type !== '配列参照')) {
1261
+ throw NakoSyntaxError.fromNode('『定める』文で定数が見当たりません。『(定数名)(値)に定める』のように使います。', sadameru);
1262
+ }
1263
+ // 変数を生成する
1264
+ const nameToken = this.getVarName(word);
1265
+ return {
1266
+ type: 'def_local_var',
1267
+ name: nameToken,
1268
+ vartype: '定数',
1269
+ value,
1270
+ josi: '',
1271
+ ...map,
1272
+ end: this.peekSourceMap()
1273
+ };
1274
+ }
1275
+ yIncDec() {
1276
+ const map = this.peekSourceMap();
1277
+ const action = this.get(); // (増やす|減らす)
1278
+ if (action === null) {
1279
+ return null;
1280
+ }
1281
+ // 『Nずつ増やして繰り返す』文か?
1282
+ if (this.check('繰返')) {
1283
+ this.pushStack({ type: 'word', value: action.value, josi: action.josi, ...map, end: this.peekSourceMap() });
1284
+ return this.yFor();
1285
+ }
1286
+ // スタックから引数をポップ
1287
+ let value = this.popStack(['だけ', '']);
1288
+ if (!value) {
1289
+ value = { type: 'number', value: 1, josi: 'だけ', ...map, end: this.peekSourceMap() };
1290
+ }
1291
+ const word = this.popStack(['を']);
1292
+ if (!word || (word.type !== 'word' && word.type !== '配列参照')) {
1293
+ throw NakoSyntaxError.fromNode(`『${action.type}』文で定数が見当たりません。『(変数名)を(値)だけ${action.type}』のように使います。`, action);
1294
+ }
1295
+ // 減らすなら-1かける
1296
+ if (action.value === '減') {
1297
+ value = { type: 'op', operator: '*', left: value, right: { type: 'number', value: -1, line: action.line }, josi: '', ...map };
1298
+ }
1299
+ return {
1300
+ type: 'inc',
1301
+ name: word,
1302
+ value,
1303
+ josi: action.josi,
1304
+ ...map,
1305
+ end: this.peekSourceMap()
1306
+ };
1307
+ }
1308
+ yCall() {
1309
+ if (this.isEOF()) {
1310
+ return null;
1311
+ }
1312
+ // スタックに積んでいく
1313
+ while (!this.isEOF()) {
1314
+ if (this.check('ここから')) {
1315
+ this.get();
1316
+ }
1317
+ // 代入
1318
+ if (this.check('代入')) {
1319
+ return this.yDainyu();
1320
+ }
1321
+ if (this.check('定める')) {
1322
+ return this.ySadameru();
1323
+ }
1324
+ // 制御構文
1325
+ if (this.check('回')) {
1326
+ return this.yRepeatTime();
1327
+ }
1328
+ if (this.check('')) {
1329
+ return this.yWhile();
1330
+ }
1331
+ if (this.check('繰返') || this.check('増繰返') || this.check('減繰返')) {
1332
+ return this.yFor();
1333
+ }
1334
+ if (this.check('反復')) {
1335
+ return this.yForEach();
1336
+ }
1337
+ if (this.check('条件分岐')) {
1338
+ return this.ySwitch();
1339
+ }
1340
+ if (this.check('戻る')) {
1341
+ return this.yReturn();
1342
+ }
1343
+ if (this.check('') || this.check('減')) {
1344
+ return this.yIncDec();
1345
+ }
1346
+ // C言語風関数
1347
+ if (this.check2([['func', 'word'], '('])) { // C言語風
1348
+ const cur = this.peek();
1349
+ if (cur && cur.josi === '') {
1350
+ const t = this.yValue();
1351
+ if (t) {
1352
+ const josi = t.josi || '';
1353
+ if (t.type === 'func' && (t.josi === '' || keizokuJosi.indexOf(josi) >= 0)) {
1354
+ t.josi = '';
1355
+ return t; // 関数なら値とする
1356
+ }
1357
+ this.pushStack(t);
1358
+ }
1359
+ if (this.check('comma')) {
1360
+ this.get();
1361
+ }
1362
+ continue;
1363
+ }
1364
+ }
1365
+ // なでしこ式関数
1366
+ if (this.check('func')) {
1367
+ const r = this.yCallFunc();
1368
+ if (r === null) {
1369
+ continue;
1370
+ }
1371
+ // 「〜する間」の形ならスタックに積む。
1372
+ if (this.check('間')) {
1373
+ this.pushStack(r);
1374
+ continue;
1375
+ }
1376
+ // 関数呼び出しの直後に、四則演算があるか?
1377
+ if (!this.checkTypes(operatorList)) {
1378
+ return r;
1379
+ } // なければ関数呼び出しを戻す
1380
+ // 四則演算があった場合、計算してスタックに載せる
1381
+ this.pushStack(this.yGetArgOperator(r));
1382
+ continue;
1383
+ }
1384
+ // 値のとき → スタックに載せる
1385
+ const t = this.yGetArg();
1386
+ if (t) {
1387
+ this.pushStack(t);
1388
+ continue;
1389
+ }
1390
+ break;
1391
+ } // end of while
1392
+ // 助詞が余ってしまった場合
1393
+ if (this.stack.length > 0) {
1394
+ this.logger.debug('--- stack dump ---\n' + JSON.stringify(this.stack, null, 2) + '\npeek: ' + JSON.stringify(this.peek(), null, 2));
1395
+ let msgDebug = `不完全な文です。${this.stack.map((n) => this.nodeToStr(n, { depth: 0 }, true)).join('、')}が解決していません。`;
1396
+ let msg = `不完全な文です。${this.stack.map((n) => this.nodeToStr(n, { depth: 0 }, false)).join('、')}が解決していません。`;
1397
+ // 各ノードについて、更に詳細な情報があるなら表示
1398
+ for (const n of this.stack) {
1399
+ const d0 = this.nodeToStr(n, { depth: 0 }, false);
1400
+ const d1 = this.nodeToStr(n, { depth: 1 }, false);
1401
+ if (d0 !== d1) {
1402
+ msgDebug += `${this.nodeToStr(n, { depth: 0 }, true)}は${this.nodeToStr(n, { depth: 1 }, true)}として使われています。`;
1403
+ msg += `${d0}は${d1}として使われています。`;
1404
+ }
1405
+ }
1406
+ const first = this.stack[0];
1407
+ const last = this.stack[this.stack.length - 1];
1408
+ this.logger.debug(msgDebug, first);
1409
+ throw NakoSyntaxError.fromNode(msg, first, last);
1410
+ }
1411
+ return this.popStack([]);
1412
+ }
1413
+ /** @returns {Ast | null} */
1414
+ yCallFunc() {
1415
+ const map = this.peekSourceMap();
1416
+ const t = this.get();
1417
+ if (!t) {
1418
+ return null;
1419
+ }
1420
+ const f = t.meta;
1421
+ const funcName = t.value;
1422
+ // (関数)には ... 構文 ... https://github.com/kujirahand/nadesiko3/issues/66
1423
+ let funcObj = null;
1424
+ if (t.josi === 'には') {
1425
+ try {
1426
+ funcObj = this.yMumeiFunc();
1427
+ }
1428
+ catch (err) {
1429
+ throw NakoSyntaxError.fromNode(`『${t.value}には...』で無名関数の定義で以下の間違いがあります。\n${err.message}`, t);
1430
+ }
1431
+ if (funcObj === null) {
1432
+ throw NakoSyntaxError.fromNode('『Fには』構文がありましたが、関数定義が見当たりません。', t);
1433
+ }
1434
+ }
1435
+ if (!f || typeof f.josi === 'undefined') {
1436
+ throw NakoSyntaxError.fromNode('関数の定義でエラー。', t);
1437
+ }
1438
+ // 最近使った関数を記録
1439
+ this.recentlyCalledFunc.push({ name: funcName, ...f });
1440
+ // 呼び出す関数が非同期呼び出しが必要(asyncFn)ならマーク
1441
+ if (f && f.asyncFn) {
1442
+ this.usedAsyncFn = true;
1443
+ }
1444
+ // 関数の引数を取り出す処理
1445
+ const args = [];
1446
+ let nullCount = 0;
1447
+ let valueCount = 0;
1448
+ for (let i = 0; i < f.josi.length; i++) {
1449
+ while (true) {
1450
+ // スタックから任意の助詞を持つ値を一つ取り出す、助詞がなければ末尾から得る
1451
+ let popArg = this.popStack(f.josi[i]);
1452
+ if (popArg !== null) {
1453
+ valueCount++;
1454
+ }
1455
+ else if (i < f.josi.length - 1 || !f.isVariableJosi) {
1456
+ nullCount++;
1457
+ popArg = funcObj;
1458
+ }
1459
+ else {
1460
+ break;
1461
+ }
1462
+ if (popArg !== null && f.funcPointers !== undefined && f.funcPointers[i] !== null) {
1463
+ if (popArg.type === 'func') { // 引数が関数の参照渡しに該当する場合、typeを『func_pointer』に変更
1464
+ popArg.type = 'func_pointer';
1465
+ }
1466
+ else {
1467
+ const varname = (f.varnames) ? f.varnames[i] : `${i + 1}番目の引数`;
1468
+ throw NakoSyntaxError.fromNode(`関数『${t.value}』の引数『${varname}』には関数オブジェクトが必要です。`, t);
1469
+ }
1470
+ }
1471
+ args.push(popArg);
1472
+ if (i < f.josi.length - 1 || !f.isVariableJosi) {
1473
+ break;
1474
+ }
1475
+ }
1476
+ }
1477
+ // 1つだけなら、変数「それ」で補完される
1478
+ if (nullCount >= 2 && (valueCount > 0 || t.josi === '' || keizokuJosi.indexOf(t.josi) >= 0)) {
1479
+ throw NakoSyntaxError.fromNode(`関数『${t.value}』の引数が不足しています。`, t);
1480
+ }
1481
+ this.usedFuncs.add(t.value);
1482
+ // 関数呼び出しのAstを構築
1483
+ const funcNode = {
1484
+ type: 'func',
1485
+ name: t.value,
1486
+ args,
1487
+ josi: t.josi,
1488
+ ...map,
1489
+ end: this.peekSourceMap()
1490
+ };
1491
+ // 「プラグイン名設定」ならば、そこでスコープを変更することを意味する
1492
+ if (funcNode.name === 'プラグイン名設定') {
1493
+ if (args.length > 0 && args[0]) {
1494
+ let fname = '' + args[0].value;
1495
+ if (fname === 'メイン') {
1496
+ fname = '' + args[0].file;
1497
+ }
1498
+ this.modName = NakoLexer.filenameToModName(fname);
1499
+ }
1500
+ }
1501
+ // 言い切りならそこで一度切る
1502
+ if (t.josi === '') {
1503
+ return funcNode;
1504
+ }
1505
+ // 「**して、**」の場合も一度切る
1506
+ if (keizokuJosi.indexOf(t.josi) >= 0) {
1507
+ funcNode.josi = 'して';
1508
+ return funcNode;
1509
+ }
1510
+ // 続き
1511
+ funcNode.meta = f;
1512
+ this.pushStack(funcNode);
1513
+ return null;
1514
+ }
1515
+ /** 関数呼び出し演算子 #891
1516
+ * @returns {Ast | null} */
1517
+ yCallOp() {
1518
+ if (!this.check2(['func', '←'])) {
1519
+ return null;
1520
+ }
1521
+ const map = this.peekSourceMap();
1522
+ // 関数名を得る
1523
+ const word = this.get();
1524
+ if (word == null) {
1525
+ throw new Error('関数が取得できません。');
1526
+ }
1527
+ try {
1528
+ const op = this.get();
1529
+ if (op == null) {
1530
+ throw new Error('関数呼び出し演算子が取得できません。');
1531
+ }
1532
+ const funcName = word.value;
1533
+ // 関数の引数なしをチェック
1534
+ if (!word.meta) {
1535
+ throw new Error('関数本体を取得できません。');
1536
+ }
1537
+ if (!word.meta.josi) {
1538
+ throw new Error('関数の引数情報を取得できません。');
1539
+ }
1540
+ const argCount = word.meta.josi.length;
1541
+ if (argCount === 0) {
1542
+ throw NakoSyntaxError.fromNode(`引数がない関数『${funcName}』を関数呼び出し演算子で呼び出すことはできません。`, word);
1543
+ }
1544
+ // 引数を順に取得
1545
+ const curStackPos = this.stack.length;
1546
+ while (!this.isEOF()) {
1547
+ const t = this.yGetArg();
1548
+ if (t) {
1549
+ this.pushStack(t);
1550
+ if ((this.stack.length - curStackPos) === argCount) {
1551
+ break;
1552
+ }
1553
+ continue;
1554
+ }
1555
+ break;
1556
+ }
1557
+ // この場合第一引数の省略は認めない
1558
+ const realArgCount = this.stack.length - curStackPos;
1559
+ if (realArgCount !== argCount) {
1560
+ throw NakoSyntaxError.fromNode(`関数『${funcName}』呼び出しで引数の数(${realArgCount})が定義(${argCount})と違います。`, word);
1561
+ }
1562
+ // 引数を取り出す
1563
+ const tmpList = this.stack.splice(curStackPos, argCount);
1564
+ // 引数が1つなら助詞は省略が可能。ただし、引数が2つ以上の時、正しく助詞の順序を入れ替える
1565
+ let argList = tmpList;
1566
+ if (argCount >= 2) {
1567
+ argList = [];
1568
+ const defList = word.meta.josi;
1569
+ defList.forEach((josiList, i) => {
1570
+ for (let j = 0; j < tmpList.length; j++) {
1571
+ const t = tmpList[j];
1572
+ if (josiList.indexOf(t.josi) >= 0) {
1573
+ argList[i] = t;
1574
+ return;
1575
+ }
1576
+ }
1577
+ const josiStr = josiList.join(',');
1578
+ throw new Error(`助詞『${josiStr}』が見当たりません。`);
1579
+ });
1580
+ }
1581
+ this.usedFuncs.add(funcName);
1582
+ // funcノードを返す
1583
+ return {
1584
+ type: 'func',
1585
+ name: funcName,
1586
+ args: argList,
1587
+ setter: true,
1588
+ josi: '',
1589
+ ...map,
1590
+ end: this.peekSourceMap()
1591
+ };
1592
+ }
1593
+ catch (err) {
1594
+ this.logger.debug(`${this.nodeToStr(word, { depth: 0 }, true)}の関数呼び出しで引数(『←』以降)が読み取れません。`, word);
1595
+ throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 0 }, false)}の関数呼び出しでエラーがあります。\n${err.message}`, word);
1596
+ }
1597
+ }
1598
+ /** @returns {Ast | null} */
1599
+ yLet() {
1600
+ const map = this.peekSourceMap();
1601
+ // 通常の変数
1602
+ if (this.check2(['word', 'eq'])) {
1603
+ const word = this.peek();
1604
+ let threw = false;
1605
+ try {
1606
+ if (this.accept(['word', 'eq', this.yCalc]) || this.accept(['word', 'eq', this.ySentence])) {
1607
+ if (this.y[2].type === 'eol') {
1608
+ throw new Error('値が空です。');
1609
+ }
1610
+ if (this.check('comma')) {
1611
+ this.get();
1612
+ } // skip comma (ex) name1=val1, name2=val2
1613
+ const nameToken = this.getVarName(this.y[0]);
1614
+ const valueToken = this.y[2];
1615
+ return {
1616
+ type: 'let',
1617
+ name: nameToken,
1618
+ value: valueToken,
1619
+ ...map,
1620
+ end: this.peekSourceMap()
1621
+ };
1622
+ }
1623
+ else {
1624
+ threw = true;
1625
+ this.logger.debug(`${this.nodeToStr(word, { depth: 1 }, true)}への代入文で計算式に書き間違いがあります。`, word);
1626
+ throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文で計算式に書き間違いがあります。`, map);
1627
+ }
1628
+ }
1629
+ catch (err) {
1630
+ if (threw) {
1631
+ throw err;
1632
+ }
1633
+ this.logger.debug(`${this.nodeToStr(word, { depth: 1 }, true)}への代入文で計算式に以下の書き間違いがあります。\n${err.message}`, word);
1634
+ throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文で計算式に以下の書き間違いがあります。\n${err.message}`, map);
1635
+ }
1636
+ }
1637
+ // let_array ?
1638
+ if (this.check2(['word', '@'])) {
1639
+ const la = this.yLetArrayAt(map);
1640
+ if (this.check('comma')) {
1641
+ this.get();
1642
+ } // skip comma (ex) name1=val1, name2=val2
1643
+ if (la) {
1644
+ la.checkInit = this.flagCheckArrayInit;
1645
+ return la;
1646
+ }
1647
+ }
1648
+ if (this.check2(['word', '['])) {
1649
+ const lb = this.yLetArrayBracket(map);
1650
+ if (this.check('comma')) {
1651
+ this.get();
1652
+ } // skip comma (ex) name1=val1, name2=val2
1653
+ if (lb) {
1654
+ lb.checkInit = this.flagCheckArrayInit;
1655
+ return lb;
1656
+ }
1657
+ }
1658
+ // ローカル変数定義
1659
+ if (this.accept(['word', 'とは'])) {
1660
+ const word = this.getVarName(this.y[0]);
1661
+ if (!this.checkTypes(['変数', '定数'])) {
1662
+ throw NakoSyntaxError.fromNode('ローカル変数『' + word.value + '』の定義エラー', word);
1663
+ }
1664
+ const vtype = this.getCur(); // 変数
1665
+ // 初期値がある?
1666
+ let value = null;
1667
+ if (this.check('eq')) {
1668
+ this.get();
1669
+ value = this.yCalc();
1670
+ }
1671
+ if (this.check('comma')) {
1672
+ this.get();
1673
+ } // skip comma (ex) name1=val1, name2=val2
1674
+ return {
1675
+ type: 'def_local_var',
1676
+ name: word,
1677
+ vartype: vtype.type,
1678
+ value,
1679
+ ...map,
1680
+ end: this.peekSourceMap()
1681
+ };
1682
+ }
1683
+ // ローカル変数定義(その2)
1684
+ if (this.accept(['変数', 'word', 'eq', this.yCalc])) {
1685
+ const word = this.getVarName(this.y[1]);
1686
+ return {
1687
+ type: 'def_local_var',
1688
+ name: word,
1689
+ vartype: '変数',
1690
+ value: this.y[3],
1691
+ ...map,
1692
+ end: this.peekSourceMap()
1693
+ };
1694
+ }
1695
+ if (this.accept(['定数', 'word', 'eq', this.yCalc])) {
1696
+ const word = this.getVarName(this.y[1]);
1697
+ return {
1698
+ type: 'def_local_var',
1699
+ name: word,
1700
+ vartype: '定数',
1701
+ value: this.y[3],
1702
+ ...map,
1703
+ end: this.peekSourceMap()
1704
+ };
1705
+ }
1706
+ // 複数定数への代入 #563
1707
+ if (this.accept(['定数', this.yJSONArray, 'eq', this.yCalc])) {
1708
+ const names = this.y[1];
1709
+ // check array
1710
+ if (names && names.value instanceof Array) {
1711
+ for (const i in names.value) {
1712
+ if (names.value[i].type !== 'word') {
1713
+ throw NakoSyntaxError.fromNode(`複数定数の代入文${i + 1}番目でエラー。『定数[A,B,C]=[1,2,3]』の書式で記述してください。`, this.y[0]);
1714
+ }
1715
+ }
1716
+ }
1717
+ else {
1718
+ throw NakoSyntaxError.fromNode('複数定数の代入文でエラー。『定数[A,B,C]=[1,2,3]』の書式で記述してください。', this.y[0]);
1719
+ }
1720
+ names.value = this.getVarNameList(names.value);
1721
+ return {
1722
+ type: 'def_local_varlist',
1723
+ names: names.value,
1724
+ vartype: '定数',
1725
+ value: this.y[3],
1726
+ ...map,
1727
+ end: this.peekSourceMap()
1728
+ };
1729
+ }
1730
+ // 複数変数への代入 #563
1731
+ if (this.accept(['変数', this.yJSONArray, 'eq', this.yCalc])) {
1732
+ const names = this.y[1];
1733
+ // check array
1734
+ if (names && names.value instanceof Array) {
1735
+ for (const i in names.value) {
1736
+ if (names.value[i].type !== 'word') {
1737
+ throw NakoSyntaxError.fromNode(`複数変数の代入文${i + 1}番目でエラー。『変数[A,B,C]=[1,2,3]』の書式で記述してください。`, this.y[0]);
1738
+ }
1739
+ }
1740
+ }
1741
+ else {
1742
+ throw NakoSyntaxError.fromNode('複数変数の代入文でエラー。『変数[A,B,C]=[1,2,3]』の書式で記述してください。', this.y[0]);
1743
+ }
1744
+ names.value = this.getVarNameList(names.value);
1745
+ return {
1746
+ type: 'def_local_varlist',
1747
+ names: names.value,
1748
+ vartype: '変数',
1749
+ value: this.y[3],
1750
+ ...map,
1751
+ end: this.peekSourceMap()
1752
+ };
1753
+ }
1754
+ // 複数変数への代入 #563
1755
+ if (this.check2(['word', 'comma', 'word'])) {
1756
+ // 2 word
1757
+ if (this.accept(['word', 'comma', 'word', 'eq', this.yCalc])) {
1758
+ let names = [this.y[0], this.y[2]];
1759
+ names = this.getVarNameList(names);
1760
+ return {
1761
+ type: 'def_local_varlist',
1762
+ names,
1763
+ vartype: '変数',
1764
+ value: this.y[4],
1765
+ ...map,
1766
+ end: this.peekSourceMap()
1767
+ };
1768
+ }
1769
+ // 3 word
1770
+ if (this.accept(['word', 'comma', 'word', 'comma', 'word', 'eq', this.yCalc])) {
1771
+ let names = [this.y[0], this.y[2], this.y[4]];
1772
+ names = this.getVarNameList(names);
1773
+ return {
1774
+ type: 'def_local_varlist',
1775
+ names,
1776
+ vartype: '変数',
1777
+ value: this.y[6],
1778
+ ...map,
1779
+ end: this.peekSourceMap()
1780
+ };
1781
+ }
1782
+ // 4 word
1783
+ if (this.accept(['word', 'comma', 'word', 'comma', 'word', 'comma', 'word', 'eq', this.yCalc])) {
1784
+ let names = [this.y[0], this.y[2], this.y[4], this.y[6]];
1785
+ names = this.getVarNameList(names);
1786
+ return {
1787
+ type: 'def_local_varlist',
1788
+ names,
1789
+ vartype: '変数',
1790
+ value: this.y[8],
1791
+ ...map,
1792
+ end: this.peekSourceMap()
1793
+ };
1794
+ }
1795
+ // 5 word
1796
+ if (this.accept(['word', 'comma', 'word', 'comma', 'word', 'comma', 'word', 'comma', 'word', 'eq', this.yCalc])) {
1797
+ let names = [this.y[0], this.y[2], this.y[4], this.y[6], this.y[8]];
1798
+ names = this.getVarNameList(names);
1799
+ return {
1800
+ type: 'def_local_varlist',
1801
+ names,
1802
+ vartype: '変数',
1803
+ value: this.y[10],
1804
+ ...map,
1805
+ end: this.peekSourceMap()
1806
+ };
1807
+ }
1808
+ }
1809
+ return null;
1810
+ }
1811
+ /**
1812
+ * 配列のインデックスが1から始まる場合を考慮するか
1813
+ * @param {Ast} node
1814
+ * @returns
1815
+ */
1816
+ checkArrayIndex(node) {
1817
+ // 配列が0から始まるのであればそのまま返す
1818
+ if (this.arrayIndexFrom === 0) {
1819
+ return node;
1820
+ }
1821
+ // 配列が1から始まるのであれば演算を加えて返す
1822
+ return {
1823
+ ...node,
1824
+ 'type': 'op',
1825
+ 'operator': '-',
1826
+ 'left': node,
1827
+ 'right': {
1828
+ ...node,
1829
+ 'type': 'number',
1830
+ 'value': this.arrayIndexFrom
1831
+ }
1832
+ };
1833
+ }
1834
+ /**
1835
+ * 配列のインデックスを逆順にするのを考慮するか
1836
+ * @param {Ast[]| null} ary
1837
+ */
1838
+ checkArrayReverse(ary) {
1839
+ if (!ary) {
1840
+ return [];
1841
+ }
1842
+ if (!this.flagReverseArrayIndex) {
1843
+ return ary;
1844
+ }
1845
+ // 二次元以上の配列変数のアクセスを[y][x]ではなく[x][y]と順序を変更する
1846
+ if (ary.length <= 1) {
1847
+ return ary;
1848
+ }
1849
+ return ary.reverse();
1850
+ }
1851
+ /** @returns {Ast | null} */
1852
+ yLetArrayAt(map) {
1853
+ // 一次元配列
1854
+ if (this.accept(['word', '@', this.yValue, 'eq', this.yCalc])) {
1855
+ return {
1856
+ type: 'let_array',
1857
+ name: this.getVarName(this.y[0]),
1858
+ index: [this.checkArrayIndex(this.y[2])],
1859
+ value: this.y[4],
1860
+ ...map,
1861
+ end: this.peekSourceMap()
1862
+ };
1863
+ }
1864
+ // 二次元配列
1865
+ if (this.accept(['word', '@', this.yValue, '@', this.yValue, 'eq', this.yCalc])) {
1866
+ return {
1867
+ type: 'let_array',
1868
+ name: this.getVarName(this.y[0]),
1869
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4])]),
1870
+ value: this.y[6],
1871
+ ...map,
1872
+ end: this.peekSourceMap()
1873
+ };
1874
+ }
1875
+ // 三次元配列
1876
+ if (this.accept(['word', '@', this.yValue, '@', this.yValue, '@', this.yValue, 'eq', this.yCalc])) {
1877
+ return {
1878
+ type: 'let_array',
1879
+ name: this.getVarName(this.y[0]),
1880
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4]), this.checkArrayIndex(this.y[6])]),
1881
+ value: this.y[8],
1882
+ ...map,
1883
+ end: this.peekSourceMap()
1884
+ };
1885
+ }
1886
+ // 二次元配列(カンマ指定)
1887
+ if (this.accept(['word', '@', this.yValue, 'comma', this.yValue, 'eq', this.yCalc])) {
1888
+ return {
1889
+ type: 'let_array',
1890
+ name: this.getVarName(this.y[0]),
1891
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4])]),
1892
+ value: this.y[6],
1893
+ ...map,
1894
+ end: this.peekSourceMap()
1895
+ };
1896
+ }
1897
+ // 三次元配列(カンマ指定)
1898
+ if (this.accept(['word', '@', this.yValue, 'comma', this.yValue, 'comma', this.yValue, 'eq', this.yCalc])) {
1899
+ return {
1900
+ type: 'let_array',
1901
+ name: this.getVarName(this.y[0]),
1902
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4]), this.checkArrayIndex(this.y[6])]),
1903
+ value: this.y[8],
1904
+ ...map,
1905
+ end: this.peekSourceMap()
1906
+ };
1907
+ }
1908
+ return null;
1909
+ }
1910
+ /** @returns {Ast | null} */
1911
+ yLetArrayBracket(map) {
1912
+ // 一次元配列
1913
+ if (this.accept(['word', '[', this.yCalc, ']', 'eq', this.yCalc])) {
1914
+ return {
1915
+ type: 'let_array',
1916
+ name: this.getVarName(this.y[0]),
1917
+ index: [this.checkArrayIndex(this.y[2])],
1918
+ value: this.y[5],
1919
+ ...map,
1920
+ end: this.peekSourceMap()
1921
+ };
1922
+ }
1923
+ // 二次元配列
1924
+ if (this.accept(['word', '[', this.yCalc, ']', '[', this.yCalc, ']', 'eq', this.yCalc])) {
1925
+ return {
1926
+ type: 'let_array',
1927
+ name: this.getVarName(this.y[0]),
1928
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[5])]),
1929
+ value: this.y[8],
1930
+ tag: '2',
1931
+ ...map,
1932
+ end: this.peekSourceMap()
1933
+ };
1934
+ }
1935
+ if (this.accept(['word', '[', this.yCalc, 'comma', this.yCalc, ']', 'eq', this.yCalc])) {
1936
+ return {
1937
+ type: 'let_array',
1938
+ name: this.getVarName(this.y[0]),
1939
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4])]),
1940
+ value: this.y[7],
1941
+ tag: '2',
1942
+ ...map,
1943
+ end: this.peekSourceMap()
1944
+ };
1945
+ }
1946
+ // 三次元配列
1947
+ if (this.accept(['word', '[', this.yCalc, ']', '[', this.yCalc, ']', '[', this.yCalc, ']', 'eq', this.yCalc])) {
1948
+ return {
1949
+ type: 'let_array',
1950
+ name: this.getVarName(this.y[0]),
1951
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[5]), this.checkArrayIndex(this.y[8])]),
1952
+ value: this.y[11],
1953
+ ...map,
1954
+ end: this.peekSourceMap()
1955
+ };
1956
+ }
1957
+ if (this.accept(['word', '[', this.yCalc, 'comma', this.yCalc, 'comma', this.yCalc, ']', 'eq', this.yCalc])) {
1958
+ return {
1959
+ type: 'let_array',
1960
+ name: this.getVarName(this.y[0]),
1961
+ index: this.checkArrayReverse([this.checkArrayIndex(this.y[2]), this.checkArrayIndex(this.y[4]), this.checkArrayIndex(this.y[6])]),
1962
+ value: this.y[9],
1963
+ ...map,
1964
+ end: this.peekSourceMap()
1965
+ };
1966
+ }
1967
+ return null;
1968
+ }
1969
+ /** @returns {Ast | null} */
1970
+ yCalc() {
1971
+ const map = this.peekSourceMap();
1972
+ if (this.check('eol')) {
1973
+ return null;
1974
+ }
1975
+ // 値を一つ読む
1976
+ const t = this.yGetArg();
1977
+ if (!t) {
1978
+ return null;
1979
+ }
1980
+ // 助詞がある? つまり、関数呼び出しがある?
1981
+ if (t.josi === '') {
1982
+ return t;
1983
+ } // 値だけの場合
1984
+ // 関数の呼び出しがあるなら、スタックに載せて関数読み出しを呼ぶ
1985
+ this.pushStack(t);
1986
+ const t1 = this.yCall();
1987
+ if (!t1) {
1988
+ return this.popStack();
1989
+ }
1990
+ // それが連文か確認
1991
+ if (t1.josi !== 'して') {
1992
+ return t1;
1993
+ } // 連文ではない
1994
+ // 連文なら右側を読んで左側とくっつける
1995
+ const t2 = this.yCalc();
1996
+ if (!t2) {
1997
+ return t1;
1998
+ }
1999
+ return {
2000
+ type: 'renbun',
2001
+ left: t1,
2002
+ right: t2,
2003
+ josi: t2.josi,
2004
+ ...map,
2005
+ end: this.peekSourceMap()
2006
+ };
2007
+ }
2008
+ /** @returns {Ast | null} */
2009
+ yValueKakko() {
2010
+ if (!this.check('(')) {
2011
+ return null;
2012
+ }
2013
+ const t = this.get(); // skip '('
2014
+ if (!t) {
2015
+ throw new Error('[System Error] check したのに get できない');
2016
+ }
2017
+ this.saveStack();
2018
+ const v = this.yCalc() || this.ySentence();
2019
+ if (v === null) {
2020
+ const v2 = this.get();
2021
+ this.logger.debug('(...)の解析エラー。' + this.nodeToStr(v2, { depth: 1 }, true) + 'の近く', t);
2022
+ throw NakoSyntaxError.fromNode('(...)の解析エラー。' + this.nodeToStr(v2, { depth: 1 }, false) + 'の近く', t);
2023
+ }
2024
+ if (!this.check(')')) {
2025
+ this.logger.debug('(...)の解析エラー。' + this.nodeToStr(v, { depth: 1 }, true) + 'の近く', t);
2026
+ throw NakoSyntaxError.fromNode('(...)の解析エラー。' + this.nodeToStr(v, { depth: 1 }, false) + 'の近く', t);
2027
+ }
2028
+ const closeParent = this.get(); // skip ')'
2029
+ this.loadStack();
2030
+ if (closeParent) {
2031
+ v.josi = closeParent.josi;
2032
+ }
2033
+ return v;
2034
+ }
2035
+ /** @returns {Ast | null} */
2036
+ yValue() {
2037
+ const map = this.peekSourceMap();
2038
+ // カンマなら飛ばす #877
2039
+ if (this.check('comma')) {
2040
+ this.get();
2041
+ }
2042
+ // プリミティブな値
2043
+ if (this.checkTypes(['number', 'string'])) {
2044
+ return this.getCur();
2045
+ }
2046
+ // 丸括弧
2047
+ if (this.check('(')) {
2048
+ return this.yValueKakko();
2049
+ }
2050
+ // マイナス記号
2051
+ if (this.check2(['-', 'number']) || this.check2(['-', 'word']) || this.check2(['-', 'func'])) {
2052
+ const m = this.get(); // skip '-'
2053
+ const v = this.yValue();
2054
+ const josi = (v && v.josi) ? v.josi : '';
2055
+ const line = (m && m.line) ? m.line : 0;
2056
+ return {
2057
+ type: 'op',
2058
+ operator: '*',
2059
+ left: { type: 'number', value: -1, line },
2060
+ right: v || [],
2061
+ josi,
2062
+ ...map,
2063
+ end: this.peekSourceMap()
2064
+ };
2065
+ }
2066
+ // NOT
2067
+ if (this.check('not')) {
2068
+ this.get(); // skip '!'
2069
+ const v = this.yValue();
2070
+ const josi = (v && v.josi) ? v.josi : '';
2071
+ return {
2072
+ type: 'not',
2073
+ value: v,
2074
+ josi,
2075
+ ...map,
2076
+ end: this.peekSourceMap()
2077
+ };
2078
+ }
2079
+ // JSON object
2080
+ const a = this.yJSONArray();
2081
+ if (a) {
2082
+ return a;
2083
+ }
2084
+ const o = this.yJSONObject();
2085
+ if (o) {
2086
+ return o;
2087
+ }
2088
+ // 一語関数
2089
+ const splitType = operatorList.concat(['eol', ')', ']', 'ならば', '回', '間', '反復', '条件分岐']);
2090
+ if (this.check2(['func', splitType])) {
2091
+ const tt = this.get();
2092
+ if (!tt) {
2093
+ throw new Error('[System Error] 正しく値が取れませんでした。');
2094
+ }
2095
+ const f = this.getVarNameRef(tt);
2096
+ this.usedFuncs.add(f.value);
2097
+ return {
2098
+ type: 'func',
2099
+ name: f.value,
2100
+ args: [],
2101
+ josi: f.josi,
2102
+ ...map,
2103
+ end: this.peekSourceMap()
2104
+ };
2105
+ }
2106
+ // C風関数呼び出し FUNC(...)
2107
+ if (this.check2([['func', 'word'], '(']) && this.peekDef().josi === '') {
2108
+ const f = this.peek();
2109
+ if (this.accept([['func', 'word'], '(', this.yGetArgParen, ')'])) {
2110
+ const funcName = this.getVarNameRef(this.y[0]).value;
2111
+ this.usedFuncs.add(funcName);
2112
+ return {
2113
+ type: 'func',
2114
+ name: funcName,
2115
+ args: this.y[2],
2116
+ josi: this.y[3].josi,
2117
+ ...map,
2118
+ end: this.peekSourceMap()
2119
+ };
2120
+ }
2121
+ else {
2122
+ throw NakoSyntaxError.fromNode('C風関数呼び出しのエラー', f || NewEmptyToken());
2123
+ }
2124
+ }
2125
+ // 関数呼び出し演算子
2126
+ if (this.check2(['func', '←'])) {
2127
+ return this.yCallOp();
2128
+ }
2129
+ // 無名関数(関数オブジェクト)
2130
+ if (this.check('def_func')) {
2131
+ return this.yMumeiFunc();
2132
+ }
2133
+ // 変数
2134
+ const word = this.yValueWord();
2135
+ if (word) {
2136
+ return word;
2137
+ }
2138
+ // その他
2139
+ return null;
2140
+ }
2141
+ yValueWordGetIndex(ast) {
2142
+ if (!ast.index) {
2143
+ ast.index = [];
2144
+ }
2145
+ // word @ a, b, c
2146
+ if (this.check('@')) {
2147
+ if (this.accept(['@', this.yValue, 'comma', this.yValue, 'comma', this.yValue])) {
2148
+ ast.index.push(this.checkArrayIndex(this.y[1]));
2149
+ ast.index.push(this.checkArrayIndex(this.y[3]));
2150
+ ast.index.push(this.checkArrayIndex(this.y[5]));
2151
+ ast.index = this.checkArrayReverse(ast.index);
2152
+ ast.josi = this.y[5].josi;
2153
+ return true;
2154
+ }
2155
+ if (this.accept(['@', this.yValue, 'comma', this.yValue])) {
2156
+ ast.index.push(this.checkArrayIndex(this.y[1]));
2157
+ ast.index.push(this.checkArrayIndex(this.y[3]));
2158
+ ast.index = this.checkArrayReverse(ast.index);
2159
+ ast.josi = this.y[3].josi;
2160
+ return true;
2161
+ }
2162
+ if (this.accept(['@', this.yValue])) {
2163
+ ast.index.push(this.checkArrayIndex(this.y[1]));
2164
+ ast.josi = this.y[1].josi;
2165
+ return true;
2166
+ }
2167
+ throw NakoSyntaxError.fromNode('変数の後ろの『@要素』の指定が不正です。', ast);
2168
+ }
2169
+ if (this.check('[')) {
2170
+ if (this.accept(['[', this.yCalc, ']'])) {
2171
+ ast.index.push(this.checkArrayIndex(this.y[1]));
2172
+ ast.josi = this.y[2].josi;
2173
+ return true;
2174
+ }
2175
+ }
2176
+ if (this.check('[')) {
2177
+ if (this.accept(['[', this.yCalc, 'comma', this.yCalc, ']'])) {
2178
+ const index = [
2179
+ this.checkArrayIndex(this.y[1]),
2180
+ this.checkArrayIndex(this.y[3])
2181
+ ];
2182
+ ast.index = this.checkArrayReverse(index);
2183
+ ast.josi = this.y[4].josi;
2184
+ return true;
2185
+ }
2186
+ }
2187
+ if (this.check('[')) {
2188
+ if (this.accept(['[', this.yCalc, 'comma', this.yCalc, 'comma', this.yCalc, ']'])) {
2189
+ const index = [
2190
+ this.checkArrayIndex(this.y[1]),
2191
+ this.checkArrayIndex(this.y[3]),
2192
+ this.checkArrayIndex(this.y[5])
2193
+ ];
2194
+ ast.index = this.checkArrayReverse(index);
2195
+ ast.josi = this.y[6].josi;
2196
+ return true;
2197
+ }
2198
+ }
2199
+ return false;
2200
+ }
2201
+ /** @returns {Ast | null} */
2202
+ yValueWord() {
2203
+ const map = this.peekSourceMap();
2204
+ if (this.check('word')) {
2205
+ const t = this.getCur();
2206
+ const word = this.getVarNameRef(t);
2207
+ // word[n] || word@n
2208
+ if (word.josi === '' && this.checkTypes(['[', '@'])) {
2209
+ const ast = {
2210
+ type: '配列参照',
2211
+ name: word,
2212
+ index: [],
2213
+ josi: '',
2214
+ ...map,
2215
+ end: this.peekSourceMap()
2216
+ };
2217
+ while (!this.isEOF()) {
2218
+ if (!this.yValueWordGetIndex(ast)) {
2219
+ break;
2220
+ }
2221
+ }
2222
+ if (ast.index && ast.index.length === 0) {
2223
+ throw NakoSyntaxError.fromNode(`配列『${word.value}』アクセスで指定ミス`, word);
2224
+ }
2225
+ return ast;
2226
+ }
2227
+ return word;
2228
+ }
2229
+ return null;
2230
+ }
2231
+ /** 変数名を検索して解決する
2232
+ * @param {Ast|Token} word
2233
+ * @return {Ast|Token}
2234
+ */
2235
+ getVarName(word) {
2236
+ // check word name
2237
+ const f = this.findVar(word.value);
2238
+ if (!f) { // 変数が見つからない
2239
+ if (this.funcLevel === 0) { // global
2240
+ let gname = word.value;
2241
+ if (gname.indexOf('__') < 0) {
2242
+ gname = this.modName + '__' + word.value;
2243
+ }
2244
+ this.funclist[gname] = { type: 'var', value: '' };
2245
+ word.value = gname;
2246
+ }
2247
+ else { // local
2248
+ this.localvars[word.value] = { type: 'var', value: '' };
2249
+ }
2250
+ }
2251
+ else if (f && f.scope === 'global') {
2252
+ word.value = f.name;
2253
+ }
2254
+ return word;
2255
+ }
2256
+ /** 変数名を検索して解決する */
2257
+ getVarNameRef(word) {
2258
+ // check word name
2259
+ const f = this.findVar(word.value);
2260
+ if (!f) { // 変数が見つからない
2261
+ if (this.funcLevel === 0 && word.value.indexOf('__') < 0) {
2262
+ word.value = this.modName + '__' + word.value;
2263
+ }
2264
+ }
2265
+ else if (f && f.scope === 'global') {
2266
+ word.value = f.name;
2267
+ }
2268
+ return word;
2269
+ }
2270
+ /** 複数の変数名を検索して解決する */
2271
+ getVarNameList(words) {
2272
+ for (let i = 0; i < words.length; i++) {
2273
+ words[i] = this.getVarName(words[i]);
2274
+ }
2275
+ return words;
2276
+ }
2277
+ yJSONObjectValue() {
2278
+ const a = [];
2279
+ const firstToken = this.peek();
2280
+ if (!firstToken) {
2281
+ return null;
2282
+ }
2283
+ while (!this.isEOF()) {
2284
+ while (this.check('eol')) {
2285
+ this.get();
2286
+ }
2287
+ if (this.check('}')) {
2288
+ break;
2289
+ }
2290
+ if (this.accept(['word', ':', this.yCalc])) {
2291
+ this.y[0].type = 'string'; // キー名の文字列記号省略の場合
2292
+ a.push({
2293
+ key: this.y[0],
2294
+ value: this.y[2]
2295
+ });
2296
+ }
2297
+ else if (this.accept(['string', ':', this.yCalc])) {
2298
+ a.push({
2299
+ key: this.y[0],
2300
+ value: this.y[2]
2301
+ });
2302
+ }
2303
+ else if (this.check('word')) {
2304
+ const w = this.getCur();
2305
+ w.type = 'string';
2306
+ a.push({
2307
+ key: w,
2308
+ value: w
2309
+ });
2310
+ }
2311
+ else if (this.checkTypes(['string', 'number'])) {
2312
+ const w = this.getCur();
2313
+ a.push({
2314
+ key: w,
2315
+ value: w
2316
+ });
2317
+ }
2318
+ else {
2319
+ throw NakoSyntaxError.fromNode('辞書オブジェクトの宣言で末尾の『}』がありません。', firstToken);
2320
+ }
2321
+ if (this.check('comma')) {
2322
+ this.get();
2323
+ }
2324
+ }
2325
+ return a;
2326
+ }
2327
+ /** @returns {Ast | null} */
2328
+ yJSONObject() {
2329
+ const map = this.peekSourceMap();
2330
+ if (this.accept(['{', '}'])) {
2331
+ return {
2332
+ type: 'json_obj',
2333
+ value: [],
2334
+ josi: this.y[1].josi,
2335
+ ...map,
2336
+ end: this.peekSourceMap()
2337
+ };
2338
+ }
2339
+ if (this.accept(['{', this.yJSONObjectValue, '}'])) {
2340
+ return {
2341
+ type: 'json_obj',
2342
+ value: this.y[1],
2343
+ josi: this.y[2].josi,
2344
+ ...map,
2345
+ end: this.peekSourceMap()
2346
+ };
2347
+ }
2348
+ // 辞書初期化に終わりがなかった場合 (エラーチェックのため) #958
2349
+ if (this.accept(['{', this.yJSONObjectValue])) {
2350
+ throw NakoSyntaxError.fromNode('辞書型変数の初期化が『}』で閉じられていません。', this.y[1]);
2351
+ }
2352
+ return null;
2353
+ }
2354
+ yJSONArrayValue() {
2355
+ if (this.check('eol')) {
2356
+ this.get();
2357
+ }
2358
+ const v1 = this.yCalc();
2359
+ if (v1 === null) {
2360
+ return null;
2361
+ }
2362
+ if (this.check('comma')) {
2363
+ this.get();
2364
+ }
2365
+ const a = [v1];
2366
+ while (!this.isEOF()) {
2367
+ if (this.check('eol')) {
2368
+ this.get();
2369
+ }
2370
+ if (this.check(']')) {
2371
+ break;
2372
+ }
2373
+ const v2 = this.yCalc();
2374
+ if (v2 === null) {
2375
+ break;
2376
+ }
2377
+ if (this.check('comma')) {
2378
+ this.get();
2379
+ }
2380
+ a.push(v2);
2381
+ }
2382
+ return a;
2383
+ }
2384
+ /** @returns {Ast | null} */
2385
+ yJSONArray() {
2386
+ const map = this.peekSourceMap();
2387
+ if (this.accept(['[', ']'])) {
2388
+ return {
2389
+ type: 'json_array',
2390
+ value: [],
2391
+ josi: this.y[1].josi,
2392
+ ...map,
2393
+ end: this.peekSourceMap()
2394
+ };
2395
+ }
2396
+ if (this.accept(['[', this.yJSONArrayValue, ']'])) {
2397
+ return {
2398
+ type: 'json_array',
2399
+ value: this.y[1],
2400
+ josi: this.y[2].josi,
2401
+ ...map,
2402
+ end: this.peekSourceMap()
2403
+ };
2404
+ }
2405
+ // 配列に終わりがなかった場合 (エラーチェックのため) #958
2406
+ if (this.accept(['[', this.yJSONArrayValue])) {
2407
+ throw NakoSyntaxError.fromNode('配列変数の初期化が『]』で閉じられていません。', this.y[1]);
2408
+ }
2409
+ return null;
2410
+ }
2411
+ /** エラー監視構文 */
2412
+ yTryExcept() {
2413
+ const map = this.peekSourceMap();
2414
+ if (!this.check('エラー監視')) {
2415
+ return null;
2416
+ }
2417
+ const kansi = this.getCur(); // skip エラー監視
2418
+ const block = this.yBlock();
2419
+ if (!this.check2(['エラー', 'ならば'])) {
2420
+ throw NakoSyntaxError.fromNode('エラー構文で『エラーならば』がありません。' +
2421
+ '『エラー監視..エラーならば..ここまで』を対で記述します。', kansi);
2422
+ }
2423
+ this.get(); // skip エラー
2424
+ this.get(); // skip ならば
2425
+ const errBlock = this.yBlock();
2426
+ if (this.check('ここまで')) {
2427
+ this.get();
2428
+ }
2429
+ else {
2430
+ throw NakoSyntaxError.fromNode('『ここまで』がありません。『エラー監視』...『エラーならば』...『ここまで』を対応させてください。', map);
2431
+ }
2432
+ return {
2433
+ type: 'try_except',
2434
+ block,
2435
+ errBlock: errBlock || [],
2436
+ josi: '',
2437
+ ...map,
2438
+ end: this.peekSourceMap()
2439
+ };
2440
+ }
2441
+ }