processorErrorFactory.js 16 KB

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