parser.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.BANG, p.parsePrefixExpression)
  41. p.registerPrefix(token.IDENT, p.parseIdentifier)
  42. p.registerPrefix(token.INT, p.parseIntegerLiteral)
  43. p.registerPrefix(token.MINUS, p.parsePrefixExpression)
  44. // Read two tokens, so curToken and peeToken are both set.
  45. p.nextToken()
  46. p.nextToken()
  47. return p
  48. }
  49. // Errors is a getter for Parser.errors.
  50. func (p *Parser) Errors() []string {
  51. return p.errors
  52. }
  53. // ParseProgram is the outermost parsing logic, accumulating statements in a
  54. // Program instance and returning that instance once parsing is done.
  55. func (p *Parser) ParseProgram() *ast.Program {
  56. program := &ast.Program{
  57. Statements: []ast.Statement{},
  58. }
  59. for !p.curTokenIs(token.EOF) {
  60. stmt := p.parseStatement()
  61. if stmt != nil {
  62. program.Statements = append(program.Statements, stmt)
  63. }
  64. p.nextToken()
  65. }
  66. return program
  67. }
  68. // Is the current token in the parser of the given type ?
  69. func (p *Parser) curTokenIs(t token.TokenType) bool {
  70. return p.curToken.Type == t
  71. }
  72. // Is the next token in the parser of the given type ? If it is, consume it,
  73. // else don't.
  74. func (p *Parser) expectPeek(t token.TokenType) bool {
  75. if p.peekTokenIs(t) {
  76. p.nextToken()
  77. return true
  78. }
  79. p.peekError(t)
  80. return false
  81. }
  82. func (p *Parser) nextToken() {
  83. p.curToken = p.peekToken
  84. p.peekToken = p.l.NextToken()
  85. }
  86. func (p *Parser) parseStatement() ast.Statement {
  87. switch p.curToken.Type {
  88. case token.LET:
  89. return p.parseLetStatement()
  90. case token.RETURN:
  91. return p.parseReturnStatement()
  92. default:
  93. return p.parseExpressionStatement()
  94. }
  95. }
  96. // Log a mismatch error on the peek token type in the parser instance.
  97. //
  98. // - t is the type of token that was expected
  99. func (p *Parser) peekError(t token.TokenType) {
  100. msg := fmt.Sprintf("expected next token to be %s, got %s instead",
  101. t, p.peekToken.Type)
  102. p.errors = append(p.errors, msg)
  103. }
  104. // Is the next token in the parser of the given type ? Don't consume it.
  105. func (p *Parser) peekTokenIs(t token.TokenType) bool {
  106. return p.peekToken.Type == t
  107. }