Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
f3b628c404 | |||
ec0425ec60 |
@ -1,3 +1,5 @@
|
||||
# goparse
|
||||
|
||||
[![Go Reference](https://pkg.go.dev/badge/git.tebibyte.media/sashakoshka/goparse.svg)](https://pkg.go.dev/git.tebibyte.media/sashakoshka/goparse)
|
||||
|
||||
Flexible and unopinionated parsing framework.
|
||||
|
37
parser.go
37
parser.go
@ -8,6 +8,10 @@ type Parser struct {
|
||||
// be set before use.
|
||||
Lexer Lexer
|
||||
|
||||
// TokenNames assigns names to token kinds. This should be set before
|
||||
// use.
|
||||
TokenNames map[TokenKind] string
|
||||
|
||||
// Token is the current token the parser is operating on.
|
||||
Token Token
|
||||
}
|
||||
@ -17,8 +21,8 @@ type Parser struct {
|
||||
func (this *Parser) Expect (allowed ...TokenKind) error {
|
||||
if !this.Token.Is(allowed...) {
|
||||
return Errorf (
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, commaList(allowed...))
|
||||
this.Token.Position, "unexpected %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), commaList(this.tokenNames(allowed...)...))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -29,8 +33,8 @@ func (this *Parser) Expect (allowed ...TokenKind) error {
|
||||
func (this *Parser) ExpectDesc (description string, allowed ...TokenKind) error {
|
||||
if !this.Token.Is(allowed...) {
|
||||
return Errorf (
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, description)
|
||||
this.Token.Position, "unexpected %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), description)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -54,8 +58,8 @@ func (this *Parser) ExpectNextDesc (description string, allowed ...TokenKind) er
|
||||
func (this *Parser) ExpectValue (kind TokenKind, allowed ...string) error {
|
||||
if !((this.Token.Is(kind) || kind == 0) && this.Token.ValueIs(allowed...)) {
|
||||
return Errorf (
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, commaList(allowed))
|
||||
this.Token.Position, "unexpected %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), commaList(allowed))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -65,8 +69,8 @@ func (this *Parser) ExpectValue (kind TokenKind, allowed ...string) error {
|
||||
func (this *Parser) ExpectValueDesc (description string, kind TokenKind, allowed ...string) error {
|
||||
if !this.Token.Is(kind) || !this.Token.ValueIs(allowed...) {
|
||||
return Errorf (
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, description)
|
||||
this.Token.Position, "unexpected %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), description)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -110,6 +114,23 @@ func (this *Parser) EOF () bool {
|
||||
return this.Token.EOF()
|
||||
}
|
||||
|
||||
func (this *Parser) tokenName (kind TokenKind) string {
|
||||
if this.TokenNames != nil {
|
||||
if name, ok := this.TokenNames[kind]; ok {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("TokenKind(%d)", kind)
|
||||
}
|
||||
|
||||
func (this *Parser) tokenNames (kinds ...TokenKind) []string {
|
||||
names := make([]string, len(kinds))
|
||||
for index, kind := range kinds {
|
||||
names[index] = this.tokenName(kind)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func commaList[ELEMENT any] (items ...ELEMENT) string {
|
||||
list := ""
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user