A templating language that looks like Lisp and compiles to HTML
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

parser.js 2.9KB

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