nadesiko3 3.2.52 → 3.3.3

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 (178) hide show
  1. package/README.md +1 -1
  2. package/bin/cnako3 +1 -1
  3. package/bin/cnako3.bat +1 -1
  4. package/demo/browsers.html +9 -10
  5. package/demo/flow.html +1 -0
  6. package/demo/runscript3.html +35 -0
  7. package/demo/turtle3.html +18 -17
  8. package/demo/turtle3d.html +1 -1
  9. package/demo/turtle3d2.html +1 -1
  10. package/doc/browsers.md +9 -10
  11. package/package.json +58 -57
  12. package/release/_hash.txt +57 -52
  13. package/release/_script-tags.txt +14 -13
  14. package/release/command.json +1 -1
  15. package/release/command.json.js +1 -1
  16. package/release/command_cnako3.json +1 -1
  17. package/release/command_list.json +1 -1
  18. package/release/editor.js +1 -1
  19. package/release/editor.js.LICENSE.txt +9 -12
  20. package/release/nako_gen_async.js +1 -1
  21. package/release/nako_gen_async.js.LICENSE.txt +10 -666
  22. package/release/plugin_caniuse.js +1 -1
  23. package/release/plugin_caniuse.js.LICENSE.txt +9 -425
  24. package/release/plugin_csv.js +1 -1
  25. package/release/plugin_csv.js.LICENSE.txt +3 -419
  26. package/release/plugin_datetime.js +1 -1
  27. package/release/plugin_datetime.js.LICENSE.txt +6 -530
  28. package/release/plugin_kansuji.js +1 -1
  29. package/release/plugin_kansuji.js.LICENSE.txt +3 -559
  30. package/release/plugin_markup.js +1 -1
  31. package/release/plugin_markup.js.LICENSE.txt +4 -420
  32. package/release/plugin_turtle.js +1 -1
  33. package/release/plugin_turtle.js.LICENSE.txt +12 -500
  34. package/release/plugin_webworker.js +1 -1
  35. package/release/plugin_webworker.js.LICENSE.txt +3 -563
  36. package/release/plugin_weykturtle3d.js +1 -0
  37. package/release/plugin_weykturtle3d.js.LICENSE.txt +3 -0
  38. package/release/stats.json +1 -1
  39. package/release/version.js +1 -1
  40. package/release/version.js.LICENSE.txt +9 -12
  41. package/release/wnako3.js +1 -1
  42. package/release/wnako3.js.LICENSE.txt +1 -1
  43. package/release/wnako3webworker.js +1 -1
  44. package/release/wnako3webworker.js.LICENSE.txt +1 -1117
  45. package/src/browsers.mjs +1 -0
  46. package/src/browsers.txt +15 -15
  47. package/src/browsers_agents.mjs +1 -0
  48. package/src/cnako3.mjs +13 -0
  49. package/src/{cnako3.js → cnako3mod.mjs} +119 -101
  50. package/src/{commander_ja.js → commander_ja.mjs} +16 -11
  51. package/src/{enako3.js → enako3.mjs} +0 -0
  52. package/src/{era.json → era.mjs} +1 -1
  53. package/src/image_turtle-elephant.mjs +5 -0
  54. package/src/image_turtle-panda.mjs +5 -0
  55. package/src/image_turtle64.mjs +5 -0
  56. package/src/index.mjs +9 -0
  57. package/src/{nako3.js → nako3.mjs} +69 -57
  58. package/src/{nako3_assert.js → nako3_assert.mjs} +7 -6
  59. package/src/{nako3server.js → nako3server.mjs} +12 -5
  60. package/src/{nako_colors.js → nako_colors.mjs} +1 -1
  61. package/src/{nako_errors.js → nako_errors.mjs} +10 -20
  62. package/src/{nako_from_dncl.js → nako_from_dncl.mjs} +11 -8
  63. package/src/{nako_gen.js → nako_gen.mjs} +93 -87
  64. package/src/{nako_gen_async.js → nako_gen_async.mjs} +5 -8
  65. package/src/{nako_global.js → nako_global.mjs} +4 -5
  66. package/src/{nako_indent.js → nako_indent.mjs} +29 -25
  67. package/src/{nako_josi_list.js → nako_josi_list.mjs} +16 -14
  68. package/src/nako_lex_rules.mjs +244 -0
  69. package/src/{nako_lexer.js → nako_lexer.mjs} +7 -10
  70. package/src/{nako_logger.js → nako_logger.mjs} +3 -4
  71. package/src/{nako_parser3.js → nako_parser3.mjs} +5 -5
  72. package/src/{nako_parser_base.js → nako_parser_base.mjs} +1 -9
  73. package/src/{nako_parser_const.js → nako_parser_const.mjs} +2 -6
  74. package/src/{nako_prepare.js → nako_prepare.mjs} +46 -8
  75. package/src/{nako_reserved_words.js → nako_reserved_words.mjs} +1 -1
  76. package/src/{nako_source_mapping.js → nako_source_mapping.mjs} +4 -11
  77. package/src/{nako_test.js → nako_test.mjs} +0 -0
  78. package/src/nako_version.mjs +7 -0
  79. package/src/{plugin_browser.js → plugin_browser.mjs} +30 -30
  80. package/src/{plugin_browser_ajax.js → plugin_browser_ajax.mjs} +7 -7
  81. package/src/{plugin_browser_audio.js → plugin_browser_audio.mjs} +1 -1
  82. package/src/{plugin_browser_canvas.js → plugin_browser_canvas.mjs} +1 -1
  83. package/src/{plugin_browser_chart.js → plugin_browser_chart.mjs} +1 -1
  84. package/src/{plugin_browser_color.js → plugin_browser_color.mjs} +1 -1
  85. package/src/{plugin_browser_crypto.js → plugin_browser_crypto.mjs} +1 -1
  86. package/src/{plugin_browser_dialog.js → plugin_browser_dialog.mjs} +1 -1
  87. package/src/{plugin_browser_dom_basic.js → plugin_browser_dom_basic.mjs} +1 -1
  88. package/src/{plugin_browser_dom_event.js → plugin_browser_dom_event.mjs} +1 -1
  89. package/src/{plugin_browser_dom_parts.js → plugin_browser_dom_parts.mjs} +1 -1
  90. package/src/{plugin_browser_geolocation.js → plugin_browser_geolocation.mjs} +1 -1
  91. package/src/{plugin_browser_hotkey.js → plugin_browser_hotkey.mjs} +2 -2
  92. package/src/{plugin_browser_html.js → plugin_browser_html.mjs} +1 -1
  93. package/src/{plugin_browser_in_worker.js → plugin_browser_in_worker.mjs} +4 -4
  94. package/src/{plugin_browser_location.js → plugin_browser_location.mjs} +1 -1
  95. package/src/{plugin_browser_speech.js → plugin_browser_speech.mjs} +1 -1
  96. package/src/{plugin_browser_storage.js → plugin_browser_storage.mjs} +1 -1
  97. package/src/{plugin_browser_system.js → plugin_browser_system.mjs} +1 -1
  98. package/src/{plugin_browser_websocket.js → plugin_browser_websocket.mjs} +1 -1
  99. package/src/{plugin_caniuse.js → plugin_caniuse.mjs} +8 -3
  100. package/src/{plugin_csv.js → plugin_csv.mjs} +4 -5
  101. package/src/{plugin_datetime.js → plugin_datetime.mjs} +19 -26
  102. package/src/{plugin_express.js → plugin_express.mjs} +4 -3
  103. package/src/{plugin_kansuji.js → plugin_kansuji.mjs} +1 -1
  104. package/src/{plugin_keigo.js → plugin_keigo.mjs} +1 -1
  105. package/src/{plugin_markup.js → plugin_markup.mjs} +6 -6
  106. package/src/{plugin_math.js → plugin_math.mjs} +1 -4
  107. package/src/{plugin_node.js → plugin_node.mjs} +25 -57
  108. package/src/{plugin_promise.js → plugin_promise.mjs} +1 -3
  109. package/src/{plugin_system.js → plugin_system.mjs} +32 -15
  110. package/src/{plugin_test.js → plugin_test.mjs} +1 -3
  111. package/src/{plugin_turtle.js → plugin_turtle.mjs} +5 -4
  112. package/src/{plugin_webworker.js → plugin_webworker.mjs} +3 -3
  113. package/src/{plugin_weykturtle3d.js → plugin_weykturtle3d.mjs} +1 -3
  114. package/src/{plugin_worker.js → plugin_worker.mjs} +4 -1
  115. package/src/{wnako3.js → wnako3.mjs} +24 -14
  116. package/src/{wnako3_editor.js → wnako3_editor.mjs} +28 -22
  117. package/src/{wnako3webworker.js → wnako3webworker.mjs} +0 -0
  118. package/test/async/{async_basic_test.js → async_basic_test.mjs} +3 -4
  119. package/test/browser/karma.config.js +9 -0
  120. package/test/browser/test/plugin_browser_test.js +2 -2
  121. package/test/browser/test/plugin_turtle_test.js +2 -2
  122. package/test/browser/test/plugin_webworker_test.js +3 -3
  123. package/test/browser/test/require_test.js +2 -2
  124. package/test/common/{array_test.js → array_test.mjs} +3 -3
  125. package/test/common/{basic_test.js → basic_test.mjs} +3 -13
  126. package/test/common/{calc_test.js → calc_test.mjs} +2 -2
  127. package/test/common/{debug_test.js → debug_test.mjs} +2 -2
  128. package/test/common/{dncl_test.js → dncl_test.mjs} +3 -3
  129. package/test/common/{error_test.js → error_test.mjs} +2 -2
  130. package/test/common/{flow_test.js → flow_test.mjs} +2 -2
  131. package/test/common/{func_call.js → func_call.mjs} +3 -3
  132. package/test/common/{func_test.js → func_test.mjs} +2 -2
  133. package/test/common/{indent_test.js → indent_test.mjs} +3 -2
  134. package/test/common/{lex_test.js → lex_test.mjs} +2 -2
  135. package/test/common/{literal_test.js → literal_test.mjs} +3 -2
  136. package/test/common/{nako_logger_test.js → nako_logger_test.mjs} +3 -3
  137. package/test/common/{plugin_browser_test.js → plugin_browser_test.mjs} +4 -4
  138. package/test/common/{plugin_browser_ut_audio_test.js → plugin_browser_ut_audio_test.mjs} +4 -4
  139. package/test/common/{plugin_browser_ut_color_test.js → plugin_browser_ut_color_test.mjs} +2 -2
  140. package/test/common/{plugin_browser_ut_dialog_test.js → plugin_browser_ut_dialog_test.mjs} +3 -3
  141. package/test/common/{plugin_browser_ut_html_test.js → plugin_browser_ut_html_test.mjs} +2 -2
  142. package/test/common/{plugin_browser_ut_system_test.js → plugin_browser_ut_system_test.mjs} +2 -2
  143. package/test/common/{plugin_csv_test.js → plugin_csv_test.mjs} +3 -3
  144. package/test/common/{plugin_datetime_test.js → plugin_datetime_test.mjs} +4 -4
  145. package/test/common/{plugin_kansuji_test.js → plugin_kansuji_test.mjs} +3 -3
  146. package/test/common/{plugin_markup_test.js → plugin_markup_test.mjs} +3 -3
  147. package/test/common/{plugin_promise_test.js → plugin_promise_test.mjs} +2 -2
  148. package/test/common/{plugin_system_test.js → plugin_system_test.mjs} +2 -2
  149. package/test/common/{prepare_test.js → prepare_test.mjs} +2 -2
  150. package/test/common/{re_test.js → re_test.mjs} +2 -2
  151. package/test/common/{variable_scope_test.js → variable_scope_test.mjs} +2 -2
  152. package/test/karma.config.js +3 -3
  153. package/test/node/{async_test.js → async_test.mjs} +4 -2
  154. package/test/node/{commander_ja_test.js → commander_ja_test.mjs} +17 -12
  155. package/test/node/{error_message_test.js → error_message_test.mjs} +13 -10
  156. package/test/node/node_test.mjs +57 -0
  157. package/test/node/{plugin_browser_ut_ajax_test.js → plugin_browser_ut_ajax_test.mjs} +7 -5
  158. package/test/node/{plugin_browser_ut_location_test.js → plugin_browser_ut_location_test.mjs} +3 -3
  159. package/test/node/{plugin_markup_test.js → plugin_markup_test.mjs} +6 -7
  160. package/test/node/{plugin_math_test.js → plugin_math_test.mjs} +5 -5
  161. package/test/node/{plugin_node_test.js → plugin_node_test.mjs} +11 -7
  162. package/test/node/{plugin_test.js → plugin_test.mjs} +11 -5
  163. package/test/node/{require_nako3_test.js → require_nako3_test.mjs} +19 -12
  164. package/test/node/{side_effects_test.js → side_effects_test.mjs} +24 -13
  165. package/test/node/{wnako3_editor_test.js → wnako3_editor_test.mjs} +16 -9
  166. package/tools/nako3edit/html/daisyui/LICENSE +22 -0
  167. package/tools/nako3edit/html/daisyui/full.css +1 -0
  168. package/tools/nako3edit/html/daisyui/tailwind.min.css +1 -0
  169. package/tools/nako3edit/html/daisyui/version_2.14.1 +1 -0
  170. package/tools/nako3edit/html/edit.html +104 -19
  171. package/tools/nako3edit/html/files.html +84 -8
  172. package/tools/nako3edit/html/nako3edit.css +6 -7
  173. package/tools/nako3edit/index.nako3 +45 -6
  174. package/tools/nako3edit/{run.js → run.mjs} +8 -2
  175. package/src/index.js +0 -5
  176. package/src/nako_lex_rules.js +0 -260
  177. package/src/nako_version.js +0 -8
  178. package/test/node/node_test.js +0 -43
