nadesiko3 3.2.27

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 (283) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +107 -0
  3. package/bin/cnako3 +10 -0
  4. package/bin/cnako3.bat +11 -0
  5. package/bin/nako3server.bat +6 -0
  6. package/demo/ace_editor.html +90 -0
  7. package/demo/ace_editor_tabs.html +162 -0
  8. package/demo/basic.html +71 -0
  9. package/demo/browsers.html +128 -0
  10. package/demo/css/basic.css +3 -0
  11. package/demo/css/common.css +157 -0
  12. package/demo/css/editor.css +8 -0
  13. package/demo/css/flow.css +3 -0
  14. package/demo/css/index.css +3 -0
  15. package/demo/extlib/ace@1.4.12/ace.js +17 -0
  16. package/demo/extlib/ace@1.4.12/ext-code_lens.min.js +1 -0
  17. package/demo/extlib/ace@1.4.12/ext-language_tools.min.js +1 -0
  18. package/demo/extlib/ace@1.4.12/ext-options.min.js +1 -0
  19. package/demo/extlib/ace@1.4.12/ext-settings_menu.js +8 -0
  20. package/demo/extlib/ace@1.4.12/keybinding-vscode.js +8 -0
  21. package/demo/extlib/ace@1.4.12/theme-monokai.js +8 -0
  22. package/demo/extlib/ace@1.4.12/theme-xcode.js +8 -0
  23. package/demo/extlib/chart.js@3.2.1/chart.min.js +13 -0
  24. package/demo/extlib/pure-min.css +11 -0
  25. package/demo/flow.html +97 -0
  26. package/demo/graph.html +53 -0
  27. package/demo/image/nako.png +0 -0
  28. package/demo/image/nakopad-icon256.png +0 -0
  29. package/demo/image/turtle.fla +0 -0
  30. package/demo/image/turtle.png +0 -0
  31. package/demo/index.html +134 -0
  32. package/demo/js/common.js +17 -0
  33. package/demo/js/turtle3d_test.js +44 -0
  34. package/demo/js/turtle_test.js +44 -0
  35. package/demo/runscript.html +47 -0
  36. package/demo/runscript2.html +33 -0
  37. package/demo/turtle.html +58 -0
  38. package/demo/turtle2.html +141 -0
  39. package/demo/turtle3.html +278 -0
  40. package/demo/turtle3d.html +58 -0
  41. package/demo/turtle3d2.html +107 -0
  42. package/demo/version.html +24 -0
  43. package/doc/SETUP.md +172 -0
  44. package/doc/about.md +34 -0
  45. package/doc/browsers.md +60 -0
  46. package/doc/docgen.md +21 -0
  47. package/doc/editor.md +44 -0
  48. package/doc/files.md +37 -0
  49. package/doc/plugins.md +195 -0
  50. package/doc/release.md +78 -0
  51. package/doc/win32.md +57 -0
  52. package/package.json +196 -0
  53. package/release/_hash.txt +65 -0
  54. package/release/_script-tags.txt +13 -0
  55. package/release/command.json +1 -0
  56. package/release/command.json.js +1 -0
  57. package/release/command_cnako3.json +1 -0
  58. package/release/command_list.json +1 -0
  59. package/release/editor.js +2 -0
  60. package/release/editor.js.LICENSE.txt +32 -0
  61. package/release/josi.json +48 -0
  62. package/release/nako_gen_async.js +1 -0
  63. package/release/nako_gen_async.js.LICENSE.txt +595 -0
  64. package/release/plugin_caniuse.js +1 -0
  65. package/release/plugin_caniuse.js.LICENSE.txt +411 -0
  66. package/release/plugin_csv.js +1 -0
  67. package/release/plugin_csv.js.LICENSE.txt +367 -0
  68. package/release/plugin_datetime.js +1 -0
  69. package/release/plugin_datetime.js.LICENSE.txt +471 -0
  70. package/release/plugin_kansuji.js +1 -0
  71. package/release/plugin_kansuji.js.LICENSE.txt +491 -0
  72. package/release/plugin_markup.js +1 -0
  73. package/release/plugin_markup.js.LICENSE.txt +363 -0
  74. package/release/plugin_turtle.js +1 -0
  75. package/release/plugin_turtle.js.LICENSE.txt +435 -0
  76. package/release/plugin_webworker.js +1 -0
  77. package/release/plugin_webworker.js.LICENSE.txt +491 -0
  78. package/release/version.js +2 -0
  79. package/release/version.js.LICENSE.txt +32 -0
  80. package/release/wnako3.js +2 -0
  81. package/release/wnako3.js.LICENSE.txt +1 -0
  82. package/release/wnako3webworker.js +1 -0
  83. package/release/wnako3webworker.js.LICENSE.txt +847 -0
  84. package/release/yoyakugo.json +30 -0
  85. package/src/browsers.md +60 -0
  86. package/src/cnako3.js +466 -0
  87. package/src/commander_ja.js +154 -0
  88. package/src/enako3.js +69 -0
  89. package/src/era.json +22 -0
  90. package/src/index.js +5 -0
  91. package/src/nako3.js +836 -0
  92. package/src/nako3_assert.js +37 -0
  93. package/src/nako3editorfix.sfd +106 -0
  94. package/src/nako3editorfix.woff +0 -0
  95. package/src/nako3server.js +51 -0
  96. package/src/nako_colors.js +86 -0
  97. package/src/nako_errors.js +176 -0
  98. package/src/nako_gen.js +1459 -0
  99. package/src/nako_gen_async.js +1622 -0
  100. package/src/nako_global.js +113 -0
  101. package/src/nako_indent.js +480 -0
  102. package/src/nako_josi_list.js +46 -0
  103. package/src/nako_lex_rules.js +259 -0
  104. package/src/nako_lexer.js +576 -0
  105. package/src/nako_logger.js +138 -0
  106. package/src/nako_parser3.js +1768 -0
  107. package/src/nako_parser_base.js +265 -0
  108. package/src/nako_parser_const.js +37 -0
  109. package/src/nako_prepare.js +293 -0
  110. package/src/nako_reserved_words.js +35 -0
  111. package/src/nako_source_mapping.js +251 -0
  112. package/src/nako_test.js +37 -0
  113. package/src/nako_version.js +8 -0
  114. package/src/plugin_browser.js +191 -0
  115. package/src/plugin_browser_ajax.js +352 -0
  116. package/src/plugin_browser_audio.js +109 -0
  117. package/src/plugin_browser_canvas.js +462 -0
  118. package/src/plugin_browser_chart.js +296 -0
  119. package/src/plugin_browser_color.js +49 -0
  120. package/src/plugin_browser_crypto.js +26 -0
  121. package/src/plugin_browser_dialog.js +53 -0
  122. package/src/plugin_browser_dom_basic.js +322 -0
  123. package/src/plugin_browser_dom_event.js +193 -0
  124. package/src/plugin_browser_dom_parts.js +163 -0
  125. package/src/plugin_browser_geolocation.js +51 -0
  126. package/src/plugin_browser_hotkey.js +25 -0
  127. package/src/plugin_browser_html.js +59 -0
  128. package/src/plugin_browser_in_worker.js +44 -0
  129. package/src/plugin_browser_location.js +21 -0
  130. package/src/plugin_browser_speech.js +111 -0
  131. package/src/plugin_browser_storage.js +121 -0
  132. package/src/plugin_browser_system.js +12 -0
  133. package/src/plugin_browser_websocket.js +73 -0
  134. package/src/plugin_caniuse.js +24 -0
  135. package/src/plugin_csv.js +57 -0
  136. package/src/plugin_datetime.js +414 -0
  137. package/src/plugin_express.js +212 -0
  138. package/src/plugin_kansuji.js +224 -0
  139. package/src/plugin_keigo.js +55 -0
  140. package/src/plugin_markup.js +32 -0
  141. package/src/plugin_math.js +319 -0
  142. package/src/plugin_node.js +1018 -0
  143. package/src/plugin_promise.js +94 -0
  144. package/src/plugin_system.js +2109 -0
  145. package/src/plugin_test.js +38 -0
  146. package/src/plugin_turtle.js +646 -0
  147. package/src/plugin_webworker.js +334 -0
  148. package/src/plugin_weykturtle3d.js +1216 -0
  149. package/src/plugin_worker.js +92 -0
  150. package/src/repl.nako3 +63 -0
  151. package/src/turtle-elephant.png +0 -0
  152. package/src/turtle-panda.png +0 -0
  153. package/src/turtle64.png +0 -0
  154. package/src/wnako3.js +162 -0
  155. package/src/wnako3_editor.css +215 -0
  156. package/src/wnako3_editor.js +1645 -0
  157. package/src/wnako3_editor_marker_red.png +0 -0
  158. package/src/wnako3_editor_marker_red.xcf +0 -0
  159. package/src/wnako3_editor_marker_yellow.png +0 -0
  160. package/src/wnako3_editor_marker_yellow.xcf +0 -0
  161. package/src/wnako3webworker.js +70 -0
  162. package/test/ace_editor/karma.config.js +94 -0
  163. package/test/ace_editor/test/.babelrc.json +3 -0
  164. package/test/ace_editor/test/ace_editor_test.js +178 -0
  165. package/test/ace_editor/test/html/custom_context.html +140 -0
  166. package/test/async/async_basic_test.js +124 -0
  167. package/test/browser/karma.config.js +212 -0
  168. package/test/browser/test/.babelrc.json +3 -0
  169. package/test/browser/test/compare_util.js +50 -0
  170. package/test/browser/test/html/canvas_basic.html +1 -0
  171. package/test/browser/test/html/div_basic.html +2 -0
  172. package/test/browser/test/html/event_dom_form.html +4 -0
  173. package/test/browser/test/html/event_dom_scrolldiv.html +5 -0
  174. package/test/browser/test/image/canvas_test1.png +0 -0
  175. package/test/browser/test/image/canvas_test2.png +0 -0
  176. package/test/browser/test/image/canvas_test3.png +0 -0
  177. package/test/browser/test/image/canvas_test4.png +0 -0
  178. package/test/browser/test/image/canvas_test7.png +0 -0
  179. package/test/browser/test/image/canvas_test8.png +0 -0
  180. package/test/browser/test/image/canvas_test_blank.png +0 -0
  181. package/test/browser/test/image/elephant_kana.png +0 -0
  182. package/test/browser/test/image/panda_kana.png +0 -0
  183. package/test/browser/test/image/turtle_kana.png +0 -0
  184. package/test/browser/test/import_plugin_checker.js +24 -0
  185. package/test/browser/test/plugin_browser_test.js +52 -0
  186. package/test/browser/test/plugin_browser_test_ajax.js +123 -0
  187. package/test/browser/test/plugin_browser_test_color.js +18 -0
  188. package/test/browser/test/plugin_browser_test_dialog.js +72 -0
  189. package/test/browser/test/plugin_browser_test_dom_event.js +598 -0
  190. package/test/browser/test/plugin_browser_test_dom_parts.js +125 -0
  191. package/test/browser/test/plugin_browser_test_system.js +9 -0
  192. package/test/browser/test/plugin_turtle_test.js +817 -0
  193. package/test/browser/test/plugin_webworker_test.js +87 -0
  194. package/test/browser/test/require_test.js +71 -0
  195. package/test/bundled/karma.config.base.js +117 -0
  196. package/test/bundled/karma.config.js +86 -0
  197. package/test/bundled/test/.babelrc.json +3 -0
  198. package/test/bundled/test/bundled_test.js +69 -0
  199. package/test/bundled/test/html/custom_context.html +65 -0
  200. package/test/bundled/test/html/custom_debug.html +66 -0
  201. package/test/bundled/test4b.cmd +52 -0
  202. package/test/bundled/test_base/.babelrc.json +3 -0
  203. package/test/bundled/test_base/_checktool_test.js +25 -0
  204. package/test/bundled/test_base/basic_ajax_test.js +56 -0
  205. package/test/bundled/test_base/basic_async_test.js +18 -0
  206. package/test/bundled/test_base/basic_test.js +153 -0
  207. package/test/bundled/test_base/calc_test.js +132 -0
  208. package/test/bundled/test_base/css/browsers_box.css +114 -0
  209. package/test/bundled/test_base/html/custom_context.html +69 -0
  210. package/test/bundled/test_base/html/custom_debug.html +71 -0
  211. package/test/bundled/test_base/js/browsers_box.js +72 -0
  212. package/test/bundled/test_base/plugin_csv_test.js +37 -0
  213. package/test/bundled/test_base/plugin_datetime_test.js +115 -0
  214. package/test/bundled/test_base/plugin_kansuji_test.js +49 -0
  215. package/test/bundled/test_base/plugin_system_test.js +410 -0
  216. package/test/bundled/test_base/plugin_webworker_test.js +53 -0
  217. package/test/bundled/test_base/resources/ok.txt +1 -0
  218. package/test/bundled/test_base/test_utils.js +191 -0
  219. package/test/common/array_test.js +40 -0
  220. package/test/common/basic_test.js +317 -0
  221. package/test/common/calc_test.js +139 -0
  222. package/test/common/debug_test.js +16 -0
  223. package/test/common/error_test.js +16 -0
  224. package/test/common/flow_test.js +360 -0
  225. package/test/common/func_call.js +136 -0
  226. package/test/common/func_test.js +149 -0
  227. package/test/common/indent_test.js +362 -0
  228. package/test/common/lex_test.js +146 -0
  229. package/test/common/literal_test.js +72 -0
  230. package/test/common/nako_logger_test.js +26 -0
  231. package/test/common/plugin_browser_test.js +24 -0
  232. package/test/common/plugin_browser_ut_audio_test.js +88 -0
  233. package/test/common/plugin_browser_ut_color_test.js +21 -0
  234. package/test/common/plugin_browser_ut_dialog_test.js +100 -0
  235. package/test/common/plugin_browser_ut_html_test.js +13 -0
  236. package/test/common/plugin_browser_ut_system_test.js +10 -0
  237. package/test/common/plugin_csv_test.js +39 -0
  238. package/test/common/plugin_datetime_test.js +120 -0
  239. package/test/common/plugin_kansuji_test.js +59 -0
  240. package/test/common/plugin_promise_test.js +18 -0
  241. package/test/common/plugin_system_test.js +451 -0
  242. package/test/common/prepare_test.js +93 -0
  243. package/test/common/re_test.js +20 -0
  244. package/test/common/variable_scope_test.js +105 -0
  245. package/test/jsconfig.json +19 -0
  246. package/test/karma.config.js +91 -0
  247. package/test/node/add_test.nako3 +1 -0
  248. package/test/node/async_test.js +80 -0
  249. package/test/node/commander_ja_test.js +82 -0
  250. package/test/node/error_message_test.js +244 -0
  251. package/test/node/kai_test.nako3 +6 -0
  252. package/test/node/node_test.js +43 -0
  253. package/test/node/plugin_broken.js.txt +3 -0
  254. package/test/node/plugin_browser_ut_ajax_test.js +355 -0
  255. package/test/node/plugin_browser_ut_location_test.js +32 -0
  256. package/test/node/plugin_markup_test.js +44 -0
  257. package/test/node/plugin_math_test.js +42 -0
  258. package/test/node/plugin_node_test.js +93 -0
  259. package/test/node/plugin_test.js +16 -0
  260. package/test/node/relative_import_test_1.nako3 +1 -0
  261. package/test/node/relative_import_test_2.nako3 +2 -0
  262. package/test/node/require_nako3_test.js +59 -0
  263. package/test/node/requiretest.nako3 +4 -0
  264. package/test/node/requiretest_indirect.nako3 +1 -0
  265. package/test/node/requiretest_name.nako3 +5 -0
  266. package/test/node/runtime_error.nako3 +2 -0
  267. package/test/node/side_effects_test.js +106 -0
  268. package/test/node/sjis.txt +5 -0
  269. package/test/node/syntax_error.nako3 +2 -0
  270. package/test/node/wnako3_editor_test.js +360 -0
  271. package/tools/README.md +7 -0
  272. package/tools/nako3edit/html/edit.html +83 -0
  273. package/tools/nako3edit/html/edit_plugin.js +6 -0
  274. package/tools/nako3edit/html/files.html +49 -0
  275. package/tools/nako3edit/html/nako3edit.css +66 -0
  276. package/tools/nako3edit/index.nako3 +145 -0
  277. package/tools/nako3edit/run.js +12 -0
  278. package/tools/nako3server/html/edit.html +104 -0
  279. package/tools/nako3server/html/edit_plugin.js +6 -0
  280. package/tools/nako3server/html/files.html +53 -0
  281. package/tools/nako3server/html/nako3edit.css +66 -0
  282. package/tools/nako3server/index.nako3 +129 -0
  283. package/tools/nako3server/run.js +12 -0
