processorErrorFactory.js 21 KB

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