A templating language that looks like Lisp and compiles to HTML
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

parser.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. lambda() {
  91. let parameters = []
  92. this.tokenStream.eat(tokenTypes.OPAREN)
  93. while (this.tokenStream.peek().type !== tokenTypes.CPAREN) {
  94. parameters.push(
  95. new AST.Identifier({
  96. name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value,
  97. }),
  98. )
  99. }
  100. this.tokenStream.eat(tokenTypes.CPAREN)
  101. return new AST.Lambda({
  102. parameters: parameters,
  103. body: this.form(),
  104. })
  105. }
  106. identifier() {
  107. return new AST.Identifier({
  108. name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value,
  109. })
  110. }
  111. number() {
  112. return new AST.Number({
  113. value: this.tokenStream.eat(tokenTypes.NUMBER).value,
  114. })
  115. }
  116. string() {
  117. this.tokenStream.eat(tokenTypes.QUOTE)
  118. let value = ''
  119. if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
  120. value = this.tokenStream.eat(tokenTypes.LITERAL).value
  121. }
  122. let node = new AST.String({
  123. value: value,
  124. })
  125. this.tokenStream.eat(tokenTypes.QUOTE)
  126. return node
  127. }
  128. symbol() {
  129. return new AST.Symbol({
  130. value: this.tokenStream.eat(tokenTypes.SYMBOL).value,
  131. })
  132. }
  133. }