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 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const Error = require('./Error')
  2. const AST = require('./ast')
  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. tree.push(this.expr())
  12. if (this.tokenStream.error) {
  13. return {
  14. error: this.tokenStream.error,
  15. }
  16. }
  17. }
  18. return tree
  19. }
  20. expr() {
  21. let token = this.tokenStream.peek()
  22. switch (token.type) {
  23. case tokenTypes.ATTRIBUTE:
  24. return this.attribute()
  25. case tokenTypes.BOOLEAN:
  26. return this.bool()
  27. case tokenTypes.IDENTIFIER:
  28. return this.identifier()
  29. case tokenTypes.NUMBER:
  30. return this.number()
  31. case tokenTypes.QUOTE:
  32. return this.string()
  33. case tokenTypes.SYMBOL:
  34. return this.symbol()
  35. case tokenTypes.OPAREN:
  36. return this.form()
  37. default:
  38. this.tokenStream.error = `Unexpected ${token.type} on line ${token.line}`
  39. break;
  40. }
  41. }
  42. form() {
  43. this.tokenStream.eat(tokenTypes.OPAREN)
  44. let node = new AST.Application()
  45. node.functionName = this.identifier()
  46. node.args = []
  47. while (this.tokenStream.peek().type !== tokenTypes.CPAREN) {
  48. node.args.push(this.expr())
  49. }
  50. this.tokenStream.eat(tokenTypes.CPAREN)
  51. return node
  52. }
  53. attribute() {
  54. return new AST.Attribute({
  55. name: this.tokenStream.eat(tokenTypes.ATTRIBUTE).value,
  56. value: this.expr()
  57. })
  58. }
  59. bool() {
  60. return new AST.Boolean({
  61. value: this.tokenStream.eat(tokenTypes.BOOLEAN).value
  62. })
  63. }
  64. identifier() {
  65. return new AST.Identifier({
  66. name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value
  67. })
  68. }
  69. number() {
  70. return new AST.Number({
  71. value: this.tokenStream.eat(tokenTypes.NUMBER).value
  72. })
  73. }
  74. string() {
  75. this.tokenStream.eat(tokenTypes.QUOTE)
  76. let node = new AST.String({
  77. value: this.tokenStream.eat(tokenTypes.LITERAL).value
  78. })
  79. this.tokenStream.eat(tokenTypes.QUOTE)
  80. return node
  81. }
  82. symbol() {
  83. return new AST.Symbol({
  84. value: this.tokenStream.eat(tokenTypes.SYMBOL).value
  85. })
  86. }
  87. }