Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. const {
  2. char,
  3. mapTo,
  4. pipeParsers,
  5. parse,
  6. sequenceOf,
  7. str,
  8. letters,
  9. choice,
  10. possibly,
  11. sepBy,
  12. many,
  13. many1,
  14. anythingExcept,
  15. digits,
  16. whitespace,
  17. between,
  18. anyOfString,
  19. recursiveParser
  20. } = require('arcsecond');
  21.  
  22. const tabspace = many (anyOfString (' \t')) .map (x => x.join(''));
  23. const whitespaceSurrounded = parser => between (whitespace) (whitespace) (parser);
  24. const tabspaceSurrounded = parser => between (tabspace) (tabspace) (parser);
  25. const spaceSurrounded = parser => between (char (' ')) (char (' ')) (parser);
  26.  
  27. const parseValue = recursiveParser(() => choice ([
  28. parseWord,
  29. parseBlock,
  30. ]));
  31.  
  32. const parseWord = pipeParsers([
  33. sequenceOf ([
  34. letters
  35. ]),
  36. mapTo(x => x.join())
  37. ]);
  38.  
  39. const lineSeparated = sepBy (tabspaceSurrounded (char ('\n')));
  40. const semicolonSeparated = sepBy (tabspaceSurrounded (char (';')));
  41.  
  42. const parseBlock =
  43. pipeParsers ([
  44. between
  45. (whitespaceSurrounded (char ('(')))
  46. (whitespaceSurrounded (char (')')))
  47. (lineSeparated (
  48. semicolonSeparated (
  49. parseValue
  50. )
  51. )),
  52. mapTo (function(x) {
  53. console.log('array: ' + x);
  54. return x;
  55. })
  56. ]);
  57.  
  58. let result = parse (parseValue) (`(
  59. a;b
  60. (
  61. c;d
  62. ))`)
  63. console.log('result.value: ' + JSON.stringify(result.value, null, 2));
  64.  
  65. if (false) {
  66. result = parse (parseValue) (`(
  67. a;b e
  68. (c;
  69. d
  70. ))`)
  71. console.log('result.value: ' + JSON.stringify(result.value, null, 2));
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement