1
0

processorErrorFactory.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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: (sourceInfo) => {
  102. if(sourceInfo) {
  103. const context = [sourceInfo.line, sourceInfo.column];
  104. return new SemanticError(LocalizedStrings.getError("loop_condition_type_full", context));
  105. } else {
  106. return ProcessorErrorFactory.loop_condition_type();
  107. }
  108. },
  109. loop_condition_type: () => {
  110. return new SemanticError(LocalizedStrings.getError("loop_condition_type"));
  111. },
  112. endless_loop_full: (sourceInfo) => {
  113. if(sourceInfo) {
  114. const context = [sourceInfo.line];
  115. return new SemanticError(LocalizedStrings.getError("endless_loop_full", context));
  116. } else {
  117. return ProcessorErrorFactory.endless_loop();
  118. }
  119. },
  120. endless_loop: () => {
  121. return new SemanticError(LocalizedStrings.getError("endless_loop"));
  122. },
  123. for_condition_type_full: (sourceInfo) => {
  124. if(sourceInfo) {
  125. const context = [sourceInfo.line, sourceInfo.column];
  126. return new SemanticError(LocalizedStrings.getError("for_condition_type_full", context));
  127. } else {
  128. return ProcessorErrorFactory.for_condition_type();
  129. }
  130. },
  131. for_condition_type: () => {
  132. return new SemanticError(LocalizedStrings.getError("for_condition_type"));
  133. },
  134. if_condition_type_full: (sourceInfo) => {
  135. if(sourceInfo) {
  136. const context = [sourceInfo.line, sourceInfo.column];
  137. return new SemanticError(LocalizedStrings.getError("if_condition_type_full", context));
  138. } else {
  139. return ProcessorErrorFactory.if_condition_type();
  140. }
  141. },
  142. if_condition_type: () => {
  143. return new SemanticError(LocalizedStrings.getError("if_condition_type"));
  144. },
  145. invalid_global_var: () => {
  146. return new RuntimeError(LocalizedStrings.getError("invalid_global_var"))
  147. },
  148. not_implemented: (id) => {
  149. const context = [id]
  150. return new RuntimeError(LocalizedStrings.getError("not_implemented", context))
  151. },
  152. invalid_case_type_full: (exp, type, dim, sourceInfo) => {
  153. if(sourceInfo) {
  154. const context = [exp, translateType(type, dim), sourceInfo.line, sourceInfo.column];
  155. return new SemanticError(LocalizedStrings.getError("invalid_case_type_full", context));
  156. } else {
  157. return ProcessorErrorFactory.invalid_case_type(exp, type, dim);
  158. }
  159. },
  160. invalid_case_type: (exp, type, dim) => {
  161. const context = [exp, translateType(type, dim)];
  162. return new SemanticError(LocalizedStrings.getError("invalid_case_type", context));
  163. },
  164. void_in_expression_full: (id, sourceInfo) => {
  165. if(sourceInfo) {
  166. const context = [sourceInfo.line, sourceInfo.column, id];
  167. return new SemanticError(LocalizedStrings.getError("void_in_expression_full", context));
  168. } else {
  169. return ProcessorErrorFactory.void_in_expression(id);
  170. }
  171. },
  172. void_in_expression: (id) => {
  173. const context = [id];
  174. return new SemanticError(LocalizedStrings.getError("void_in_expression", context));
  175. },
  176. invalid_array_access_full: (id, sourceInfo) => {
  177. if(sourceInfo) {
  178. const context = [id, sourceInfo.line, sourceInfo.column];
  179. return new SemanticError(LocalizedStrings.getError("invalid_array_access_full", context));
  180. } else {
  181. return ProcessorErrorFactory.invalid_array_access(id);
  182. }
  183. },
  184. invalid_array_access: (id) => {
  185. const context = [id];
  186. return new SemanticError(LocalizedStrings.getError("invalid_array_access", context));
  187. },
  188. invalid_matrix_access_full: (id, sourceInfo) => {
  189. if(sourceInfo) {
  190. const context = [id, sourceInfo.line, sourceInfo.column];
  191. return new SemanticError(LocalizedStrings.getError("invalid_matrix_access_full", context));
  192. } else {
  193. return ProcessorErrorFactory.invalid_matrix_access(id);
  194. }
  195. },
  196. invalid_matrix_access: (id) => {
  197. const context = [id];
  198. return new SemanticError(LocalizedStrings.getError("invalid_matrix_access", context));
  199. },
  200. matrix_column_outbounds_full: (id, value, columns, sourceInfo) => {
  201. if(sourceInfo) {
  202. const context = [sourceInfo.line, value, id, columns];
  203. return new RuntimeError(LocalizedStrings.getError("matrix_column_outbounds_full", context));
  204. } else {
  205. return ProcessorErrorFactory.matrix_column_outbounds(id, value, columns);
  206. }
  207. },
  208. matrix_column_outbounds: (id, value, columns) => {
  209. const context = [value, id, columns];
  210. return new RuntimeError(LocalizedStrings.getError("matrix_column_outbounds", context));
  211. },
  212. matrix_line_outbounds_full: (id, value, lines, sourceInfo) => {
  213. if(sourceInfo) {
  214. const context = [sourceInfo.line, value, id, lines];
  215. return new RuntimeError(LocalizedStrings.getError("matrix_line_outbounds_full", context));
  216. } else {
  217. return ProcessorErrorFactory.matrix_line_outbounds(id, value, lines);
  218. }
  219. },
  220. matrix_line_outbounds: (id, value, lines) => {
  221. const context = [value, id, lines];
  222. return new RuntimeError(LocalizedStrings.getError("matrix_line_outbounds", context));
  223. },
  224. vector_line_outbounds_full: (id, value, lines, sourceInfo) => {
  225. if(sourceInfo) {
  226. const context = [sourceInfo.line, value, id, lines];
  227. return new RuntimeError(LocalizedStrings.getError("vector_line_outbounds_full", context));
  228. } else {
  229. return ProcessorErrorFactory.vector_line_outbounds(id, value, lines);
  230. }
  231. },
  232. vector_line_outbounds: (id, value, lines) => {
  233. const context = [value, id, lines];
  234. return new RuntimeError(LocalizedStrings.getError("vector_line_outbounds", context));
  235. },
  236. vector_not_matrix_full: (id, sourceInfo) => {
  237. if(sourceInfo) {
  238. const context = [sourceInfo.line, id];
  239. return new RuntimeError(LocalizedStrings.getError("vector_not_matrix_full", context));
  240. } else {
  241. return ProcessorErrorFactory.vector_not_matrix(id);
  242. }
  243. },
  244. vector_not_matrix: (id) => {
  245. const context = [id];
  246. return new RuntimeError(LocalizedStrings.getError("vector_not_matrix", context));
  247. },
  248. function_no_return: (id) => {
  249. const context = [id];
  250. return new SemanticError(LocalizedStrings.getError("function_no_return", context));
  251. },
  252. invalid_void_return_full: (id, type, dim, sourceInfo) => {
  253. if(sourceInfo) {
  254. const context = [sourceInfo.line, id, translateType(type, dim)];
  255. return new SemanticError(LocalizedStrings.getError("invalid_void_return_full", context));
  256. } else {
  257. return ProcessorErrorFactory.invalid_void_return(id, type, dim);
  258. }
  259. },
  260. invalid_void_return: (id, type, dim) => {
  261. const context = [id, translateType(type, dim)];
  262. return new SemanticError(LocalizedStrings.getError("invalid_void_return_full", context));
  263. },
  264. invalid_return_type_full: (id, type, dim, sourceInfo) => {
  265. if(sourceInfo) {
  266. const context = [sourceInfo.line, id, translateType(type, dim)];
  267. return new SemanticError(LocalizedStrings.getError("invalid_return_type_full", context));
  268. } else {
  269. return ProcessorErrorFactory.invalid_return_type(id, type, dim);
  270. }
  271. },
  272. invalid_return_type: (id, type, dim) => {
  273. const context = [id, translateType(type, dim)];
  274. return new SemanticError(LocalizedStrings.getError("invalid_return_type", context));
  275. },
  276. invalid_parameters_size_full: (id, expected, actual, sourceInfo) => {
  277. if(sourceInfo) {
  278. const context = [sourceInfo.line, id, expected, actual];
  279. return new SemanticError(LocalizedStrings.getError("invalid_parameters_size_full", context));
  280. } else {
  281. return ProcessorErrorFactory.invalid_parameters_size(id, expected, actual);
  282. }
  283. },
  284. invalid_parameters_size: (id, expected, actual) => {
  285. const context = [id, expected, actual];
  286. return new SemanticError(LocalizedStrings.getError("invalid_parameters_size", context));
  287. },
  288. invalid_parameter_type_full: (id, exp, sourceInfo) => {
  289. if(sourceInfo) {
  290. const context = [exp, id, sourceInfo.line];
  291. return new SemanticError(LocalizedStrings.getError("invalid_parameter_type_full", context));
  292. } else {
  293. return ProcessorErrorFactory.invalid_parameter_type(id, exp);
  294. }
  295. },
  296. invalid_parameter_type: (id, exp) => {
  297. const context = [exp, id];
  298. return new SemanticError(LocalizedStrings.getError("invalid_parameter_type_full", context));
  299. },
  300. invalid_ref_full: (id, exp, sourceInfo) => {
  301. if(sourceInfo) {
  302. const context = [exp, id , sourceInfo.line];
  303. return new SemanticError(LocalizedStrings.getError("invalid_ref_full", context));
  304. } else {
  305. return ProcessorErrorFactory.invalid_ref(id, exp);
  306. }
  307. },
  308. invalid_ref: (id, exp) => {
  309. const context = [exp, id];
  310. return new SemanticError(LocalizedStrings.getError("invalid_ref", context));
  311. },
  312. unexpected_break_command_full: (sourceInfo) => {
  313. if(sourceInfo) {
  314. const context = [sourceInfo.line];
  315. return new RuntimeError(LocalizedStrings.getError("unexpected_break_command_full", context));
  316. } else {
  317. return ProcessorErrorFactory.unexpected_break_command();
  318. }
  319. },
  320. unexpected_break_command: () => {
  321. return new RuntimeError(LocalizedStrings.getError("unexpected_break_command"));
  322. },
  323. invalid_array_literal_type_full: (exp, sourceInfo) => {
  324. if(sourceInfo) {
  325. const context = [sourceInfo.line, exp];
  326. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_type_full", context));
  327. } else {
  328. return ProcessorErrorFactory.invalid_array_literal_type(exp);
  329. }
  330. },
  331. invalid_array_literal_type: (exp) => {
  332. const context = [exp];
  333. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_type", context));
  334. },
  335. invalid_array_literal_line_full: (expected, actual, sourceInfo) => {
  336. if(sourceInfo) {
  337. const context = [sourceInfo.line, expected, actual];
  338. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_line_full", context));
  339. } else {
  340. return ProcessorErrorFactory.invalid_array_literal_type(expected, actual);
  341. }
  342. },
  343. invalid_array_literal_line: (expected, actual) => {
  344. const context = [expected, actual];
  345. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_line", context));
  346. },
  347. invalid_array_literal_column_full: (expected, actual, sourceInfo) => {
  348. if(sourceInfo) {
  349. const context = [sourceInfo.line, expected, actual];
  350. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_column_full", context));
  351. } else {
  352. return ProcessorErrorFactory.invalid_array_literal_column(expected, actual);
  353. }
  354. },
  355. invalid_array_literal_column: (expected, actual) => {
  356. const context = [expected, actual];
  357. return new RuntimeError(LocalizedStrings.getError("invalid_array_literal_column", context));
  358. },
  359. invalid_unary_op_full: (opName, type, dim, sourceInfo) => {
  360. if(sourceInfo) {
  361. const context = [sourceInfo.line, translateOp(opName), translateType(type, dim)];
  362. return new RuntimeError(LocalizedStrings.getError("invalid_unary_op_full", context));
  363. } else {
  364. return ProcessorErrorFactory.invalid_unary_op(opName, type, dim);
  365. }
  366. },
  367. invalid_unary_op: (opName, type, dim) => {
  368. const context = [translateOp(opName), translateType(type, dim)];
  369. return new RuntimeError(LocalizedStrings.getError("invalid_unary_op", context));
  370. },
  371. invalid_infix_op_full: (opName, typeLeft, dimLeft, typeRight, dimRight, sourceInfo) => {
  372. if(sourceInfo) {
  373. const context = [sourceInfo.line, translateOp(opName), translateType(typeLeft, dimLeft), translateType(typeRight, dimRight)];
  374. return new RuntimeError(LocalizedStrings.getError("invalid_infix_op_full", context));
  375. } else {
  376. return ProcessorErrorFactory.invalid_infix_op(opName, typeLeft, dimLeft, typeRight, dimRight);
  377. }
  378. },
  379. invalid_infix_op: (opName, typeLeft, dimLeft, typeRight, dimRight) => {
  380. const context = [translateOp(opName), translateType(typeLeft, dimLeft), translateType(typeRight, dimRight)];
  381. return new RuntimeError(LocalizedStrings.getError("invalid_infix_op", context));
  382. },
  383. array_dimension_not_positive_full: (sourceInfo) => {
  384. if(sourceInfo) {
  385. const context = [sourceInfo.line];
  386. return new SemanticError(LocalizedStrings.getError("array_dimension_not_positive_full", context));
  387. } else {
  388. return ProcessorErrorFactory.array_dimension_not_positive();
  389. }
  390. },
  391. array_dimension_not_positive: () => {
  392. return new SemanticError(LocalizedStrings.getError("array_dimension_not_positive"));
  393. }
  394. });