Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4aceeabdf2 |
+5
-21
@@ -25,29 +25,17 @@ type Error struct {
|
|||||||
Column int
|
Column int
|
||||||
|
|
||||||
Content string
|
Content string
|
||||||
Hint string
|
|
||||||
Err error
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err Error) prefix() string {
|
|
||||||
switch {
|
|
||||||
case err.Line > 0 || err.Column > 0:
|
|
||||||
if err.Filename != "" {
|
|
||||||
return fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%v:%v", err.Line, err.Column)
|
|
||||||
default:
|
|
||||||
return err.Filename
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (err Error) Error() string {
|
func (err Error) Error() string {
|
||||||
var s []string
|
var s []string
|
||||||
|
|
||||||
prefix := err.prefix()
|
switch {
|
||||||
if prefix != "" {
|
case err.Line > 0 || err.Column > 0:
|
||||||
s = append(s, prefix)
|
s = append(s, fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column))
|
||||||
|
case err.Filename != "":
|
||||||
|
s = append(s, err.Filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err.Err != nil {
|
if err.Err != nil {
|
||||||
@@ -58,10 +46,6 @@ func (err Error) Error() string {
|
|||||||
s = append(s, fmt.Sprintf("%q", err.Content))
|
s = append(s, fmt.Sprintf("%q", err.Content))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err.Hint != "" {
|
|
||||||
s = append(s, err.Hint)
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(s, ": ")
|
return strings.Join(s, ": ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
// Package parser implements some common logic of asciigoat based parsers
|
|
||||||
package parser
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
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))
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
// Package reflective assists reflection-driven unmarshalling
|
|
||||||
package reflective
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
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()
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
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()
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user