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 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. function: 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. function: new AST.Identifier({ name: 'p' }),
  21. args: [
  22. new AST.Attribute({
  23. name: 'class',
  24. value: new AST.Application({
  25. function: 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. function: new AST.Identifier({ name: 'p' }),
  45. args: [new AST.String({ value: '' })],
  46. }),
  47. ])
  48. })
  49. test('parse lambdas and expressions in function position', t => {
  50. t.plan(1)
  51. const tree = helpers.parse('((lambda (n) (+ n 1)) 5)')
  52. t.deepEqual(tree, [
  53. new AST.Application({
  54. function: new AST.Lambda({
  55. parameters: [new AST.Identifier({ name: 'n' })],
  56. body: new AST.Application({
  57. function: new AST.Identifier({ name: '+' }),
  58. args: [
  59. new AST.Identifier({ name: 'n' }),
  60. new AST.Number({ value: 1 }),
  61. ],
  62. }),
  63. }),
  64. args: [new AST.Number({ value: 5 })],
  65. }),
  66. ])
  67. })
  68. test('parse conditionals', t => {
  69. t.plan(1)
  70. const tree = helpers.parse('(if #t (do this) (do that))')
  71. t.deepEqual(tree, [
  72. new AST.Conditional({
  73. condition: new AST.Boolean({ value: true }),
  74. ifCase: new AST.Application({
  75. function: new AST.Identifier({ name: 'do' }),
  76. args: [
  77. new AST.Identifier({ name: 'this' })
  78. ]
  79. }),
  80. elseCase: new AST.Application({
  81. function: new AST.Identifier({ name: 'do' }),
  82. args: [
  83. new AST.Identifier({ name: 'that' })
  84. ]
  85. }),
  86. })
  87. ])
  88. })