Compare commits

..

9 Commits

Author SHA1 Message Date
Alejandro Mery 6df387ffeb chore: update dependencies 1 year ago
Alejandro Mery 123770119e parser: TextParser [WIP] 1 year ago
Alejandro Mery 667803fea5 reflective: IsZero() 1 year ago
Alejandro Mery a38f0c74e8 reflective: Add Reflection{} placeholder and the New() factory 1 year ago
Alejandro Mery 7e0b7cfb47 reflective: introduce UnmarshalTypeError{} 1 year ago
Alejandro Mery fd4f6439b8 reflective: introduce InvalidUnmarshalError{} 1 year ago
Alejandro Mery f74745471d reflective: add placeholder for reflection assistant 1 year ago
Alejandro Mery 2b5dcec64d lexer: add Hint to Error, which is expanded as "%s" instead of "%q" 1 year ago
Alejandro Mery 4aa6233e4f lexer: refactor Error.Error()'s prefix generator 1 year ago
  1. 20
      lexer/position.go
  2. 2
      parser/parser.go
  3. 72
      parser/text.go
  4. 31
      parser/token.go
  5. 2
      reflective/docs.go
  6. 69
      reflective/error.go
  7. 15
      reflective/helpers.go
  8. 27
      reflective/reflection.go

20
lexer/position.go

@ -64,23 +64,3 @@ func (p *Position) StepLine() {
p.Line++
p.Column = 1
}
// Add adds a relative position considering
// potential new lines
func (p *Position) Add(rel Position) {
if p.Line == 0 {
p.Reset()
}
switch {
case rel.Line == 0:
// nothing
case rel.Line > 1:
// includes new lines
p.Line += rel.Line - 1
p.Column = rel.Column
default:
// same line
p.Column += rel.Column - 1
}
}

2
parser/parser.go

@ -0,0 +1,2 @@
// Package parser implements some common logic of asciigoat based parsers
package parser

72
parser/text.go

@ -0,0 +1,72 @@
package parser
import (
"bytes"
"io"
"strings"
"asciigoat.org/core/lexer"
)
// TextParser ...
type TextParser struct {
src *lexer.Reader
pos lexer.Position
}
// Position ...
func (p *TextParser) Position() lexer.Position {
return p.pos
}
// StepLine ...
func (p *TextParser) StepLine() {
p.src.Discard()
p.pos.StepLine()
}
// Step ...
func (p *TextParser) Step() {
s := p.src.Emit()
p.pos.StepN(len(s))
}
// Emit ...
func (p *TextParser) Emit() Token {
t := Token{
Value: p.src.Emit(),
Position: p.pos,
}
p.pos.StepN(t.Len())
return t
}
// Peek ...
func (p *TextParser) Peek() string {
return p.src.String()
}
// NewTextParser ...
func NewTextParser(r io.Reader) *TextParser {
if r == nil {
r = strings.NewReader("")
}
p := &TextParser{
src: lexer.NewReader(r),
}
p.pos.Reset()
return p
}
// NewTextParserBytes ...
func NewTextParserBytes(b []byte) *TextParser {
return NewTextParser(bytes.NewReader(b))
}
// NewTextParserString ...
func NewTextParserString(s string) *TextParser {
return NewTextParser(strings.NewReader(s))
}

31
parser/token.go

@ -0,0 +1,31 @@
package parser
import (
"fmt"
"asciigoat.org/core/lexer"
)
// Token ...
type Token struct {
lexer.Position
Type uint
Value string
}
// Len ...
func (t Token) Len() int {
return len(t.Value)
}
// String generates the '%v' value
func (t Token) String() string {
return t.Value
}
// GoString generates the '%#v' value
func (t Token) GoString() string {
return fmt.Sprintf("Token{Position{%v,%v}, %v, %q}",
t.Line, t.Column, t.Type, t.Value)
}

2
reflective/docs.go

@ -0,0 +1,2 @@
// Package reflective assists reflection-driven unmarshalling
package reflective

69
reflective/error.go

@ -0,0 +1,69 @@
package reflective
import (
"bytes"
"fmt"
"reflect"
)
// An InvalidUnmarshalError describes an invalid argument passed to New.
// (It must be a non-nil pointer.)
type InvalidUnmarshalError struct {
Method string
Prefix string
reflect.Type
}
func (e *InvalidUnmarshalError) Error() string {
var buf bytes.Buffer
if e.Prefix != "" {
_, _ = fmt.Fprintf(&buf, "%s: ", e.Prefix)
}
method := e.Method
if method == "" {
method = "New"
}
_, _ = fmt.Fprintf(&buf, "%s(", method)
switch {
case e.Type == nil:
_, _ = buf.WriteString("nil")
case e.Type.Kind() != reflect.Pointer:
_, _ = fmt.Fprintf(&buf, "%s %s", "non-pointer", e.Type.String())
default:
_, _ = fmt.Fprintf(&buf, "%s %s", "nil", e.Type.String())
}
_, _ = buf.WriteString(")")
return buf.String()
}
// An UnmarshalTypeError tells something went wrong while processing
// a type.
type UnmarshalTypeError struct {
Prefix string
Err error
reflect.Type
}
func (e UnmarshalTypeError) Unwrap() error {
return e.Err
}
func (e UnmarshalTypeError) Error() string {
var buf bytes.Buffer
if e.Prefix != "" {
_, _ = fmt.Fprintf(&buf, "%s: ", e.Prefix)
}
_, _ = fmt.Fprintf(&buf, "%s: %s", e.Type.String(), e.Err)
return buf.String()
}

15
reflective/helpers.go

@ -0,0 +1,15 @@
package reflective
import "reflect"
// IsZero tells if a value is zero either via
// v.IsZero() or reflection.
func IsZero(iv any) bool {
if p, ok := iv.(interface {
IsZero() bool
}); ok {
return p.IsZero()
}
return reflect.ValueOf(iv).IsZero()
}

27
reflective/reflection.go

@ -0,0 +1,27 @@
package reflective
import "reflect"
// Reflection provides Marshalling/Unmarshalling oriented view
// of a value
type Reflection struct {
v reflect.Value
}
// New creates a Reflection of the given pointer
func New(v any) (*Reflection, error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Pointer || rv.IsNil() {
err := &InvalidUnmarshalError{
Type: rv.Type(),
}
return nil, err
}
r := &Reflection{
v: rv,
}
return r, nil
}
Loading…
Cancel
Save