Compare commits
No commits in common. "main" and "v0.1.0" have entirely different histories.
@ -1,5 +1,3 @@
|
||||
# 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,10 +8,6 @@ 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
|
||||
}
|
||||
@ -21,8 +17,8 @@ type Parser struct {
|
||||
func (this *Parser) Expect (allowed ...TokenKind) error {
|
||||
if !this.Token.Is(allowed...) {
|
||||
return Errorf (
|
||||
this.Token.Position, "unexpected %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), commaList(this.tokenNames(allowed...)...))
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, commaList(allowed...))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -33,8 +29,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 %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), description)
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, description)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -58,8 +54,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 %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), commaList(allowed))
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, commaList(allowed))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -69,8 +65,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 %s; expected %s",
|
||||
this.tokenName(this.Token.Kind), description)
|
||||
this.Token.Position, "unexpected %v; expected %s",
|
||||
this.Token, description)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -114,23 +110,6 @@ 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