processorErrorFactory.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. import { RuntimeError } from './runtimeError';
  2. import { SemanticError } from './semanticError';
  3. import * as LocalizedStringsService from './../../services/localizedStringsService';
  4. import { LanguageDefinedFunction } from '../definedFunctions';
  5. const LocalizedStrings = LocalizedStringsService.getInstance();
  6. function createRuntimeError (i18n_id, context = []) {
  7. return new RuntimeError(LocalizedStrings.getError(i18n_id, context))
  8. }
  9. function createSemanticError (i18n_id, context = []) {
  10. return new SemanticError(LocalizedStrings.getError(i18n_id, context))
  11. }
  12. export const ProcessorErrorFactory = Object.freeze({
  13. symbol_not_found_full: (id, sourceInfo) => {
  14. if(sourceInfo) {
  15. const context = [id, sourceInfo.line, sourceInfo.column];
  16. return createSemanticError("symbol_not_found_full", context);
  17. } else {
  18. return ProcessorErrorFactory.symbol_not_found(id);
  19. }
  20. },
  21. symbol_not_found: (id) => {
  22. const context = [id];
  23. return createSemanticError("symbol_not_found", context);
  24. },
  25. function_missing_full: (id, sourceInfo) => {
  26. if(sourceInfo) {
  27. const context = [id, sourceInfo.line, sourceInfo.column];
  28. return createSemanticError("function_missing_full", context);
  29. } else {
  30. return ProcessorErrorFactory.function_missing(id);
  31. }
  32. },
  33. function_missing: (id) => {
  34. const context = [id];
  35. return createSemanticError("function_missing", context);
  36. },
  37. main_missing: () => {
  38. return createSemanticError("main_missing");
  39. },
  40. array_dimension_not_int_full: (sourceInfo) => {
  41. if(sourceInfo) {
  42. const context = [sourceInfo.line];
  43. return createSemanticError("array_dimension_not_int_full", context);
  44. } else {
  45. return ProcessorErrorFactory.array_dimension_not_int();
  46. }
  47. },
  48. array_dimension_not_int: () => {
  49. return createSemanticError("array_dimension_not_int");
  50. },
  51. unknown_command_full: (sourceInfo)=> {
  52. if(sourceInfo) {
  53. const context = [sourceInfo.line];
  54. return createRuntimeError("unknown_command_full", context);
  55. } else {
  56. return ProcessorErrorFactory.unknown_command();
  57. }
  58. },
  59. unknown_command: ()=> {
  60. return createRuntimeError("unknown_command");
  61. },
  62. incompatible_types_full: (left_type, left_dim, right_type, right_dim, exp, source_info) => {
  63. if(source_info) {
  64. const context = [LocalizedStrings.translateType(left_type, left_dim), exp, source_info.line,
  65. LocalizedStrings.translateType(right_type, right_dim)];
  66. return createSemanticError("incompatible_types_full", context);
  67. } else {
  68. return ProcessorErrorFactory.incompatible_types(left_type, left_dim, right_type, right_dim, exp);
  69. }
  70. },
  71. incompatible_types: (left_type, left_dim, right_type, right_dim, exp) => {
  72. const context = [LocalizedStrings.translateType(left_type, left_dim), exp,
  73. LocalizedStrings.translateType(right_type, right_dim)];
  74. return createSemanticError("incompatible_types", context);
  75. },
  76. incompatible_types_array_full: (exp, type, dim, sourceInfo) => {
  77. if(sourceInfo) {
  78. const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  79. return createSemanticError("incompatible_types_array_full", context);
  80. } else {
  81. return ProcessorErrorFactory.incompatible_types_array(exp, type, dim);
  82. }
  83. },
  84. incompatible_types_array: (exp, type, dim) => {
  85. const context = [exp, LocalizedStrings.translateType(type, dim)];
  86. return createSemanticError("incompatible_types_array", context);
  87. },
  88. loop_condition_type_full: (exp, sourceInfo) => {
  89. if(sourceInfo) {
  90. const context = [sourceInfo.line, sourceInfo.column, exp];
  91. return createSemanticError("loop_condition_type_full", context);
  92. } else {
  93. return ProcessorErrorFactory.loop_condition_type(exp);
  94. }
  95. },
  96. loop_condition_type: (exp) => {
  97. const context = [exp];
  98. return createSemanticError("loop_condition_type", context);
  99. },
  100. endless_loop_full: (sourceInfo) => {
  101. if(sourceInfo) {
  102. const context = [sourceInfo.line];
  103. return createSemanticError("endless_loop_full", context);
  104. } else {
  105. return ProcessorErrorFactory.endless_loop();
  106. }
  107. },
  108. endless_loop: () => {
  109. return createSemanticError("endless_loop");
  110. },
  111. if_condition_type_full: (exp, sourceInfo) => {
  112. if(sourceInfo) {
  113. const context = [sourceInfo.line, sourceInfo.column, exp];
  114. return createSemanticError("if_condition_type_full", context);
  115. } else {
  116. return ProcessorErrorFactory.if_condition_type(exp);
  117. }
  118. },
  119. if_condition_type: (exp) => {
  120. const context = [exp];
  121. return createSemanticError("if_condition_type", context);
  122. },
  123. invalid_global_var: () => {
  124. return createRuntimeError("invalid_global_var")
  125. },
  126. not_implemented: (id) => {
  127. const context = [id]
  128. return createRuntimeError("not_implemented", context)
  129. },
  130. invalid_case_type_full: (exp, type, dim, sourceInfo) => {
  131. if(sourceInfo) {
  132. const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  133. return createSemanticError("invalid_case_type_full", context);
  134. } else {
  135. return ProcessorErrorFactory.invalid_case_type(exp, type, dim);
  136. }
  137. },
  138. invalid_case_type: (exp, type, dim) => {
  139. const context = [exp, LocalizedStrings.translateType(type, dim)];
  140. return createSemanticError("invalid_case_type", context);
  141. },
  142. void_in_expression_full: (id, sourceInfo) => {
  143. if(sourceInfo) {
  144. const context = [sourceInfo.line, sourceInfo.column, id];
  145. return createSemanticError("void_in_expression_full", context);
  146. } else {
  147. return ProcessorErrorFactory.void_in_expression(id);
  148. }
  149. },
  150. void_in_expression: (id) => {
  151. const context = [id];
  152. return createSemanticError("void_in_expression", context);
  153. },
  154. invalid_array_access_full: (id, sourceInfo) => {
  155. if(sourceInfo) {
  156. const context = [id, sourceInfo.line, sourceInfo.column];
  157. return createSemanticError("invalid_array_access_full", context);
  158. } else {
  159. return ProcessorErrorFactory.invalid_array_access(id);
  160. }
  161. },
  162. invalid_array_access: (id) => {
  163. const context = [id];
  164. return createSemanticError("invalid_array_access", context);
  165. },
  166. invalid_matrix_access_full: (id, sourceInfo) => {
  167. if(sourceInfo) {
  168. const context = [id, sourceInfo.line, sourceInfo.column];
  169. return createSemanticError("invalid_matrix_access_full", context);
  170. } else {
  171. return ProcessorErrorFactory.invalid_matrix_access(id);
  172. }
  173. },
  174. invalid_matrix_access: (id) => {
  175. const context = [id];
  176. return createSemanticError("invalid_matrix_access", context);
  177. },
  178. matrix_column_outbounds_full: (id, value, columns, sourceInfo) => {
  179. if(sourceInfo) {
  180. const context = [sourceInfo.line, value, id, columns];
  181. return createRuntimeError("matrix_column_outbounds_full", context);
  182. } else {
  183. return ProcessorErrorFactory.matrix_column_outbounds(id, value, columns);
  184. }
  185. },
  186. matrix_column_outbounds: (id, value, columns) => {
  187. const context = [value, id, columns];
  188. return createRuntimeError("matrix_column_outbounds", context);
  189. },
  190. matrix_line_outbounds_full: (id, value, lines, sourceInfo) => {
  191. if(sourceInfo) {
  192. const context = [sourceInfo.line, value, id, lines];
  193. return createRuntimeError("matrix_line_outbounds_full", context);
  194. } else {
  195. return ProcessorErrorFactory.matrix_line_outbounds(id, value, lines);
  196. }
  197. },
  198. matrix_line_outbounds: (id, value, lines) => {
  199. const context = [value, id, lines];
  200. return createRuntimeError("matrix_line_outbounds", context);
  201. },
  202. vector_line_outbounds_full: (id, value, lines, sourceInfo) => {
  203. if(sourceInfo) {
  204. const context = [sourceInfo.line, value, id, lines];
  205. return createRuntimeError("vector_line_outbounds_full", context);
  206. } else {
  207. return ProcessorErrorFactory.vector_line_outbounds(id, value, lines);
  208. }
  209. },
  210. vector_line_outbounds: (id, value, lines) => {
  211. const context = [value, id, lines];
  212. return createRuntimeError("vector_line_outbounds", context);
  213. },
  214. vector_not_matrix_full: (id, sourceInfo) => {
  215. if(sourceInfo) {
  216. const context = [sourceInfo.line, id];
  217. return createRuntimeError("vector_not_matrix_full", context);
  218. } else {
  219. return ProcessorErrorFactory.vector_not_matrix(id);
  220. }
  221. },
  222. vector_not_matrix: (id) => {
  223. const context = [id];
  224. return createRuntimeError("vector_not_matrix", context);
  225. },
  226. function_no_return: (id) => {
  227. const context = [id];
  228. return createSemanticError("function_no_return", context);
  229. },
  230. invalid_void_return_full: (id, type, dim, sourceInfo) => {
  231. if(sourceInfo) {
  232. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  233. return createSemanticError("invalid_void_return_full", context);
  234. } else {
  235. return ProcessorErrorFactory.invalid_void_return(id, type, dim);
  236. }
  237. },
  238. invalid_void_return: (id, type, dim) => {
  239. const context = [id, LocalizedStrings.translateType(type, dim)];
  240. return createSemanticError("invalid_void_return_full", context);
  241. },
  242. invalid_return_type_full: (id, type, dim, sourceInfo) => {
  243. if(sourceInfo) {
  244. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  245. return createSemanticError("invalid_return_type_full", context);
  246. } else {
  247. return ProcessorErrorFactory.invalid_return_type(id, type, dim);
  248. }
  249. },
  250. invalid_return_type: (id, type, dim) => {
  251. const context = [id, LocalizedStrings.translateType(type, dim)];
  252. return createSemanticError("invalid_return_type", context);
  253. },
  254. invalid_parameters_size_full: (id, expected, actual, sourceInfo) => {
  255. if(sourceInfo) {
  256. const context = [sourceInfo.line, id, expected, actual];
  257. return createSemanticError("invalid_parameters_size_full", context);
  258. } else {
  259. return ProcessorErrorFactory.invalid_parameters_size(id, expected, actual);
  260. }
  261. },
  262. invalid_parameters_size: (id, expected, actual) => {
  263. const context = [id, expected, actual];
  264. return createSemanticError("invalid_parameters_size", context);
  265. },
  266. invalid_parameter_type_full: (fun_name, exp, sourceInfo) => {
  267. if(sourceInfo) {
  268. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  269. return createSemanticError("invalid_parameter_type_full", context);
  270. } else {
  271. return ProcessorErrorFactory.invalid_parameter_type(fun_name, exp);
  272. }
  273. },
  274. invalid_parameter_type: (fun_name, exp) => {
  275. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  276. return createSemanticError("invalid_parameter_type_full", context);
  277. },
  278. invalid_ref_full: (id, exp, sourceInfo) => {
  279. if(sourceInfo) {
  280. const context = [exp, id , sourceInfo.line];
  281. return createSemanticError("invalid_ref_full", context);
  282. } else {
  283. return ProcessorErrorFactory.invalid_ref(id, exp);
  284. }
  285. },
  286. invalid_ref: (id, exp) => {
  287. const context = [exp, id];
  288. return createSemanticError("invalid_ref", context);
  289. },
  290. unexpected_break_command_full: (sourceInfo) => {
  291. if(sourceInfo) {
  292. const context = [sourceInfo.line];
  293. return createRuntimeError("unexpected_break_command_full", context);
  294. } else {
  295. return ProcessorErrorFactory.unexpected_break_command();
  296. }
  297. },
  298. unexpected_break_command: () => {
  299. return createRuntimeError("unexpected_break_command");
  300. },
  301. invalid_array_literal_type_full: (exp, sourceInfo) => {
  302. if(sourceInfo) {
  303. const context = [sourceInfo.line, exp];
  304. return createRuntimeError("invalid_array_literal_type_full", context);
  305. } else {
  306. return ProcessorErrorFactory.invalid_array_literal_type(exp);
  307. }
  308. },
  309. invalid_array_literal_type: (exp) => {
  310. const context = [exp];
  311. return createRuntimeError("invalid_array_literal_type", context);
  312. },
  313. invalid_array_literal_line_full: (expected, actual, sourceInfo) => {
  314. if(sourceInfo) {
  315. const context = [sourceInfo.line, expected, actual];
  316. return createRuntimeError("invalid_array_literal_line_full", context);
  317. } else {
  318. return ProcessorErrorFactory.invalid_array_literal_type(expected, actual);
  319. }
  320. },
  321. invalid_array_literal_line: (expected, actual) => {
  322. const context = [expected, actual];
  323. return createRuntimeError("invalid_array_literal_line", context);
  324. },
  325. invalid_array_literal_column_full: (expected, actual, sourceInfo) => {
  326. if(sourceInfo) {
  327. const context = [sourceInfo.line, expected, actual];
  328. return createRuntimeError("invalid_array_literal_column_full", context);
  329. } else {
  330. return ProcessorErrorFactory.invalid_array_literal_column(expected, actual);
  331. }
  332. },
  333. invalid_array_literal_column: (expected, actual) => {
  334. const context = [expected, actual];
  335. return createRuntimeError("invalid_array_literal_column", context);
  336. },
  337. invalid_unary_op_full: (expString, opName, type, dim, sourceInfo) => {
  338. if(sourceInfo) {
  339. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  340. return createRuntimeError("invalid_unary_op_full", context);
  341. } else {
  342. return ProcessorErrorFactory.invalid_unary_op(opName, type, dim);
  343. }
  344. },
  345. invalid_unary_op: (expString, opName, type, dim) => {
  346. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  347. return createRuntimeError("invalid_unary_op", context);
  348. },
  349. invalid_infix_op_full: (expString, opName, typeLeft, dimLeft, typeRight, dimRight, sourceInfo) => {
  350. if(sourceInfo) {
  351. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  352. return createRuntimeError("invalid_infix_op_full", context);
  353. } else {
  354. return ProcessorErrorFactory.invalid_infix_op(opName, typeLeft, dimLeft, typeRight, dimRight);
  355. }
  356. },
  357. invalid_infix_op: (expString, opName, typeLeft, dimLeft, typeRight, dimRight) => {
  358. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  359. return createRuntimeError("invalid_infix_op", context);
  360. },
  361. array_dimension_not_positive_full: (sourceInfo) => {
  362. if(sourceInfo) {
  363. const context = [sourceInfo.line];
  364. return createSemanticError("array_dimension_not_positive_full", context);
  365. } else {
  366. return ProcessorErrorFactory.array_dimension_not_positive();
  367. }
  368. },
  369. array_dimension_not_positive: () => {
  370. return createSemanticError("array_dimension_not_positive");
  371. },
  372. invalid_type_conversion: (value, type, dim) => {
  373. const context = [value, LocalizedStrings.translateType(type, dim)];
  374. return createRuntimeError("invalid_type_conversion", context);
  375. },
  376. invalid_read_type: (exp, type, dim, name, source_info) => {
  377. const context = [source_info.line, exp, LocalizedStrings.translateType(type, dim), name];
  378. return createRuntimeError("invalid_read_type", context)
  379. },
  380. invalid_read_type_array: (exp, typePos, dimPos, name, typeArray, dimArray, sourceInfo) => {
  381. const context = [sourceInfo.line, exp, LocalizedStrings.translateType(typePos, dimPos), name,LocalizedStrings.translateType(typeArray, dimArray)];
  382. return createRuntimeError("invalid_read_type_array", context)
  383. },
  384. invalid_const_ref_full: (fun_name, exp, sourceInfo)=> {
  385. if(sourceInfo) {
  386. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  387. return createSemanticError("invalid_const_ref_full", context);
  388. } else {
  389. return ProcessorErrorFactory.invalid_const_ref(fun_name, exp);
  390. }
  391. },
  392. invalid_const_ref: (fun_name, exp) => {
  393. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  394. return createSemanticError("invalid_const_ref", context);
  395. },
  396. invalid_const_assignment_full: (var_id, source_info) => {
  397. if(source_info) {
  398. const context = [source_info.line, var_id];
  399. return createSemanticError("invalid_const_assignment_full", context);
  400. } else {
  401. return ProcessorErrorFactory.invalid_const_assignment(var_id);
  402. }
  403. },
  404. invalid_const_assignment: (var_id) => {
  405. const context = [var_id];
  406. return createSemanticError("invalid_const_assignment", context);
  407. },
  408. invalid_vector_assignment_full: (left_id, left_size, right_id, right_size, source_info) => {
  409. if(source_info) {
  410. const context = [source_info.line, left_id, left_size, right_id, right_size]
  411. return createRuntimeError("invalid_vector_assignment_full", context);
  412. } else {
  413. return ProcessorErrorFactory.invalid_vector_assignment(left_id, left_size, right_id, right_size);
  414. }
  415. },
  416. invalid_vector_assignment: (left_id, left_size, right_id, right_size) => {
  417. const context = [left_id, left_size, right_id, right_size]
  418. return createRuntimeError("invalid_vector_assignment", context);
  419. },
  420. invalid_matrix_assignment_full: (left_id, left_line, left_column, right_id, right_line, right_column, source_info) => {
  421. if(source_info) {
  422. const context = [source_info.line, left_id, left_line, left_column, right_id, right_line, right_column]
  423. return createRuntimeError("invalid_matrix_assignment_full", context);
  424. } else {
  425. return ProcessorErrorFactory.invalid_matrix_assignment(left_id, left_line, left_column, right_id, right_line, right_column);
  426. }
  427. },
  428. invalid_matrix_assignment: (left_id, left_line, left_column, right_id, right_line, right_column) => {
  429. const context = [left_id, left_line, left_column, right_id, right_line, right_column]
  430. return createRuntimeError("invalid_matrix_assignment", context);
  431. },
  432. matrix_to_vector_attr: (left_id, right_id, source_info) => {
  433. // SourceInfo have to be valid...
  434. const context = [source_info.line, right_id, left_id];
  435. return createSemanticError("matrix_to_vector_attr", context);
  436. },
  437. vector_to_matrix_attr: (left_id, right_id, source_info) => {
  438. // SourceInfo have to be valid...
  439. const context = [source_info.line, right_id, left_id];
  440. return createSemanticError("vector_to_matrix_attr", context);
  441. },
  442. invalid_matrix_index_assign_full: (mat_id, mat_line, mat_size, exp, exp_size, source_info) => {
  443. if(source_info){
  444. const context = [source_info.line, mat_line, mat_id, mat_size, exp, exp_size];
  445. return createRuntimeError("invalid_matrix_index_assign_full", context);
  446. } else {
  447. return ProcessorErrorFactory.invalid_matrix_index_assign(mat_id, mat_line, mat_size, exp, exp_size)
  448. }
  449. },
  450. invalid_matrix_index_assign: (mat_id, mat_line, mat_size, exp, exp_size) =>{
  451. const context = [mat_line, mat_id, mat_size, exp, exp_size];
  452. return createRuntimeError("invalid_matrix_index_assign", context);
  453. },
  454. invalid_number_elements_vector: (expected_num, exp, actual_num, source_info) => {
  455. // SourceInfo have to be valid...
  456. const context = [expected_num, source_info.line, exp, actual_num];
  457. return createRuntimeError("invalid_number_elements_vector", context);
  458. },
  459. invalid_number_lines_matrix: (expected_num, exp, actual_num, source_info) => {
  460. // SourceInfo have to be valid...
  461. const context = [expected_num, source_info.line, exp, actual_num];
  462. return createRuntimeError("invalid_number_lines_matrix", context);
  463. },
  464. divsion_by_zero_full: (exp, source_info) => {
  465. if(source_info) {
  466. const context = [source_info.line, exp];
  467. return createRuntimeError("divsion_by_zero_full", context);
  468. } else {
  469. return ProcessorErrorFactory.divsion_by_zero(exp);
  470. }
  471. },
  472. divsion_by_zero: (exp) => {
  473. const context = [exp];
  474. return createRuntimeError("divsion_by_zero", context);
  475. },
  476. undefined_tanget_value: (value, source_info) => {
  477. const context = [source_info.line, value];
  478. return createRuntimeError("undefined_tanget_value", context);
  479. },
  480. negative_log_value: (source_info) => {
  481. return createRuntimeError("negative_log_value",[source_info.line]);
  482. },
  483. invalid_string_index: (index, str, source_info) => {
  484. const local_fun_name = LanguageDefinedFunction.getLocalName("$charAt");
  485. const context = [source_info.line, local_fun_name, index, str, str.length - 1];
  486. return createRuntimeError("invalid_string_index", context);
  487. },
  488. negative_sqrt_value: (source_info) => {
  489. return createRuntimeError("negative_sqrt_value",[source_info.line]);
  490. },
  491. exceeded_recursive_calls: (source_info) => {
  492. const context = [source_info.line];
  493. return createRuntimeError("exceeded_recursive_calls", context);
  494. },
  495. invalid_for_variable: (id, source_info) => {
  496. const context = [source_info.line, id];
  497. return createSemanticError("invalid_for_variable", context);
  498. },
  499. invalid_for_from: (exp, source_info) => {
  500. const context = [source_info.line, exp];
  501. return createSemanticError("invalid_for_from", context);
  502. },
  503. invalid_for_to: (exp, source_info) => {
  504. const context = [source_info.line, exp];
  505. return createSemanticError("invalid_for_to", context);
  506. },
  507. invalid_for_pass: (exp, source_info) => {
  508. const context = [source_info.line, exp];
  509. return createSemanticError("invalid_for_pass", context);
  510. }
  511. });