processorErrorFactory.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. /**
  101. * @deprecated 01/10/2019
  102. */
  103. endless_loop_full: (sourceInfo) => {
  104. if(sourceInfo) {
  105. const context = [sourceInfo.line];
  106. return createSemanticError("endless_loop_full", context);
  107. } else {
  108. return ProcessorErrorFactory.endless_loop();
  109. }
  110. },
  111. endless_loop: () => {
  112. return createSemanticError("endless_loop");
  113. },
  114. if_condition_type_full: (exp, sourceInfo) => {
  115. if(sourceInfo) {
  116. const context = [sourceInfo.line, sourceInfo.column, exp];
  117. return createSemanticError("if_condition_type_full", context);
  118. } else {
  119. return ProcessorErrorFactory.if_condition_type(exp);
  120. }
  121. },
  122. if_condition_type: (exp) => {
  123. const context = [exp];
  124. return createSemanticError("if_condition_type", context);
  125. },
  126. invalid_global_var: () => {
  127. return createRuntimeError("invalid_global_var")
  128. },
  129. not_implemented: (id) => {
  130. const context = [id]
  131. return createRuntimeError("not_implemented", context)
  132. },
  133. invalid_case_type_full: (exp, type, dim, sourceInfo) => {
  134. if(sourceInfo) {
  135. const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  136. return createSemanticError("invalid_case_type_full", context);
  137. } else {
  138. return ProcessorErrorFactory.invalid_case_type(exp, type, dim);
  139. }
  140. },
  141. invalid_case_type: (exp, type, dim) => {
  142. const context = [exp, LocalizedStrings.translateType(type, dim)];
  143. return createSemanticError("invalid_case_type", context);
  144. },
  145. void_in_expression_full: (id, sourceInfo) => {
  146. if(sourceInfo) {
  147. const context = [sourceInfo.line, sourceInfo.column, id];
  148. return createSemanticError("void_in_expression_full", context);
  149. } else {
  150. return ProcessorErrorFactory.void_in_expression(id);
  151. }
  152. },
  153. void_in_expression: (id) => {
  154. const context = [id];
  155. return createSemanticError("void_in_expression", context);
  156. },
  157. invalid_array_access_full: (id, sourceInfo) => {
  158. if(sourceInfo) {
  159. const context = [id, sourceInfo.line, sourceInfo.column];
  160. return createSemanticError("invalid_array_access_full", context);
  161. } else {
  162. return ProcessorErrorFactory.invalid_array_access(id);
  163. }
  164. },
  165. invalid_array_access: (id) => {
  166. const context = [id];
  167. return createSemanticError("invalid_array_access", context);
  168. },
  169. invalid_matrix_access_full: (id, sourceInfo) => {
  170. if(sourceInfo) {
  171. const context = [id, sourceInfo.line, sourceInfo.column];
  172. return createSemanticError("invalid_matrix_access_full", context);
  173. } else {
  174. return ProcessorErrorFactory.invalid_matrix_access(id);
  175. }
  176. },
  177. invalid_matrix_access: (id) => {
  178. const context = [id];
  179. return createSemanticError("invalid_matrix_access", context);
  180. },
  181. matrix_column_outbounds_full: (id, value, columns, sourceInfo) => {
  182. if(sourceInfo) {
  183. const context = [sourceInfo.line, value, id, columns];
  184. return createRuntimeError("matrix_column_outbounds_full", context);
  185. } else {
  186. return ProcessorErrorFactory.matrix_column_outbounds(id, value, columns);
  187. }
  188. },
  189. matrix_column_outbounds: (id, value, columns) => {
  190. const context = [value, id, columns];
  191. return createRuntimeError("matrix_column_outbounds", context);
  192. },
  193. matrix_line_outbounds_full: (id, value, lines, sourceInfo) => {
  194. if(sourceInfo) {
  195. const context = [sourceInfo.line, value, id, lines];
  196. return createRuntimeError("matrix_line_outbounds_full", context);
  197. } else {
  198. return ProcessorErrorFactory.matrix_line_outbounds(id, value, lines);
  199. }
  200. },
  201. matrix_line_outbounds: (id, value, lines) => {
  202. const context = [value, id, lines];
  203. return createRuntimeError("matrix_line_outbounds", context);
  204. },
  205. vector_line_outbounds_full: (id, value, lines, sourceInfo) => {
  206. if(sourceInfo) {
  207. const context = [sourceInfo.line, value, id, lines];
  208. return createRuntimeError("vector_line_outbounds_full", context);
  209. } else {
  210. return ProcessorErrorFactory.vector_line_outbounds(id, value, lines);
  211. }
  212. },
  213. vector_line_outbounds: (id, value, lines) => {
  214. const context = [value, id, lines];
  215. return createRuntimeError("vector_line_outbounds", context);
  216. },
  217. vector_not_matrix_full: (id, sourceInfo) => {
  218. if(sourceInfo) {
  219. const context = [sourceInfo.line, id];
  220. return createRuntimeError("vector_not_matrix_full", context);
  221. } else {
  222. return ProcessorErrorFactory.vector_not_matrix(id);
  223. }
  224. },
  225. vector_not_matrix: (id) => {
  226. const context = [id];
  227. return createRuntimeError("vector_not_matrix", context);
  228. },
  229. function_no_return: (id) => {
  230. const context = [id];
  231. return createSemanticError("function_no_return", context);
  232. },
  233. invalid_void_return_full: (id, type, dim, sourceInfo) => {
  234. if(sourceInfo) {
  235. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  236. return createSemanticError("invalid_void_return_full", context);
  237. } else {
  238. return ProcessorErrorFactory.invalid_void_return(id, type, dim);
  239. }
  240. },
  241. invalid_void_return: (id, type, dim) => {
  242. const context = [id, LocalizedStrings.translateType(type, dim)];
  243. return createSemanticError("invalid_void_return_full", context);
  244. },
  245. invalid_return_type_full: (id, type, dim, sourceInfo) => {
  246. if(sourceInfo) {
  247. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  248. return createSemanticError("invalid_return_type_full", context);
  249. } else {
  250. return ProcessorErrorFactory.invalid_return_type(id, type, dim);
  251. }
  252. },
  253. invalid_return_type: (id, type, dim) => {
  254. const context = [id, LocalizedStrings.translateType(type, dim)];
  255. return createSemanticError("invalid_return_type", context);
  256. },
  257. invalid_parameters_size_full: (id, expected, actual, sourceInfo) => {
  258. if(sourceInfo) {
  259. const context = [sourceInfo.line, id, expected, actual];
  260. return createSemanticError("invalid_parameters_size_full", context);
  261. } else {
  262. return ProcessorErrorFactory.invalid_parameters_size(id, expected, actual);
  263. }
  264. },
  265. invalid_parameters_size: (id, expected, actual) => {
  266. const context = [id, expected, actual];
  267. return createSemanticError("invalid_parameters_size", context);
  268. },
  269. invalid_parameter_type_full: (fun_name, exp, sourceInfo) => {
  270. if(sourceInfo) {
  271. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  272. return createSemanticError("invalid_parameter_type_full", context);
  273. } else {
  274. return ProcessorErrorFactory.invalid_parameter_type(fun_name, exp);
  275. }
  276. },
  277. invalid_parameter_type: (fun_name, exp) => {
  278. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  279. return createSemanticError("invalid_parameter_type_full", context);
  280. },
  281. invalid_ref_full: (id, exp, sourceInfo) => {
  282. if(sourceInfo) {
  283. const context = [exp, id , sourceInfo.line];
  284. return createSemanticError("invalid_ref_full", context);
  285. } else {
  286. return ProcessorErrorFactory.invalid_ref(id, exp);
  287. }
  288. },
  289. invalid_ref: (id, exp) => {
  290. const context = [exp, id];
  291. return createSemanticError("invalid_ref", context);
  292. },
  293. unexpected_break_command_full: (sourceInfo) => {
  294. if(sourceInfo) {
  295. const context = [sourceInfo.line];
  296. return createRuntimeError("unexpected_break_command_full", context);
  297. } else {
  298. return ProcessorErrorFactory.unexpected_break_command();
  299. }
  300. },
  301. unexpected_break_command: () => {
  302. return createRuntimeError("unexpected_break_command");
  303. },
  304. invalid_array_literal_type_full: (exp, sourceInfo) => {
  305. if(sourceInfo) {
  306. const context = [sourceInfo.line, exp];
  307. return createRuntimeError("invalid_array_literal_type_full", context);
  308. } else {
  309. return ProcessorErrorFactory.invalid_array_literal_type(exp);
  310. }
  311. },
  312. invalid_array_literal_type: (exp) => {
  313. const context = [exp];
  314. return createRuntimeError("invalid_array_literal_type", context);
  315. },
  316. invalid_array_literal_line_full: (expected, actual, sourceInfo) => {
  317. if(sourceInfo) {
  318. const context = [sourceInfo.line, expected, actual];
  319. return createRuntimeError("invalid_array_literal_line_full", context);
  320. } else {
  321. return ProcessorErrorFactory.invalid_array_literal_type(expected, actual);
  322. }
  323. },
  324. invalid_array_literal_line: (expected, actual) => {
  325. const context = [expected, actual];
  326. return createRuntimeError("invalid_array_literal_line", context);
  327. },
  328. invalid_array_literal_column_full: (expected, actual, sourceInfo) => {
  329. if(sourceInfo) {
  330. const context = [sourceInfo.line, expected, actual];
  331. return createRuntimeError("invalid_array_literal_column_full", context);
  332. } else {
  333. return ProcessorErrorFactory.invalid_array_literal_column(expected, actual);
  334. }
  335. },
  336. invalid_array_literal_column: (expected, actual) => {
  337. const context = [expected, actual];
  338. return createRuntimeError("invalid_array_literal_column", context);
  339. },
  340. invalid_unary_op_full: (expString, opName, type, dim, sourceInfo) => {
  341. if(sourceInfo) {
  342. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  343. return createRuntimeError("invalid_unary_op_full", context);
  344. } else {
  345. return ProcessorErrorFactory.invalid_unary_op(opName, type, dim);
  346. }
  347. },
  348. invalid_unary_op: (expString, opName, type, dim) => {
  349. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  350. return createRuntimeError("invalid_unary_op", context);
  351. },
  352. invalid_infix_op_full: (expString, opName, typeLeft, dimLeft, typeRight, dimRight, sourceInfo) => {
  353. if(sourceInfo) {
  354. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  355. return createRuntimeError("invalid_infix_op_full", context);
  356. } else {
  357. return ProcessorErrorFactory.invalid_infix_op(opName, typeLeft, dimLeft, typeRight, dimRight);
  358. }
  359. },
  360. invalid_infix_op: (expString, opName, typeLeft, dimLeft, typeRight, dimRight) => {
  361. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  362. return createRuntimeError("invalid_infix_op", context);
  363. },
  364. array_dimension_not_positive_full: (sourceInfo) => {
  365. if(sourceInfo) {
  366. const context = [sourceInfo.line];
  367. return createSemanticError("array_dimension_not_positive_full", context);
  368. } else {
  369. return ProcessorErrorFactory.array_dimension_not_positive();
  370. }
  371. },
  372. array_dimension_not_positive: () => {
  373. return createSemanticError("array_dimension_not_positive");
  374. },
  375. invalid_type_conversion: (value, type, dim) => {
  376. const context = [value, LocalizedStrings.translateType(type, dim)];
  377. return createRuntimeError("invalid_type_conversion", context);
  378. },
  379. invalid_read_type: (exp, type, dim, name, source_info) => {
  380. const context = [source_info.line, exp, LocalizedStrings.translateType(type, dim), name];
  381. return createRuntimeError("invalid_read_type", context)
  382. },
  383. invalid_read_type_array: (exp, typePos, dimPos, name, typeArray, dimArray, sourceInfo) => {
  384. const context = [sourceInfo.line, exp, LocalizedStrings.translateType(typePos, dimPos), name,LocalizedStrings.translateType(typeArray, dimArray)];
  385. return createRuntimeError("invalid_read_type_array", context)
  386. },
  387. invalid_const_ref_full: (fun_name, exp, sourceInfo)=> {
  388. if(sourceInfo) {
  389. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  390. return createSemanticError("invalid_const_ref_full", context);
  391. } else {
  392. return ProcessorErrorFactory.invalid_const_ref(fun_name, exp);
  393. }
  394. },
  395. invalid_const_ref: (fun_name, exp) => {
  396. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  397. return createSemanticError("invalid_const_ref", context);
  398. },
  399. invalid_const_assignment_full: (var_id, source_info) => {
  400. if(source_info) {
  401. const context = [source_info.line, var_id];
  402. return createSemanticError("invalid_const_assignment_full", context);
  403. } else {
  404. return ProcessorErrorFactory.invalid_const_assignment(var_id);
  405. }
  406. },
  407. invalid_const_assignment: (var_id) => {
  408. const context = [var_id];
  409. return createSemanticError("invalid_const_assignment", context);
  410. },
  411. invalid_vector_assignment_full: (left_id, left_size, right_id, right_size, source_info) => {
  412. if(source_info) {
  413. const context = [source_info.line, left_id, left_size, right_id, right_size]
  414. return createRuntimeError("invalid_vector_assignment_full", context);
  415. } else {
  416. return ProcessorErrorFactory.invalid_vector_assignment(left_id, left_size, right_id, right_size);
  417. }
  418. },
  419. invalid_vector_assignment: (left_id, left_size, right_id, right_size) => {
  420. const context = [left_id, left_size, right_id, right_size]
  421. return createRuntimeError("invalid_vector_assignment", context);
  422. },
  423. invalid_matrix_assignment_full: (left_id, left_line, left_column, right_id, right_line, right_column, source_info) => {
  424. if(source_info) {
  425. const context = [source_info.line, left_id, left_line, left_column, right_id, right_line, right_column]
  426. return createRuntimeError("invalid_matrix_assignment_full", context);
  427. } else {
  428. return ProcessorErrorFactory.invalid_matrix_assignment(left_id, left_line, left_column, right_id, right_line, right_column);
  429. }
  430. },
  431. invalid_matrix_assignment: (left_id, left_line, left_column, right_id, right_line, right_column) => {
  432. const context = [left_id, left_line, left_column, right_id, right_line, right_column]
  433. return createRuntimeError("invalid_matrix_assignment", context);
  434. },
  435. matrix_to_vector_attr: (left_id, right_id, source_info) => {
  436. // SourceInfo have to be valid...
  437. const context = [source_info.line, right_id, left_id];
  438. return createSemanticError("matrix_to_vector_attr", context);
  439. },
  440. vector_to_matrix_attr: (left_id, right_id, source_info) => {
  441. // SourceInfo have to be valid...
  442. const context = [source_info.line, right_id, left_id];
  443. return createSemanticError("vector_to_matrix_attr", context);
  444. },
  445. invalid_matrix_index_assign_full: (mat_id, mat_line, mat_size, exp, exp_size, source_info) => {
  446. if(source_info){
  447. const context = [source_info.line, mat_line, mat_id, mat_size, exp, exp_size];
  448. return createRuntimeError("invalid_matrix_index_assign_full", context);
  449. } else {
  450. return ProcessorErrorFactory.invalid_matrix_index_assign(mat_id, mat_line, mat_size, exp, exp_size)
  451. }
  452. },
  453. invalid_matrix_index_assign: (mat_id, mat_line, mat_size, exp, exp_size) =>{
  454. const context = [mat_line, mat_id, mat_size, exp, exp_size];
  455. return createRuntimeError("invalid_matrix_index_assign", context);
  456. },
  457. invalid_number_elements_vector: (expected_num, exp, actual_num, source_info) => {
  458. // SourceInfo have to be valid...
  459. const context = [expected_num, source_info.line, exp, actual_num];
  460. return createRuntimeError("invalid_number_elements_vector", context);
  461. },
  462. invalid_number_lines_matrix: (expected_num, exp, actual_num, source_info) => {
  463. // SourceInfo have to be valid...
  464. const context = [expected_num, source_info.line, exp, actual_num];
  465. return createRuntimeError("invalid_number_lines_matrix", context);
  466. },
  467. divsion_by_zero_full: (exp, source_info) => {
  468. if(source_info) {
  469. const context = [source_info.line, exp];
  470. return createRuntimeError("divsion_by_zero_full", context);
  471. } else {
  472. return ProcessorErrorFactory.divsion_by_zero(exp);
  473. }
  474. },
  475. divsion_by_zero: (exp) => {
  476. const context = [exp];
  477. return createRuntimeError("divsion_by_zero", context);
  478. },
  479. undefined_tanget_value: (value, source_info) => {
  480. const context = [source_info.line, value];
  481. return createRuntimeError("undefined_tanget_value", context);
  482. },
  483. negative_log_value: (source_info) => {
  484. return createRuntimeError("negative_log_value",[source_info.line]);
  485. },
  486. invalid_string_index: (index, str, source_info) => {
  487. const local_fun_name = LanguageDefinedFunction.getLocalName("$charAt");
  488. const context = [source_info.line, local_fun_name, index, str, str.length - 1];
  489. return createRuntimeError("invalid_string_index", context);
  490. },
  491. negative_sqrt_value: (source_info) => {
  492. return createRuntimeError("negative_sqrt_value",[source_info.line]);
  493. },
  494. /**
  495. * @deprecated 01/10/2019
  496. */
  497. exceeded_recursive_calls: (source_info) => {
  498. const context = [source_info.line];
  499. return createRuntimeError("exceeded_recursive_calls", context);
  500. },
  501. invalid_for_variable: (id, source_info) => {
  502. const context = [source_info.line, id];
  503. return createSemanticError("invalid_for_variable", context);
  504. },
  505. invalid_for_from: (exp, source_info) => {
  506. const context = [source_info.line, exp];
  507. return createSemanticError("invalid_for_from", context);
  508. },
  509. invalid_for_to: (exp, source_info) => {
  510. const context = [source_info.line, exp];
  511. return createSemanticError("invalid_for_to", context);
  512. },
  513. invalid_for_pass: (exp, source_info) => {
  514. const context = [source_info.line, exp];
  515. return createSemanticError("invalid_for_pass", context);
  516. },
  517. exceed_max_instructions: () => {
  518. return createRuntimeError('exceed_max_instructions');
  519. }
  520. });