formal-language/grammar Home Manual Reference Source

src/grammar/expandobject.js

  1. import {list} from '@iterable-iterator/list';
  2.  
  3. import _expandproduction from './_expandproduction.js';
  4.  
  5. /**
  6. * @param {Record<string,any>} productions
  7. * @return {IterableIterator<[any,Map<any,any>]>}
  8. */
  9. function* _expandproductions(productions) {
  10. for (const key in productions) {
  11. if (Object.prototype.hasOwnProperty.call(productions, key)) {
  12. yield [key, list(_expandproduction(productions[key]))];
  13. }
  14. }
  15. }
  16.  
  17. /**
  18. * @param {object} object
  19. * @return {IterableIterator<[any,Map<any,any>]>}
  20. */
  21. function* _expandobject(object) {
  22. for (const nonterminal in object) {
  23. if (Object.prototype.hasOwnProperty.call(object, nonterminal)) {
  24. yield [nonterminal, new Map(_expandproductions(object[nonterminal]))];
  25. }
  26. }
  27. }
  28.  
  29. export default function expandobject(object) {
  30. return new Map(_expandobject(object));
  31. }