Use unscan rather than passing around tokens

This commit is contained in:
Elara 2021-10-04 09:26:50 -07:00
parent 567a1391b5
commit 554786792e
4 changed files with 17 additions and 8 deletions

View File

@ -7,10 +7,12 @@ import (
"go.arsenm.dev/amu/scanner" "go.arsenm.dev/amu/scanner"
) )
func (p *Parser) parseCode(tok scanner.Token, lit string) *ast.Code { func (p *Parser) parseCode() *ast.Code {
// Create new code // Create new code
code := &ast.Code{} code := &ast.Code{}
tok, lit := p.scan()
// If token is not WORD or literal is not "=list" // If token is not WORD or literal is not "=list"
if tok != scanner.WORD || lit != "=code" { if tok != scanner.WORD || lit != "=code" {
// Return nil as this code is invalid // Return nil as this code is invalid

View File

@ -6,10 +6,12 @@ import (
) )
// parseList attempts to parse a list // parseList attempts to parse a list
func (p *Parser) parseList(tok scanner.Token, lit string) *ast.List { func (p *Parser) parseList() *ast.List {
// Create new list // Create new list
list := &ast.List{} list := &ast.List{}
tok, lit := p.scan()
// If token is not WORD or literal is not "=list" // If token is not WORD or literal is not "=list"
if tok != scanner.WORD || lit != "=list" { if tok != scanner.WORD || lit != "=list" {
// Return nil as this list is invalid // Return nil as this list is invalid

View File

@ -39,8 +39,9 @@ parseLoop:
para.Fragments = append(para.Fragments, ast.ParaFragment{Punct: &lit}) para.Fragments = append(para.Fragments, ast.ParaFragment{Punct: &lit})
case scanner.WORD: case scanner.WORD:
if strings.HasPrefix(lit, "+") { if strings.HasPrefix(lit, "+") {
p.unscan()
// Attempt to parse function // Attempt to parse function
function := p.parseFunc(tok, lit) function := p.parseFunc()
// If successful // If successful
if function != nil { if function != nil {
// Add function to para // Add function to para
@ -113,10 +114,12 @@ parseLoop:
} }
// parseFunc appempts to parse a function call // parseFunc appempts to parse a function call
func (p *Parser) parseFunc(tok scanner.Token, lit string) *ast.Func { func (p *Parser) parseFunc() *ast.Func {
// Create new function // Create new function
function := &ast.Func{} function := &ast.Func{}
tok, lit := p.scan()
// If the token is not a word or does not have a prefix of "+" // If the token is not a word or does not have a prefix of "+"
if tok != scanner.WORD || !strings.HasPrefix(lit, "+") { if tok != scanner.WORD || !strings.HasPrefix(lit, "+") {
// Return nil as this is an invalid function call // Return nil as this is an invalid function call

View File

@ -144,8 +144,9 @@ parseLoop:
continue continue
} }
} else if tok == scanner.WORD && lit == "=list" { } else if tok == scanner.WORD && lit == "=list" {
p.unscan()
// Attempt to parse list // Attempt to parse list
list := p.parseList(tok, lit) list := p.parseList()
// If successful // If successful
if list != nil { if list != nil {
// Add list to AST // Add list to AST
@ -154,8 +155,9 @@ parseLoop:
continue continue
} }
} else if tok == scanner.WORD && lit == "=code" { } else if tok == scanner.WORD && lit == "=code" {
p.unscan()
// Attempt to parse code // Attempt to parse code
code := p.parseCode(tok, lit) code := p.parseCode()
// If successful // If successful
if code != nil { if code != nil {
// Add code to AST // Add code to AST