a5638bb897
Signed-off-by: Alejandro Mery <amery@jpi.io>
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package ini
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"asciigoat.org/core/lexer"
|
|
"asciigoat.org/ini/parser"
|
|
)
|
|
|
|
type token struct {
|
|
pos lexer.Position
|
|
typ parser.TokenType
|
|
value string
|
|
}
|
|
|
|
func (t token) String() string {
|
|
return fmt.Sprintf("%s %s: %q", t.pos, t.typ, t.value)
|
|
}
|
|
|
|
// typeOK tells if a token of the specified type is acceptable
|
|
// at this time.
|
|
func (dec *Decoder[T]) typeOK(typ parser.TokenType) bool
|
|
|
|
// execute is called after each acceptable token is appended to the queue
|
|
func (dec *Decoder[T]) execute() error
|
|
|
|
// executeFinal is called after an error
|
|
func (dec *Decoder[T]) executeFinal()
|
|
|
|
// parserOnToken is the callback from the parser
|
|
func (dec *Decoder[T]) parserOnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
|
var err error
|
|
|
|
log.Printf("%s: %s %s %q", "ini", pos, typ, value)
|
|
|
|
t := &token{pos, typ, value}
|
|
switch {
|
|
case typ == parser.TokenComment:
|
|
// ignore comments
|
|
case dec.typeOK(typ):
|
|
// acceptable token
|
|
dec.queue = append(dec.queue, t)
|
|
err = dec.execute()
|
|
default:
|
|
// unacceptable
|
|
err = dec.newErrInvalidToken(t)
|
|
}
|
|
|
|
if err != nil {
|
|
dec.executeFinal()
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|