processorErrorFactory.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. export const ProcessorErrorFactory = Object.freeze({
  7. symbol_not_found_full: (id, sourceInfo) => {
  8. if(sourceInfo) {
  9. const context = [id, sourceInfo.line, sourceInfo.column];
  10. return new SemanticError(LocalizedStrings.getError("symbol_not_found_full", context));
  11. } else {
  12. return ProcessorErrorFactory.symbol_not_found(id);
  13. }
  14. },
  15. symbol_not_found: (id) => {
  16. const context = [id];
  17. return new SemanticError(LocalizedStrings.getError("symbol_not_found", context));
  18. },
  19. function_missing_full: (id, sourceInfo) => {
  20. if(sourceInfo) {
  21. const context = [id, sourceInfo.line, sourceInfo.column];
  22. return new SemanticError(LocalizedStrings.getError("function_missing_full", context));
  23. } else {
  24. return ProcessorErrorFactory.function_missing(id);
  25. }
  26. },
  27. function_missing: (id) => {
  28. const context = [id];
  29. return new SemanticError(LocalizedStrings.getError("function_missing", context));
  30. },
  31. main_missing: () => {
  32. return new SemanticError(LocalizedStrings.getError("main_missing"));
  33. }, // TODO: better urgent error message
  34. array_dimension_not_int_full: (sourceInfo) => {
  35. if(sourceInfo) {
  36. const context = [sourceInfo.line];
  37. return new SemanticError(LocalizedStrings.getError("array_dimension_not_int_full", context));
  38. } else {
  39. return ProcessorErrorFactory.array_dimension_not_int();
  40. }
  41. },
  42. array_dimension_not_int: () => {
  43. return new SemanticError(LocalizedStrings.getError("array_dimension_not_int"));
  44. },
  45. unknown_command_full: (sourceInfo)=> {
  46. if(sourceInfo) {
  47. const context = [sourceInfo.line];
  48. return new RuntimeError(LocalizedStrings.getError("unknown_command_full", context));
  49. } else {
  50. return ProcessorErrorFactory.unknown_command();
  51. }
  52. },
  53. unknown_command: ()=> {
  54. return new RuntimeError(LocalizedStrings.getError("unknown_command"));
  55. },
  56. incompatible_types_full: (type, dim, sourceInfo) => {
  57. if(sourceInfo) {
  58. const context = [LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  59. return new SemanticError(LocalizedStrings.getError("incompatible_types_full", context));
  60. } else {
  61. return ProcessorErrorFactory.incompatible_types(type, dim);
  62. }
  63. },
  64. incompatible_types: (type, dim) => {
  65. const context = [LocalizedStrings.translateType(type, dim)];
  66. return new SemanticError(LocalizedStrings.getError("incompatible_types", context));
  67. },
  68. incompatible_types_array_full: (exp, type, dim, sourceInfo) => {
  69. if(sourceInfo) {
  70. const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  71. return new SemanticError(LocalizedStrings.getError("incompatible_types_array_full", context));
  72. } else {
  73. return ProcessorErrorFactory.incompatible_types_array(exp, type, dim);
  74. }
  75. },
  76. incompatible_types_array: (exp, type, dim) => {
  77. const context = [exp, LocalizedStrings.translateType(type, dim)];
  78. return new SemanticError(LocalizedStrings.getError("incompatible_types_array", context));
  79. },
  80. loop_condition_type_full: (exp, sourceInfo) => {
  81. if(sourceInfo) {
  82. const context = [sourceInfo.line, sourceInfo.column, exp];
  83. return new SemanticError(LocalizedStrings.getError("loop_condition_type_full", context));
  84. } else {
  85. return ProcessorErrorFactory.loop_condition_type(exp);
  86. }
  87. },
  88. loop_condition_type: (exp) => {
  89. const context = [exp];
  90. return new SemanticError(LocalizedStrings.getError("loop_condition_type", context));
  91. },
  92. endless_loop_full: (sourceInfo) => {
  93. if(sourceInfo) {
  94. const context = [sourceInfo.line];
  95. return new SemanticError(LocalizedStrings.getError("endless_loop_full", context));
  96. } else {
  97. return ProcessorErrorFactory.endless_loop();
  98. }
  99. },
  100. endless_loop: () => {
  101. return new SemanticError(LocalizedStrings.getError("endless_loop"));
  102. },
  103. for_condition_type_full: (exp, sourceInfo) => {
  104. if(sourceInfo) {
  105. const context = [sourceInfo.line, sourceInfo.column, exp];
  106. return new SemanticError(LocalizedStrings.getError("for_condition_type_full", context));
  107. } else {
  108. return ProcessorErrorFactory.for_condition_type(exp);
  109. }
  110. },
  111. for_condition_type: (exp) => {
  112. const context = [exp];
  113. return new SemanticError(LocalizedStrings.getError("for_condition_type", context));
  114. },
  115. if_condition_type_full: (exp, sourceInfo) => {
  116. if(sourceInfo) {
  117. const context = [sourceInfo.line, sourceInfo.column, exp];
  118. return new SemanticError(LocalizedStrings.getError("if_condition_type_full", context));
  119. } else {
  120. return ProcessorErrorFactory.if_condition_type(exp);
  121. }
  122. },
  123. if_condition_type: (exp) => {
  124. const context = [exp];
  125. return new SemanticError(LocalizedStrings.getError("if_condition_type", context));
  126. },
  127. invalid_global_var: () => {
  128. return new RuntimeError(LocalizedStrings.getError("invalid_global_var"))
  129. },
  130. not_implemented: (id) => {
  131. const context = [id]
  132. return new RuntimeError(LocalizedStrings.getError("not_implemented", context))
  133. },
  134. invalid_case_type_full: (exp, type, dim, sourceInfo) => {
  135. if(sourceInfo) {
  136. const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
  137. return new SemanticError(LocalizedStrings.getError("invalid_case_type_full", context));
  138. } else {
  139. return ProcessorErrorFactory.invalid_case_type(exp, type, dim);
  140. }
  141. },
  142. invalid_case_type: (exp, type, dim) => {
  143. const context = [exp, LocalizedStrings.translateType(type, dim)];
  144. return new SemanticError(LocalizedStrings.getError("invalid_case_type", context));
  145. },
  146. void_in_expression_full: (id, sourceInfo) => {
  147. if(sourceInfo) {
  148. const context = [sourceInfo.line, sourceInfo.column, id];
  149. return new SemanticError(LocalizedStrings.getError("void_in_expression_full", context));
  150. } else {
  151. return ProcessorErrorFactory.void_in_expression(id);
  152. }
  153. },
  154. void_in_expression: (id) => {
  155. const context = [id];
  156. return new SemanticError(LocalizedStrings.getError("void_in_expression", context));
  157. },
  158. invalid_array_access_full: (id, sourceInfo) => {
  159. if(sourceInfo) {
  160. const context = [id, sourceInfo.line, sourceInfo.column];
  161. return new SemanticError(LocalizedStrings.getError("invalid_array_access_full", context));
  162. } else {
  163. return ProcessorErrorFactory.invalid_array_access(id);
  164. }
  165. },
  166. invalid_array_access: (id) => {
  167. const context = [id];
  168. return new SemanticError(LocalizedStrings.getError("invalid_array_access", context));
  169. },
  170. invalid_matrix_access_full: (id, sourceInfo) => {
  171. if(sourceInfo) {
  172. const context = [id, sourceInfo.line, sourceInfo.column];
  173. return new SemanticError(LocalizedStrings.getError("invalid_matrix_access_full", context));
  174. } else {
  175. return ProcessorErrorFactory.invalid_matrix_access(id);
  176. }
  177. },
  178. invalid_matrix_access: (id) => {
  179. const context = [id];
  180. return new SemanticError(LocalizedStrings.getError("invalid_matrix_access", context));
  181. },
  182. matrix_column_outbounds_full: (id, value, columns, sourceInfo) => {
  183. if(sourceInfo) {
  184. const context = [sourceInfo.line, value, id, columns];
  185. return new RuntimeError(LocalizedStrings.getError("matrix_column_outbounds_full", context));
  186. } else {
  187. return ProcessorErrorFactory.matrix_column_outbounds(id, value, columns);
  188. }
  189. },
  190. matrix_column_outbounds: (id, value, columns) => {
  191. const context = [value, id, columns];
  192. return new RuntimeError(LocalizedStrings.getError("matrix_column_outbounds", context));
  193. },
  194. matrix_line_outbounds_full: (id, value, lines, sourceInfo) => {
  195. if(sourceInfo) {
  196. const context = [sourceInfo.line, value, id, lines];
  197. return new RuntimeError(LocalizedStrings.getError("matrix_line_outbounds_full", context));
  198. } else {
  199. return ProcessorErrorFactory.matrix_line_outbounds(id, value, lines);
  200. }
  201. },
  202. matrix_line_outbounds: (id, value, lines) => {
  203. const context = [value, id, lines];
  204. return new RuntimeError(LocalizedStrings.getError("matrix_line_outbounds", context));
  205. },
  206. vector_line_outbounds_full: (id, value, lines, sourceInfo) => {
  207. if(sourceInfo) {
  208. const context = [sourceInfo.line, value, id, lines];
  209. return new RuntimeError(LocalizedStrings.getError("vector_line_outbounds_full", context));
  210. } else {
  211. return ProcessorErrorFactory.vector_line_outbounds(id, value, lines);
  212. }
  213. },
  214. vector_line_outbounds: (id, value, lines) => {
  215. const context = [value, id, lines];
  216. return new RuntimeError(LocalizedStrings.getError("vector_line_outbounds", context));
  217. },
  218. vector_not_matrix_full: (id, sourceInfo) => {
  219. if(sourceInfo) {
  220. const context = [sourceInfo.line, id];
  221. return new RuntimeError(LocalizedStrings.getError("vector_not_matrix_full", context));
  222. } else {
  223. return ProcessorErrorFactory.vector_not_matrix(id);
  224. }
  225. },
  226. vector_not_matrix: (id) => {
  227. const context = [id];
  228. return new RuntimeError(LocalizedStrings.getError("vector_not_matrix", context));
  229. },
  230. function_no_return: (id) => {
  231. const context = [id];
  232. return new SemanticError(LocalizedStrings.getError("function_no_return", context));
  233. },
  234. invalid_void_return_full: (id, type, dim, sourceInfo) => {
  235. if(sourceInfo) {
  236. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  237. return new SemanticError(LocalizedStrings.getError("invalid_void_return_full", context));
  238. } else {
  239. return ProcessorErrorFactory.invalid_void_return(id, type, dim);
  240. }
  241. },
  242. invalid_void_return: (id, type, dim) => {
  243. const context = [id, LocalizedStrings.translateType(type, dim)];
  244. return new SemanticError(LocalizedStrings.getError("invalid_void_return_full", context));
  245. },
  246. invalid_return_type_full: (id, type, dim, sourceInfo) => {
  247. if(sourceInfo) {
  248. const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
  249. return new SemanticError(LocalizedStrings.getError("invalid_return_type_full", context));
  250. } else {
  251. return ProcessorErrorFactory.invalid_return_type(id, type, dim);
  252. }
  253. },
  254. invalid_return_type: (id, type, dim) => {
  255. const context = [id, LocalizedStrings.translateType(type, dim)];
  256. return new SemanticError(LocalizedStrings.getError("invalid_return_type", context));
  257. },
  258. invalid_parameters_size_full: (id, expected, actual, sourceInfo) => {
  259. if(sourceInfo) {
  260. const context = [sourceInfo.line, id, expected, actual];
  261. return new SemanticError(LocalizedStrings.getError("invalid_parameters_size_full", context));
  262. } else {
  263. return ProcessorErrorFactory.invalid_parameters_size(id, expected, actual);
  264. }
  265. },
  266. invalid_parameters_size: (id, expected, actual) => {
  267. const context = [id, expected, actual];
  268. return new SemanticError(LocalizedStrings.getError("invalid_parameters_size", context));
  269. },
  270. invalid_parameter_type_full: (fun_name, exp, sourceInfo) => {
  271. if(sourceInfo) {
  272. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  273. return new SemanticError(LocalizedStrings.getError("invalid_parameter_type_full", context));
  274. } else {
  275. return ProcessorErrorFactory.invalid_parameter_type(fun_name, exp);
  276. }
  277. },
  278. invalid_parameter_type: (fun_name, exp) => {
  279. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  280. return new SemanticError(LocalizedStrings.getError("invalid_parameter_type_full", context));
  281. },
  282. invalid_ref_full: (id, exp, sourceInfo) => {
  283. if(sourceInfo) {
  284. const context = [exp, id , sourceInfo.line];
  285. return new SemanticError(LocalizedStrings.getError("invalid_ref_full", context));
  286. } else {
  287. return ProcessorErrorFactory.invalid_ref(id, exp);
  288. }
  289. },
  290. invalid_ref: (id, exp) => {
  291. const context = [exp, id];
  292. return new SemanticError(LocalizedStrings.getError("invalid_ref", context));
  293. },
  294. unexpected_break_command_full: (sourceInfo) => {
  295. if(sourceInfo) {
  296. const context = [sourceInfo.line];
  297. return new RuntimeError(LocalizedStrings.getError("unexpected_break_command_full", context));
  298. } else {
  299. return ProcessorErrorFactory.unexpected_break_command();
  300. }
  301. },
  302. unexpected_break_command: () => {
  303. return new RuntimeError(LocalizedStrings.getError("unexpected_break_command"));
  304. },
  305. invalid_array_literal_type_full: (exp, sourceInfo) => {
  306. if(sourceInfo) {
  307. const context = [sourceInfo.line, exp];
  308. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_type_full", context));
  309. } else {
  310. return ProcessorErrorFactory.invalid_array_literal_type(exp);
  311. }
  312. },
  313. invalid_array_literal_type: (exp) => {
  314. const context = [exp];
  315. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_type", context));
  316. },
  317. invalid_array_literal_line_full: (expected, actual, sourceInfo) => {
  318. if(sourceInfo) {
  319. const context = [sourceInfo.line, expected, actual];
  320. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_line_full", context));
  321. } else {
  322. return ProcessorErrorFactory.invalid_array_literal_type(expected, actual);
  323. }
  324. },
  325. invalid_array_literal_line: (expected, actual) => {
  326. const context = [expected, actual];
  327. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_line", context));
  328. },
  329. invalid_array_literal_column_full: (expected, actual, sourceInfo) => {
  330. if(sourceInfo) {
  331. const context = [sourceInfo.line, expected, actual];
  332. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_column_full", context));
  333. } else {
  334. return ProcessorErrorFactory.invalid_array_literal_column(expected, actual);
  335. }
  336. },
  337. invalid_array_literal_column: (expected, actual) => {
  338. const context = [expected, actual];
  339. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_column", context));
  340. },
  341. invalid_unary_op_full: (expString, opName, type, dim, sourceInfo) => {
  342. if(sourceInfo) {
  343. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  344. return new RuntimeError(LocalizedStrings.getError("invalid_unary_op_full", context));
  345. } else {
  346. return ProcessorErrorFactory.invalid_unary_op(opName, type, dim);
  347. }
  348. },
  349. invalid_unary_op: (expString, opName, type, dim) => {
  350. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
  351. return new RuntimeError(LocalizedStrings.getError("invalid_unary_op", context));
  352. },
  353. invalid_infix_op_full: (expString, opName, typeLeft, dimLeft, typeRight, dimRight, sourceInfo) => {
  354. if(sourceInfo) {
  355. const context = [sourceInfo.line, expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  356. return new RuntimeError(LocalizedStrings.getError("invalid_infix_op_full", context));
  357. } else {
  358. return ProcessorErrorFactory.invalid_infix_op(opName, typeLeft, dimLeft, typeRight, dimRight);
  359. }
  360. },
  361. invalid_infix_op: (expString, opName, typeLeft, dimLeft, typeRight, dimRight) => {
  362. const context = [expString, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
  363. return new RuntimeError(LocalizedStrings.getError("invalid_infix_op", context));
  364. },
  365. array_dimension_not_positive_full: (sourceInfo) => {
  366. if(sourceInfo) {
  367. const context = [sourceInfo.line];
  368. return new SemanticError(LocalizedStrings.getError("array_dimension_not_positive_full", context));
  369. } else {
  370. return ProcessorErrorFactory.array_dimension_not_positive();
  371. }
  372. },
  373. array_dimension_not_positive: () => {
  374. return new SemanticError(LocalizedStrings.getError("array_dimension_not_positive"));
  375. },
  376. invalid_type_conversion: (value, type, dim) => {
  377. const context = [value, LocalizedStrings.translateType(type, dim)];
  378. return new RuntimeError(LocalizedStrings.getError("invalid_type_conversion", context));
  379. },
  380. invalid_read_type: (exp, type, dim, name) => {
  381. const context = [exp, LocalizedStrings.translateType(type, dim), name];
  382. return new RuntimeError(LocalizedStrings.getError("invalid_read_type", context))
  383. },
  384. invalid_read_type_array: (exp, typePos, dimPos, name, typeArray, dimArray) => {
  385. const context = [exp, LocalizedStrings.translateType(typePos, dimPos), name,LocalizedStrings.translateType(typeArray, dimArray)];
  386. return new RuntimeError(LocalizedStrings.getError("invalid_read_type_array", context))
  387. },
  388. invalid_const_ref_full: (fun_name, exp, sourceInfo)=> {
  389. if(sourceInfo) {
  390. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name), sourceInfo.line];
  391. return new SemanticError(LocalizedStrings.getError("invalid_const_ref_full", context));
  392. } else {
  393. return ProcessorErrorFactory.invalid_const_ref(fun_name, exp);
  394. }
  395. },
  396. invalid_const_ref: (fun_name, exp) => {
  397. const context = [exp, LanguageDefinedFunction.getLocalName(fun_name)];
  398. return new SemanticError(LocalizedStrings.getError("invalid_const_ref", context));
  399. },
  400. invalid_const_assignment_full: (var_id, source_info) => {
  401. if(source_info) {
  402. const context = [source_info.line, var_id];
  403. return new SemanticError(LocalizedStrings.getError("invalid_const_assignment_full", context));
  404. } else {
  405. return ProcessorErrorFactory.invalid_const_assignment(var_id);
  406. }
  407. },
  408. invalid_const_assignment: (var_id) => {
  409. const context = [var_id];
  410. return new SemanticError(LocalizedStrings.getError("invalid_const_assignment", context));
  411. }
  412. });