whitespace.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 WhitespaceParserImpl {
  20. static meta = {
  21. inputs: 'bytes',
  22. outputs: 'node'
  23. }
  24. static data = {
  25. whitespaceCharCodes: ' \r\t'.split('')
  26. .map(chr => chr.charCodeAt(0))
  27. }
  28. parse (lexer) {
  29. const { whitespaceCharCodes } = this.constructor.data;
  30. let text = '';
  31. for ( ;; ) {
  32. const { done, value } = lexer.look();
  33. if ( done ) break;
  34. if ( ! whitespaceCharCodes.includes(value) ) break;
  35. text += String.fromCharCode(value);
  36. lexer.next();
  37. }
  38. if ( text.length === 0 ) return;
  39. return { $: 'whitespace', text };
  40. }
  41. }