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"
)
func (p *Parser) parseCode(tok scanner.Token, lit string) *ast.Code {
func (p *Parser) parseCode() *ast.Code {
// Create new code
code := &ast.Code{}
tok, lit := p.scan()
// If token is not WORD or literal is not "=list"
if tok != scanner.WORD || lit != "=code" {
// Return nil as this code is invalid

View File

@ -6,10 +6,12 @@ import (
)
// 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
list := &ast.List{}
tok, lit := p.scan()
// If token is not WORD or literal is not "=list"
if tok != scanner.WORD || lit != "=list" {
// Return nil as this list is invalid

View File

@ -39,8 +39,9 @@ parseLoop:
para.Fragments = append(para.Fragments, ast.ParaFragment{Punct: &lit})
case scanner.WORD:
if strings.HasPrefix(lit, "+") {
p.unscan()
// Attempt to parse function
function := p.parseFunc(tok, lit)
function := p.parseFunc()
// If successful
if function != nil {
// Add function to para
@ -55,7 +56,7 @@ parseLoop:
// Create new nil slice of ast.FormatType
var types []ast.FormatType
if strings.HasPrefix(lit, "_") {
// Remove leading and trailing "_"
// Remove leading and trailing "_"
lit = strings.Trim(lit, "_")
// Add italic format to slice
types = append(types, ast.FormatTypeItalic)
@ -113,10 +114,12 @@ parseLoop:
}
// 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
function := &ast.Func{}
tok, lit := p.scan()
// If the token is not a word or does not have a prefix of "+"
if tok != scanner.WORD || !strings.HasPrefix(lit, "+") {
// Return nil as this is an invalid function call
@ -174,7 +177,7 @@ parseLoop:
// Write word to current buffer
currentBuf.WriteString(lit)
case scanner.PUNCT:
// If closing bracket found but no text stored
// If closing bracket found but no text stored
if lit == "]" && currentBuf.Len() == 0 {
// Unscan token
p.unscan()

View File

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