|
| 1 | +package parser |
| 2 | + |
| 3 | +// Package parser implements the parser that takes as input tokens from the |
| 4 | +// lexer and produces as output an AST (Abstract Syntax Tree). |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/cedrickchee/hou/ast" |
| 8 | + "github.com/cedrickchee/hou/lexer" |
| 9 | + "github.com/cedrickchee/hou/token" |
| 10 | +) |
| 11 | + |
| 12 | +// Parser implements the parser. |
| 13 | +type Parser struct { |
| 14 | + l *lexer.Lexer |
| 15 | + |
| 16 | + curToken token.Token |
| 17 | + peekToken token.Token |
| 18 | +} |
| 19 | + |
| 20 | +// New constructs a new Parser with a Lexer as input. |
| 21 | +func New(l *lexer.Lexer) *Parser { |
| 22 | + p := &Parser{l: l} |
| 23 | + |
| 24 | + // Read two tokens, so curToken and peekToken are both set. |
| 25 | + p.nextToken() |
| 26 | + p.nextToken() |
| 27 | + |
| 28 | + return p |
| 29 | +} |
| 30 | + |
| 31 | +// Helper method that advances both curToken and peekToken. |
| 32 | +func (p *Parser) nextToken() { |
| 33 | + p.curToken = p.peekToken |
| 34 | + p.peekToken = p.l.NextToken() |
| 35 | +} |
| 36 | + |
| 37 | +// ParseProgram starts the parsing process and is the entry point for all other |
| 38 | +// sub-parsers that are responsible for other nodes in the AST. |
| 39 | +func (p *Parser) ParseProgram() *ast.Program { |
| 40 | + // Construct the root node of the AST. |
| 41 | + program := &ast.Program{} |
| 42 | + program.Statements = []ast.Statement{} |
| 43 | + |
| 44 | + // Iterate over every token in the input until it encounters an token.EOF |
| 45 | + // token. |
| 46 | + for p.curToken.Type != token.EOF { |
| 47 | + stmt := p.parseStatement() |
| 48 | + if stmt != nil { |
| 49 | + program.Statements = append(program.Statements, stmt) |
| 50 | + } |
| 51 | + p.nextToken() |
| 52 | + } |
| 53 | + return program |
| 54 | +} |
| 55 | + |
| 56 | +// Parse a statement. |
| 57 | +func (p *Parser) parseStatement() ast.Statement { |
| 58 | + switch p.curToken.Type { |
| 59 | + case token.LET: |
| 60 | + return p.parseLetStatement() |
| 61 | + default: |
| 62 | + return nil |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (p *Parser) parseLetStatement() *ast.LetStatement { |
| 67 | + // Constructs an *ast.LetStatement node with the token it’s currently |
| 68 | + // sitting on (a token.LET token). |
| 69 | + stmt := &ast.LetStatement{Token: p.curToken} |
| 70 | + |
| 71 | + // Advances the tokens while making assertions about the next token. |
| 72 | + if !p.expectPeek(token.IDENT) { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + // Use token.IDENT token to construct an *ast.Identifier node. |
| 77 | + stmt.Name = &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal} |
| 78 | + |
| 79 | + // Expects an equal sign and jumps over the expression following the |
| 80 | + // equal sign. |
| 81 | + if !p.expectPeek(token.ASSIGN) { |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: We're skipping the expressions until we |
| 86 | + // encounter a semicolon |
| 87 | + for !p.curTokenIs(token.SEMICOLON) { |
| 88 | + p.nextToken() |
| 89 | + } |
| 90 | + |
| 91 | + return stmt |
| 92 | +} |
| 93 | + |
| 94 | +// "assertion functions". |
| 95 | +// Enforce the correctness of the order of tokens by checking the type of the |
| 96 | +// next token. |
| 97 | +func (p *Parser) expectPeek(t token.TokenType) bool { |
| 98 | + if p.peekTokenIs(t) { |
| 99 | + p.nextToken() |
| 100 | + return true |
| 101 | + } else { |
| 102 | + return false |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (p *Parser) peekTokenIs(t token.TokenType) bool { |
| 107 | + return p.peekToken.Type == t |
| 108 | +} |
| 109 | + |
| 110 | +func (p *Parser) curTokenIs(t token.TokenType) bool { |
| 111 | + return p.curToken.Type == t |
| 112 | +} |
0 commit comments