parser.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package parser
  2. import (
  3. "fmt"
  4. "code.osinet.fr/fgm/waiig15/ast"
  5. "code.osinet.fr/fgm/waiig15/lexer"
  6. "code.osinet.fr/fgm/waiig15/token"
  7. )
  8. // Precedence constants.
  9. const (
  10. _ int = iota
  11. LOWEST
  12. EQUALS // ==
  13. LESSGREATER // > or <
  14. SUM // +
  15. PRODUCT // *
  16. PREFIX // -X or !X
  17. CALL // myFunction(X)
  18. )
  19. // Parser implements the parsing mechanism top-level layer.
  20. type Parser struct {
  21. errors []string
  22. l *lexer.Lexer
  23. curToken token.Token
  24. peekToken token.Token
  25. prefixParseFns map[token.TokenType]prefixParseFn
  26. infixParseFns map[token.TokenType]infixParseFn
  27. }
  28. type (
  29. prefixParseFn func() ast.Expression
  30. infixParseFn func(ast.Expression) ast.Expression
  31. )
  32. // New returns a new Parser instance with the first two parser tokens already
  33. // loaded.
  34. func New(l *lexer.Lexer) *Parser {
  35. p := &Parser{
  36. l: l,
  37. errors: []string{},
  38. }
  39. p.prefixParseFns = make(map[token.TokenType]prefixParseFn)
  40. p.registerPrefix(token.IDENT, p.parseIdentifier)
  41. // Read two tokens, so curToken and peeToken are both set.
  42. p.nextToken()
  43. p.nextToken()
  44. return p
  45. }
  46. // Errors is a getter for Parser.errors.
  47. func (p *Parser) Errors() []string {
  48. return p.errors
  49. }
  50. // ParseProgram is the outermost parsing logic, accumulating statements in a
  51. // Program instance and returning that instance once parsing is done.
  52. func (p *Parser) ParseProgram() *ast.Program {
  53. program := &ast.Program{
  54. Statements: []ast.Statement{},
  55. }
  56. for !p.curTokenIs(token.EOF) {
  57. stmt := p.parseStatement()
  58. if stmt != nil {
  59. program.Statements = append(program.Statements, stmt)
  60. }
  61. p.nextToken()
  62. }
  63. return program
  64. }
  65. // Is the current token in the parser of the given type ?
  66. func (p *Parser) curTokenIs(t token.TokenType) bool {
  67. return p.curToken.Type == t
  68. }
  69. // Is the next token in the parser of the given type ? If it is, consume it,
  70. // else don't.
  71. func (p *Parser) expectPeek(t token.TokenType) bool {
  72. if p.peekTokenIs(t) {
  73. p.nextToken()
  74. return true
  75. }
  76. p.peekError(t)
  77. return false
  78. }
  79. func (p *Parser) nextToken() {
  80. p.curToken = p.peekToken
  81. p.peekToken = p.l.NextToken()
  82. }
  83. func (p *Parser) parseStatement() ast.Statement {
  84. switch p.curToken.Type {
  85. case token.LET:
  86. return p.parseLetStatement()
  87. case token.RETURN:
  88. return p.parseReturnStatement()
  89. default:
  90. return p.parseExpressionStatement()
  91. }
  92. }
  93. // Log a mismatch error on the peek token type in the parser instance.
  94. //
  95. // - t is the type of token that was expected
  96. func (p *Parser) peekError(t token.TokenType) {
  97. msg := fmt.Sprintf("expected next token to be %s, got %s instead",
  98. t, p.peekToken.Type)
  99. p.errors = append(p.errors, msg)
  100. }
  101. // Is the next token in the parser of the given type ? Don't consume it.
  102. func (p *Parser) peekTokenIs(t token.TokenType) bool {
  103. return p.peekToken.Type == t
  104. }