semantic_ordering.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.reduce = reduce;
  4. const semantic_meaning_js_1 = require("./semantic_meaning.js");
  5. const comparators = [];
  6. function add(comparator) {
  7. comparators.push(comparator);
  8. }
  9. function apply(meaning1, meaning2) {
  10. for (let i = 0, comparator; (comparator = comparators[i]); i++) {
  11. const result = comparator.compare(meaning1, meaning2);
  12. if (result !== 0) {
  13. return result;
  14. }
  15. }
  16. return 0;
  17. }
  18. function sort(meanings) {
  19. meanings.sort(apply);
  20. }
  21. function reduce(meanings) {
  22. if (meanings.length <= 1) {
  23. return meanings;
  24. }
  25. const copy = meanings.slice();
  26. sort(copy);
  27. const result = [];
  28. let last;
  29. do {
  30. last = copy.pop();
  31. result.push(last);
  32. } while (last && copy.length && apply(copy[copy.length - 1], last) === 0);
  33. return result;
  34. }
  35. class SemanticComparator {
  36. constructor(comparator, type = null) {
  37. this.comparator = comparator;
  38. this.type = type;
  39. add(this);
  40. }
  41. compare(meaning1, meaning2) {
  42. return this.type &&
  43. this.type === meaning1.type &&
  44. this.type === meaning2.type
  45. ? this.comparator(meaning1, meaning2)
  46. : 0;
  47. }
  48. }
  49. function simpleFunction(meaning1, meaning2) {
  50. if (meaning1.role === semantic_meaning_js_1.SemanticRole.SIMPLEFUNC) {
  51. return 1;
  52. }
  53. if (meaning2.role === semantic_meaning_js_1.SemanticRole.SIMPLEFUNC) {
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. new SemanticComparator(simpleFunction, semantic_meaning_js_1.SemanticType.IDENTIFIER);