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

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