nadesiko3 3.3.48 → 3.3.49

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 (105) hide show
  1. package/core/.editorconfig +6 -0
  2. package/core/.eslintrc.cjs +33 -0
  3. package/core/.github/dependabot.yml +7 -0
  4. package/core/.github/workflows/nodejs.yml +37 -0
  5. package/core/.github/workflows/super-linter.yml +61 -0
  6. package/core/.github/workflows/textlint.yml +199 -0
  7. package/core/LICENSE +21 -0
  8. package/core/README.md +66 -0
  9. package/core/batch/build_nako_version.nako3 +42 -0
  10. package/core/command/snako.mjs +105 -0
  11. package/core/command/snako.mts +116 -0
  12. package/core/index.mjs +21 -0
  13. package/core/index.mts +21 -0
  14. package/core/package.json +47 -0
  15. package/core/sample/hello.nako3 +7 -0
  16. package/core/sample/hoge.mjs +4 -0
  17. package/core/sample/hoge.mts +6 -0
  18. package/core/src/nako3.mjs +858 -0
  19. package/core/src/nako3.mts +967 -0
  20. package/core/src/nako_colors.mjs +78 -0
  21. package/core/src/nako_colors.mts +86 -0
  22. package/core/src/nako_core_version.mjs +8 -0
  23. package/core/src/nako_core_version.mts +19 -0
  24. package/core/src/nako_csv.mjs +185 -0
  25. package/core/src/nako_csv.mts +188 -0
  26. package/core/src/nako_errors.mjs +173 -0
  27. package/core/src/nako_errors.mts +197 -0
  28. package/core/src/nako_from_dncl.mjs +255 -0
  29. package/core/src/nako_from_dncl.mts +250 -0
  30. package/core/src/nako_gen.mjs +1648 -0
  31. package/core/src/nako_gen.mts +1719 -0
  32. package/core/src/nako_gen_async.mjs +1659 -0
  33. package/core/src/nako_gen_async.mts +1732 -0
  34. package/core/src/nako_global.mjs +107 -0
  35. package/core/src/nako_global.mts +138 -0
  36. package/core/src/nako_indent.mjs +445 -0
  37. package/core/src/nako_indent.mts +492 -0
  38. package/core/src/nako_josi_list.mjs +38 -0
  39. package/core/src/nako_josi_list.mts +45 -0
  40. package/core/src/nako_lex_rules.mjs +253 -0
  41. package/core/src/nako_lex_rules.mts +260 -0
  42. package/core/src/nako_lexer.mjs +609 -0
  43. package/core/src/nako_lexer.mts +612 -0
  44. package/core/src/nako_logger.mjs +199 -0
  45. package/core/src/nako_logger.mts +232 -0
  46. package/core/src/nako_parser3.mjs +2439 -0
  47. package/core/src/nako_parser3.mts +2195 -0
  48. package/core/src/nako_parser_base.mjs +370 -0
  49. package/core/src/nako_parser_base.mts +370 -0
  50. package/core/src/nako_parser_const.mjs +37 -0
  51. package/core/src/nako_parser_const.mts +37 -0
  52. package/core/src/nako_prepare.mjs +304 -0
  53. package/core/src/nako_prepare.mts +315 -0
  54. package/core/src/nako_reserved_words.mjs +38 -0
  55. package/core/src/nako_reserved_words.mts +38 -0
  56. package/core/src/nako_source_mapping.mjs +207 -0
  57. package/core/src/nako_source_mapping.mts +262 -0
  58. package/core/src/nako_test.mjs +37 -0
  59. package/core/src/nako_types.mjs +25 -0
  60. package/core/src/nako_types.mts +151 -0
  61. package/core/src/plugin_csv.mjs +49 -0
  62. package/core/src/plugin_csv.mts +50 -0
  63. package/core/src/plugin_math.mjs +328 -0
  64. package/core/src/plugin_math.mts +326 -0
  65. package/core/src/plugin_promise.mjs +91 -0
  66. package/core/src/plugin_promise.mts +91 -0
  67. package/core/src/plugin_system.mjs +2832 -0
  68. package/core/src/plugin_system.mts +2690 -0
  69. package/core/src/plugin_test.mjs +34 -0
  70. package/core/src/plugin_test.mts +34 -0
  71. package/core/test/array_test.mjs +34 -0
  72. package/core/test/basic_test.mjs +344 -0
  73. package/core/test/calc_test.mjs +140 -0
  74. package/core/test/core_module_test.mjs +23 -0
  75. package/core/test/debug_test.mjs +16 -0
  76. package/core/test/dncl_test.mjs +94 -0
  77. package/core/test/error_message_test.mjs +210 -0
  78. package/core/test/error_test.mjs +16 -0
  79. package/core/test/flow_test.mjs +373 -0
  80. package/core/test/func_call.mjs +160 -0
  81. package/core/test/func_test.mjs +149 -0
  82. package/core/test/indent_test.mjs +364 -0
  83. package/core/test/lex_test.mjs +168 -0
  84. package/core/test/literal_test.mjs +73 -0
  85. package/core/test/nako_lexer_test.mjs +35 -0
  86. package/core/test/nako_logger_test.mjs +76 -0
  87. package/core/test/nako_logger_test.mts +78 -0
  88. package/core/test/plugin_csv_test.mjs +38 -0
  89. package/core/test/plugin_promise_test.mjs +18 -0
  90. package/core/test/plugin_system_test.mjs +630 -0
  91. package/core/test/prepare_test.mjs +96 -0
  92. package/core/test/re_test.mjs +22 -0
  93. package/core/test/side_effects_test.mjs +92 -0
  94. package/core/test/variable_scope_test.mjs +149 -0
  95. package/core/tsconfig.json +101 -0
  96. package/package.json +4 -2
  97. package/release/_hash.txt +12 -12
  98. package/release/_script-tags.txt +14 -14
  99. package/release/editor.js +1 -1
  100. package/release/stats.json +1 -1
  101. package/release/version.js +1 -1
  102. package/release/wnako3.js +1 -1
  103. package/src/nako_version.mjs +2 -2
  104. package/src/nako_version.mts +2 -2
  105. package/test/async/async_basic_test.mjs +3 -3
