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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 === 'if') {
  50. this.tokenStream.eat(tokenTypes.IDENTIFIER)
  51. node = this.conditional()
  52. } else {
  53. node = new AST.Application()
  54. node.function = this.expr()
  55. node.args = []
  56. while (this.tokenStream.peek().type !== tokenTypes.CPAREN) {
  57. node.args.push(this.expr())
  58. }
  59. }
  60. this.tokenStream.eat(tokenTypes.CPAREN)
  61. return node
  62. }
  63. attribute() {
  64. return new AST.Attribute({
  65. name: this.tokenStream.eat(tokenTypes.ATTRIBUTE).value,
  66. value: this.expr(),
  67. })
  68. }
  69. bool() {
  70. return new AST.Boolean({
  71. value: this.tokenStream.eat(tokenTypes.BOOLEAN).value,
  72. })
  73. }
  74. conditional() {
  75. return new AST.Conditional({
  76. condition: this.expr(),
  77. ifCase: this.expr(),
  78. elseCase: this.expr(),
  79. })
  80. }
  81. lambda() {
  82. let parameters = []
  83. this.tokenStream.eat(tokenTypes.OPAREN)
  84. while (this.tokenStream.peek().type !== tokenTypes.CPAREN) {
  85. parameters.push(
  86. new AST.Identifier({
  87. name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value,
  88. }),
  89. )
  90. }
  91. this.tokenStream.eat(tokenTypes.CPAREN)
  92. return new AST.Lambda({
  93. parameters: parameters,
  94. body: this.form(),
  95. })
  96. }
  97. identifier() {
  98. return new AST.Identifier({
  99. name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value,
  100. })
  101. }
  102. number() {
  103. return new AST.Number({
  104. value: this.tokenStream.eat(tokenTypes.NUMBER).value,
  105. })
  106. }
  107. string() {
  108. this.tokenStream.eat(tokenTypes.QUOTE)
  109. let value = ''
  110. if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
  111. value = this.tokenStream.eat(tokenTypes.LITERAL).value
  112. }
  113. let node = new AST.String({
  114. value: value,
  115. })
  116. this.tokenStream.eat(tokenTypes.QUOTE)
  117. return node
  118. }
  119. symbol() {
  120. return new AST.Symbol({
  121. value: this.tokenStream.eat(tokenTypes.SYMBOL).value,
  122. })
  123. }
  124. }