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.

parser.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const AST = require('./ast')
  2. const Error = require('./Error')
  3. const tokenTypes = require('./tokenTypes')
  4. module.exports = class Parser {
  5. parse(tokenStream) {
  6. this.tokenStream = tokenStream
  7. let tree = []
  8. while (this.tokenStream.peek().type !== tokenTypes.EOF) {
  9. let expr = this.expr()
  10. tree.push(expr)
  11. if (this.tokenStream.error) {
  12. return {
  13. error: this.tokenStream.error,
  14. }
  15. }
  16. }
  17. return tree
  18. }
  19. expr() {
  20. let token = this.tokenStream.peek()
  21. switch (token.type) {
  22. case tokenTypes.ATTRIBUTE:
  23. return this.attribute()
  24. case tokenTypes.BOOLEAN:
  25. return this.bool()
  26. case tokenTypes.IDENTIFIER:
  27. return this.identifier()
  28. case tokenTypes.NUMBER:
  29. return this.number()
  30. case tokenTypes.QUOTE:
  31. return this.string()
  32. case tokenTypes.SYMBOL:
  33. return this.symbol()
  34. case tokenTypes.OPAREN:
  35. return this.form()
  36. default:
  37. this.tokenStream.error = `Unexpected ${token.type} on line ${
  38. token.line
  39. }`
  40. break
  41. }
  42. }
  43. form() {
  44. this.tokenStream.eat(tokenTypes.OPAREN)
  45. let node
  46. if (this.tokenStream.peek().value === 'lambda') {
  47. this.tokenStream.eat(tokenTypes.IDENTIFIER)
  48. node = this.lambda()
  49. } else if (this.tokenStream.peek().value === 'define') {
  50. this.tokenStream.eat(tokenTypes.IDENTIFIER)
  51. node = this.definition()
  52. } else if (this.tokenStream.peek().value === 'if') {
  53. this.tokenStream.eat(tokenTypes.IDENTIFIER)
  54. node = this.conditional()
  55. } else {
  56. node = new AST.Application()
  57. node.function = this.expr()
  58. node.args = []
  59. while (this.tokenStream.peek().type !== tokenTypes.CPAREN) {
  60. node.args.push(this.expr())
  61. }
  62. }
  63. this.tokenStream.eat(tokenTypes.CPAREN)
  64. return node
  65. }
  66. attribute() {
  67. return new AST.Attribute({
  68. name: this.tokenStream.eat(tokenTypes.ATTRIBUTE).value,
  69. value: this.expr(),
  70. })
  71. }
  72. bool() {
  73. return new AST.Boolean({
  74. value: this.tokenStream.eat(tokenTypes.BOOLEAN).value,
  75. })
  76. }
  77. conditional() {
  78. return new AST.Conditional({
  79. condition: this.expr(),
  80. ifCase: this.expr(),
  81. elseCase: this.expr(),
  82. })
  83. }
  84. definition() {
  85. return new AST.Definition({
  86. symbol: this.identifier(),
  87. value: this.expr(),
  88. })
  89. }
  90. identifier() {
  91. return new AST.Identifier({
  92. name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value,
  93. })
  94. }
  95. lambda() {
  96. let parameters = []
  97. this.tokenStream.eat(tokenTypes.OPAREN)
  98. while (this.tokenStream.peek().type !== tokenTypes.CPAREN) {
  99. parameters.push(
  100. new AST.Identifier({
  101. name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value,
  102. }),
  103. )
  104. }
  105. this.tokenStream.eat(tokenTypes.CPAREN)
  106. return new AST.Lambda({
  107. parameters: parameters,
  108. body: this.form(),
  109. })
  110. }
  111. }
  112. number() {
  113. return new AST.Number({
  114. value: this.tokenStream.eat(tokenTypes.NUMBER).value,
  115. })
  116. }
  117. string() {
  118. this.tokenStream.eat(tokenTypes.QUOTE)
  119. let value = ''
  120. if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
  121. value = this.tokenStream.eat(tokenTypes.LITERAL).value
  122. }
  123. let node = new AST.String({
  124. value: value,
  125. })
  126. this.tokenStream.eat(tokenTypes.QUOTE)
  127. return node
  128. }
  129. symbol() {
  130. return new AST.Symbol({
  131. value: this.tokenStream.eat(tokenTypes.SYMBOL).value,
  132. })
  133. }
  134. }