SymbolParserImpl.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // Here for safe-keeping - wasn't correct for shell tokens but
  20. // it will be needed later for variable assignments
  21. export class SymbolParserImpl {
  22. static meta = {
  23. inputs: 'bytes',
  24. outputs: 'node'
  25. }
  26. static data = {
  27. rexp0: /[A-Za-z_]/,
  28. rexpN: /[A-Za-z0-9_]/,
  29. }
  30. parse (lexer) {
  31. let { done, value } = lexer.look();
  32. if ( done ) return;
  33. const { rexp0, rexpN } = this.constructor.data;
  34. value = String.fromCharCode(value);
  35. if ( ! rexp0.test(value) ) return;
  36. let text = '' + value;
  37. lexer.next();
  38. for ( ;; ) {
  39. ({ done, value } = lexer.look());
  40. if ( done ) break;
  41. value = String.fromCharCode(value);
  42. if ( ! rexpN.test(value) ) break;
  43. text += value;
  44. lexer.next();
  45. }
  46. return { $: 'symbol', text };
  47. }
  48. }