@@ -0,0 +1,967 @@
1
+ /**
2
+ * nadesiko v3
3
+ */
4
+ // types
5
+ import { Token, Ast, FuncList, FuncListItem, FuncArgs, NakoEvent, CompilerOptions, NakoComEventName } from './nako_types.mjs'
6
+ // parser / lexer
7
+ import { NakoParser } from './nako_parser3.mjs'
8
+ import { NakoLexer } from './nako_lexer.mjs'
9
+ import { NakoPrepare } from './nako_prepare.mjs'
10
+ import { NakoGen, generateJS, NakoGenOptions, NakoGenResult } from './nako_gen.mjs'
11
+ import { NakoGenASync } from './nako_gen_async.mjs'
12
+ import NakoIndent from './nako_indent.mjs'
13
+ import { convertDNCL } from './nako_from_dncl.mjs'
14
+ import { SourceMappingOfTokenization, SourceMappingOfIndentSyntax, OffsetToLineColumn, subtractSourceMapByPreCodeLength } from './nako_source_mapping.mjs'
15
+ import { NakoLexerError, NakoImportError, NakoSyntaxError, InternalLexerError } from './nako_errors.mjs'
16
+ import { NakoLogger } from './nako_logger.mjs'
17
+ import { NakoGlobal } from './nako_global.mjs'
18
+ // version info
19
+ import coreVersion from './nako_core_version.mjs'
20
+ // basic plugins
21
+ import PluginSystem from './plugin_system.mjs'
22
+ import PluginMath from './plugin_math.mjs'
23
+ import PluginCSV from './plugin_csv.mjs'
24
+ import PluginPromise from './plugin_promise.mjs'
25
+ import PluginTest from './plugin_test.mjs'
26
+
27
+ const cloneAsJSON = (x: any): any => JSON.parse(JSON.stringify(x))
28
+
29
+ export interface NakoCompilerOption {
30
+ useBasicPlugin: boolean;
31
+ }
32
+
33
+ /** インタプリタに「取り込み」文を追加するために用意するオブジェクト */
34
+ export interface LoaderToolTask<T> {
35
+ task: Promise<T>;
36
+ }
37
+ export interface LoaderTool {
38
+ // type: 'nako3' | 'js' | 'invalid' | 'mjs'
39
+ resolvePath: (name: string, token: Token, fromFile: string) => { type: string, filePath: string };
40
+ readNako3: (filePath: string, token: Token) => LoaderToolTask<string>;
41
+ readJs: (filePath: string, token: Token) => LoaderToolTask<any>;
42
+ }
43
+
44
+ interface DependenciesItem {
45
+ tokens: Token[];
46
+ alias: Set<string>;
47
+ addPluginFile: () => void;
48
+ funclist: any;
49
+ }
50
+ type Dependencies = { [key:string]:DependenciesItem }
51
+
52
+ interface LexResult {
53
+ commentTokens: Token[];
54
+ tokens: Token[];
55
+ requireTokens: Token[];
56
+ }
57
+
58
+ type NakoVars = {[key: string]: any}
59
+
60
+ /** なでしこコンパイラ */
61
+ export class NakoCompiler {
62
+ private nakoFuncList: FuncList;
63
+ private funclist: FuncList;
64
+ private pluginFunclist: Record<string, FuncListItem>;
65
+ private pluginfiles: Record<string, any>;
66
+ private commandlist: Set<string>;
67
+ private prepare: NakoPrepare;
68
+ private parser: NakoParser;
69
+ private lexer: NakoLexer;
70
+ private dependencies: Dependencies;
71
+ private usedFuncs: Set<string>;
72
+ protected logger: NakoLogger;
73
+ protected eventList: NakoEvent[];
74
+ // global objects
75
+ __varslist: NakoVars[];
76
+ __locals: NakoVars;
77
+ __self: NakoCompiler;
78
+ __vars: NakoVars;
79
+ __v0: NakoVars;
80
+ __v1: NakoVars;
81
+ __globals: NakoGlobal[];
82
+ __module: Record<string, Record<string, FuncListItem>>;
83
+ numFailures: number; // エラーレポートの数を記録
84
+ public version: string;
85
+ public coreVersion: string;
86
+ /**
87
+ * @param {undefined | {'useBasicPlugin':true|false}} options
88
+ */
89
+ constructor (options: NakoCompilerOption | undefined = undefined) {
90
+ if (options === undefined) {
91
+ options = { useBasicPlugin: true }
92
+ }
93
+ // 環境のリセット
94
+ /** @type {Record<string, any>[]} */
95
+ this.__varslist = [{}, {}, {}] // このオブジェクトは変更しないこと (this.gen.__varslist と共有する)
96
+ this.__locals = {} // ローカル変数
97
+ this.__self = this
98
+ this.__vars = this.__varslist[2]
99
+ this.__v0 = this.__varslist[0]
100
+ this.__v1 = this.__varslist[1]
101
+ // バージョンを設定
102
+ this.version = coreVersion.version
103
+ this.coreVersion = coreVersion.version
104
+ /**
105
+ * @type {NakoGlobal[]}
106
+ */
107
+ this.__globals = [] // 生成した NakoGlobal のインスタンスを保持
108
+ /** @type {Record<string, Record<string, NakoFunction>>} */
109
+ this.__module = {} // requireなどで取り込んだモジュールの一覧
110
+ this.pluginFunclist = {} // プラグインで定義された関数
111
+ this.funclist = {} // プラグインで定義された関数 + ユーザーが定義した関数
112
+ this.pluginfiles = {} // 取り込んだファイル一覧
113
+ this.commandlist = new Set() // プラグインで定義された定数・変数・関数の名前
114
+ this.nakoFuncList = {} // __v1に配置するJavaScriptのコードで定義された関数
115
+ this.eventList = [] // 実行前に環境を変更するためのイベント
116
+
117
+ this.logger = new NakoLogger()
118
+
119
+ // 必要なオブジェクトを覚えておく
120
+ this.prepare = NakoPrepare.getInstance()
121
+ this.parser = new NakoParser(this.logger)
122
+ this.lexer = new NakoLexer(this.logger)
123
+
124
+ /**
125
+ * 取り込み文を置換するためのオブジェクト。
126
+ * 正規化されたファイル名がキーになり、取り込み文の引数に指定された正規化されていないファイル名はaliasに入れられる。
127
+ * JavaScriptファイルによるプラグインの場合、contentは空文字列。
128
+ * funclistはシンタックスハイライトの高速化のために事前に取り出した、ファイルが定義する関数名のリスト。
129
+ */
130
+ this.dependencies = {}
131
+ this.usedFuncs = new Set()
132
+ this.numFailures = 0
133
+
134
+ if (options.useBasicPlugin) { this.addBasicPlugins() }
135
+ }
136
+
137
+ getLogger (): NakoLogger {
138
+ return this.logger
139
+ }
140
+
141
+ getNakoFuncList (): FuncList {
142
+ return this.nakoFuncList
143
+ }
144
+
145
+ getNakoFunc (name: string): FuncListItem|undefined {
146
+ return this.nakoFuncList[name]
147
+ }
148
+
149
+ getPluginfiles (): Record<string, any> {
150
+ return this.pluginfiles
151
+ }
152
+
153
+ /**
154
+ * 基本的なプラグインを追加する
155
+ */
156
+ addBasicPlugins () {
157
+ this.addPluginObject('PluginSystem', PluginSystem)
158
+ this.addPluginObject('PluginMath', PluginMath)
159
+ this.addPluginObject('PluginPromise', PluginPromise)
160
+ this.addPluginObject('PluginAssert', PluginTest)
161
+ this.addPluginObject('PluginCSV', PluginCSV)
162
+ }
163
+
164
+ /**
165
+ * loggerを新しいインスタンスで置き換える。
166
+ */
167
+ replaceLogger () {
168
+ const logger = this.lexer.logger = this.parser.logger = this.logger = new NakoLogger()
169
+ return logger
170
+ }
171
+
172
+ /**
173
+ * ファイル内のrequire文の位置を列挙する。出力の配列はstartでソートされている。
174
+ * @param {Token[]} tokens rawtokenizeの出力
175
+ */
176
+ static listRequireStatements (tokens: Token[]): Token[] {
177
+ const requireStatements: Token[] = []
178
+ for (let i = 0; i + 2 < tokens.length; i++) {
179
+ // not (string|string_ex) '取り込み'
180
+ if (!(tokens[i].type === 'not' &&
181
+ (tokens[i + 1].type === 'string' || tokens[i + 1].type === 'string_ex') &&
182
+ tokens[i + 2].value === '取込')) {
183
+ continue
184
+ }
185
+ requireStatements.push({
186
+ ...tokens[i],
187
+ start: i,
188
+ end: i + 3,
189
+ value: tokens[i + 1].value + '',
190
+ firstToken: tokens[i],
191
+ lastToken: tokens[i + 2]
192
+ })
193
+ i += 2
194
+ }
195
+ return requireStatements
196
+ }
197
+
198
+ /**
199
+ * プログラムが依存するファイルを再帰的に取得する。
200
+ * - 依存するファイルがJavaScriptファイルの場合、そのファイルを実行して評価結果をthis.addPluginFileに渡す。
201
+ * - 依存するファイルがなでしこ言語の場合、ファイルの中身を取得して変数に保存し、再帰する。
202
+ *
203
+ * @param {string} code
204
+ * @param {string} filename
205
+ * @param {string} preCode
206
+ * @param {LoaderTool} tools 実行環境 (ブラウザ or Node.js) によって外部ファイルの取得・実行方法は異なるため、引数でそれらを行う関数を受け取る。
207
+ * - resolvePath は指定した名前をもつファイルを検索し、正規化されたファイル名を返す関数。返されたファイル名はreadNako3かreadJsの引数になる。
208
+ * - readNako3は指定されたファイルの中身を返す関数。
209
+ * - readJsは指定したファイルをJavaScriptのプログラムとして実行し、`export default` でエクスポートされた値を返す関数。
210
+ * @returns {Promise<unknown> | void}
211
+ * @protected
212
+ */
213
+ _loadDependencies (code: string, filename: string, preCode: string, tools: LoaderTool) {
214
+ const dependencies: Dependencies = {}
215
+ const compiler = new NakoCompiler({ useBasicPlugin: true })
216
+
217
+ /**
218
+ * @param {any} item
219
+ * @param {any} tasks
220
+ */
221
+ const loadJS = (item: any, tasks: any) => {
222
+ // jsならプラグインとして読み込む。(ESMでは必ず動的に読む)
223
+ const obj = tools.readJs(item.filePath, item.firstToken)
224
+ tasks.push(obj.task.then((res: any) => {
225
+ const pluginFuncs = res()
226
+ this.addPluginFile(item.value, item.filePath, pluginFuncs, false)
227
+ dependencies[item.filePath].funclist = pluginFuncs
228
+ dependencies[item.filePath].addPluginFile = () => { this.addPluginFile(item.value, item.filePath, pluginFuncs, false) }
229
+ }))
230
+ }
231
+ const loadNako3 = (item: any, tasks: any) => {
232
+ // nako3ならファイルを読んでdependenciesに保存する。
233
+ const content = tools.readNako3(item.filePath, item.firstToken)
234
+ const registerFile = (code: string) => {
235
+ // シンタックスハイライトの高速化のために、事前にファイルが定義する関数名のリストを取り出しておく。
236
+ // preDefineFuncはトークン列に変更を加えるため、事前にクローンしておく。
237
+ // 「プラグイン名設定」を行う (#956)
238
+ const modName = NakoLexer.filenameToModName(item.filePath)
239
+ code = `『${modName}』にプラグイン名設定;` + code + ';『メイン』にプラグイン名設定;'
240
+ const tokens = this.rawtokenize(code, 0, item.filePath)
241
+ dependencies[item.filePath].tokens = tokens
242
+ const funclist = {}
243
+ NakoLexer.preDefineFunc(cloneAsJSON(tokens), this.logger, funclist)
244
+ dependencies[item.filePath].funclist = funclist
245
+ // 再帰
246
+ return loadRec(code, item.filePath, '')
247
+ }
248
+ // 取り込み構文における問題を減らすため、必ず非同期でプログラムを読み込む仕様とした #1219
249
+ tasks.push(content.task.then((res) => registerFile(res)))
250
+ }
251
+ const loadRec = (code: string, filename: string, preCode: string): Promise<unknown>|void => {
252
+ const tasks: Promise<unknown>[] = []
253
+ // 取り込みが必要な情報一覧を調べる(トークン分割して、取り込みタグを得る)
254
+ const tags = NakoCompiler.listRequireStatements(compiler.rawtokenize(code, 0, filename, preCode))
255
+ // パスを解決する
256
+ const tagsResolvePath = tags.map((v) => ({ ...v, ...tools.resolvePath(v.value, v.firstToken as Token, filename) }))
257
+ // 取り込み開始
258
+ for (const item of tagsResolvePath) {
259
+ // 2回目以降の読み込み
260
+ // eslint-disable-next-line no-prototype-builtins
261
+ if (dependencies.hasOwnProperty(item.filePath)) {
262
+ dependencies[item.filePath].alias.add(item.value)
263
+ continue
264
+ }
265
+
266
+ // 初回の読み込み
267
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
268
+ dependencies[item.filePath] = { tokens: [], alias: new Set([item.value]), addPluginFile: ():void => {}, funclist: {} }
269
+ if (item.type === 'js' || item.type === 'mjs') {
270
+ loadJS(item, tasks)
271
+ } else if (item.type === 'nako3') {
272
+ loadNako3(item, tasks)
273
+ } else {
274
+ throw new NakoImportError(`ファイル『${item.value}』を読み込めません。ファイルが存在しないか未対応の拡張子です。`,
275
+ (item.firstToken as Token).file, (item.firstToken as Token).line)
276
+ }
277
+ }
278
+ if (tasks.length > 0) {
279
+ return Promise.all(tasks)
280
+ }
281
+ return undefined
282
+ }
283
+
284
+ try {
285
+ const result = loadRec(code, filename, preCode)
286
+
287
+ // 非同期な場合のエラーハンドリング
288
+ if (result !== undefined) {
289
+ result.catch((err) => {
290
+ // 読み込みに失敗しても処理は続ける方針なので、失敗しても例外は投げない
291
+ // たぶん、その後の構文解析でエラーになるため
292
+ this.logger.warn(err.msg)
293
+ })
294
+ }
295
+
296
+ // すべてが終わってからthis.dependenciesに代入する。そうしないと、「実行」ボタンを連打した場合など、
297
+ // loadDependencies() が並列実行されるときに正しく動作しない。
298
+ this.dependencies = dependencies
299
+ return result
300
+ } catch (err) {
301
+ // 同期処理では素直に例外を投げる
302
+ this.logger.warn('' + err)
303
+ throw err
304
+ }
305
+ }
306
+
307
+ /**
308
+ * コードを単語に分割する
309
+ * @param {string} code なでしこのプログラム
310
+ * @param {number} line なでしこのプログラムの行番号
311
+ * @param {string} filename
312
+ * @param {string} [preCode]
313
+ * @returns {TokenWithSourceMap[]} トークンのリスト
314
+ */
315
+ rawtokenize (code: string, line: number, filename: string, preCode = ''): Token[] {
316
+ if (!code.startsWith(preCode)) {
317
+ throw new Error('codeの先頭にはpreCodeを含める必要があります。')
318
+ }
319
+ // インデント構文 (#596)
320
+ const { code: code2, insertedLines, deletedLines } = NakoIndent.convert(code, filename)
321
+ // DNCL構文 (#1140)
322
+ const code3 = convertDNCL(code2, filename)
323
+ // 全角半角の統一処理
324
+ const preprocessed = this.prepare.convert(code3)
325
+
326
+ const tokenizationSourceMapping = new SourceMappingOfTokenization(code2.length, preprocessed)
327
+ const indentationSyntaxSourceMapping = new SourceMappingOfIndentSyntax(code2, insertedLines, deletedLines)
328
+ const offsetToLineColumn = new OffsetToLineColumn(code)
329
+
330
+ // トークン分割
331
+ /** @type {import('./nako_lexer.mjs').Token[]} */
332
+ let tokens
333
+ try {
334
+ tokens = this.lexer.tokenize(preprocessed.map((v) => v.text).join(''), line, filename)
335
+ } catch (err) {
336
+ if (!(err instanceof InternalLexerError)) {
337
+ throw err
338
+ }
339
+ // エラー位置をソースコード上の位置に変換して返す
340
+ const dest = indentationSyntaxSourceMapping.map(tokenizationSourceMapping.map(err.preprocessedCodeStartOffset), tokenizationSourceMapping.map(err.preprocessedCodeEndOffset))
341
+ /** @type {number | undefined} */
342
+ const line = dest.startOffset === null ? err.line : offsetToLineColumn.map(dest.startOffset, false).line
343
+ const map = subtractSourceMapByPreCodeLength({ ...dest, line }, preCode)
344
+ throw new NakoLexerError(err.msg, map.startOffset, map.endOffset, map.line, filename)
345
+ }
346
+
347
+ // ソースコード上の位置に変換
348
+ return tokens.map((token) => {
349
+ const dest = indentationSyntaxSourceMapping.map(
350
+ tokenizationSourceMapping.map(token.preprocessedCodeOffset || 0),
351
+ tokenizationSourceMapping.map((token.preprocessedCodeOffset || 0) + (token.preprocessedCodeLength || 0))
352
+ )
353
+ let line = token.line
354
+ let column = 0
355
+ if (token.type === 'eol' && dest.endOffset !== null) {
356
+ // eolはparserで `line = ${eolToken.line};` に変換されるため、
357
+ // 行末のeolのlineは次の行の行数を表す必要がある。
358
+ const out = offsetToLineColumn.map(dest.endOffset, false)
359
+ line = out.line
360
+ column = out.column
361
+ } else if (dest.startOffset !== null) {
362
+ const out = offsetToLineColumn.map(dest.startOffset, false)
363
+ line = out.line
364
+ column = out.column
365
+ }
366
+ return {
367
+ ...token,
368
+ ...subtractSourceMapByPreCodeLength({ line, column, startOffset: dest.startOffset, endOffset: dest.endOffset }, preCode),
369
+ rawJosi: token.josi
370
+ }
371
+ })
372
+ }
373
+
374
+ /**
375
+ * 単語の属性を構文解析に先立ち補正する
376
+ * @param {Token[]} tokens トークンのリスト
377
+ * @param {boolean} isFirst 最初の呼び出しかどうか
378
+ * @param {string} filename
379
+ * @returns コード (なでしこ)
380
+ */
381
+ converttoken (tokens: Token[], isFirst: boolean, filename: string): Token[] {
382
+ const tok = this.lexer.replaceTokens(tokens, isFirst, filename)
383
+ return tok
384
+ }
385
+
386
+ /**
387
+ * 環境のリセット
388
+ */
389
+ reset () {
390
+ /**
391
+ * なでしこのローカル変数をスタックで管理
392
+ * __varslist[0] プラグイン領域
393
+ * __varslist[1] なでしこグローバル領域
394
+ * __varslist[2] 最初のローカル変数 ( == __vars }
395
+ * @type {Record<string, any>[]}
396
+ */
397
+ this.__varslist = [this.__varslist[0], {}, {}]
398
+ this.__v0 = this.__varslist[0]
399
+ this.__v1 = this.__varslist[1]
400
+ this.__vars = this.__varslist[2]
401
+ this.__locals = {}
402
+
403
+ // プラグイン命令以外を削除する。
404
+ this.funclist = {}
405
+ for (const name of Object.keys(this.__v0)) {
406
+ const original = this.pluginFunclist[name]
407
+ if (!original) {
408
+ continue
409
+ }
410
+ this.funclist[name] = JSON.parse(JSON.stringify(original))
411
+ }
412
+
413
+ this.lexer.setFuncList(this.funclist)
414
+ this.clearPlugins()
415
+ this.logger.clear()
416
+ }
417
+
418
+ /**
419
+ * typeがcodeのトークンを単語に分割するための処理
420
+ * @param {string} code
421
+ * @param {number} line
422
+ * @param {string} filename
423
+ * @param {number | null} startOffset
424
+ * @returns
425
+ * @private
426
+ */
427
+ lexCodeToken (code: string, line: number, filename: string, startOffset: number|null): {commentTokens: Token[], tokens: Token[]} {
428
+ // 単語に分割
429
+ let tokens = this.rawtokenize(code, line, filename, '')
430
+
431
+ // 文字列内位置からファイル内位置へ変換
432
+ if (startOffset === null) {
433
+ for (const token of tokens) {
434
+ token.startOffset = undefined
435
+ token.endOffset = undefined
436
+ }
437
+ } else {
438
+ for (const token of tokens) {
439
+ if (token.startOffset !== undefined) {
440
+ token.startOffset += startOffset
441
+ }
442
+ if (token.endOffset !== undefined) {
443
+ token.endOffset += startOffset
444
+ }
445
+ }
446
+ }
447
+
448
+ // convertTokenで消されるコメントのトークンを残す
449
+ const commentTokens = tokens.filter((t) => t.type === 'line_comment' || t.type === 'range_comment')
450
+ .map((v) => ({ ...v })) // clone
451
+
452
+ tokens = this.converttoken(tokens, false, filename)
453
+
454
+ return { tokens, commentTokens }
455
+ }
456
+
457
+ /**
458
+ * 再帰的にrequire文を置換する。
459
+ * .jsであれば削除し、.nako3であればそのファイルのトークン列で置換する。
460
+ * @param {TokenWithSourceMap[]} tokens
461
+ * @param {Set<string>} [includeGuard]
462
+ * @returns {Token[]} 削除された取り込み文のトークン
463
+ */
464
+ replaceRequireStatements (tokens: Token[], includeGuard:Set<string> = new Set()): Token[] {
465
+ /** @type {TokenWithSourceMap[]} */
466
+ const deletedTokens = []
467
+ for (const r of NakoCompiler.listRequireStatements(tokens).reverse()) {
468
+ // C言語のinclude guardと同じ仕組みで無限ループを防ぐ。
469
+ if (includeGuard.has(r.value)) {
470
+ deletedTokens.push(...tokens.splice((r.start || 0), (r.end || 0) - (r.start || 0)))
471
+ continue
472
+ }
473
+ const filePath = Object.keys(this.dependencies).find((key) => this.dependencies[key].alias.has(r.value))
474
+ if (filePath === undefined) {
475
+ if (!r.firstToken) { throw new Error(`ファイル『${r.value}』が読み込まれていません。`) }
476
+ throw new NakoLexerError(`ファイル『${r.value}』が読み込まれていません。`,
477
+ (r.firstToken as Token).startOffset || 0,
478
+ (r.firstToken as Token).endOffset || 0,
479
+ (r.firstToken as Token).line, (r.firstToken as Token).file)
480
+ }
481
+ this.dependencies[filePath].addPluginFile()
482
+ const children = cloneAsJSON(this.dependencies[filePath].tokens)
483
+ includeGuard.add(r.value)
484
+ deletedTokens.push(...this.replaceRequireStatements(children, includeGuard))
485
+ deletedTokens.push(...tokens.splice(r.start || 0, (r.end || 0) - (r.start || 0), ...children))
486
+ }
487
+ return deletedTokens
488
+ }
489
+
490
+ /**
491
+ * replaceRequireStatementsのシンタックスハイライト用の実装。
492
+ * @param {TokenWithSourceMap[]} tokens
493
+ * @returns {TokenWithSourceMap[]} 削除された取り込み文のトークン
494
+ */
495
+ removeRequireStatements (tokens: Token[]): Token[] {
496
+ /** @type {TokenWithSourceMap[]} */
497
+ const deletedTokens = []
498
+ for (const r of NakoCompiler.listRequireStatements(tokens).reverse()) {
499
+ // プラグイン命令のシンタックスハイライトのために、addPluginFileを呼んで関数のリストをthis.dependencies[filePath].funclistに保存させる。
500
+ const filePath = Object.keys(this.dependencies).find((key) => this.dependencies[key].alias.has(r.value))
501
+ if (filePath !== undefined) {
502
+ this.dependencies[filePath].addPluginFile()
503
+ }
504
+
505
+ // 全ての取り込み文を削除する。そうしないとトークン化に時間がかかりすぎる。
506
+ deletedTokens.push(...tokens.splice(r.start || 0, (r.end || 0) - (r.start || 0)))
507
+ }
508
+ return deletedTokens
509
+ }
510
+
511
+ /**
512
+ * @param {string} code
513
+ * @param {string} filename
514
+ * @param {string} [preCode]
515
+ */
516
+ lex (code: string, filename = 'main.nako3', preCode = '', syntaxHighlighting = false): LexResult {
517
+ // 単語に分割
518
+ let tokens = this.rawtokenize(code, 0, filename, preCode)
519
+
520
+ // require文を再帰的に置換する
521
+ const requireStatementTokens = syntaxHighlighting ? this.removeRequireStatements(tokens) : this.replaceRequireStatements(tokens, undefined)
522
+ for (const t of requireStatementTokens) {
523
+ if (t.type === 'word' || t.type === 'not') {
524
+ t.type = 'require'
525
+ }
526
+ }
527
+ if (requireStatementTokens.length >= 3) {
528
+ // modList を更新
529
+ for (let i = 0; i < requireStatementTokens.length; i += 3) {
530
+ let modName = requireStatementTokens[i + 1].value
531
+ modName = NakoLexer.filenameToModName(modName)
532
+ if (this.lexer.modList.indexOf(modName) < 0) {
533
+ this.lexer.modList.push(modName)
534
+ }
535
+ }
536
+ }
537
+
538
+ // convertTokenで消されるコメントのトークンを残す
539
+ /** @type {TokenWithSourceMap[]} */
540
+ const commentTokens = tokens.filter((t) => t.type === 'line_comment' || t.type === 'range_comment')
541
+ .map((v) => ({ ...v })) // clone
542
+
543
+ tokens = this.converttoken(tokens, true, filename)
544
+
545
+ for (let i = 0; i < tokens.length; i++) {
546
+ if (tokens[i] && tokens[i].type === 'code') {
547
+ const children = this.lexCodeToken(tokens[i].value, tokens[i].line, filename, tokens[i].startOffset || 0)
548
+ commentTokens.push(...children.commentTokens)
549
+ tokens.splice(i, 1, ...children.tokens)
550
+ i--
551
+ }
552
+ }
553
+
554
+ this.logger.trace('--- lex ---\n' + JSON.stringify(tokens, null, 2))
555
+
556
+ return { commentTokens, tokens, requireTokens: requireStatementTokens }
557
+ }
558
+
559
+ /**
560
+ * コードをパースしてASTにする
561
+ * @param code なでしこのプログラム
562
+ * @param filename
563
+ * @param [preCode]
564
+ * @return Ast
565
+ */
566
+ parse (code: string, filename: string, preCode = '') {
567
+ // 関数を字句解析と構文解析に登録
568
+ this.lexer.setFuncList(this.funclist)
569
+ this.parser.setFuncList(this.funclist)
570
+
571
+ const lexerOutput = this.lex(code, filename, preCode)
572
+
573
+ // 構文木を作成
574
+ /** @type {Ast} */
575
+ let ast
576
+ try {
577
+ this.parser.genMode = 'sync' // set default
578
+ ast = this.parser.parse(lexerOutput.tokens, filename)
579
+ } catch (err: any) {
580
+ if (typeof err.startOffset !== 'number') {
581
+ throw NakoSyntaxError.fromNode(err.message, lexerOutput.tokens[this.parser.getIndex()])
582
+ }
583
+ throw err
584
+ }
585
+ this.usedFuncs = this.getUsedFuncs(ast)
586
+ this.logger.trace('--- ast ---\n' + JSON.stringify(ast, null, 2))
587
+ return ast
588
+ }
589
+
590
+ /**
591
+ * @param {Ast} ast
592
+ */
593
+ getUsedFuncs (ast: Ast): Set<string> {
594
+ const queue = [ast]
595
+ this.usedFuncs = new Set()
596
+
597
+ while (queue.length > 0) {
598
+ const ast_ = queue.pop()
599
+
600
+ if (ast_ !== null && ast_ !== undefined && ast_.block !== null && ast_.block !== undefined) {
601
+ this.getUsedAndDefFuncs(queue, JSON.parse(JSON.stringify(ast_.block)))
602
+ }
603
+ }
604
+
605
+ return this.deleteUnNakoFuncs()
606
+ }
607
+
608
+ getUsedAndDefFuncs (astQueue: Ast[], blockQueue: Ast[]): void {
609
+ while (blockQueue.length > 0) {
610
+ const block = blockQueue.pop()
611
+
612
+ if (block !== null && block !== undefined) {
613
+ this.getUsedAndDefFunc(block, astQueue, blockQueue)
614
+ }
615
+ }
616
+ }
617
+
618
+ getUsedAndDefFunc (block: Ast, astQueue: any[], blockQueue: Ast[]): void {
619
+ if (['func', 'func_pointer'].includes(block.type) && block.name !== null && block.name !== undefined) {
620
+ this.usedFuncs.add(block.name as string)
621
+ }
622
+ astQueue.push([block, block.block as Ast])
623
+ blockQueue.push.apply(blockQueue, [block.value].concat(block.args))
624
+ }
625
+
626
+ deleteUnNakoFuncs (): Set<string> {
627
+ for (const func of this.usedFuncs) {
628
+ if (!this.commandlist.has(func)) {
629
+ this.usedFuncs.delete(func)
630
+ }
631
+ }
632
+ return this.usedFuncs
633
+ }
634
+
635
+ /**
636
+ * プログラムをコンパイルしてランタイム用のJavaScriptのコードを返す
637
+ * @param code コード (なでしこ)
638
+ * @param filename
639
+ * @param isTest テストかどうか
640
+ * @param preCode
641
+ */
642
+ compile (code: string, filename: string, isTest = false, preCode = ''): string {
643
+ const opt = new CompilerOptions()
644
+ opt.testOnly = isTest
645
+ opt.preCode = preCode
646
+ const res = this.compileFromCode(code, filename, opt)
647
+ return res.runtimeEnv
648
+ }
649
+
650
+ /** parse & generate */
651
+ compileFromCode (code: string, filename: string, options: CompilerOptions): NakoGenResult {
652
+ if (filename === '') { filename = 'main.nako3' }
653
+ try {
654
+ if (options.resetEnv) { this.reset() }
655
+ if (options.resetAll) { this.clearPlugins() }
656
+ // onBeforeParse
657
+ this.eventList.filter(o => o.eventName === 'beforeParse').map(e => e.callback(code))
658
+ const ast = this.parse(code, filename, options.preCode)
659
+ // onBeforeGenerate
660
+ this.eventList.filter(o => o.eventName === 'beforeGenerate').map(e => e.callback(ast))
661
+ // generate
662
+ const outCode = this.generateCode(ast, new NakoGenOptions(options.testOnly))
663
+ // onAfterGenerate
664
+ this.eventList.filter(o => o.eventName === 'afterGenerate').map(e => e.callback(outCode))
665
+ return outCode
666
+ } catch (e: any) {
667
+ this.logger.error(e)
668
+ throw e
669
+ }
670
+ }
671
+
672
+ /**
673
+ * プログラムをコンパイルしてJavaScriptのコードオブジェクトを返す
674
+ * @param {AST} ast
675
+ * @param opt テストかどうか
676
+ * @return {Object}
677
+ */
678
+ generateCode (ast: Ast, opt: NakoGenOptions): NakoGenResult {
679
+ // Select Code Generator #637
680
+ switch (ast.genMode) {
681
+ // ノーマルモード
682
+ case 'sync':
683
+ return generateJS(this, ast, opt)
684
+ // 『!非同期モード』は非推奨
685
+ case '非同期モード':
686
+ this.logger.warn('『!非同期モード』の利用は非推奨です。[詳細](https://github.com/kujirahand/nadesiko3/issues/1164)')
687
+ return NakoGenASync.generate(this, ast, opt.isTest)
688
+ default:
689
+ throw new Error(`コードジェネレータの「${ast.genMode}」はサポートされていません。`)
690
+ }
691
+ }
692
+
693
+ /** (非推奨)
694
+ * @param code
695
+ * @param fname
696
+ * @param isReset
697
+ * @param isTest テストかどうか。stringの場合は1つのテストのみ。
698
+ * @param [preCode]
699
+ */
700
+ async _run (code: string, fname: string, isReset: boolean, isTest: boolean, preCode = ''): Promise<NakoGlobal> {
701
+ const opts: CompilerOptions = new CompilerOptions({
702
+ resetEnv: isReset,
703
+ resetAll: isReset,
704
+ testOnly: isTest,
705
+ preCode
706
+ })
707
+ return this._runEx(code, fname, opts)
708
+ }
709
+
710
+ /** 各プラグインをリセットする */
711
+ clearPlugins () {
712
+ // 他に実行している「なでしこ」があればクリアする
713
+ this.__globals.forEach((sys: NakoGlobal) => {
714
+ sys.reset()
715
+ })
716
+ this.__globals = [] // clear
717
+ }
718
+
719
+ /**
720
+ * 環境を指定してJavaScriptのコードを実行する
721
+ * @param code JavaScriptのコード
722
+ * @param nakoGlobal 実行環境
723
+ */
724
+ private evalJS (code: string, nakoGlobal: NakoGlobal): void {
725
+ // 実行前に環境を初期化するイベントを実行(beforeRun)
726
+ this.eventList.filter(o => o.eventName === 'beforeRun').map(e => e.callback(nakoGlobal))
727
+ // eslint-disable-next-line no-new-func
728
+ const f = new Function(code)
729
+ f.apply(nakoGlobal)
730
+ // 実行後に終了イベントを実行(finish)
731
+ this.eventList.filter(o => o.eventName === 'finish').map(e => e.callback(nakoGlobal))
732
+ }
733
+
734
+ /**
735
+ * 同期的になでしこのプログラムcodeを実行する
736
+ * @param code なでしこのプログラム
737
+ * @param filename ファイル名
738
+ * @param options オプション
739
+ * @returns 実行に利用したグローバルオブジェクト
740
+ */
741
+ public runSync (code: string, filename: string, options: CompilerOptions = new CompilerOptions()): NakoGlobal {
742
+ // コンパイル
743
+ const out = this.compileFromCode(code, filename, options)
744
+ // 実行前に環境を生成
745
+ const nakoGlobal = this.getNakoGlobal(options, out.gen)
746
+ // 実行
747
+ this.evalJS(out.runtimeEnv, nakoGlobal)
748
+ return nakoGlobal
749
+ }
750
+
751
+ /**
752
+ * 非同期になでしこのプログラムcodeを実行する
753
+ * @param code なでしこのプログラム
754
+ * @param filename ファイル名
755
+ * @param options オプション
756
+ * @returns 実行に利用したグローバルオブジェクト
757
+ */
758
+ public async runAsync (code: string, filename: string, options: CompilerOptions = new CompilerOptions()): Promise<NakoGlobal> {
759
+ // コンパイル
760
+ const out = this.compileFromCode(code, filename, options)
761
+ // 実行前に環境を生成
762
+ const nakoGlobal = this.getNakoGlobal(options, out.gen)
763
+ // 実行
764
+ this.evalJS(out.runtimeEnv, nakoGlobal)
765
+ return nakoGlobal
766
+ }
767
+
768
+ private getNakoGlobal (options: CompilerOptions, gen: NakoGen): NakoGlobal {
769
+ // オプションを参照
770
+ let g: NakoGlobal|null = options.nakoGlobal
771
+ if (!g) {
772
+ // 空ならば前回の値を参照(リセットするなら新規で作成する)
773
+ if (this.__globals.length > 0 && options.resetAll === false && options.resetEnv === false) {
774
+ g = this.__globals[this.__globals.length - 1]
775
+ } else {
776
+ g = new NakoGlobal(this, gen)
777
+ }
778
+ }
779
+ if (this.__globals.indexOf(g) < 0) { this.__globals.push(g) }
780
+ return g
781
+ }
782
+
783
+ /**
784
+ * イベントを登録する
785
+ * @param eventName イベント名
786
+ * @param callback コールバック関数
787
+ */
788
+ addListener (eventName: NakoComEventName, callback: (event:any) => void) {
789
+ this.eventList.push({ eventName, callback })
790
+ }
791
+
792
+ /**
793
+ * テストを実行する
794
+ * @param code
795
+ * @param fname
796
+ * @param preCode
797
+ * @param testOnly
798
+ */
799
+ test (code: string, fname: string, preCode = '', testOnly = false) {
800
+ const options = new CompilerOptions()
801
+ options.preCode = preCode
802
+ options.testOnly = testOnly
803
+ return this.runSync(code, fname, options)
804
+ }
805
+
806
+ /**
807
+ * なでしこのプログラムを実行(他に実行しているインスタンスはそのまま)
808
+ * @param code
809
+ * @param fname
810
+ * @param [preCode]
811
+ */
812
+ run (code: string, fname = 'main.nako3', preCode = ''): NakoGlobal {
813
+ const options = new CompilerOptions()
814
+ options.preCode = preCode
815
+ return this.runSync(code, fname, options)
816
+ }
817
+
818
+ /**
819
+ * JavaScriptのみで動くコードを取得する場合
820
+ * @param code
821
+ * @param filename
822
+ * @param opt
823
+ */
824
+ compileStandalone (code: string, filename: string, opt: NakoGenOptions|null = null): string {
825
+ if (opt === null) { opt = new NakoGenOptions() }
826
+ const ast = this.parse(code, filename)
827
+ return this.generateCode(ast, opt).standalone
828
+ }
829
+
830
+ /**
831
+ * プラグイン・オブジェクトを追加
832
+ * @param po プラグイン・オブジェクト
833
+ * @param persistent falseのとき、次以降の実行では使えない
834
+ */
835
+ addPlugin (po: {[key: string]: any}, persistent = true): void {
836
+ // 変数のメタ情報を確認
837
+ const __v0 = this.__varslist[0]
838
+ if (__v0.meta === undefined) { __v0.meta = {} }
839
+
840
+ // プラグインの値をオブジェクトにコピー
841
+ for (const key in po) {
842
+ const v = po[key]
843
+ this.funclist[key] = v
844
+ if (persistent) {
845
+ this.pluginFunclist[key] = JSON.parse(JSON.stringify(v))
846
+ }
847
+ if (v.type === 'func') {
848
+ __v0[key] = v.fn
849
+ } else if (v.type === 'const' || v.type === 'var') {
850
+ __v0[key] = v.value
851
+ __v0.meta[key] = {
852
+ readonly: (v.type === 'const')
853
+ }
854
+ } else {
855
+ console.error('[プラグイン追加エラー]', v)
856
+ throw new Error('プラグインの追加でエラー。')
857
+ }
858
+ // コマンドを登録するか?
859
+ if (key === '初期化' || key.substring(0, 1) === '!') { // 登録しない関数名
860
+ continue
861
+ }
862
+ this.commandlist.add(key)
863
+ }
864
+ }
865
+
866
+ /**
867
+ * プラグイン・オブジェクトを追加(ブラウザ向け)
868
+ * @param objName オブジェクト名
869
+ * @param po 関数リスト
870
+ * @param persistent falseのとき、次以降の実行では使えない
871
+ */
872
+ addPluginObject (objName: string, po: {[key: string]: any}, persistent = true): void {
873
+ this.__module[objName] = po
874
+ this.pluginfiles[objName] = '*'
875
+ // 初期化をチェック
876
+ if (typeof (po['初期化']) === 'object') {
877
+ const def = po['初期化']
878
+ delete po['初期化']
879
+ const initKey = `!${objName}:初期化`
880
+ po[initKey] = def
881
+ }
882
+ // メタ情報をチェック (#1034)
883
+ if (po.meta && po.meta.value && typeof (po.meta) === 'object') {
884
+ const meta = po.meta
885
+ delete po.meta
886
+ const pluginName = meta.value.pluginName || objName
887
+ const metaKey = `__${pluginName}`.replace('-', '__')
888
+ po[metaKey] = meta
889
+ }
890
+ this.addPlugin(po, persistent)
891
+ }
892
+
893
+ /**
894
+ * プラグイン・ファイルを追加(Node.js向け)
895
+ * @param objName オブジェクト名
896
+ * @param fpath ファイルパス
897
+ * @param po 登録するオブジェクト
898
+ * @param persistent falseのとき、次以降の実行では使えない
899
+ */
900
+ addPluginFile (objName: string, fpath: string, po: {[key: string]: any}, persistent = true): void {
901
+ // Windowsのパスがあると、JSファイル書き出しでエラーになるので、置換する
902
+ if (objName.indexOf('\\') >= 0) {
903
+ objName = objName.replace(/\\/g, '/')
904
+ }
905
+ this.addPluginObject(objName, po, persistent)
906
+ if (this.pluginfiles[objName] === undefined) {
907
+ this.pluginfiles[objName] = fpath
908
+ }
909
+ }
910
+
911
+ /**
912
+ * 関数を追加する
913
+ * @param {string} key 関数名
914
+ * @param {string[][]} josi 助詞
915
+ * @param {Function} fn 関数
916
+ * @param {boolean} returnNone 値を返す関数の場合はfalseを指定
917
+ * @param {boolean} asyncFn Promiseを返す関数かを指定
918
+ */
919
+ addFunc (key: string, josi: FuncArgs, fn: any, returnNone = true, asyncFn = false): void {
920
+ this.funclist[key] = { josi, fn, type: 'func', return_none: returnNone, asyncFn }
921
+ this.pluginFunclist[key] = cloneAsJSON(this.funclist[key])
922
+ this.__varslist[0][key] = fn
923
+ }
924
+
925
+ // (非推奨) 互換性のため ... 関数を追加する
926
+ public setFunc (key: string, josi: FuncArgs, fn: any, returnNone = true, asyncFn = false): void {
927
+ this.addFunc(key, josi, fn, returnNone, asyncFn)
928
+ }
929
+
930
+ /**
931
+ * プラグイン関数を参照する
932
+ * @param key プラグイン関数の関数名
933
+ * @returns プラグイン・オブジェクト
934
+ */
935
+ getFunc (key: string): FuncListItem {
936
+ return this.funclist[key]
937
+ }
938
+
939
+ /** (非推奨) 同期的になでしこのプログラムcodeを実行する */
940
+ private _runEx (code: string, filename: string, opts: Partial<CompilerOptions>, preCode = '', nakoGlobal: NakoGlobal|undefined = undefined): NakoGlobal {
941
+ // コンパイル
942
+ const options: CompilerOptions = new CompilerOptions(opts)
943
+ options.preCode = preCode
944
+ if (nakoGlobal) { options.nakoGlobal = nakoGlobal }
945
+ return this.runSync(code, filename, options)
946
+ }
947
+
948
+ /** (非推奨) 同期的に実行
949
+ * @param code
950
+ * @param fname
951
+ * @param opts
952
+ * @param [preCode]
953
+ */
954
+ public runEx (code: string, fname: string, opts: Partial<CompilerOptions>, preCode = '') {
955
+ return this._runEx(code, fname, opts, preCode)
956
+ }
957
+
958
+ /**
959
+ * (非推奨) なでしこのプログラムを実行(他に実行しているインスタンスもリセットする)
960
+ * @param code
961
+ * @param fname
962
+ * @param [preCode]
963
+ */
964
+ async runReset (code: string, fname = 'main.nako3', preCode = ''): Promise<NakoGlobal> {
965
+ return this._runEx(code, fname, { resetAll: true, resetEnv: true }, preCode)
966
+ }
967
+ }