ContextSwitchingPStratumImpl.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import { AcceptParserUtil, ParseResult, Parser } from "../parse.js";
  20. export default class ContextSwitchingPStratumImpl {
  21. constructor ({ contexts, entry }) {
  22. this.contexts = { ...contexts };
  23. for ( const key in this.contexts ) {
  24. const new_array = [];
  25. for ( const parser of this.contexts[key] ) {
  26. if ( parser.hasOwnProperty('transition') ) {
  27. new_array.push({
  28. ...parser,
  29. parser: AcceptParserUtil.adapt(parser.parser),
  30. })
  31. } else {
  32. new_array.push(AcceptParserUtil.adapt(parser));
  33. }
  34. }
  35. this.contexts[key] = new_array;
  36. }
  37. this.stack = [{
  38. context_name: entry,
  39. }];
  40. this.valid = true;
  41. this.lastvalue = null;
  42. }
  43. get stack_top () {
  44. return this.stack[this.stack.length - 1];
  45. }
  46. get current_context () {
  47. return this.contexts[this.stack_top.context_name];
  48. }
  49. next (api) {
  50. if ( ! this.valid ) return { done: true };
  51. const lexer = api.delegate;
  52. const context = this.current_context;
  53. for ( const spec of context ) {
  54. {
  55. const { done, value } = lexer.look();
  56. this.anti_cycle_i = value === this.lastvalue ? (this.anti_cycle_i || 0) + 1 : 0;
  57. if ( this.anti_cycle_i > 30 ) {
  58. throw new Error('infinite loop');
  59. }
  60. this.lastvalue = value;
  61. if ( done ) return { done };
  62. }
  63. let parser, transition, peek;
  64. if ( spec.hasOwnProperty('parser') ) {
  65. ({ parser, transition, peek } = spec);
  66. } else {
  67. parser = spec;
  68. }
  69. const subLexer = lexer.fork();
  70. const result = parser.parse(subLexer);
  71. if ( result.status === ParseResult.UNRECOGNIZED ) {
  72. continue;
  73. }
  74. if ( result.status === ParseResult.INVALID ) {
  75. return { done: true, value: result };
  76. }
  77. if ( ! peek ) lexer.join(subLexer);
  78. if ( transition ) {
  79. if ( transition.pop ) this.stack.pop();
  80. if ( transition.to ) this.stack.push({
  81. context_name: transition.to,
  82. });
  83. }
  84. if ( result.value.$discard || peek ) return this.next(api);
  85. return { done: false, value: result.value };
  86. }
  87. return { done: true, value: 'ran out of parsers' };
  88. }
  89. }