@@ -7,9 +7,9 @@
7
7
 
8
8
  'use strict'
9
9
 
10
- const { NakoSyntaxError, NakoError, NakoRuntimeError } = require('./nako_errors')
11
- const nakoVersion = require('./nako_version')
12
- const NakoGen = require('./nako_gen')
10
+ import { NakoSyntaxError, NakoError, NakoRuntimeError } from './nako_errors.mjs'
11
+ import nakoVersion from './nako_version.mjs'
12
+ import { NakoGen } from './nako_gen.mjs'
13
13
 
14
14
  /**
15
15
  * @typedef {import("./nako3").Ast} Ast
@@ -62,14 +62,14 @@ class NakoCode {
62
62
  /**
63
63
  * 構文木からJSのコードを生成するクラス
64
64
  */
65
- class NakoGenAsync {
65
+ export class NakoGenASync {
66
66
  /**
67
67
  * @param {import('./nako3')} com
68
68
  * @param {Ast} ast
69
69
  * @param {boolean | string} isTest 文字列なら1つのテストだけを実行する
70
70
  */
71
71
  static generate (com, ast, isTest) {
72
- const gen = new NakoGenAsync(com)
72
+ const gen = new NakoGenASync(com)
73
73
 
74
74
  // ユーザー定義関数をシステムに登録する
75
75
  gen.registerFunction(ast)
@@ -1704,7 +1704,4 @@ if (typeof (navigator) === 'object' && typeof (navigator.nako3) === 'object') {
1704
1704
  // Webブラウザの場合
1705
1705
  const nako3 = navigator.nako3
1706
1706
  if (nako3.addCodeGenerator) { nako3.addCodeGenerator('非同期モード', NakoGenAsync) }
1707
- } else {
1708
- // モジュールモード
1709
- module.exports = NakoGenAsync
1710
1707
  }
@@ -1,9 +1,10 @@
1
- const NakoColors = require('./nako_colors')
2
- const {NakoRuntimeError} = require('./nako_errors')
1
+ import { NakoColors } from './nako_colors.mjs'
2
+ import { NakoRuntimeError } from './nako_errors.mjs'
3
+
3
4
  /**
4
5
  * コンパイルされたなでしこのプログラムで、グローバル空間のthisが指すオブジェクト
5
6
  */
6
- class NakoGlobal {
7
+ export class NakoGlobal {
7
8
  /**
8
9
  * @param {import('./nako3')} compiler
9
10
  * @param {import('./nako_gen')} gen
@@ -110,5 +111,3 @@ class NakoGlobal {
110
111
  this.reset()
111
112
  }
112
113
  }
113
-
114
- module.exports = NakoGlobal
@@ -1,5 +1,8 @@
1
- const { NakoIndentError } = require('./nako_errors')
2
- const NakoPrepare = require('./nako_prepare')
1
+ import { NakoIndentError } from './nako_errors.mjs'
2
+ import { NakoPrepare, checkNakoMode } from './nako_prepare.mjs'
3
+
4
+ // インデント構文のキーワード
5
+ const INDENT_MODE_KEYWORDS = ['!インデント構文', '!ここまでだるい']
3
6
 
4
7
  /**
5
8
  * インデント構文指定があればコードを変換する
@@ -8,34 +11,25 @@ const NakoPrepare = require('./nako_prepare')
8
11
  * @returns {{ code: string, insertedLines: number[], deletedLines: { lineNumber: number, len: number }[] }}
9
12
  */
10
13
  function convert (code, filename) {
11
- // 最初の30行をチェック
12
- if (isIndentSyntaxEnabled(code)) {
14
+ // インデント構文の適用が必要か?
15
+ if (checkNakoMode(code, INDENT_MODE_KEYWORDS)) {
13
16
  return convertGo(code, filename)
14
17
  }
15
18
  return { code, insertedLines: [], deletedLines: [] }
16
19
  }
17
20
 
18
- // ありえない改行マークを定義
19
- const SpecialRetMark = '🌟🌟改行🌟🌟s4j#WjcSb😀/FcX3🌟🌟'
20
-
21
21
  /**
22
+ * インデント構文指定があるかチェックする
22
23
  * @param {string} code
23
24
  * @returns {boolean}
24
25
  */
25
- function isIndentSyntaxEnabled (code) {
26
- // プログラム冒頭に「!インデント構文」があればインデント構文が有効
27
- const keywords = ['!インデント構文', '!ここまでだるい']
28
- const lines = code.split('\n', 30)
29
- for (const line of lines) {
30
- const sline = line.replace(/^(!|💡)/, '!')
31
- const s9 = sline.substring(0, 8)
32
- if (keywords.indexOf(s9) >= 0) {
33
- return true
34
- }
35
- }
36
- return false
26
+ function isIndentSyntaxEnabled(code) {
27
+ return checkNakoMode(code, INDENT_MODE_KEYWORDS)
37
28
  }
38
29
 
30
+ // ありえない改行マークを定義
31
+ const SpecialRetMark = '🌟🌟改行🌟🌟s4j#WjcSb😀/FcX3🌟🌟'
32
+
39
33
  /**
40
34
  * ソースコードのある1行の中のコメントを全て取り除く。
41
35
  * 事前にreplaceRetMarkによって文字列や範囲コメント内の改行文字が置換されている必要がある。
@@ -43,7 +37,7 @@ function isIndentSyntaxEnabled (code) {
43
37
  * @return {string}
44
38
  */
45
39
  function removeCommentsFromLine (src) {
46
- const prepare = new NakoPrepare() // `※`, `//`, `/*` といったパターン全てに対応するために必要
40
+ const prepare = NakoPrepare.getInstance(null) // `※`, `//`, `/*` といったパターン全てに対応するために必要
47
41
  const len = src.length
48
42
  let result = ''
49
43
  let eos = ''
@@ -211,9 +205,10 @@ function convertGo (code, filename) {
211
205
  // |
212
206
  lastIndent = indent
213
207
  while (indentStack.length > 0) {
208
+ /** @type {number} */
214
209
  const n = indentStack.pop()
215
210
  if (n === indent) {
216
- if (lineTrimed.substr(0, 3) !== '違えば') {
211
+ if (lineTrimed.substring(0, 3) !== '違えば') {
217
212
  insertedLines.push(lines2.length)
218
213
  lines2.push(makeIndent(n) + END)
219
214
  }
@@ -263,6 +258,11 @@ function convertGo (code, filename) {
263
258
  return { code: lines3.join('\n'), insertedLines, deletedLines }
264
259
  }
265
260
 
261
+ /**
262
+ * count分だけ字下げする
263
+ * @param {number} count
264
+ * @returns {string}
265
+ */
266
266
  function makeIndent (count) {
267
267
  let s = ''
268
268
  for (let i = 0; i < count; i++) {
@@ -311,8 +311,12 @@ function countIndent (line) {
311
311
  return cnt
312
312
  }
313
313
 
314
+ /**
315
+ * @param {string} src
316
+ * @returns {string}
317
+ */
314
318
  function replaceRetMark (src) {
315
- const prepare = new NakoPrepare() // `※`, `//`, `/*` といったパターン全てに対応するために必要
319
+ const prepare = NakoPrepare.getInstance(null) // `※`, `//`, `/*` といったパターン全てに対応するために必要
316
320
  const len = src.length
317
321
  let result = ''
318
322
  let eos = ''
@@ -422,7 +426,7 @@ function replaceRetMark (src) {
422
426
  * インデント構文が有効化されていない場合にも使われる。
423
427
  * @param {string} code
424
428
  */
425
- function getBlockStructure (code) {
429
+ export function getBlockStructure (code) {
426
430
  /** @type {{ lines: number[], pairs: [number, number][], parents: (number | null)[], spaces: string[] }} */
427
431
  const result = {
428
432
  lines: [], // 各行のインデント量
@@ -472,10 +476,10 @@ function getBlockStructure (code) {
472
476
  return result
473
477
  }
474
478
 
475
- module.exports = {
479
+ export default {
476
480
  convert,
477
481
  getBlockStructure,
478
482
  getIndent,
479
483
  countIndent,
480
- isIndentSyntaxEnabled
484
+ isIndentSyntaxEnabled,
481
485
  }
@@ -2,7 +2,7 @@
2
2
  * 助詞の一覧
3
3
  */
4
4
 
5
- const josiList = [
5
+ export const josiList = [
6
6
  'について', 'くらい', 'なのか', 'までを', 'までの', 'による',
7
7
  'とは', 'から', 'まで', 'だけ', 'より', 'ほど', 'など',
8
8
  'いて', 'えて', 'きて', 'けて', 'して', 'って', 'にて', 'みて',
@@ -11,22 +11,30 @@ const josiList = [
11
11
  ]
12
12
 
13
13
  // 「もし」文で使う助詞
14
- const tararebaJosiList = [
14
+ export const tararebaJosiList = [
15
15
  'でなければ', 'なければ', 'ならば', 'なら', 'たら', 'れば'
16
16
  ]
17
17
 
18
18
  // 意味のない助詞(削除する) #936 #939 #974
19
- const removeJosiList = [
19
+ export const removeJosiList = [
20
20
  'こと', 'である', 'です', 'します', 'でした'
21
21
  ]
22
22
 
23
- // 一覧をプログラムで扱いやすいようマップに変換
24
- const tararebaMap = {}
23
+ /**
24
+ * 「もし」文で使う「たら」「れば」などの一覧をプログラムで扱いやすいようマップに変換したもの
25
+ * @type {Object<string, boolean>}
26
+ */
27
+ export const tararebaMap = {}
25
28
  tararebaJosiList.forEach(josi => {
26
29
  josiList.push(josi)
27
30
  tararebaMap[josi] = true
28
31
  })
29
- const removeJosiMap = {}
32
+
33
+ /**
34
+ * 意味のない助詞(削除する)をマップに変換したもの
35
+ * @type {Object<string, boolean>}
36
+ */
37
+ export const removeJosiMap = {}
30
38
  removeJosiList.forEach(josi => {
31
39
  josiList.push(josi)
32
40
  removeJosiMap[josi] = true
@@ -36,11 +44,5 @@ removeJosiList.forEach(josi => {
36
44
  josiList.sort((a, b) => b.length - a.length)
37
45
 
38
46
  // 正規表現で助詞をマッチできるようにする
39
- const josiRE = new RegExp('^[\\t ]*(' + josiList.join('|') + ')')
40
-
41
- module.exports = {
42
- josiRE,
43
- tararebaMap,
44
- removeJosiMap,
45
- josiList
46
- }
47
+ export const josiRE = new RegExp('^[\\t ]*(' + josiList.join('|') + ')')
48
+
@@ -0,0 +1,244 @@
1
+ /**
2
+ * なでしこ3字句解析のためのルール
3
+ */
4
+
5
+ import { josiRE, removeJosiMap } from './nako_josi_list.mjs'
6
+
7
+ const kanakanji = /^[\u3005\u4E00-\u9FCF_a-zA-Z0-9ァ-ヶー\u2460-\u24FF\u2776-\u277F\u3251-\u32BF]+/
8
+ const hira = /^[ぁ-ん]/
9
+ const allHiragana = /^[ぁ-ん]+$/
10
+ const wordHasIjoIka = /^.+(以上|以下|超|未満)$/
11
+ const errorRead = (ch) => {
12
+ return function () { throw new Error('突然の『' + ch + '』があります。') }
13
+ }
14
+
15
+ export const unitRE = /^(円|ドル|元|歩|㎡|坪|度|℃|°|個|つ|本|冊|才|歳|匹|枚|皿|セット|羽|人|件|行|列|機|品|m|mm|cm|km|g|kg|t|px|dot|pt|em|b|mb|kb|gb)/
16
+ export const rules = [
17
+ // 上から順にマッチさせていく
18
+ { name: 'ここまで', pattern: /^;;;/ }, // #925
19
+ { name: 'eol', pattern: /^\n/ },
20
+ { name: 'eol', pattern: /^;/ },
21
+ // eslint-disable-next-line no-control-regex
22
+ { name: 'space', pattern: /^(\x20|\x09|・)+/ }, // #877,#1015
23
+ { name: 'comma', pattern: /^,/ },
24
+ { name: 'line_comment', pattern: /^#[^\n]*/ },
25
+ { name: 'line_comment', pattern: /^\/\/[^\n]*/ },
26
+ { name: 'range_comment', pattern: /^\/\*/, cbParser: cbRangeComment },
27
+ { name: 'def_test', pattern: /^●テスト:/ },
28
+ { name: 'def_func', pattern: /^●/ },
29
+ // 数値の判定 --- この後nako_lexerにて単位を読む処理が入る(#994)
30
+ { name: 'number', pattern: /^0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*/, readJosi: true, cb: parseNumber },
31
+ { name: 'number', pattern: /^0[oO][0-7]+(_[0-7]+)*/, readJosi: true, cb: parseNumber },
32
+ { name: 'number', pattern: /^0[bB][0-1]+(_[0-1]+)*/, readJosi: true, cb: parseNumber },
33
+ // 下の三つは小数点が挟まっている場合、小数点から始まっている場合、小数点がない場合の十進法の数値にマッチします
34
+ { name: 'number', pattern: /^\d+(_\d+)*\.(\d+(_\d+)*)?([eE][+|-]?\d+(_\d+)*)?/, readJosi: true, cb: parseNumber },
35
+ { name: 'number', pattern: /^\.\d+(_\d+)*([eE][+|-]?\d+(_\d+)*)?/, readJosi: true, cb: parseNumber },
36
+ { name: 'number', pattern: /^\d+(_\d+)*([eE][+|-]?\d+(_\d+)*)?/, readJosi: true, cb: parseNumber },
37
+ { name: 'ここから', pattern: /^(ここから),?/ },
38
+ { name: 'ここまで', pattern: /^(ここまで|💧)/ },
39
+ { name: 'もし', pattern: /^もしも?/ },
40
+ // 「ならば」は助詞として定義している
41
+ { name: '違えば', pattern: /^違(えば)?/ },
42
+ // 「回」「間」「繰返」「反復」「抜」「続」「戻」「代入」「条件分岐」などは replaceWord で word から変換
43
+ // @see nako_reserved_words.js
44
+ { name: 'shift_r0', pattern: /^>>>/ },
45
+ { name: 'shift_r', pattern: /^>>/ },
46
+ { name: 'shift_l', pattern: /^<</ },
47
+ { name: '===', pattern: /^===/ }, // #999
48
+ { name: '!==', pattern: /^!==/ }, // #999
49
+ { name: 'gteq', pattern: /^(≧|>=|=>)/ },
50
+ { name: 'lteq', pattern: /^(≦|<=|=<)/ },
51
+ { name: 'noteq', pattern: /^(≠|<>|!=)/ },
52
+ { name: '←', pattern: /^(←|<--)/ }, // 関数呼び出し演算子 #891 #899
53
+ { name: 'eq', pattern: /^(=|🟰)/ },
54
+ { name: 'line_comment', pattern: /^(!|💡)(インデント構文|ここまでだるい)[^\n]*/ }, // #1184
55
+ { name: 'not', pattern: /^(!|💡)/ }, // #1184
56
+ { name: 'gt', pattern: /^>/ },
57
+ { name: 'lt', pattern: /^</ },
58
+ { name: 'and', pattern: /^(かつ|&&)/ },
59
+ { name: 'or', pattern: /^(または|或いは|あるいは|\|\|)/ },
60
+ { name: '@', pattern: /^@/ },
61
+ { name: '+', pattern: /^\+/ },
62
+ { name: '-', pattern: /^-/ },
63
+ { name: '*', pattern: /^(×|\*)/ },
64
+ { name: '÷÷', pattern: /^÷÷/ }, // 整数の割り算
65
+ { name: '÷', pattern: /^(÷|\/)/ }, // 普通の割り算
66
+ { name: '%', pattern: /^%/ },
67
+ { name: '^', pattern: /^\^/ },
68
+ { name: '&', pattern: /^&/ },
69
+ { name: '[', pattern: /^\[/ },
70
+ { name: ']', pattern: /^]/, readJosi: true },
71
+ { name: '(', pattern: /^\(/ },
72
+ { name: ')', pattern: /^\)/, readJosi: true },
73
+ { name: '|', pattern: /^\|/ },
74
+ { name: 'string', pattern: /^🌿/, cbParser: src => cbString('🌿', '🌿', src) },
75
+ { name: 'string_ex', pattern: /^🌴/, cbParser: src => cbString('🌴', '🌴', src) },
76
+ { name: 'string_ex', pattern: /^「/, cbParser: src => cbString('「', '」', src) },
77
+ { name: 'string', pattern: /^『/, cbParser: src => cbString('『', '』', src) },
78
+ { name: 'string_ex', pattern: /^“/, cbParser: src => cbString('“', '”', src) },
79
+ { name: 'string_ex', pattern: /^"/, cbParser: src => cbString('"', '"', src) },
80
+ { name: 'string', pattern: /^'/, cbParser: src => cbString('\'', '\'', src) },
81
+ { name: '」', pattern: /^」/, cbParser: errorRead('」') }, // error
82
+ { name: '』', pattern: /^』/, cbParser: errorRead('』') }, // error
83
+ { name: 'func', pattern: /^\{関数\},?/ },
84
+ { name: '{', pattern: /^\{/ },
85
+ { name: '}', pattern: /^\}/, readJosi: true },
86
+ { name: ':', pattern: /^:/ },
87
+ { name: '_eol', pattern: /^_\s*\n/ },
88
+ { name: 'dec_lineno', pattern: /^‰/ },
89
+ // 絵文字変数 = (絵文字)英数字*
90
+ { name: 'word', pattern: /^[\uD800-\uDBFF][\uDC00-\uDFFF][_a-zA-Z0-9]*/, readJosi: true },
91
+ { name: 'word', pattern: /^[\u1F60-\u1F6F][_a-zA-Z0-9]*/, readJosi: true }, // 絵文字
92
+ { name: 'word', pattern: /^《.+?》/, readJosi: true }, // 《特別名前トークン》(#672)
93
+ // 単語句
94
+ {
95
+ name: 'word',
96
+ pattern: /^[_a-zA-Z\u3005\u4E00-\u9FCFぁ-んァ-ヶ\u2460-\u24FF\u2776-\u277F\u3251-\u32BF]/,
97
+ cbParser: cbWordParser
98
+ }
99
+ ]
100
+
101
+ export function trimOkurigana (s) {
102
+ // ひらがなから始まらない場合、送り仮名を削除。(例)置換する
103
+ if (!hira.test(s)) {
104
+ return s.replace(/[ぁ-ん]+/g, '')
105
+ }
106
+ // 全てひらがな? (例) どうぞ
107
+ if (allHiragana.test(s)) { return s }
108
+ // 末尾のひらがなのみ (例)お願いします →お願
109
+ return s.replace(/[ぁ-ん]+$/g, '')
110
+ }
111
+
112
+ // Utility for Rule
113
+ function cbRangeComment (src) {
114
+ let res = ''
115
+ const josi = ''
116
+ let numEOL = 0
117
+ src = src.substr(2) // skip /*
118
+ const i = src.indexOf('*/')
119
+ if (i < 0) { // not found
120
+ res = src
121
+ src = ''
122
+ } else {
123
+ res = src.substr(0, i)
124
+ src = src.substr(i + 2)
125
+ }
126
+ // 改行を数える
127
+ for (let i = 0; i < res.length; i++) { if (res.charAt(i) === '\n') { numEOL++ } }
128
+
129
+ res = res.replace(/(^\s+|\s+$)/, '') // trim
130
+ return { src: src, res: res, josi: josi, numEOL: numEOL }
131
+ }
132
+
133
+ /**
134
+ * @param {string} src
135
+ */
136
+ function cbWordParser (src, isTrimOkurigana = true) {
137
+ /*
138
+ kanji = [\u3005\u4E00-\u9FCF]
139
+ hiragana = [ぁ-ん]
140
+ katakana = [ァ-ヶー]
141
+ emoji = [\u1F60-\u1F6F]
142
+ uni_extra = [\uD800-\uDBFF] [\uDC00-\uDFFF]
143
+ alphabet = [_a-zA-Z]
144
+ numchars = [0-9]
145
+ */
146
+ let res = ''
147
+ let josi = ''
148
+ while (src !== '') {
149
+ if (res.length > 0) {
150
+ // 助詞の判定
151
+ const j = josiRE.exec(src)
152
+ if (j) {
153
+ josi = j[0].replace(/^\s+/, '')
154
+ src = src.substr(j[0].length)
155
+ // 助詞の直後にある「,」を飛ばす #877
156
+ if (src.charAt(0) === ',') { src = src.substr(1) }
157
+ break
158
+ }
159
+ }
160
+ // カタカナ漢字英数字か?
161
+ const m = kanakanji.exec(src)
162
+ if (m) {
163
+ res += m[0]
164
+ src = src.substr(m[0].length)
165
+ continue
166
+ }
167
+ // ひらがな?
168
+ const h = hira.test(src)
169
+ if (h) {
170
+ res += src.charAt(0)
171
+ src = src.substr(1)
172
+ continue
173
+ }
174
+ break // other chars
175
+ }
176
+ // 「間」の特殊ルール (#831)
177
+ // 「等しい間」や「一致する間」なら「間」をsrcに戻す。ただし「システム時間」はそのままにする。
178
+ if (/[ぁ-ん]間$/.test(res)) {
179
+ src = res.charAt(res.length - 1) + src
180
+ res = res.slice(0, -1)
181
+ }
182
+ // 「以上」「以下」「超」「未満」 #918
183
+ const ii = wordHasIjoIka.exec(res)
184
+ if (ii) {
185
+ src = ii[1] + josi + src
186
+ josi = ''
187
+ res = res.substr(0, res.length - ii[1].length)
188
+ }
189
+ // 助詞「こと」「である」「です」などは「**すること」のように使うので削除 #936 #939 #974
190
+ if (removeJosiMap[josi]) { josi = '' }
191
+
192
+ // 漢字カタカナ英語から始まる語句 --- 送り仮名を省略
193
+ if (isTrimOkurigana) {
194
+ res = trimOkurigana(res)
195
+ }
196
+ // 助詞だけの語句の場合
197
+ if (res === '' && josi !== '') {
198
+ res = josi
199
+ josi = ''
200
+ }
201
+ return { src: src, res: res, josi: josi, numEOL: 0 }
202
+ }
203
+
204
+ function cbString (beginTag, closeTag, src) {
205
+ let res = ''
206
+ let josi = ''
207
+ let numEOL = 0
208
+ src = src.substr(beginTag.length) // skip beginTag
209
+ const i = src.indexOf(closeTag)
210
+ if (i < 0) { // not found
211
+ res = src
212
+ src = ''
213
+ } else {
214
+ res = src.substr(0, i)
215
+ src = src.substr(i + closeTag.length)
216
+ // res の中に beginTag があればエラーにする #953
217
+ if (res.indexOf(beginTag) >= 0) {
218
+ if (beginTag === '『') {
219
+ throw new Error('「『」で始めた文字列の中に「『」を含めることはできません。')
220
+ } else {
221
+ throw new Error(`『${beginTag}』で始めた文字列の中に『${beginTag}』を含めることはできません。`)
222
+ }
223
+ }
224
+ }
225
+ // 文字列直後の助詞を取得
226
+ const j = josiRE.exec(src)
227
+ if (j) {
228
+ josi = j[0].replace(/^\s+/, '')
229
+ src = src.substr(j[0].length)
230
+ // 助詞の後のカンマ #877
231
+ if (src.charAt(0) === ',') { src = src.substr(1) }
232
+ }
233
+ // 助詞「こと」「である」「です」などは「**すること」のように使うので削除 #936 #939 #974
234
+ if (removeJosiMap[josi]) { josi = '' }
235
+
236
+ // 改行を数える
237
+ for (let i = 0; i < res.length; i++) { if (res.charAt(i) === '\n') { numEOL++ } }
238
+
239
+ return { src: src, res: res, josi: josi, numEOL: numEOL }
240
+ }
241
+
242
+ function parseNumber (n) {
243
+ return Number(n.replace(/_/g, ''))
244
+ }
@@ -1,19 +1,18 @@
1
1
  // なでしこの字句解析を行う
2
2
  // 既に全角半角を揃えたコードに対して字句解析を行う
3
- const { opPriority } = require('./nako_parser_const')
3
+ import { opPriority } from './nako_parser_const.mjs'
4
4
 
5
5
  // 予約語句
6
6
  // (memo)「回」「間」「繰返」「反復」「抜」「続」「戻」「代入」などは replaceWord で word から変換
7
- const reservedWords = require('./nako_reserved_words')
7
+ import reservedWords from './nako_reserved_words.mjs'
8
8
 
9
9
  // 助詞の一覧
10
- const { josiRE, removeJosiMap, tararebaMap } = require('./nako_josi_list')
10
+ import { josiRE, removeJosiMap, tararebaMap } from './nako_josi_list.mjs'
11
11
 
12
12
  // 字句解析ルールの一覧
13
- const lexRules = require('./nako_lex_rules')
14
- const rules = lexRules.rules
13
+ import { rules, unitRE } from './nako_lex_rules.mjs'
15
14
 
16
- const { NakoLexerError, InternalLexerError } = require('./nako_errors')
15
+ import { NakoLexerError, InternalLexerError } from './nako_errors.mjs'
17
16
 
18
17
  /**
19
18
  * @typedef {import('./nako3').TokenWithSourceMap} TokenWithSourceMap
@@ -43,7 +42,7 @@ const { NakoLexerError, InternalLexerError } = require('./nako_errors')
43
42
  * )>} FuncList
44
43
  */
45
44
 
46
- class NakoLexer {
45
+ export class NakoLexer {
47
46
  /**
48
47
  * @param {import("./nako_logger")} logger
49
48
  */
@@ -508,7 +507,7 @@ class NakoLexer {
508
507
  // 数値なら単位を持つか? --- #994
509
508
  if (rule.name === 'number') {
510
509
  // 単位があれば読み飛ばす
511
- const um = lexRules.unitRE.exec(src)
510
+ const um = unitRE.exec(src)
512
511
  if (um) {
513
512
  src = src.substring(um[0].length)
514
513
  column += m[0].length
@@ -573,5 +572,3 @@ class NakoLexer {
573
572
  return result
574
573
  }
575
574
  }
576
-
577
- module.exports = NakoLexer
@@ -3,8 +3,8 @@
3
3
  * @typedef {'trace' | 'debug' | 'info' | 'warn' | 'error' | 'stdout'} LogLevel
4
4
  */
5
5
 
6
- const { NakoError } = require('./nako_errors')
7
- const NakoColors = require('./nako_colors')
6
+ import { NakoError } from './nako_errors.mjs'
7
+ import { NakoColors } from './nako_colors.mjs'
8
8
 
9
9
  /**
10
10
  * @typedef {{ startOffset?: number | null, endOffset?: number | null, line?: number, file?: string }} Position
@@ -34,7 +34,7 @@ const levels = ['trace', 'debug', 'info', 'warn', 'error', 'stdout']
34
34
  * trace(), debug(), info(), warn(), error() はそれぞれメッセージに `[警告]` などのタグとエラー位置の日本語表現を付けて表示する。
35
35
  * error() は引数にエラーオブジェクトを受け取ることもでき、その場合エラーオブジェクトからエラーメッセージとエラー位置が取り出される。
36
36
  */
37
- class NakoLogger {
37
+ export class NakoLogger {
38
38
  constructor () {
39
39
  /** @type {{ level: LogLevel, callback: LogListener, includeHigherLevels: boolean }[]} */
40
40
  this.listeners = []
@@ -135,4 +135,3 @@ class NakoLogger {
135
135
  }
136
136
  }
137
137
 
138
- module.exports = NakoLogger
@@ -1,8 +1,10 @@
1
1
  /**
2
2
  * nadesiko v3 parser (demo version)
3
3
  */
4
- const { opPriority, keizokuJosi } = require('./nako_parser_const')
5
- const { NakoParserBase, NakoSyntaxError } = require('./nako_parser_base')
4
+ import { opPriority, keizokuJosi } from './nako_parser_const.mjs'
5
+ import { NakoParserBase } from './nako_parser_base.mjs'
6
+ import { NakoSyntaxError } from './nako_errors.mjs'
7
+
6
8
  /**
7
9
  * @type {string[]}
8
10
  */
@@ -15,7 +17,7 @@ for (const key in opPriority) { operatorList.push(key) }
15
17
  */
16
18
 
17
19
  // @ts-ignore
18
- class NakoParser extends NakoParserBase {
20
+ export class NakoParser extends NakoParserBase {
19
21
  /**
20
22
  * @param {TokenWithSourceMap[]} tokens 字句解析済みのトークンの配列
21
23
  * @return {Ast} AST(構文木)
@@ -2015,5 +2017,3 @@ class NakoParser extends NakoParserBase {
2015
2017
  }
2016
2018
  }
2017
2019
  }
2018
-
2019
- module.exports = NakoParser
@@ -1,10 +1,7 @@
1
1
  /**
2
2
  * なでしこの構文解析のためのユーティリティクラス
3
3
  */
4
-
5
- const { NakoSyntaxError } = require('./nako_errors')
6
-
7
- class NakoParserBase {
4
+ export class NakoParserBase {
8
5
  /**
9
6
  * @param {import("./nako_logger")} logger
10
7
  */
@@ -264,8 +261,3 @@ class NakoParserBase {
264
261
  }
265
262
  }
266
263
  }
267
-
268
- module.exports = {
269
- NakoParserBase,
270
- NakoSyntaxError
271
- }
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable quote-props */
2
- const opPriority = {
2
+ export const opPriority = {
3
3
  // and or
4
4
  'and': 1,
5
5
  'or': 1,
@@ -29,11 +29,7 @@ const opPriority = {
29
29
  '^': 6
30
30
  }
31
31
 
32
- const keizokuJosi = [
32
+ export const keizokuJosi = [
33
33
  'いて', 'えて', 'きて', 'けて', 'して', 'って', 'にて', 'みて', 'めて', 'ねて', 'には', 'んで'
34
34
  ]
35
35
 
36
- module.exports = {
37
- opPriority,
38
- keizokuJosi
39
- }