syntaxErrorFactory.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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(LocalizedStrings.getError(message_id, context));
  6. }
  7. export const SyntaxErrorFactory = Object.freeze({
  8. extra_lines: () => new SyntaxError(LocalizedStrings.getError("extra_lines")),
  9. token_missing_one: (expected, token) => {
  10. const context = [expected, token.text, token.line, token.column];
  11. return createError("token_missing_one", context);
  12. },
  13. token_missing_list: (expectedList, token) => {
  14. const line = expectedList.join(LocalizedStrings.getOR());
  15. return SyntaxErrorFactory.token_missing_one(line, token);
  16. },
  17. id_missing: (token) => {
  18. const context = [token.text, token.line, token.column];
  19. return createError("id_missing", context);
  20. },
  21. eos_missing: (token) => {
  22. const context = [token.line, token.column];
  23. return createError("eos_missing", context);
  24. },
  25. invalid_array_dimension: (typeName, token) => {
  26. const context = [token.line, token.column, typeName];
  27. return createError("invalid_array_dimension", context);
  28. },
  29. invalid_array_size: (token) => {
  30. const context = [token.line];
  31. return createError("invalid_array_size", context);
  32. },
  33. invalid_main_return: (name, typeName, token) => {
  34. const context = [name, typeName, token.line];
  35. return createError("invalid_main_return", context);
  36. },
  37. invalid_var_declaration: (token) => {
  38. const context = [token.line];
  39. return createError("invalid_var_declaration", context);
  40. },
  41. invalid_break_command: (cmdName, token) => {
  42. const context = [token.line, cmdName];
  43. return createError("invalid_break_command", context);
  44. },
  45. invalid_terminal: (token) => {
  46. const context = [token.text, token.line, token.column];
  47. return createError('invalid_terminal', context);
  48. },
  49. invalid_type: (list, token) => {
  50. const line = list.join(LocalizedStrings.getOR());
  51. const context = [token.text, token.line, token.column, line]
  52. return createError("invalid_type", context);
  53. },
  54. const_not_init: (token) => {
  55. const context = [token.line, token.column];
  56. return createError("const_not_init", context);
  57. },
  58. invalid_id_format: (token) => {
  59. const context = [token.text, token.line, token.column];
  60. return createError("invalid_id_format", context);
  61. },
  62. duplicate_function: (token) => {
  63. const context = [token.text, token.line, token.column];
  64. return createError("duplicate_function", context);
  65. },
  66. main_parameters: () => {
  67. return createError("main_parameters");
  68. },
  69. duplicate_variable: (token) => {
  70. const context = [token.text, token.line, token.column];
  71. return createError("duplicate_variable", context);
  72. },
  73. invalid_character: (text, line, _column) => {
  74. const context = [text, line];
  75. return createError("invalid_character", context);
  76. },
  77. annonymous_array_literal: (token) => {
  78. const context = [token.line];
  79. return createError("annonymous_array_literal", context);
  80. },
  81. invalid_matrix_literal_line: (exp, sourceInfo) => {
  82. const context = [exp, sourceInfo.line];
  83. return createError("invalid_matrix_literal_line", context);
  84. },
  85. cannot_infer_matrix_line: (name, sourceInfo) => {
  86. const context = [name, sourceInfo.line];
  87. return createError("cannot_infer_matrix_line", context);
  88. },
  89. cannot_infer_matrix_column: (name, sourceInfo) => {
  90. const context = [name, sourceInfo.line];
  91. return createError("cannot_infer_matrix_column", context);
  92. },
  93. cannot_infer_vector_size: (name, sourceInfo) => {
  94. const context = [name, sourceInfo.line];
  95. return createError("cannot_infer_vector_size", context);
  96. },
  97. matrix_to_vector_literal_attr: (name, exp, sourceInfo) => {
  98. const context = [sourceInfo.line, exp, name];
  99. return createError("matrix_to_vector_literal_attr", context);
  100. },
  101. vector_to_matrix_literal_attr: (name, exp, sourceInfo) => {
  102. const context = [sourceInfo.line, exp, name];
  103. return createError("vector_to_matrix_literal_attr", context);
  104. },
  105. array_init_not_literal: (sourceInfo) => {
  106. const context = [sourceInfo.line];
  107. return createError("array_init_not_literal", context);
  108. },
  109. array_exceeds_2d: (sourceInfo) => {
  110. const context = [sourceInfo.line];
  111. return createError("array_exceeds_2d", context);
  112. },
  113. invalid_matrix_id_dimension: (sourceInfo) => {
  114. const context = [sourceInfo.line];
  115. return createError("invalid_matrix_id_dimension", context);
  116. },
  117. invalid_vector_init: (sourceInfo) => {
  118. const context = [sourceInfo.line];
  119. return createError("invalid_vector_init", context);
  120. },
  121. invalid_matrix_init: (sourceInfo) => {
  122. const context = [sourceInfo.line];
  123. return createError("invalid_matrix_init", context);
  124. }
  125. });