Skip to content

Commit

Permalink
breaking up parser file into smaller ones
Browse files Browse the repository at this point in the history
  • Loading branch information
54L1M committed Jan 28, 2024
1 parent 7abb78c commit 941b047
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
37 changes: 2 additions & 35 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package parser

import (
"fmt"

"github.com/54L1m/mil-lang/ast"
"github.com/54L1m/mil-lang/lexer"
"github.com/54L1m/mil-lang/token"
Expand All @@ -28,21 +26,6 @@ func New(l *lexer.Lexer) *Parser {
return p
}

func (p *Parser) Errors() []string {
return p.errors
}

func (p *Parser) peekError(t token.TokenType) {
msg := fmt.Sprintf("expected next token to be %s, got %s instead",
t, p.peekToken.Type)
p.errors = append(p.errors, msg)
}

func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.l.NextToken()
}

func (p *Parser) ParseProgram() *ast.Program {
program := &ast.Program{}
program.Statements = []ast.Statement{}
Expand All @@ -57,6 +40,7 @@ func (p *Parser) ParseProgram() *ast.Program {
return program
}

// parsing statements
func (p *Parser) parseStatement() ast.Statement {
switch p.curToken.Type {
case token.VAR:
Expand All @@ -67,6 +51,7 @@ func (p *Parser) parseStatement() ast.Statement {
}
}

// parsing var statements
func (p *Parser) parseVarStatement() *ast.VarStatement {
stmt := &ast.VarStatement{Token: p.curToken}

Expand All @@ -88,21 +73,3 @@ func (p *Parser) parseVarStatement() *ast.VarStatement {

return stmt
}

func (p *Parser) curTokenIs(t token.TokenType) bool {
return p.curToken.Type == t
}

func (p *Parser) peekTokenIs(t token.TokenType) bool {
return p.peekToken.Type == t
}

func (p *Parser) expectPeek(t token.TokenType) bool {
if p.peekTokenIs(t) {
p.nextToken()
return true
} else {
p.peekError(t)
return false
}
}
17 changes: 17 additions & 0 deletions parser/parser_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package parser

import (
"fmt"

"github.com/54L1m/mil-lang/token"
)

func (p *Parser) Errors() []string {
return p.errors
}

func (p *Parser) peekError(t token.TokenType) {
msg := fmt.Sprintf("expected next token to be %s, got %s instead",
t, p.peekToken.Type)
p.errors = append(p.errors, msg)
}
26 changes: 26 additions & 0 deletions parser/parser_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package parser

import "github.com/54L1m/mil-lang/token"

func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.l.NextToken()
}

func (p *Parser) curTokenIs(t token.TokenType) bool {
return p.curToken.Type == t
}

func (p *Parser) peekTokenIs(t token.TokenType) bool {
return p.peekToken.Type == t
}

func (p *Parser) expectPeek(t token.TokenType) bool {
if p.peekTokenIs(t) {
p.nextToken()
return true
} else {
p.peekError(t)
return false
}
}

0 comments on commit 941b047

Please sign in to comment.