Improve doc comments for Parser

This commit is contained in:
Sasha Koshka 2024-02-13 13:07:37 -05:00
parent 548c8c4de1
commit 6bb5846427

View File

@ -14,7 +14,6 @@ type Parser struct {
// Expect checks the current token to see if it matches a list of token kind(s),
// else it returns an error describing what it expected.
func (this *Parser) Expect (allowed ...lexer.TokenKind) error {
// fmt.Println("expect", this.Token, allowed)
if !this.Token.Is(allowed...) {
return errors.Errorf (
this.Token.Position, "unexpected %v; expected %s",
@ -25,9 +24,8 @@ func (this *Parser) Expect (allowed ...lexer.TokenKind) error {
// ExpectDesc is like Expect, but the expected entitie(s) are described
// manually. This can be helpful when a large syntactical entity is expected and
// the first token(s) of it offer insufficient information.
// the first token(s) of it would offer useless information to the user.
func (this *Parser) ExpectDesc (description string, allowed ...lexer.TokenKind) error {
// fmt.Println("expectDesc", this.Token, description, allowed)
if !this.Token.Is(allowed...) {
return errors.Errorf (
this.Token.Position, "unexpected %v; expected %s",
@ -40,7 +38,6 @@ func (this *Parser) ExpectDesc (description string, allowed ...lexer.TokenKind)
func (this *Parser) ExpectNext (allowed ...lexer.TokenKind) error {
err := this.Next()
if err != nil { return err }
// fmt.Println("expectNext", this.Token, allowed)
return this.Expect(allowed...)
}
@ -48,14 +45,12 @@ func (this *Parser) ExpectNext (allowed ...lexer.TokenKind) error {
func (this *Parser) ExpectNextDesc (description string, allowed ...lexer.TokenKind) error {
err := this.Next()
if err != nil { return err }
// fmt.Println("expectNextDesc", this.Token, description, allowed)
return this.ExpectDesc(description, allowed...)
}
// ExpectValue returns an error if the current token's value does not match the
// allowed values.
func (this *Parser) ExpectValue (kind lexer.TokenKind, allowed ...string) error {
// fmt.Println("expectValue", this.Token, kind, allowed)
if !((this.Token.Is(kind) || kind == 0) && this.Token.ValueIs(allowed...)) {
return errors.Errorf (
this.Token.Position, "unexpected %v; expected %s",
@ -67,7 +62,6 @@ func (this *Parser) ExpectValue (kind lexer.TokenKind, allowed ...string) error
// ExpectValueDesc is like ExpectValue, but the expected value(s) are described
// manually.
func (this *Parser) ExpectValueDesc (description string, kind lexer.TokenKind, allowed ...string) error {
// fmt.Println("expectValueDesc", this.Token, description, kind, allowed)
if !this.Token.Is(kind) || !this.Token.ValueIs(allowed...) {
return errors.Errorf (
this.Token.Position, "unexpected %v; expected %s",
@ -84,26 +78,33 @@ func (this *Parser) Next () error {
return nil
}
// Kind returns the token kind of the current token.
func (this *Parser) Kind () lexer.TokenKind {
return this.Token.Kind
}
// Value returns the value of the current token.
func (this *Parser) Value () string {
return this.Token.Value
}
// Is returns whether or not the current token matches any of the given kinds.
func (this *Parser) Is (allowed ...lexer.TokenKind) bool {
return this.Token.Is(allowed...)
}
// ValueIs returns whether or not the current token matches any of the given
// values.
func (this *Parser) ValueIs (allowed ...string) bool {
return this.Token.ValueIs(allowed...)
}
// Pos returns the position of the current token.
func (this *Parser) Pos () errors.Position {
return this.Token.Position
}
// EOF returns whether or not the lexer has been exhausted.
func (this *Parser) EOF () bool {
return this.Token.EOF()
}