A templating language that looks like Lisp and compiles to HTML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parserTest.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const test = require("tape");
  2. const helpers = require("./helpers");
  3. const AST = require("../src/ast/index");
  4. const tt = require("../src/tokenTypes");
  5. test("parses token stream into a tree", t => {
  6. t.plan(1);
  7. const tree = helpers.parse(`
  8. (div :class "foobar"
  9. (p :class (cond #t "primary" "secondary")))
  10. `);
  11. t.deepEqual(tree, [
  12. new AST.Application({
  13. functionName: new AST.Identifier({ name: "div" }),
  14. args: [
  15. new AST.Attribute({
  16. name: "class",
  17. value: new AST.String({ value: "foobar" })
  18. }),
  19. new AST.Application({
  20. functionName: new AST.Identifier({ name: "p" }),
  21. args: [
  22. new AST.Attribute({
  23. name: "class",
  24. value: new AST.Application({
  25. functionName: new AST.Identifier({ name: "cond" }),
  26. args: [
  27. new AST.Boolean({ value: true }),
  28. new AST.String({ value: "primary" }),
  29. new AST.String({ value: "secondary" })
  30. ]
  31. })
  32. })
  33. ]
  34. })
  35. ]
  36. })
  37. ]);
  38. });
  39. test("allow empty strings", t => {
  40. t.plan(1);
  41. const tree = helpers.parse('(p "")');
  42. t.deepEqual(tree, [
  43. new AST.Application({
  44. functionName: new AST.Identifier({ name: "p" }),
  45. args: [new AST.String({ value: "" })]
  46. })
  47. ]);
  48. });
  49. test("parses calls to define into a function definition", t => {
  50. t.plan(1);
  51. const tree = helpers.parse("(define plusOne (n) (+ n 1))");
  52. t.deepEqual(tree, [
  53. new AST.FunctionDefinition({
  54. name: new AST.Identifier({ name: "plusOne" }),
  55. parameters: [new AST.Identifier({ name: "n" })],
  56. body: new AST.Application({
  57. functionName: new AST.Identifier({ name: "+" }),
  58. args: [new AST.Identifier({ name: "n" }), new AST.Number({ value: 1 })]
  59. })
  60. })
  61. ]);
  62. });