syntaxErrorFactory.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import * as LocalizedStringsService from "./../../services/localizedStringsService";
  2. import { SyntaxError } from "./syntaxError";
  3. const LocalizedStrings = LocalizedStringsService.getInstance();
  4. function createError (message_id, context = []) {
  5. return new SyntaxError(
  6. LocalizedStrings.getError(message_id, context),
  7. message_id
  8. );
  9. }
  10. export const SyntaxErrorFactory = Object.freeze({
  11. extra_lines: () => new SyntaxError(LocalizedStrings.getError("extra_lines")),
  12. token_missing_one: (expected, token) => {
  13. const context = [expected, token.text, token.line, token.col];
  14. const error = createError("token_missing_one", context);
  15. error.context = { line: token.line, column: token.col};
  16. return error;
  17. },
  18. token_missing_list: (expectedList, token) => {
  19. const line = expectedList.join(LocalizedStrings.getOR());
  20. const error = SyntaxErrorFactory.token_missing_one(line, token);
  21. error.context = { line: token.line, column: token.col};
  22. return error;
  23. },
  24. id_missing: (token) => {
  25. const context = [token.text, token.line, token.col];
  26. const error = createError("id_missing", context);
  27. error.context = { line: token.line, column: token.col};
  28. return error;
  29. },
  30. eos_missing: (token) => {
  31. const context = [token.line, token.col];
  32. const error = createError("eos_missing", context);
  33. error.context = { line: token.line, column: token.col};
  34. return error;
  35. },
  36. invalid_array_dimension: (typeName, token) => {
  37. const context = [token.line, token.col, typeName];
  38. const error = createError("invalid_array_dimension", context);
  39. error.context = { line: token.line, column: token.col};
  40. return error;
  41. },
  42. invalid_array_size: (token) => {
  43. const context = [token.line];
  44. const error = createError("invalid_array_size", context);
  45. error.context = { line: token.line, column: token.col};
  46. return error;
  47. },
  48. invalid_main_return: (name, typeName, token) => {
  49. const context = [name, typeName, token.line];
  50. const error = createError("invalid_main_return", context);
  51. error.context = { line: token.line, column: token.col};
  52. return error;
  53. },
  54. invalid_var_declaration: (token) => {
  55. const context = [token.line];
  56. const error = createError("invalid_var_declaration", context);
  57. error.context = { line: token.line, column: token.col};
  58. return error;
  59. },
  60. invalid_break_command: (cmdName, token) => {
  61. const context = [token.line, cmdName];
  62. const error = createError("invalid_break_command", context);
  63. error.context = { line: token.line, column: token.col};
  64. return error;
  65. },
  66. invalid_terminal: (token) => {
  67. const context = [token.text, token.line, token.col];
  68. const error = createError("invalid_terminal", context);
  69. error.context = { line: token.line, column: token.col};
  70. return error;
  71. },
  72. invalid_type: (list, token) => {
  73. const line = list.join(LocalizedStrings.getOR());
  74. const context = [token.text, token.line, token.col, line];
  75. const error = createError("invalid_type", context);
  76. error.context = { line: token.line, column: token.col};
  77. return error;
  78. },
  79. const_not_init: (token) => {
  80. const context = [token.line, token.col];
  81. const error = createError("const_not_init", context);
  82. error.context = { line: token.line, column: token.col};
  83. return error;
  84. },
  85. invalid_id_format: (token) => {
  86. const context = [token.text, token.line, token.col];
  87. const error = createError("invalid_id_format", context);
  88. error.context = { line: token.line, column: token.col};
  89. return error;
  90. },
  91. duplicate_function: (token) => {
  92. const context = [token.text, token.line, token.col];
  93. const error = createError("duplicate_function", context);
  94. error.context = { line: token.line, column: token.col};
  95. return error;
  96. },
  97. main_parameters: () => {
  98. return createError("main_parameters");
  99. },
  100. duplicate_variable: (token) => {
  101. const context = [token.text, token.line, token.col];
  102. const error = createError("duplicate_variable", context);
  103. error.context = { line: token.line, column: token.col};
  104. return error;
  105. },
  106. invalid_character: (text, line, column) => {
  107. const context = [text, line];
  108. const error = createError("invalid_character", context);
  109. error.context = { line: line, column: column };
  110. return error;
  111. },
  112. annonymous_array_literal: (token) => {
  113. const context = [token.line];
  114. const error = createError("annonymous_array_literal", context);
  115. error.context = { line: token.line, column: token.col};
  116. return error;
  117. },
  118. invalid_matrix_literal_line: (exp, sourceInfo) => {
  119. const context = [exp, sourceInfo.line];
  120. const error = createError("invalid_matrix_literal_line", context);
  121. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  122. return error;
  123. },
  124. cannot_infer_matrix_line: (name, sourceInfo) => {
  125. const context = [name, sourceInfo.line];
  126. const error = createError("cannot_infer_matrix_line", context);
  127. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  128. return error;
  129. },
  130. cannot_infer_matrix_column: (name, sourceInfo) => {
  131. const context = [name, sourceInfo.line];
  132. const error = createError("cannot_infer_matrix_column", context);
  133. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  134. return error;
  135. },
  136. cannot_infer_vector_size: (name, sourceInfo) => {
  137. const context = [name, sourceInfo.line];
  138. const error = createError("cannot_infer_vector_size", context);
  139. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  140. return error;
  141. },
  142. matrix_to_vector_literal_attr: (name, exp, sourceInfo) => {
  143. const context = [sourceInfo.line, exp, name];
  144. const error = createError("matrix_to_vector_literal_attr", context);
  145. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  146. return error;
  147. },
  148. vector_to_matrix_literal_attr: (name, exp, sourceInfo) => {
  149. const context = [sourceInfo.line, exp, name];
  150. const error = createError("vector_to_matrix_literal_attr", context);
  151. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  152. return error;
  153. },
  154. array_init_not_literal: (sourceInfo) => {
  155. const context = [sourceInfo.line];
  156. const error = createError("array_init_not_literal", context);
  157. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  158. return error;
  159. },
  160. array_exceeds_2d: (sourceInfo) => {
  161. const context = [sourceInfo.line];
  162. const error = createError("array_exceeds_2d", context);
  163. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  164. return error;
  165. },
  166. invalid_matrix_id_dimension: (sourceInfo) => {
  167. const context = [sourceInfo.line];
  168. const error = createError("invalid_matrix_id_dimension", context);
  169. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  170. return error;
  171. },
  172. invalid_vector_init: (sourceInfo) => {
  173. const context = [sourceInfo.line];
  174. const error = createError("invalid_vector_init", context);
  175. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  176. return error;
  177. },
  178. invalid_matrix_init: (sourceInfo) => {
  179. const context = [sourceInfo.line];
  180. const error = createError("invalid_matrix_init", context);
  181. error.context = { line: sourceInfo.line, column: sourceInfo.column };
  182. return error;
  183. },
  184. invalid_syntax: (text, line, column) => {
  185. const context = [text, line];
  186. const error = createError("invalid_syntax", context);
  187. error.context = { line: line, column: column };
  188. return error;
  189. }
  190. });