semantic_annotations.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { SemanticAnnotator } from './semantic_annotator.js';
  2. export const annotators = new Map();
  3. export const visitors = new Map();
  4. export function register(annotator) {
  5. const name = annotator.domain + ':' + annotator.name;
  6. annotator instanceof SemanticAnnotator
  7. ? annotators.set(name, annotator)
  8. : visitors.set(name, annotator);
  9. }
  10. export function activate(domain, name) {
  11. const key = domain + ':' + name;
  12. const annotator = annotators.get(key) || visitors.get(key);
  13. if (annotator) {
  14. annotator.active = true;
  15. }
  16. }
  17. export function deactivate(domain, name) {
  18. const key = domain + ':' + name;
  19. const annotator = annotators.get(key) || visitors.get(key);
  20. if (annotator) {
  21. annotator.active = false;
  22. }
  23. }
  24. export function annotate(node) {
  25. for (const annotator of annotators.values()) {
  26. if (annotator.active) {
  27. annotator.annotate(node);
  28. }
  29. }
  30. for (const visitor of visitors.values()) {
  31. if (visitor.active) {
  32. visitor.visit(node, Object.assign({}, visitor.def));
  33. }
  34. }
  35. }