StrUntilParserImpl.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. export default class StrUntilParserImpl {
  20. constructor ({ stopChars }) {
  21. this.stopChars = stopChars;
  22. }
  23. parse (lexer) {
  24. let text = '';
  25. for ( ;; ) {
  26. let { done, value } = lexer.look();
  27. if ( done ) break;
  28. // TODO: doing this strictly one byte at a time
  29. // doesn't allow multi-byte stop characters
  30. if ( typeof value === 'number' ) value =
  31. String.fromCharCode(value);
  32. if ( this.stopChars.includes(value) ) break;
  33. text += value;
  34. lexer.next();
  35. }
  36. if ( text.length === 0 ) return;
  37. return { $: 'until', text };
  38. }
  39. }