processorErrorFactory.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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: (type, dim, sourceInfo) => {
  63. if(sourceInfo) {
  64. const context = [LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  65. return createSemanticError("incompatible_types_full", context);
  66. } else {
  67. return ProcessorErrorFactory.incompatible_types(type, dim);
  68. }
  69. },
  70. incompatible_types: (type, dim) => {
  71. const context = [LocalizedStrings.translateType(type, dim)];
  72. return createSemanticError("incompatible_types", context);
  73. },
  74. incompatible_types_array_full: (exp, type, dim, sourceInfo) => {
  75. if(sourceInfo) {
  76. const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  77. return createSemanticError("incompatible_types_array_full", context);
  78. } else {
  79. return ProcessorErrorFactory.incompatible_types_array(exp, type, dim);
  80. }
  81. },
  82. incompatible_types_array: (exp, type, dim) => {
  83. const context = [exp, LocalizedStrings.translateType(type, dim)];
  84. return createSemanticError("incompatible_types_array", context);
  85. },
  86. loop_condition_type_full: (exp, sourceInfo) => {
  87. if(sourceInfo) {
  88. const context = [sourceInfo.line, sourceInfo.column, exp];
  89. return createSemanticError("loop_condition_type_full", context);
  90. } else {
  91. return ProcessorErrorFactory.loop_condition_type(exp);
  92. }
  93. },
  94. loop_condition_type: (exp) => {
  95. const context = [exp];
  96. return createSemanticError("loop_condition_type", context);
  97. },
  98. endless_loop_full: (sourceInfo) => {
  99. if(sourceInfo) {
  100. const context = [sourceInfo.line];
  101. return createSemanticError("endless_loop_full", context);
  102. } else {
  103. return ProcessorErrorFactory.endless_loop();
  104. }
  105. },
  106. endless_loop: () => {
  107. return createSemanticError("endless_loop");
  108. },
  109. for_condition_type_full: (exp, sourceInfo) => {
  110. if(sourceInfo) {
  111. const context = [sourceInfo.line, sourceInfo.column, exp];
  112. return createSemanticError("for_condition_type_full", context);
  113. } else {
  114. return ProcessorErrorFactory.for_condition_type(exp);
  115. }
  116. },
  117. for_condition_type: (exp) => {
  118. const context = [exp];
  119. return createSemanticError("for_condition_type", context);
  120. },
  121. if_condition_type_full: (exp, sourceInfo) => {
  122. if(sourceInfo) {
  123. const context = [sourceInfo.line, sourceInfo.column, exp];
  124. return createSemanticError("if_condition_type_full", context);
  125. } else {
  126. return ProcessorErrorFactory.if_condition_type(exp);
  127. }
  128. },
  129. if_condition_type: (exp) => {
  130. const context = [exp];
  131. return createSemanticError("if_condition_type", context);
  132. },
  133. invalid_global_var: () => {
  134. return createRuntimeError("invalid_global_var")
  135. },
  136. not_implemented: (id) => {
  137. const context = [id]
  138. return createRuntimeError("not_implemented", context)
  139. },
  140. invalid_case_type_full: (exp, type, dim, sourceInfo) => {
  141. if(sourceInfo) {
  142. const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  143. return createSemanticError("invalid_case_type_full", context);
  144. } else {
  145. return ProcessorErrorFactory.invalid_case_type(exp, type, dim);
  146. }
  147. },
  148. invalid_case_type: (exp, type, dim) => {
  149. const context = [exp, LocalizedStrings.translateType(type, dim)];
  150. return createSemanticError("invalid_case_type", context);
  151. },
  152. void_in_expression_full: (id, sourceInfo) => {
  153. if(sourceInfo) {
  154. const context = [sourceInfo.line, sourceInfo.column, id];
  155. return createSemanticError("void_in_expression_full", context);
  156. } else {
  157. return ProcessorErrorFactory.void_in_expression(id);
  158. }
  159. },
  160. void_in_expression: (id) => {
  161. const context = [id];
  162. return createSemanticError("void_in_expression", context);
  163. },
  164. invalid_array_access_full: (id, sourceInfo) => {
  165. if(sourceInfo) {
  166. const context = [id, sourceInfo.line, sourceInfo.column];
  167. return createSemanticError("invalid_array_access_full", context);
  168. } else {
  169. return ProcessorErrorFactory.invalid_array_access(id);
  170. }
  171. },
  172. invalid_array_access: (id) => {
  173. const context = [id];
  174. return createSemanticError("invalid_array_access", context);
  175. },
  176. invalid_matrix_access_full: (id, sourceInfo) => {
  177. if(sourceInfo) {
  178. const context = [id, sourceInfo.line, sourceInfo.column];
  179. return createSemanticError("invalid_matrix_access_full", context);
  180. } else {
  181. return ProcessorErrorFactory.invalid_matrix_access(id);
  182. }
  183. },
  184. invalid_matrix_access: (id) => {
  185. const context = [id];
  186. return createSemanticError("invalid_matrix_access", context);
  187. },
  188. matrix_column_outbounds_full: (id, value, columns, sourceInfo) => {
  189. if(sourceInfo) {
  190. const context = [sourceInfo.line, value, id, columns];
  191. return createRuntimeError("matrix_column_outbounds_full", context);
  192. } else {
  193. return ProcessorErrorFactory.matrix_column_outbounds(id, value, columns);
  194. }
  195. },
  196. matrix_column_outbounds: (id, value, columns) => {
  197. const context = [value, id, columns];
  198. return createRuntimeError("matrix_column_outbounds", context);
  199. },
  200. matrix_line_outbounds_full: (id, value, lines, sourceInfo) => {
  201. if(sourceInfo) {
  202. const context = [sourceInfo.line, value, id, lines];
  203. return createRuntimeError("matrix_line_outbounds_full", context);
  204. } else {
  205. return ProcessorErrorFactory.matrix_line_outbounds(id, value, lines);
  206. }
  207. },
  208. matrix_line_outbounds: (id, value, lines) => {
  209. const context = [value, id, lines];
  210. return createRuntimeError("matrix_line_outbounds", context);
  211. },
  212. vector_line_outbounds_full: (id, value, lines, sourceInfo) => {
  213. if(sourceInfo) {
  214. const context = [sourceInfo.line, value, id, lines];
  215. return createRuntimeError("vector_line_outbounds_full", context);
  216. } else {
  217. return ProcessorErrorFactory.vector_line_outbounds(id, value, lines);
  218. }
  219. },
  220. vector_line_outbounds: (id, value, lines) => {
  221. const context = [value, id, lines];
  222. return createRuntimeError("vector_line_outbounds", context);
  223. },
  224. vector_not_matrix_full: (id, sourceInfo) => {
  225. if(sourceInfo) {
  226. const context = [sourceInfo.line, id];
  227. return createRuntimeError("vector_not_matrix_full", context);
  228. } else {
  229. return ProcessorErrorFactory.vector_not_matrix(id);
  230. }
  231. },
  232. vector_not_matrix: (id) => {
  233. const context = [id];
  234. return createRuntimeError("vector_not_matrix", context);
  235. },
  236. function_no_return: (id) => {
  237. const context = [id];
  238. return createSemanticError("function_no_return", context);
  239. },
  240. invalid_void_return_full: (id, type, dim, sourceInfo) => {
  241. if(sourceInfo) {
  242. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  243. return createSemanticError("invalid_void_return_full", context);
  244. } else {
  245. return ProcessorErrorFactory.invalid_void_return(id, type, dim);
  246. }
  247. },
  248. invalid_void_return: (id, type, dim) => {
  249. const context = [id, LocalizedStrings.translateType(type, dim)];
  250. return createSemanticError("invalid_void_return_full", context);
  251. },
  252. invalid_return_type_full: (id, type, dim, sourceInfo) => {
  253. if(sourceInfo) {
  254. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  255. return createSemanticError("invalid_return_type_full", context);
  256. } else {
  257. return ProcessorErrorFactory.invalid_return_type(id, type, dim);
  258. }
  259. },
  260. invalid_return_type: (id, type, dim) => {
  261. const context = [id, LocalizedStrings.translateType(type, dim)];
  262. return createSemanticError("invalid_return_type", context);
  263. },
  264. invalid_parameters_size_full: (id, expected, actual, sourceInfo) => {
  265. if(sourceInfo) {
  266. const context = [sourceInfo.line, id, expected, actual];
  267. return createSemanticError("invalid_parameters_size_full", context);
  268. } else {
  269. return ProcessorErrorFactory.invalid_parameters_size(id, expected, actual);
  270. }
  271. },
  272. invalid_parameters_size: (id, expected, actual) => {
  273. const context = [id, expected, actual];
  274. return createSemanticError("invalid_parameters_size", context);
  275. },
  276. invalid_parameter_type_full: (fun_name, exp, sourceInfo) => {
  277. if(sourceInfo) {
  278. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  279. return createSemanticError("invalid_parameter_type_full", context);
  280. } else {
  281. return ProcessorErrorFactory.invalid_parameter_type(fun_name, exp);
  282. }
  283. },
  284. invalid_parameter_type: (fun_name, exp) => {
  285. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  286. return createSemanticError("invalid_parameter_type_full", context);
  287. },
  288. invalid_ref_full: (id, exp, sourceInfo) => {
  289. if(sourceInfo) {
  290. const context = [exp, id , sourceInfo.line];
  291. return createSemanticError("invalid_ref_full", context);
  292. } else {
  293. return ProcessorErrorFactory.invalid_ref(id, exp);
  294. }
  295. },
  296. invalid_ref: (id, exp) => {
  297. const context = [exp, id];
  298. return createSemanticError("invalid_ref", context);
  299. },
  300. unexpected_break_command_full: (sourceInfo) => {
  301. if(sourceInfo) {
  302. const context = [sourceInfo.line];
  303. return createRuntimeError("unexpected_break_command_full", context);
  304. } else {
  305. return ProcessorErrorFactory.unexpected_break_command();
  306. }
  307. },
  308. unexpected_break_command: () => {
  309. return createRuntimeError("unexpected_break_command");
  310. },
  311. invalid_array_literal_type_full: (exp, sourceInfo) => {
  312. if(sourceInfo) {
  313. const context = [sourceInfo.line, exp];
  314. return createRuntimeError("invalid_array_literal_type_full", context);
  315. } else {
  316. return ProcessorErrorFactory.invalid_array_literal_type(exp);
  317. }
  318. },
  319. invalid_array_literal_type: (exp) => {
  320. const context = [exp];
  321. return createRuntimeError("invalid_array_literal_type", context);
  322. },
  323. invalid_array_literal_line_full: (expected, actual, sourceInfo) => {
  324. if(sourceInfo) {
  325. const context = [sourceInfo.line, expected, actual];
  326. return createRuntimeError("invalid_array_literal_line_full", context);
  327. } else {
  328. return ProcessorErrorFactory.invalid_array_literal_type(expected, actual);
  329. }
  330. },
  331. invalid_array_literal_line: (expected, actual) => {
  332. const context = [expected, actual];
  333. return createRuntimeError("invalid_array_literal_line", context);
  334. },
  335. invalid_array_literal_column_full: (expected, actual, sourceInfo) => {
  336. if(sourceInfo) {
  337. const context = [sourceInfo.line, expected, actual];
  338. return createRuntimeError("invalid_array_literal_column_full", context);
  339. } else {
  340. return ProcessorErrorFactory.invalid_array_literal_column(expected, actual);
  341. }
  342. },
  343. invalid_array_literal_column: (expected, actual) => {
  344. const context = [expected, actual];
  345. return createRuntimeError("invalid_array_literal_column", context);
  346. },
  347. invalid_unary_op_full: (expString, opName, type, dim, sourceInfo) => {
  348. if(sourceInfo) {
  349. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  350. return createRuntimeError("invalid_unary_op_full", context);
  351. } else {
  352. return ProcessorErrorFactory.invalid_unary_op(opName, type, dim);
  353. }
  354. },
  355. invalid_unary_op: (expString, opName, type, dim) => {
  356. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  357. return createRuntimeError("invalid_unary_op", context);
  358. },
  359. invalid_infix_op_full: (expString, opName, typeLeft, dimLeft, typeRight, dimRight, sourceInfo) => {
  360. if(sourceInfo) {
  361. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  362. return createRuntimeError("invalid_infix_op_full", context);
  363. } else {
  364. return ProcessorErrorFactory.invalid_infix_op(opName, typeLeft, dimLeft, typeRight, dimRight);
  365. }
  366. },
  367. invalid_infix_op: (expString, opName, typeLeft, dimLeft, typeRight, dimRight) => {
  368. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  369. return createRuntimeError("invalid_infix_op", context);
  370. },
  371. array_dimension_not_positive_full: (sourceInfo) => {
  372. if(sourceInfo) {
  373. const context = [sourceInfo.line];
  374. return createSemanticError("array_dimension_not_positive_full", context);
  375. } else {
  376. return ProcessorErrorFactory.array_dimension_not_positive();
  377. }
  378. },
  379. array_dimension_not_positive: () => {
  380. return createSemanticError("array_dimension_not_positive");
  381. },
  382. invalid_type_conversion: (value, type, dim) => {
  383. const context = [value, LocalizedStrings.translateType(type, dim)];
  384. return createRuntimeError("invalid_type_conversion", context);
  385. },
  386. invalid_read_type: (exp, type, dim, name) => {
  387. const context = [exp, LocalizedStrings.translateType(type, dim), name];
  388. return createRuntimeError("invalid_read_type", context)
  389. },
  390. invalid_read_type_array: (exp, typePos, dimPos, name, typeArray, dimArray) => {
  391. const context = [exp, LocalizedStrings.translateType(typePos, dimPos), name,LocalizedStrings.translateType(typeArray, dimArray)];
  392. return createRuntimeError("invalid_read_type_array", context)
  393. },
  394. invalid_const_ref_full: (fun_name, exp, sourceInfo)=> {
  395. if(sourceInfo) {
  396. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  397. return createSemanticError("invalid_const_ref_full", context);
  398. } else {
  399. return ProcessorErrorFactory.invalid_const_ref(fun_name, exp);
  400. }
  401. },
  402. invalid_const_ref: (fun_name, exp) => {
  403. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  404. return createSemanticError("invalid_const_ref", context);
  405. },
  406. invalid_const_assignment_full: (var_id, source_info) => {
  407. if(source_info) {
  408. const context = [source_info.line, var_id];
  409. return createSemanticError("invalid_const_assignment_full", context);
  410. } else {
  411. return ProcessorErrorFactory.invalid_const_assignment(var_id);
  412. }
  413. },
  414. invalid_const_assignment: (var_id) => {
  415. const context = [var_id];
  416. return createSemanticError("invalid_const_assignment", context);
  417. },
  418. invalid_vector_assignment_full: (left_id, left_size, right_id, right_size, source_info) => {
  419. if(source_info) {
  420. const context = [source_info.line, left_id, left_size, right_id, right_size]
  421. return createRuntimeError("invalid_vector_assignment_full", context);
  422. } else {
  423. return ProcessorErrorFactory.invalid_vector_assignment(left_id, left_size, right_id, right_size);
  424. }
  425. },
  426. invalid_vector_assignment: (left_id, left_size, right_id, right_size) => {
  427. const context = [left_id, left_size, right_id, right_size]
  428. return createRuntimeError("invalid_vector_assignment", context);
  429. },
  430. invalid_matrix_assignment_full: (left_id, left_line, left_column, right_id, right_line, right_column, source_info) => {
  431. if(source_info) {
  432. const context = [source_info.line, left_id, left_line, left_column, right_id, right_line, right_column]
  433. return createRuntimeError("invalid_matrix_assignment_full", context);
  434. } else {
  435. return ProcessorErrorFactory.invalid_matrix_assignment(left_id, left_line, left_column, right_id, right_line, right_column);
  436. }
  437. },
  438. invalid_matrix_assignment: (left_id, left_line, left_column, right_id, right_line, right_column) => {
  439. const context = [left_id, left_line, left_column, right_id, right_line, right_column]
  440. return createRuntimeError("invalid_matrix_assignment", context);
  441. },
  442. matrix_to_vector_attr: (left_id, right_id, source_info) => {
  443. // SourceInfo have to be valid...
  444. const context = [source_info.line, right_id, left_id];
  445. return createSemanticError("matrix_to_vector_attr", context);
  446. },
  447. vector_to_matrix_attr: (left_id, right_id, source_info) => {
  448. // SourceInfo have to be valid...
  449. const context = [source_info.line, right_id, left_id];
  450. return createSemanticError("vector_to_matrix_attr", context);
  451. }
  452. });