@@ -0,0 +1,265 @@
1
+ /**
2
+ * なでしこの構文解析のためのユーティリティクラス
3
+ */
4
+
5
+ const { NakoSyntaxError } = require('./nako_errors')
6
+
7
+ class NakoParserBase {
8
+ /**
9
+ * @param {import("./nako_logger")} logger
10
+ */
11
+ constructor (logger) {
12
+ this.logger = logger
13
+ this.stackList = [] // 関数定義の際にスタックが混乱しないように整理する
14
+ this.init()
15
+ /** @type {import('./nako3').TokenWithSourceMap[]} */
16
+ this.tokens = []
17
+ /** @type {import('./nako3').Ast[]} */
18
+ this.stack = []
19
+ this.index = 0
20
+ /** @type {import('./nako3').Ast[]} */
21
+ this.y = []
22
+ this.genMode = 'sync' // #637
23
+ }
24
+
25
+ init () {
26
+ this.funclist = {} // 関数の一覧
27
+ this.reset()
28
+ }
29
+
30
+ reset () {
31
+ /** @type {import('./nako3').TokenWithSourceMap[]} */
32
+ this.tokens = [] // 字句解析済みのトークンの一覧を保存
33
+ this.index = 0 // tokens[] のどこまで読んだかを管理する
34
+ this.stack = [] // 計算用のスタック ... 直接は操作せず、pushStack() popStack() を介して使う
35
+ this.y = [] // accept()で解析済みのトークンを配列で得るときに使う
36
+ this.genMode = 'sync' // #637, #1056
37
+ }
38
+
39
+ setFuncList (funclist) {
40
+ this.funclist = funclist
41
+ }
42
+
43
+ /**
44
+ * 特定の助詞を持つ要素をスタックから一つ下ろす、指定がなければ末尾を下ろす
45
+ * @param {string[]} josiList 下ろしたい助詞の配列
46
+ * @returns {import('./nako3').Ast | null | undefined}
47
+ */
48
+ popStack (josiList) {
49
+ if (!josiList) { return this.stack.pop() }
50
+
51
+ // josiList にマッチする助詞を探す
52
+ for (let i = 0; i < this.stack.length; i++) {
53
+ const t = this.stack[i]
54
+ if (josiList.length === 0 || josiList.indexOf(t.josi) >= 0) {
55
+ this.stack.splice(i, 1) // remove stack
56
+ this.logger.trace('POP :' + JSON.stringify(t))
57
+ return t
58
+ }
59
+ }
60
+ // 該当する助詞が見つからなかった場合
61
+ return null
62
+ }
63
+
64
+ /**
65
+ * saveStack と loadStack は対で使う。
66
+ * 関数定義などでスタックが混乱しないように配慮するためのもの
67
+ */
68
+ saveStack () {
69
+ this.stackList.push(this.stack)
70
+ this.stack = []
71
+ }
72
+
73
+ loadStack () {
74
+ this.stack = this.stackList.pop()
75
+ }
76
+
77
+ /**
78
+ * 計算用に要素をスタックに積む
79
+ */
80
+ pushStack (item) {
81
+ this.logger.debug('PUSH:' + JSON.stringify(item))
82
+ this.stack.push(item)
83
+ }
84
+
85
+ /**
86
+ * トークンの末尾に達したか
87
+ */
88
+ isEOF () {
89
+ return (this.index >= this.tokens.length)
90
+ }
91
+
92
+ /**
93
+ * カーソル位置にある単語の型を確かめる
94
+ */
95
+ check (ttype) {
96
+ return (this.tokens[this.index].type === ttype)
97
+ }
98
+
99
+ /**
100
+ * カーソル位置以降にある単語の型を確かめる 2単語以上に対応
101
+ * @param a [単語1の型, 単語2の型, ... ]
102
+ */
103
+ check2 (a) {
104
+ for (let i = 0; i < a.length; i++) {
105
+ const idx = i + this.index
106
+ if (this.tokens.length <= idx) { return false }
107
+ if (a[i] === '*') { continue } // ワイルドカード(どんなタイプも許容)
108
+ const t = this.tokens[idx]
109
+ if (a[i] instanceof Array) {
110
+ if (a[i].indexOf(t.type) < 0) { return false }
111
+ continue
112
+ }
113
+ if (t.type !== a[i]) { return false }
114
+ }
115
+ return true
116
+ }
117
+
118
+ /**
119
+ * カーソル位置の型を確認するが、複数の種類を確かめられる
120
+ */
121
+ checkTypes (a) {
122
+ const type = this.tokens[this.index].type
123
+ return (a.indexOf(type) >= 0)
124
+ }
125
+
126
+ /**
127
+ * check2の高度なやつ、型名の他にコールバック関数を指定できる
128
+ * 型にマッチしなければ null を返し、カーソルを巻き戻す
129
+ */
130
+ accept (types) {
131
+ const y = []
132
+ const tmpIndex = this.index
133
+ const rollback = () => {
134
+ this.index = tmpIndex
135
+ return false
136
+ }
137
+ for (let i = 0; i < types.length; i++) {
138
+ if (this.isEOF()) { return rollback() }
139
+ const type = types[i]
140
+ if (typeof type === 'string') {
141
+ const token = this.get()
142
+ if (token.type !== type) { return rollback() }
143
+ y[i] = token
144
+ continue
145
+ }
146
+ if (typeof type === 'function') {
147
+ const f = type.bind(this)
148
+ const r = f(y)
149
+ if (r === null) { return rollback() }
150
+ y[i] = r
151
+ continue
152
+ }
153
+ if (type instanceof Array) {
154
+ if (!this.checkTypes(type)) { return rollback() }
155
+ y[i] = this.get()
156
+ continue
157
+ }
158
+ throw new Error('System Error : accept broken : ' + typeof type)
159
+ }
160
+ this.y = y
161
+ return true
162
+ }
163
+
164
+ /**
165
+ * カーソル語句を取得して、カーソルを後ろに移動する
166
+ * @returns {import('./nako3').TokenWithSourceMap | null}
167
+ */
168
+ get () {
169
+ if (this.isEOF()) { return null }
170
+ return this.tokens[this.index++]
171
+ }
172
+
173
+ unget () {
174
+ if (this.index > 0) { this.index-- }
175
+ }
176
+
177
+ /**
178
+ * @returns {import('./nako3').TokenWithSourceMap | null}
179
+ */
180
+ peek (i = 0) {
181
+ if (this.isEOF()) { return null }
182
+ return this.tokens[this.index + i]
183
+ }
184
+
185
+ /**
186
+ * 現在のカーソル語句のソースコード上の位置を取得する。
187
+ * @returns {{
188
+ * startOffset: number | null
189
+ * endOffset: number | null
190
+ * file: string | undefined
191
+ * line: number
192
+ * column: number
193
+ * }}
194
+ */
195
+ peekSourceMap () {
196
+ const token = this.peek()
197
+ if (token === null) {
198
+ return { startOffset: null, endOffset: null, file: undefined, line: 0, column: 0 }
199
+ }
200
+ return { startOffset: token.startOffset, endOffset: token.endOffset, file: token.file, line: token.line, column: token.column }
201
+ }
202
+
203
+ /**
204
+ * depth: 表示する深さ
205
+ * typeName: 先頭のtypeの表示を上書きする場合に設定する
206
+ * @param {{ depth: number, typeName?: string }} opts
207
+ * @param {boolean} debugMode
208
+ */
209
+ nodeToStr (node, opts, debugMode) {
210
+ const depth = opts.depth - 1
211
+ const typeName = (name) => opts.typeName !== undefined ? opts.typeName : name
212
+ const debug = debugMode ? (' debug: ' + JSON.stringify(node, null, 2)) : ''
213
+ if (!node) {
214
+ return '(NULL)'
215
+ }
216
+ switch (node.type) {
217
+ case 'not':
218
+ if (depth >= 0) {
219
+ return `${typeName('')}『${this.nodeToStr(node.value, { depth }, debugMode)}に演算子『not』を適用した式${debug}』`
220
+ } else {
221
+ return `${typeName('演算子')}『not』`
222
+ }
223
+ case 'op': {
224
+ let operator = node.operator
225
+ const table = { eq: '=', not: '!', gt: '>', lt: '<', and: 'かつ', or: 'または' }
226
+ if (operator in table) {
227
+ operator = table[operator]
228
+ }
229
+ if (depth >= 0) {
230
+ const left = this.nodeToStr(node.left, { depth }, debugMode)
231
+ const right = this.nodeToStr(node.right, { depth }, debugMode)
232
+ if (node.operator === 'eq') {
233
+ return `${typeName('')}『${left}と${right}が等しいかどうかの比較${debug}』`
234
+ }
235
+ return `${typeName('')}『${left}と${right}に演算子『${operator}』を適用した式${debug}』`
236
+ } else {
237
+ return `${typeName('演算子')}『${operator}${debug}』`
238
+ }
239
+ }
240
+ case 'number':
241
+ return `${typeName('数値')}${node.value}`
242
+ case 'string':
243
+ return `${typeName('文字列')}『${node.value}${debug}』`
244
+ case 'word':
245
+ return `${typeName('単語')}『${node.value}${debug}』`
246
+ case 'func':
247
+ return `${typeName('関数')}『${node.name || node.value}${debug}』`
248
+ case 'eol':
249
+ return '行の末尾'
250
+ case 'eof':
251
+ return 'ファイルの末尾'
252
+ default: {
253
+ let name = node.name
254
+ if (!name) { name = node.value }
255
+ if (typeof name !== 'string') { name = node.type }
256
+ return `${typeName('')}『${name}${debug}』`
257
+ }
258
+ }
259
+ }
260
+ }
261
+
262
+ module.exports = {
263
+ NakoParserBase,
264
+ NakoSyntaxError
265
+ }
@@ -0,0 +1,37 @@
1
+ /* eslint-disable quote-props */
2
+ const opPriority = {
3
+ // and or
4
+ 'and': 1,
5
+ 'or': 1,
6
+ // compare
7
+ 'eq': 2,
8
+ 'noteq': 2,
9
+ '===': 2,
10
+ '!==': 2,
11
+ 'gt': 2,
12
+ 'gteq': 2,
13
+ 'lt': 2,
14
+ 'lteq': 2,
15
+ '&': 3,
16
+ // + - << >> >>>
17
+ '+': 4,
18
+ '-': 4,
19
+ 'shift_l': 4,
20
+ 'shift_r': 4,
21
+ 'shift_r0': 4,
22
+ // * /
23
+ '*': 5,
24
+ '/': 5,
25
+ '%': 5,
26
+ // ^
27
+ '^': 6
28
+ }
29
+
30
+ const keizokuJosi = [
31
+ 'いて', 'えて', 'きて', 'けて', 'して', 'って', 'にて', 'みて', 'めて', 'ねて', 'には', 'んで'
32
+ ]
33
+
34
+ module.exports = {
35
+ opPriority,
36
+ keizokuJosi
37
+ }
@@ -0,0 +1,293 @@
1
+ /**
2
+ * nako_prepare.js
3
+ * 字句解析の前の前処理。全角文字を半角文字に変換するのが主な処理。
4
+ * ただし、コメントや文字列の中は変換しないように考慮して変換する。
5
+ */
6
+
7
+ /**
8
+ * 置換後の位置から置換前の位置へマッピングできる文字列
9
+ */
10
+ class Replace {
11
+ /**
12
+ * @param {string} code
13
+ */
14
+ constructor (code) {
15
+ /** @private @type {{ from: number, to: number, index: number }[]} */
16
+ this.history = []
17
+
18
+ /** @private */
19
+ this.code = code
20
+ }
21
+
22
+ /**
23
+ * @returns {string}
24
+ */
25
+ getText () {
26
+ return this.code
27
+ }
28
+
29
+ /**
30
+ * @param {string} from
31
+ * @param {string} to
32
+ */
33
+ replaceAll (from, to) {
34
+ while (true) {
35
+ const index = this.getText().indexOf(from)
36
+ if (index === -1) {
37
+ break
38
+ }
39
+ if (from.length !== to.length) {
40
+ this.history.unshift({ index, from: from.length, to: to.length })
41
+ }
42
+ this.code = this.code.replace(from, to)
43
+ }
44
+ }
45
+
46
+ /**
47
+ * @param {number} i
48
+ * @returns {number}
49
+ */
50
+ getSourcePosition (i) {
51
+ // 少し遅い。パース時間1.4秒に対して0.15秒かかる。iが単調増加することを利用して高速化できるはず。
52
+ for (const item of this.history) {
53
+ if (i >= item.index + item.to) { // 置換範囲より後ろ
54
+ i += item.from - item.to
55
+ } else if (item.index <= i && i < item.index + item.to) { // 置換範囲
56
+ // 置換文字列が2文字以上のとき、最後の文字は最後の文字へマップする。それ以外は最初の文字へマップする。
57
+ if (item.to >= 2 && i === item.index + item.to - 1) {
58
+ i = item.index + item.from - 1
59
+ } else {
60
+ i = item.index
61
+ }
62
+ }
63
+ }
64
+ return i
65
+ }
66
+ }
67
+
68
+ // 字句解析を行う前に全角文字を半角に揃える
69
+ // ただし、文字列部分だけは、そのまま全角で出力するようにする
70
+ // for https://github.com/kujirahand/nadesiko3/issues/94
71
+ class NakoPrepare {
72
+ /**
73
+ * @param {import("./nako_logger")} logger
74
+ */
75
+ constructor (logger) {
76
+ this.logger = logger
77
+
78
+ // 参考) https://hydrocul.github.io/wiki/blog/2014/1101-hyphen-minus-wave-tilde.html
79
+ this.HYPHENS = { // ハイフン問題
80
+ 0x2d: true, // ASCIIのハイフン
81
+ 0x2010: true, // 別のハイフン
82
+ 0x2011: true, // 改行しないハイフン
83
+ 0x2013: true, // ENダッシュ
84
+ 0x2014: true, // EMダッシュ
85
+ 0x2015: true, // 全角のダッシュ
86
+ 0x2212: true // 全角のマイナス
87
+ }
88
+ this.TILDES = { // チルダ問題
89
+ 0x7e: true,
90
+ 0x02dc: true, // 小さなチルダ
91
+ 0x02F7: true, // Modifier Letter Low Tilde
92
+ 0x2053: true, // Swung Dash - 辞書のみだし
93
+ 0x223c: true, // Tilde Operator: 数学で Similar to
94
+ 0x301c: true, // Wave Dash(一般的な波ダッシュ)
95
+ 0xFF5E: true // 全角チルダ
96
+ }
97
+ // スペース問題
98
+ // 参考) http://anti.rosx.net/etc/memo/002_space.html
99
+ this.SPACES = {
100
+ 0x20: true,
101
+ 0x2000: true, // EN QUAD
102
+ 0x2002: true, // EN SPACE
103
+ 0x2003: true, // EM SPACE
104
+ 0x2004: true, // THREE-PER-EM SPACE
105
+ 0x2005: true, // FOUR-PER-EM SPACE
106
+ 0x2006: true, // SIX-PER-EM SPACE
107
+ 0x2007: true, // FIGURE SPACE
108
+ 0x2009: true, // THIN SPACE
109
+ 0x200A: true, // HAIR SPACE
110
+ 0x200B: true, // ZERO WIDTH SPACE
111
+ 0x202F: true, // NARROW NO-BREAK SPACE
112
+ 0x205F: true, // MEDIUM MATHEMATICAL SPACE
113
+ 0x3000: true, // 全角スペース
114
+ 0x3164: true // HANGUL FILLER
115
+ }
116
+ // その他の変換
117
+ this.convertTable = {
118
+ 0x09: ' ', // TAB --> SPC
119
+ 0x203B: '#', // '※' --- コメント
120
+ 0x3002: ';', // 句点
121
+ 0x3010: '[', // '【'
122
+ 0x3011: ']', // '】'
123
+ // 読点は「,」に変換する (#877)
124
+ 0x3001: ',', // 読点 --- JSON記法で「,」と「、」を区別したいので読点は変換しないことに。(#276)
125
+ 0xFF0C: ',' // 読点 ',' 論文などで利用、ただし句点はドットと被るので変換しない (#735)
126
+ }
127
+ }
128
+
129
+ // 一文字だけ変換
130
+ /**
131
+ * @param {string} ch
132
+ */
133
+ convert1ch (ch) {
134
+ if (!ch) { return '' }
135
+ const c = ch.codePointAt(0)
136
+ // テーブルによる変換
137
+ if (this.convertTable[c]) { return this.convertTable[c] }
138
+ // ASCIIエリア
139
+ if (c < 0x7F) { return ch }
140
+ // 全角半角単純変換可能 --- '!' - '~'
141
+ if (c >= 0xFF01 && c <= 0xFF5E) {
142
+ const c2 = c - 0xFEE0
143
+ return String.fromCodePoint(c2)
144
+ }
145
+ // 問題のエリア
146
+ if (this.HYPHENS[c]) { return '-' }
147
+ if (this.TILDES[c]) { return '~' }
148
+ if (this.SPACES[c]) { return ' ' }
149
+ return ch
150
+ }
151
+
152
+ /**
153
+ * @param {string} code
154
+ * @returns {{ text: string, sourcePosition: number }[]}
155
+ */
156
+ convert (code) {
157
+ if (!code) { return [] }
158
+ const src = new Replace(code)
159
+
160
+ // 改行コードを統一
161
+ src.replaceAll('\r\n', '\n')
162
+ src.replaceAll('\r', '\n')
163
+
164
+ let flagStr = false // 文字列リテラル内かどうか
165
+ let flagStr2 = false // 絵文字による文字列リテラル内かどうか
166
+ let endOfStr = '' // 文字列リテラルを終了させる記号
167
+ /** @type {{ text: string, sourcePosition: number }[]} */
168
+ const res = []
169
+ let left = 0 // 現在処理中の部分文字列の左端の位置
170
+ let str = '' // 文字列リテラルの値
171
+
172
+ // 一文字ずつ全角を半角に置換する
173
+ let i = 0
174
+ while (i < src.getText().length) {
175
+ const c = src.getText().charAt(i)
176
+ const ch2 = src.getText().substr(i, 2)
177
+ // 文字列のとき
178
+ if (flagStr) {
179
+ if (c === endOfStr) {
180
+ flagStr = false
181
+ res.push({ text: str + endOfStr, sourcePosition: src.getSourcePosition(left) })
182
+ i++
183
+ left = i
184
+ continue
185
+ }
186
+ str += c
187
+ i++
188
+ continue
189
+ }
190
+ // 絵文字制御による文字列のとき
191
+ if (flagStr2) {
192
+ if (ch2 === endOfStr) {
193
+ flagStr2 = false
194
+ res.push({ text: str + endOfStr, sourcePosition: src.getSourcePosition(left) })
195
+ i += 2
196
+ left = i
197
+ continue
198
+ }
199
+ str += c
200
+ i++
201
+ continue
202
+ }
203
+ // 文字列判定
204
+ if (c === '「') {
205
+ res.push({ text: c, sourcePosition: src.getSourcePosition(left) })
206
+ i++
207
+ left = i
208
+ flagStr = true
209
+ endOfStr = '」'
210
+ str = ''
211
+ continue
212
+ }
213
+ if (c === '『') {
214
+ res.push({ text: c, sourcePosition: src.getSourcePosition(left) })
215
+ i++
216
+ left = i
217
+ flagStr = true
218
+ endOfStr = '』'
219
+ str = ''
220
+ continue
221
+ }
222
+ if (c === '“') {
223
+ res.push({ text: c, sourcePosition: src.getSourcePosition(left) })
224
+ i++
225
+ left = i
226
+ flagStr = true
227
+ endOfStr = '”'
228
+ str = ''
229
+ continue
230
+ }
231
+ // JavaScriptの内部的には文字列はUTF-16で扱われてるので charAt を使う場合 絵文字が2文字扱いになる --- #726
232
+ if (ch2 === '🌴' || ch2 === '🌿') {
233
+ res.push({ text: ch2, sourcePosition: src.getSourcePosition(left) })
234
+ i += 2
235
+ left = i
236
+ flagStr2 = true
237
+ endOfStr = ch2
238
+ str = ''
239
+ continue
240
+ }
241
+ const c1 = this.convert1ch(c)
242
+ if (c1 === '"' || c1 === '\'') {
243
+ res.push({ text: c1, sourcePosition: src.getSourcePosition(left) })
244
+ i++
245
+ left = i
246
+ flagStr = true
247
+ endOfStr = c
248
+ str = ''
249
+ continue
250
+ }
251
+ // ラインコメントを飛ばす (#725)
252
+ if (c1 === '#') {
253
+ res.push({ text: c1, sourcePosition: src.getSourcePosition(left) })
254
+ i++
255
+ left = i
256
+ flagStr = true // 本当はコメントだけど便宜上
257
+ endOfStr = '\n'
258
+ str = ''
259
+ continue
260
+ }
261
+ // ラインコメントを飛ばす
262
+ if (ch2 === '//' || ch2 === '//') {
263
+ res.push({ text: '//', sourcePosition: src.getSourcePosition(left) }) // 強制的に'//'とする
264
+ i += 2
265
+ left = i
266
+ flagStr = true
267
+ endOfStr = '\n'
268
+ str = ''
269
+ continue
270
+ }
271
+ // 複数行コメント内を飛ばす (#731)
272
+ if (ch2 === '/*') {
273
+ res.push({ text: ch2, sourcePosition: src.getSourcePosition(left) })
274
+ i += 2
275
+ left = i
276
+ flagStr2 = true
277
+ endOfStr = '*/'
278
+ str = ''
279
+ continue
280
+ }
281
+ // 変換したものを追加
282
+ res.push({ text: c1, sourcePosition: src.getSourcePosition(left) })
283
+ i++
284
+ left = i
285
+ }
286
+ if (flagStr || flagStr2) {
287
+ res.push({ text: str + endOfStr, sourcePosition: src.getSourcePosition(left) })
288
+ }
289
+ return res
290
+ }
291
+ }
292
+
293
+ module.exports = NakoPrepare
@@ -0,0 +1,35 @@
1
+ /* eslint-disable quote-props */
2
+ /**
3
+ * 予約語
4
+ */
5
+
6
+ module.exports = {
7
+ '回': '回',
8
+ '回繰返': '回', // (#924)
9
+ '間': '間',
10
+ '間繰返': '間', // (#927)
11
+ '繰返': '繰り返す',
12
+ '反復': '反復',
13
+ '抜': '抜ける',
14
+ '続': '続ける',
15
+ '戻': '戻る',
16
+ '先': '先に',
17
+ '次': '次に',
18
+ '代入': '代入',
19
+ '実行速度優先': '実行速度優先',
20
+ 'パフォーマンスモニタ適用': 'パフォーマンスモニタ適用', // (#986)
21
+ '定': '定める',
22
+ '逐次実行': '逐次実行',
23
+ '条件分岐': '条件分岐',
24
+ '変数': '変数',
25
+ '定数': '定数',
26
+ 'エラー監視': 'エラー監視', // 例外処理:エラーならばと対
27
+ 'エラー': 'エラー',
28
+ 'それ': 'word',
29
+ 'そう': 'word', // 「それ」のエイリアス
30
+ '関数': 'def_func', // 無名関数の定義用
31
+ 'インデント構文': 'インデント構文', // https://nadesi.com/v3/doc/go.php?949
32
+ '非同期モード': '非同期モード', // (#637)
33
+ 'モード設定': 'モード設定', // (#1020)
34
+ '取込': '取込'
35
+ }