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.
- package/core/.editorconfig +6 -0
- package/core/.eslintrc.cjs +33 -0
- package/core/.github/dependabot.yml +7 -0
- package/core/.github/workflows/nodejs.yml +37 -0
- package/core/.github/workflows/super-linter.yml +61 -0
- package/core/.github/workflows/textlint.yml +199 -0
- package/core/LICENSE +21 -0
- package/core/README.md +66 -0
- package/core/batch/build_nako_version.nako3 +42 -0
- package/core/command/snako.mjs +105 -0
- package/core/command/snako.mts +116 -0
- package/core/index.mjs +21 -0
- package/core/index.mts +21 -0
- package/core/package.json +47 -0
- package/core/sample/hello.nako3 +7 -0
- package/core/sample/hoge.mjs +4 -0
- package/core/sample/hoge.mts +6 -0
- package/core/src/nako3.mjs +858 -0
- package/core/src/nako3.mts +967 -0
- package/core/src/nako_colors.mjs +78 -0
- package/core/src/nako_colors.mts +86 -0
- package/core/src/nako_core_version.mjs +8 -0
- package/core/src/nako_core_version.mts +19 -0
- package/core/src/nako_csv.mjs +185 -0
- package/core/src/nako_csv.mts +188 -0
- package/core/src/nako_errors.mjs +173 -0
- package/core/src/nako_errors.mts +197 -0
- package/core/src/nako_from_dncl.mjs +255 -0
- package/core/src/nako_from_dncl.mts +250 -0
- package/core/src/nako_gen.mjs +1648 -0
- package/core/src/nako_gen.mts +1719 -0
- package/core/src/nako_gen_async.mjs +1659 -0
- package/core/src/nako_gen_async.mts +1732 -0
- package/core/src/nako_global.mjs +107 -0
- package/core/src/nako_global.mts +138 -0
- package/core/src/nako_indent.mjs +445 -0
- package/core/src/nako_indent.mts +492 -0
- package/core/src/nako_josi_list.mjs +38 -0
- package/core/src/nako_josi_list.mts +45 -0
- package/core/src/nako_lex_rules.mjs +253 -0
- package/core/src/nako_lex_rules.mts +260 -0
- package/core/src/nako_lexer.mjs +609 -0
- package/core/src/nako_lexer.mts +612 -0
- package/core/src/nako_logger.mjs +199 -0
- package/core/src/nako_logger.mts +232 -0
- package/core/src/nako_parser3.mjs +2439 -0
- package/core/src/nako_parser3.mts +2195 -0
- package/core/src/nako_parser_base.mjs +370 -0
- package/core/src/nako_parser_base.mts +370 -0
- package/core/src/nako_parser_const.mjs +37 -0
- package/core/src/nako_parser_const.mts +37 -0
- package/core/src/nako_prepare.mjs +304 -0
- package/core/src/nako_prepare.mts +315 -0
- package/core/src/nako_reserved_words.mjs +38 -0
- package/core/src/nako_reserved_words.mts +38 -0
- package/core/src/nako_source_mapping.mjs +207 -0
- package/core/src/nako_source_mapping.mts +262 -0
- package/core/src/nako_test.mjs +37 -0
- package/core/src/nako_types.mjs +25 -0
- package/core/src/nako_types.mts +151 -0
- package/core/src/plugin_csv.mjs +49 -0
- package/core/src/plugin_csv.mts +50 -0
- package/core/src/plugin_math.mjs +328 -0
- package/core/src/plugin_math.mts +326 -0
- package/core/src/plugin_promise.mjs +91 -0
- package/core/src/plugin_promise.mts +91 -0
- package/core/src/plugin_system.mjs +2832 -0
- package/core/src/plugin_system.mts +2690 -0
- package/core/src/plugin_test.mjs +34 -0
- package/core/src/plugin_test.mts +34 -0
- package/core/test/array_test.mjs +34 -0
- package/core/test/basic_test.mjs +344 -0
- package/core/test/calc_test.mjs +140 -0
- package/core/test/core_module_test.mjs +23 -0
- package/core/test/debug_test.mjs +16 -0
- package/core/test/dncl_test.mjs +94 -0
- package/core/test/error_message_test.mjs +210 -0
- package/core/test/error_test.mjs +16 -0
- package/core/test/flow_test.mjs +373 -0
- package/core/test/func_call.mjs +160 -0
- package/core/test/func_test.mjs +149 -0
- package/core/test/indent_test.mjs +364 -0
- package/core/test/lex_test.mjs +168 -0
- package/core/test/literal_test.mjs +73 -0
- package/core/test/nako_lexer_test.mjs +35 -0
- package/core/test/nako_logger_test.mjs +76 -0
- package/core/test/nako_logger_test.mts +78 -0
- package/core/test/plugin_csv_test.mjs +38 -0
- package/core/test/plugin_promise_test.mjs +18 -0
- package/core/test/plugin_system_test.mjs +630 -0
- package/core/test/prepare_test.mjs +96 -0
- package/core/test/re_test.mjs +22 -0
- package/core/test/side_effects_test.mjs +92 -0
- package/core/test/variable_scope_test.mjs +149 -0
- package/core/tsconfig.json +101 -0
- package/package.json +4 -2
- package/release/_hash.txt +12 -12
- package/release/_script-tags.txt +14 -14
- package/release/editor.js +1 -1
- package/release/stats.json +1 -1
- package/release/version.js +1 -1
- package/release/wnako3.js +1 -1
- package/src/nako_version.mjs +2 -2
- package/src/nako_version.mts +2 -2
- package/test/async/async_basic_test.mjs +3 -3
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import nakoVersion from './nako_core_version.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* なでしこ言語が投げる全てのエラーが継承するクラス
|
|
4
|
+
*/
|
|
5
|
+
export class NakoError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} tag
|
|
8
|
+
* @param {string} msg
|
|
9
|
+
* @param {string | undefined} file
|
|
10
|
+
* @param {number | undefined} line
|
|
11
|
+
*/
|
|
12
|
+
constructor(tag, msg, file = undefined, line = undefined) {
|
|
13
|
+
// エラー位置を分かりやすく日本語に変換
|
|
14
|
+
const positionJa = `${file || ''}${line === undefined ? '' : `(${line + 1}行目): `}`;
|
|
15
|
+
// #1223 エラーメッセージに「main__関数名」と表示されるので、main__は省略して表示
|
|
16
|
+
msg = msg.replace(/『main__(.+?)』/g, '『$1』');
|
|
17
|
+
// 親のErrorを呼ぶ
|
|
18
|
+
super(`[${tag}]${positionJa}${msg}\n[バージョン] ${nakoVersion.version}`);
|
|
19
|
+
// エラーの種類を設定
|
|
20
|
+
this.name = 'NakoError';
|
|
21
|
+
this.type = 'NakoError';
|
|
22
|
+
this.tag = '[' + tag + ']';
|
|
23
|
+
this.positionJa = positionJa;
|
|
24
|
+
this.msg = msg;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class NakoIndentError extends NakoError {
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} msg
|
|
30
|
+
* @param {number} line
|
|
31
|
+
* @param {string} file
|
|
32
|
+
*/
|
|
33
|
+
constructor(msg, line, file) {
|
|
34
|
+
super('インデントエラー', msg, file, line);
|
|
35
|
+
this.type = 'NakoIndentError';
|
|
36
|
+
this.line = line;
|
|
37
|
+
this.file = file;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// コンパイラの内部でのみ使うエラー。投げられたらtryでキャッチしてLexerErrorへ変更する。
|
|
41
|
+
export class InternalLexerError extends NakoError {
|
|
42
|
+
/**
|
|
43
|
+
* @param {string} msg
|
|
44
|
+
* @param {number} preprocessedCodeStartOffset
|
|
45
|
+
* @param {number} preprocessedCodeEndOffset
|
|
46
|
+
* @param {number | undefined} [line]
|
|
47
|
+
* @param {string | undefined} [file]
|
|
48
|
+
*/
|
|
49
|
+
constructor(msg, preprocessedCodeStartOffset, preprocessedCodeEndOffset, line, file) {
|
|
50
|
+
super('字句解析エラー(内部エラー)', msg, file, line);
|
|
51
|
+
this.type = 'InternalLexerError';
|
|
52
|
+
this.preprocessedCodeStartOffset = preprocessedCodeStartOffset;
|
|
53
|
+
this.preprocessedCodeEndOffset = preprocessedCodeEndOffset;
|
|
54
|
+
this.line = line;
|
|
55
|
+
this.file = file;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export class NakoLexerError extends NakoError {
|
|
59
|
+
/**
|
|
60
|
+
* @param {string} msg
|
|
61
|
+
* @param {number | null} startOffset
|
|
62
|
+
* @param {number | null} endOffset,
|
|
63
|
+
* @param {number | undefined} line
|
|
64
|
+
* @param {string | undefined} file
|
|
65
|
+
*/
|
|
66
|
+
constructor(msg, startOffset, endOffset, line, file) {
|
|
67
|
+
super('字句解析エラー', msg, file, line);
|
|
68
|
+
this.type = 'NakoLexerError';
|
|
69
|
+
this.startOffset = startOffset;
|
|
70
|
+
this.endOffset = endOffset;
|
|
71
|
+
this.line = line;
|
|
72
|
+
this.file = file;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export class NakoSyntaxError extends NakoError {
|
|
76
|
+
/**
|
|
77
|
+
* @param {string} msg
|
|
78
|
+
* @param {number | undefined} line
|
|
79
|
+
* @param {number | undefined} startOffset
|
|
80
|
+
* @param {number | undefined} endOffset
|
|
81
|
+
* @param {string | undefined} file
|
|
82
|
+
*/
|
|
83
|
+
constructor(msg, line, startOffset, endOffset, file) {
|
|
84
|
+
super('文法エラー', msg, file, line);
|
|
85
|
+
this.type = 'NakoSyntaxError';
|
|
86
|
+
this.file = file;
|
|
87
|
+
this.line = line;
|
|
88
|
+
this.startOffset = startOffset;
|
|
89
|
+
this.endOffset = endOffset;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* @param {string} msg
|
|
93
|
+
* @param {Ast} first
|
|
94
|
+
* @param {Ast} [last]
|
|
95
|
+
*/
|
|
96
|
+
static fromNode(msg, first, last = undefined) {
|
|
97
|
+
if (!first) {
|
|
98
|
+
return new NakoSyntaxError(msg, undefined, undefined, undefined, undefined);
|
|
99
|
+
}
|
|
100
|
+
const startOffset = typeof first.startOffset === 'number' ? first.startOffset : undefined;
|
|
101
|
+
const endOffset = (last && typeof last.endOffset === 'number')
|
|
102
|
+
? last.endOffset
|
|
103
|
+
: (typeof first.endOffset === 'number' ? first.endOffset : undefined);
|
|
104
|
+
return new NakoSyntaxError(msg, first.line, startOffset, endOffset, first.file);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export class NakoRuntimeError extends NakoError {
|
|
108
|
+
/**
|
|
109
|
+
* @param error エラー
|
|
110
|
+
* @param lineNo 発生行
|
|
111
|
+
*/
|
|
112
|
+
constructor(error, lineNo) {
|
|
113
|
+
let msg = 'unknown';
|
|
114
|
+
if (typeof error === 'string') {
|
|
115
|
+
msg = error;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
if (error instanceof NakoRuntimeError) {
|
|
119
|
+
msg = error.msg;
|
|
120
|
+
}
|
|
121
|
+
else if (error instanceof NakoError) {
|
|
122
|
+
msg = error.msg;
|
|
123
|
+
}
|
|
124
|
+
else if (error instanceof Error) {
|
|
125
|
+
if (error.name === 'Error') {
|
|
126
|
+
msg = error.message;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
msg = `${error.name}: ${error.message}`;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// 行番号を表す文字列をパースする。
|
|
134
|
+
let line;
|
|
135
|
+
let file;
|
|
136
|
+
let matches;
|
|
137
|
+
if (lineNo === undefined) {
|
|
138
|
+
line = undefined;
|
|
139
|
+
file = undefined;
|
|
140
|
+
// eslint-disable-next-line no-cond-assign
|
|
141
|
+
}
|
|
142
|
+
else if (matches = /^l(-?\d+):(.*)$/.exec(lineNo)) {
|
|
143
|
+
line = parseInt(matches[1]);
|
|
144
|
+
file = matches[2];
|
|
145
|
+
// eslint-disable-next-line no-cond-assign
|
|
146
|
+
}
|
|
147
|
+
else if (matches = /^l(-?\d+)$/.exec(lineNo)) {
|
|
148
|
+
line = parseInt(matches[1]);
|
|
149
|
+
file = 'main.nako3';
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
line = 0;
|
|
153
|
+
file = lineNo;
|
|
154
|
+
}
|
|
155
|
+
super('実行時エラー', msg, file, line);
|
|
156
|
+
this.type = 'NakoRuntimeError';
|
|
157
|
+
this.lineNo = lineNo;
|
|
158
|
+
this.line = line;
|
|
159
|
+
this.file = file;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
export class NakoImportError extends NakoError {
|
|
163
|
+
/**
|
|
164
|
+
* @param {string} msg
|
|
165
|
+
* @param {string} file
|
|
166
|
+
* @param {number} line
|
|
167
|
+
*/
|
|
168
|
+
constructor(msg, file, line) {
|
|
169
|
+
super('取り込みエラー', msg, file, line);
|
|
170
|
+
this.file = file;
|
|
171
|
+
this.line = line;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import nakoVersion from './nako_core_version.mjs'
|
|
2
|
+
import { Ast, Token, SourceMap } from './nako_types.mjs'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* なでしこ言語が投げる全てのエラーが継承するクラス
|
|
6
|
+
*/
|
|
7
|
+
export class NakoError extends Error {
|
|
8
|
+
public type: string;
|
|
9
|
+
public tag: string;
|
|
10
|
+
public msg: string;
|
|
11
|
+
public file: string | undefined;
|
|
12
|
+
public line: number | undefined;
|
|
13
|
+
public positionJa: string;
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} tag
|
|
16
|
+
* @param {string} msg
|
|
17
|
+
* @param {string | undefined} file
|
|
18
|
+
* @param {number | undefined} line
|
|
19
|
+
*/
|
|
20
|
+
constructor (tag: string, msg: string, file: string | undefined = undefined, line: number | undefined = undefined) {
|
|
21
|
+
// エラー位置を分かりやすく日本語に変換
|
|
22
|
+
const positionJa = `${file || ''}${line === undefined ? '' : `(${line + 1}行目): `}`
|
|
23
|
+
// #1223 エラーメッセージに「main__関数名」と表示されるので、main__は省略して表示
|
|
24
|
+
msg = msg.replace(/『main__(.+?)』/g, '『$1』')
|
|
25
|
+
// 親のErrorを呼ぶ
|
|
26
|
+
super(`[${tag}]${positionJa}${msg}\n[バージョン] ${nakoVersion.version}`)
|
|
27
|
+
// エラーの種類を設定
|
|
28
|
+
this.name = 'NakoError'
|
|
29
|
+
this.type = 'NakoError'
|
|
30
|
+
this.tag = '[' + tag + ']'
|
|
31
|
+
this.positionJa = positionJa
|
|
32
|
+
this.msg = msg
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class NakoIndentError extends NakoError {
|
|
37
|
+
/**
|
|
38
|
+
* @param {string} msg
|
|
39
|
+
* @param {number} line
|
|
40
|
+
* @param {string} file
|
|
41
|
+
*/
|
|
42
|
+
constructor (msg: string, line: number, file: string) {
|
|
43
|
+
super('インデントエラー', msg, file, line)
|
|
44
|
+
this.type = 'NakoIndentError'
|
|
45
|
+
this.line = line
|
|
46
|
+
this.file = file
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// コンパイラの内部でのみ使うエラー。投げられたらtryでキャッチしてLexerErrorへ変更する。
|
|
51
|
+
export class InternalLexerError extends NakoError {
|
|
52
|
+
public preprocessedCodeStartOffset: number;
|
|
53
|
+
public preprocessedCodeEndOffset: number;
|
|
54
|
+
/**
|
|
55
|
+
* @param {string} msg
|
|
56
|
+
* @param {number} preprocessedCodeStartOffset
|
|
57
|
+
* @param {number} preprocessedCodeEndOffset
|
|
58
|
+
* @param {number | undefined} [line]
|
|
59
|
+
* @param {string | undefined} [file]
|
|
60
|
+
*/
|
|
61
|
+
constructor (msg: string, preprocessedCodeStartOffset: number, preprocessedCodeEndOffset: number, line: number | undefined, file: string | undefined) {
|
|
62
|
+
super('字句解析エラー(内部エラー)', msg, file, line)
|
|
63
|
+
this.type = 'InternalLexerError'
|
|
64
|
+
this.preprocessedCodeStartOffset = preprocessedCodeStartOffset
|
|
65
|
+
this.preprocessedCodeEndOffset = preprocessedCodeEndOffset
|
|
66
|
+
this.line = line
|
|
67
|
+
this.file = file
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class NakoLexerError extends NakoError {
|
|
72
|
+
public startOffset: number;
|
|
73
|
+
public endOffset: number;
|
|
74
|
+
/**
|
|
75
|
+
* @param {string} msg
|
|
76
|
+
* @param {number | null} startOffset
|
|
77
|
+
* @param {number | null} endOffset,
|
|
78
|
+
* @param {number | undefined} line
|
|
79
|
+
* @param {string | undefined} file
|
|
80
|
+
*/
|
|
81
|
+
constructor (
|
|
82
|
+
msg: string,
|
|
83
|
+
startOffset: number,
|
|
84
|
+
endOffset: number,
|
|
85
|
+
line: number | undefined,
|
|
86
|
+
file: string | undefined
|
|
87
|
+
) {
|
|
88
|
+
super('字句解析エラー', msg, file, line)
|
|
89
|
+
this.type = 'NakoLexerError'
|
|
90
|
+
this.startOffset = startOffset
|
|
91
|
+
this.endOffset = endOffset
|
|
92
|
+
this.line = line
|
|
93
|
+
this.file = file
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export class NakoSyntaxError extends NakoError {
|
|
98
|
+
public startOffset: number | undefined;
|
|
99
|
+
public endOffset: number | undefined;
|
|
100
|
+
/**
|
|
101
|
+
* @param {string} msg
|
|
102
|
+
* @param {Ast} first
|
|
103
|
+
* @param {Ast} [last]
|
|
104
|
+
*/
|
|
105
|
+
public static fromNode (msg: string, first: Ast | Token | SourceMap, last: Ast|undefined = undefined) {
|
|
106
|
+
if (!first) {
|
|
107
|
+
return new NakoSyntaxError(msg, undefined, undefined, undefined, undefined)
|
|
108
|
+
}
|
|
109
|
+
const startOffset = typeof first.startOffset === 'number' ? first.startOffset : undefined
|
|
110
|
+
const endOffset =
|
|
111
|
+
(last && typeof last.endOffset === 'number')
|
|
112
|
+
? last.endOffset
|
|
113
|
+
: (typeof first.endOffset === 'number' ? first.endOffset : undefined)
|
|
114
|
+
return new NakoSyntaxError(msg, first.line, startOffset, endOffset, first.file)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param {string} msg
|
|
119
|
+
* @param {number | undefined} line
|
|
120
|
+
* @param {number | undefined} startOffset
|
|
121
|
+
* @param {number | undefined} endOffset
|
|
122
|
+
* @param {string | undefined} file
|
|
123
|
+
*/
|
|
124
|
+
constructor (msg: string, line: number | undefined, startOffset: number | undefined, endOffset: number | undefined, file: string | undefined) {
|
|
125
|
+
super('文法エラー', msg, file, line)
|
|
126
|
+
this.type = 'NakoSyntaxError'
|
|
127
|
+
this.file = file
|
|
128
|
+
this.line = line
|
|
129
|
+
this.startOffset = startOffset
|
|
130
|
+
this.endOffset = endOffset
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export class NakoRuntimeError extends NakoError {
|
|
135
|
+
public lineNo: string | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* @param error エラー
|
|
138
|
+
* @param lineNo 発生行
|
|
139
|
+
*/
|
|
140
|
+
constructor (error: Error | string, lineNo: string | undefined) {
|
|
141
|
+
let msg = 'unknown'
|
|
142
|
+
if (typeof error === 'string') {
|
|
143
|
+
msg = error
|
|
144
|
+
} else {
|
|
145
|
+
if (error instanceof NakoRuntimeError) {
|
|
146
|
+
msg = error.msg
|
|
147
|
+
} else if (error instanceof NakoError) {
|
|
148
|
+
msg = error.msg
|
|
149
|
+
} else if (error instanceof Error) {
|
|
150
|
+
if (error.name === 'Error') {
|
|
151
|
+
msg = error.message
|
|
152
|
+
} else {
|
|
153
|
+
msg = `${error.name}: ${error.message}`
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 行番号を表す文字列をパースする。
|
|
159
|
+
let line: undefined | number
|
|
160
|
+
let file: undefined | string
|
|
161
|
+
let matches: any
|
|
162
|
+
if (lineNo === undefined) {
|
|
163
|
+
line = undefined
|
|
164
|
+
file = undefined
|
|
165
|
+
// eslint-disable-next-line no-cond-assign
|
|
166
|
+
} else if (matches = /^l(-?\d+):(.*)$/.exec(lineNo)) {
|
|
167
|
+
line = parseInt(matches[1])
|
|
168
|
+
file = matches[2]
|
|
169
|
+
// eslint-disable-next-line no-cond-assign
|
|
170
|
+
} else if (matches = /^l(-?\d+)$/.exec(lineNo)) {
|
|
171
|
+
line = parseInt(matches[1])
|
|
172
|
+
file = 'main.nako3'
|
|
173
|
+
} else {
|
|
174
|
+
line = 0
|
|
175
|
+
file = lineNo
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
super('実行時エラー', msg, file, line)
|
|
179
|
+
this.type = 'NakoRuntimeError'
|
|
180
|
+
this.lineNo = lineNo
|
|
181
|
+
this.line = line
|
|
182
|
+
this.file = file
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export class NakoImportError extends NakoError {
|
|
187
|
+
/**
|
|
188
|
+
* @param {string} msg
|
|
189
|
+
* @param {string} file
|
|
190
|
+
* @param {number} line
|
|
191
|
+
*/
|
|
192
|
+
constructor (msg: string, file: string, line: number) {
|
|
193
|
+
super('取り込みエラー', msg, file, line)
|
|
194
|
+
this.file = file
|
|
195
|
+
this.line = line
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DNCLに対応する構文
|
|
3
|
+
*/
|
|
4
|
+
// import { NakoIndentError } from './nako_errors.mjs'
|
|
5
|
+
import { NakoPrepare, checkNakoMode } from './nako_prepare.mjs';
|
|
6
|
+
// DNCLモードのキーワード
|
|
7
|
+
const DNCL_KEYWORDS = ['!DNCLモード'];
|
|
8
|
+
/**
|
|
9
|
+
* DNCLのソースコードをなでしこに変換する
|
|
10
|
+
* @param {String} src
|
|
11
|
+
* @param {String} filename
|
|
12
|
+
* @returns {String} converted soruce
|
|
13
|
+
*/
|
|
14
|
+
export function convertDNCL(src, filename) {
|
|
15
|
+
// 改行を合わせる
|
|
16
|
+
src = src.replace(/(\r\n|\r)/g, '\n');
|
|
17
|
+
// 「!DNCLモード」を使うかチェック
|
|
18
|
+
if (!checkNakoMode(src, DNCL_KEYWORDS)) {
|
|
19
|
+
return src;
|
|
20
|
+
}
|
|
21
|
+
const result = dncl2nako(src, filename);
|
|
22
|
+
// console.log("=====\n" + result)
|
|
23
|
+
// process.exit()
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* make space string
|
|
28
|
+
* @param {number} n
|
|
29
|
+
*/
|
|
30
|
+
function makeSpaces(n) {
|
|
31
|
+
let s = '';
|
|
32
|
+
for (let i = 0; i < n; i++) {
|
|
33
|
+
s += ' ';
|
|
34
|
+
}
|
|
35
|
+
return s;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* DNCLからなでしこに変換する(判定なし)
|
|
39
|
+
* @param {string} src
|
|
40
|
+
* @param {string} filename
|
|
41
|
+
* @returns {string} converted source
|
|
42
|
+
*/
|
|
43
|
+
function dncl2nako(src, filename) {
|
|
44
|
+
// 全角半角を統一
|
|
45
|
+
src = conv2half(src);
|
|
46
|
+
// 行頭の「|」はインデントを表す記号なので無視する
|
|
47
|
+
// 後判定の「繰り返し,」を「後判定で繰り返す」に置換する
|
|
48
|
+
const a = src.split('\n');
|
|
49
|
+
for (let i = 0; i < a.length; i++) {
|
|
50
|
+
// インデントを消す
|
|
51
|
+
let line = a[i];
|
|
52
|
+
a[i] = line.replace(/^(\s*[|\s]+)(.*$)/, (m0, m1, m2) => {
|
|
53
|
+
return makeSpaces(m1.length) + m2;
|
|
54
|
+
});
|
|
55
|
+
line = a[i];
|
|
56
|
+
// 後判定の繰り返しの実装のため
|
|
57
|
+
const line2 = line.replace(/^\s+/, '').replace(/\s+$/, '');
|
|
58
|
+
if (line2 === '繰り返し,' || line2 === '繰り返し') {
|
|
59
|
+
a[i] = '後判定で繰り返し';
|
|
60
|
+
}
|
|
61
|
+
const r = line.match(/^\s*を,?(.+)になるまで(繰り返す|実行する)/);
|
|
62
|
+
if (r) {
|
|
63
|
+
a[i] = `ここまで、(${r[1]})になるまでの間`;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
// 『もしj>hakosuならばhakosu←jを実行する』のような単文のもし文
|
|
67
|
+
const rif = line.match(/^もし(.+)を実行する(。|.)*/);
|
|
68
|
+
if (rif) {
|
|
69
|
+
const sent = dncl2nako(rif[1], filename);
|
|
70
|
+
a[i] = `もし、${sent};`;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
// 'のすべての値を0にする'
|
|
74
|
+
// 'のすべての要素を0にする'
|
|
75
|
+
// 'のすべての要素に0を代入する'
|
|
76
|
+
const rall = line.match(/^(.+?)のすべての(要素|値)(を|に)(.+?)(にする|を代入)/);
|
|
77
|
+
if (rall) {
|
|
78
|
+
const varname = rall[1];
|
|
79
|
+
const v = rall[4];
|
|
80
|
+
a[i] = `${varname} = [${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v},${v}]`;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
src = a.join('\n');
|
|
85
|
+
// ---------------------------------
|
|
86
|
+
// 置換開始
|
|
87
|
+
// ---------------------------------
|
|
88
|
+
// 単純置換リスト
|
|
89
|
+
const simpleConvList = {
|
|
90
|
+
'を実行する': 'ここまで',
|
|
91
|
+
'を実行し,そうでなくもし': '違えば、もし',
|
|
92
|
+
'を実行し,そうでなくもし': '違えば、もし',
|
|
93
|
+
'を実行し、そうでなくもし': '違えば、もし',
|
|
94
|
+
'を実行し,そうでなければ': '違えば',
|
|
95
|
+
'を実行し,そうでなければ': '違えば',
|
|
96
|
+
'を実行し、そうでなければ': '違えば',
|
|
97
|
+
'を繰り返す': 'ここまで',
|
|
98
|
+
'改行なしで表示': '連続無改行表示',
|
|
99
|
+
'ずつ増やしながら': 'ずつ増やし繰り返す',
|
|
100
|
+
'ずつ減らしながら': 'ずつ減らし繰り返す',
|
|
101
|
+
'二進で表示': '二進表示',
|
|
102
|
+
'でないならば': 'でなければ'
|
|
103
|
+
};
|
|
104
|
+
const nextChar = () => {
|
|
105
|
+
const ch = src.charAt(0);
|
|
106
|
+
src = src.substring(1);
|
|
107
|
+
return ch;
|
|
108
|
+
};
|
|
109
|
+
// 文字列を判定するフラグ
|
|
110
|
+
let flagStr = false;
|
|
111
|
+
let poolStr = '';
|
|
112
|
+
let endStr = '';
|
|
113
|
+
// 結果
|
|
114
|
+
let result = '';
|
|
115
|
+
while (src !== '') {
|
|
116
|
+
// 代入記号を変更
|
|
117
|
+
const ch = src.charAt(0);
|
|
118
|
+
if (flagStr) {
|
|
119
|
+
if (ch === endStr) {
|
|
120
|
+
result += poolStr + endStr;
|
|
121
|
+
poolStr = '';
|
|
122
|
+
flagStr = false;
|
|
123
|
+
nextChar();
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
poolStr += nextChar();
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
// 文字列?
|
|
130
|
+
if (ch === '"') {
|
|
131
|
+
flagStr = true;
|
|
132
|
+
endStr = '"';
|
|
133
|
+
poolStr = nextChar();
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (ch === '「') {
|
|
137
|
+
flagStr = true;
|
|
138
|
+
endStr = '」';
|
|
139
|
+
poolStr = nextChar();
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (ch === '『') {
|
|
143
|
+
flagStr = true;
|
|
144
|
+
endStr = '』';
|
|
145
|
+
poolStr = nextChar();
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
// 空白を飛ばす
|
|
149
|
+
if (ch === ' ' || ch === ' ' || ch === '\t') {
|
|
150
|
+
result += nextChar();
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
// 表示を連続表示に置き換える
|
|
154
|
+
const ch3 = src.substring(0, 3);
|
|
155
|
+
if (ch3 === 'を表示') {
|
|
156
|
+
result += 'を連続表示';
|
|
157
|
+
src = src.substring(3);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (src.substring(0, 4) === 'を 表示') {
|
|
161
|
+
result += 'を連続表示';
|
|
162
|
+
src = src.substring(4);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
// 乱数を乱数範囲に置き換える
|
|
166
|
+
if (src.substring(0, 2) === '乱数' && src.substring(0, 4) !== '乱数範囲') {
|
|
167
|
+
result += '乱数範囲';
|
|
168
|
+
src = src.substring(2);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
// 増やす・減らすの前に「だけ」を追加する #1149
|
|
172
|
+
if (ch3 === '増やす' || ch3 === '減らす') {
|
|
173
|
+
if (result.substring(result.length - 2) !== 'だけ') {
|
|
174
|
+
result += 'だけ';
|
|
175
|
+
}
|
|
176
|
+
result += ch3;
|
|
177
|
+
src = src.substring(3);
|
|
178
|
+
}
|
|
179
|
+
// 一覧から単純な変換
|
|
180
|
+
let flag = false;
|
|
181
|
+
for (const key in simpleConvList) {
|
|
182
|
+
const srcKey = src.substring(0, key.length);
|
|
183
|
+
if (srcKey === key) {
|
|
184
|
+
result += simpleConvList[key];
|
|
185
|
+
src = src.substring(key.length);
|
|
186
|
+
flag = true;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (flag) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
// 1文字削る
|
|
194
|
+
result += nextChar();
|
|
195
|
+
}
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 半角に変換
|
|
200
|
+
* @param {String} src
|
|
201
|
+
* @returns {string} converted source
|
|
202
|
+
*/
|
|
203
|
+
function conv2half(src) {
|
|
204
|
+
const prepare = NakoPrepare.getInstance(); // `※`, `//`, `/*` といったパターン全てに対応するために必要
|
|
205
|
+
// 全角半角の統一
|
|
206
|
+
let result = '';
|
|
207
|
+
let flagStr = false;
|
|
208
|
+
let flagStrClose = '';
|
|
209
|
+
for (let i = 0; i < src.length; i++) {
|
|
210
|
+
const c = src.charAt(i);
|
|
211
|
+
let cHalf = prepare.convert1ch(c);
|
|
212
|
+
if (flagStr) {
|
|
213
|
+
if (cHalf === flagStrClose) {
|
|
214
|
+
flagStr = false;
|
|
215
|
+
flagStrClose = '';
|
|
216
|
+
result += cHalf;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
result += c;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (cHalf === '「') {
|
|
223
|
+
flagStr = true;
|
|
224
|
+
flagStrClose = '」';
|
|
225
|
+
result += cHalf;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (cHalf === '"') {
|
|
229
|
+
flagStr = true;
|
|
230
|
+
flagStrClose = '"';
|
|
231
|
+
result += cHalf;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
// 単純な置き換えはここでやってしまう
|
|
235
|
+
// 配列記号の { ... } を [ ... ] に置換
|
|
236
|
+
if (cHalf === '{') {
|
|
237
|
+
cHalf = '[';
|
|
238
|
+
}
|
|
239
|
+
if (cHalf === '}') {
|
|
240
|
+
cHalf = ']';
|
|
241
|
+
}
|
|
242
|
+
if (cHalf === '←') {
|
|
243
|
+
cHalf = '=';
|
|
244
|
+
}
|
|
245
|
+
if (cHalf === '÷') {
|
|
246
|
+
cHalf = '÷÷';
|
|
247
|
+
} // #1152
|
|
248
|
+
result += cHalf;
|
|
249
|
+
}
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
/** @type {Object} */
|
|
253
|
+
export const NakoDncl = {
|
|
254
|
+
convert: convertDNCL
|
|
255
|
+
};
|