decoder [WIP]

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-09-01 22:43:36 +00:00
parent 652f7a7aa0
commit 8557ef9a7e
3 changed files with 161 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
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
}