Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c94e5e74ad | |||
| dd252fafae | |||
| f68272eb84 | |||
| 1e95acf21d | |||
| 2b5dcec64d | |||
| 4aa6233e4f | |||
| c31cfb9244 | |||
| 9fcc64347b | |||
| 8e4f759260 | |||
| 2ab838ea78 | |||
| 3bd2c58fc3 | |||
| eb9261b478 | |||
| 0098052142 | |||
| a02326a0a6 |
@@ -1 +1,78 @@
|
||||
# asciigoat's core library
|
||||
|
||||
[![Go Reference][godoc-badge]][godoc]
|
||||
[![Go Report Card][goreport-badge]][goreport]
|
||||
|
||||
This package contains the basics for writing simple parsers of
|
||||
text languages heavily inspired by
|
||||
[Rob Pike](https://en.wikipedia.org/wiki/Rob_Pike)'s talk on
|
||||
[Lexical Scanning in Go](https://go.dev/talks/2011/lex.slide#1) in 2011 which
|
||||
you can [watch online](https://www.youtube.com/watch?v=HxaD_trXwRE) to get
|
||||
better understanding of the ideas behind **asciigoat**.
|
||||
|
||||
**asciigoat** is [MIT](https://opensource.org/license/mit/) licensed.
|
||||
|
||||
[godoc]: https://pkg.go.dev/asciigoat.org/core
|
||||
[godoc-badge]: https://pkg.go.dev/badge/asciigoat.org/core.svg
|
||||
[goreport]: https://goreportcard.com/report/asciigoat.org/core
|
||||
[goreport-badge]: https://goreportcard.com/badge/asciigoat.org/core
|
||||
|
||||
[godoc-lexer-reader]: https://pkg.go.dev/asciigoat.org/core/lexer#Reader
|
||||
[godoc-readcloser]: https://pkg.go.dev/asciigoat.org/core#ReadCloser
|
||||
|
||||
## Lexer
|
||||
|
||||
### lexer.Reader
|
||||
|
||||
The lexer package provides [`lexer.Reader`][godoc-lexer-reader] which is
|
||||
actually an [`io.RuneScanner`](https://pkg.go.dev/io#RuneScanner)
|
||||
that buffers accepted runes until you are ready to
|
||||
[emit](https://pkg.go.dev/asciigoat.org/core/lexer#Reader.Emit) or
|
||||
[discard](https://pkg.go.dev/asciigoat.org/core/lexer#Reader.Discard).
|
||||
|
||||
### lexer.Position
|
||||
|
||||
[`lexer.Position`](https://pkg.go.dev/asciigoat.org/core/lexer#Position)
|
||||
is a `(Line, Column)` pair with methods to facilitate tracking
|
||||
your position on the source [Reader](https://pkg.go.dev/io#Reader).
|
||||
|
||||
### lexer.Error
|
||||
|
||||
[`lexer.Error`](https://pkg.go.dev/asciigoat.org/core/lexer#Error)
|
||||
is an [unwrappable](https://pkg.go.dev/errors#Unwrap) error with a
|
||||
token position and hint attached.
|
||||
|
||||
### lexer.StateFn
|
||||
|
||||
At the heart of **asciigoat** we have _state functions_ as proposed on [Rob Pike's famous talk](https://www.youtube.com/watch?v=HxaD_trXwRE) which return the next _state function_ parsing is done.
|
||||
Additionally there is a [`Run()`](https://pkg.go.dev/asciigoat.org/lexer#Run) helper that implements the loop.
|
||||
|
||||
### rune checkers
|
||||
|
||||
_Rune checkers_ are simple functions that tell if a rune is of a class or it's not.
|
||||
Fundamental checkers are provided by the [`unicode` package](https://pkg.go.dev/unicode).
|
||||
|
||||
Our [`lexer.Reader`][godoc-lexer-reader] uses them on its `Accept()` and `AcceptAll()` methods to
|
||||
make it easier to consume the _source_ document.
|
||||
|
||||
To facilitate the declaration of _rune classes_ in the context of **asciigoat** powered parsers we include
|
||||
a series of rune checker factories.
|
||||
|
||||
* `NewIsIn(string)`
|
||||
* `NewIsInRunes(...rune)`
|
||||
* `NewIsNot(checker)`
|
||||
* `NewIsOneOf(...checker)`
|
||||
|
||||
## Others
|
||||
|
||||
### ReadCloser
|
||||
|
||||
[ReadCloser][godoc-readcloser] assists in providing a
|
||||
[io.Closer](https://pkg.go.dev/io#Closer) to Readers or buffers without on,
|
||||
or unearthing one if available so
|
||||
[io.ReadCloser](https://pkg.go.dev/io#ReadCloser) can be fulfilled.
|
||||
|
||||
## See also
|
||||
|
||||
* [asciigoat.org/ini](https://asciigoat.org/ini)
|
||||
* [oss.jpi.io](https://oss.jpi.io)
|
||||
|
||||
@@ -2,20 +2,22 @@ module asciigoat.org/core
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/mgechev/revive v1.3.3
|
||||
require (
|
||||
github.com/fatih/structtag v1.2.0
|
||||
github.com/mgechev/revive v1.3.3
|
||||
github.com/pkg/errors v0.9.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab // indirect
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/fatih/structtag v1.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
golang.org/x/tools v0.12.0 // indirect
|
||||
)
|
||||
|
||||
+30
-5
@@ -1,6 +1,7 @@
|
||||
package lexer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
@@ -9,6 +10,14 @@ var (
|
||||
_ error = (*Error)(nil)
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrUnacceptableRune indicates the read rune isn't acceptable in the context
|
||||
ErrUnacceptableRune = errors.New("rune not acceptable in context")
|
||||
|
||||
// ErrNotImplemented indicates something hasn't been implemented yet
|
||||
ErrNotImplemented = errors.New("not implemented")
|
||||
)
|
||||
|
||||
// Error represents a generic parsing error
|
||||
type Error struct {
|
||||
Filename string
|
||||
@@ -16,17 +25,29 @@ type Error struct {
|
||||
Column int
|
||||
|
||||
Content string
|
||||
Hint string
|
||||
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 {
|
||||
var s []string
|
||||
|
||||
switch {
|
||||
case err.Line > 0 || err.Column > 0:
|
||||
s = append(s, fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column))
|
||||
case err.Filename != "":
|
||||
s = append(s, err.Filename)
|
||||
prefix := err.prefix()
|
||||
if prefix != "" {
|
||||
s = append(s, prefix)
|
||||
}
|
||||
|
||||
if err.Err != nil {
|
||||
@@ -37,6 +58,10 @@ func (err Error) Error() string {
|
||||
s = append(s, fmt.Sprintf("%q", err.Content))
|
||||
}
|
||||
|
||||
if err.Hint != "" {
|
||||
s = append(s, err.Hint)
|
||||
}
|
||||
|
||||
return strings.Join(s, ": ")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package lexer
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// NewIsNot generates a rune condition checker that reverses the
|
||||
// decision of the given checker.
|
||||
func NewIsNot(cond func(rune) bool) func(rune) bool {
|
||||
return func(r rune) bool {
|
||||
return !cond(r)
|
||||
}
|
||||
}
|
||||
|
||||
// NewIsIn generates a rune condition checker that accepts runes
|
||||
// contained on the provided string
|
||||
func NewIsIn(s string) func(rune) bool {
|
||||
return func(r rune) bool {
|
||||
return strings.ContainsRune(s, r)
|
||||
}
|
||||
}
|
||||
|
||||
// NewIsInRunes generates a rune condition checker that accepts
|
||||
// the runes specified
|
||||
func NewIsInRunes(s ...rune) func(rune) bool {
|
||||
return NewIsIn(string(s))
|
||||
}
|
||||
|
||||
// NewIsOneOf generates a run condition checker that accepts runes
|
||||
// accepted by any of the given checkers
|
||||
func NewIsOneOf(s ...func(rune) bool) func(rune) bool {
|
||||
return func(r rune) bool {
|
||||
for _, cond := range s {
|
||||
if cond(r) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IsSpace reports whether the rune is a space character as
|
||||
// defined by Unicode's White Space property
|
||||
func IsSpace(r rune) bool {
|
||||
return unicode.IsSpace(r)
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package reflection
|
||||
|
||||
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()
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func structFieldName(sf reflect.StructField) string {
|
||||
if sf.Anonymous {
|
||||
idx := sf.Index[len(sf.Index)-1]
|
||||
|
||||
return fmt.Sprintf("%s-%v", "anonymous", idx)
|
||||
}
|
||||
|
||||
return sf.Name
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Package reflection helps Marshalling/Unmarshalling data to structs
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Reflection provides Marshalling/Unmarshalling oriented view
|
||||
// of a value
|
||||
type Reflection struct {
|
||||
v reflect.Value
|
||||
|
||||
types map[reflect.Type]TypeInfo
|
||||
}
|
||||
|
||||
// Value returns the object it reflects
|
||||
func (r *Reflection) Value() any {
|
||||
return r.v.Interface()
|
||||
}
|
||||
|
||||
// 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,
|
||||
types: make(map[reflect.Type]TypeInfo),
|
||||
}
|
||||
|
||||
if err := r.scan(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func (r *Reflection) scanType(rt reflect.Type) error {
|
||||
switch rt.Kind() {
|
||||
case reflect.Pointer, reflect.Slice:
|
||||
rt = rt.Elem()
|
||||
}
|
||||
|
||||
if _, ok := r.types[rt]; ok {
|
||||
// cached
|
||||
return nil
|
||||
}
|
||||
|
||||
ti, err := NewTypeInfo(rt)
|
||||
if err != nil {
|
||||
return &UnmarshalTypeError{Type: rt, Err: err}
|
||||
}
|
||||
|
||||
r.types[rt] = ti
|
||||
|
||||
for i := range ti.fields {
|
||||
err := r.scanType(ti.fields[i].sf.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Reflection) scan() error {
|
||||
return r.scanType(r.v.Type())
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
"github.com/fatih/structtag"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// TypeInfo ...
|
||||
type TypeInfo struct {
|
||||
rt reflect.Type
|
||||
|
||||
fields []TypeInfoField
|
||||
}
|
||||
|
||||
// Type ...
|
||||
func (ti *TypeInfo) Type() reflect.Type {
|
||||
return ti.rt
|
||||
}
|
||||
|
||||
// Kind ...
|
||||
func (ti *TypeInfo) Kind() reflect.Kind {
|
||||
return ti.rt.Kind()
|
||||
}
|
||||
|
||||
// TypeInfoField ...
|
||||
type TypeInfoField struct {
|
||||
sf reflect.StructField
|
||||
tags *structtag.Tags
|
||||
|
||||
Name string
|
||||
}
|
||||
|
||||
// Type ...
|
||||
func (tif *TypeInfoField) Type() reflect.Type {
|
||||
return tif.sf.Type
|
||||
}
|
||||
|
||||
// Kind ...
|
||||
func (tif *TypeInfoField) Kind() reflect.Kind {
|
||||
return tif.sf.Type.Kind()
|
||||
}
|
||||
|
||||
// Tag ...
|
||||
func (tif *TypeInfoField) Tag(key string) (*structtag.Tag, bool) {
|
||||
if tif.tags != nil {
|
||||
tag, _ := tif.tags.Get(key)
|
||||
if tag != nil {
|
||||
return tag, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// NewTypeInfo ...
|
||||
func NewTypeInfo(rt reflect.Type) (TypeInfo, error) {
|
||||
log.Printf("%s.%s: %s (%s)", "reflection", "NewTypeInfo", rt, rt.Kind())
|
||||
|
||||
ti := TypeInfo{
|
||||
rt: rt,
|
||||
}
|
||||
|
||||
err := ti.init()
|
||||
return ti, err
|
||||
}
|
||||
|
||||
func (ti *TypeInfo) init() error {
|
||||
switch ti.Kind() {
|
||||
case reflect.Struct:
|
||||
return ti.initStruct()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (ti *TypeInfo) initStruct() error {
|
||||
// load fields
|
||||
n := ti.rt.NumField()
|
||||
fields := make([]TypeInfoField, 0, n)
|
||||
for i := 0; i < n; i++ {
|
||||
sf := ti.rt.Field(i)
|
||||
tags, err := structtag.Parse(string(sf.Tag))
|
||||
|
||||
if err != nil {
|
||||
// tag parse error
|
||||
err = &UnmarshalTypeError{
|
||||
Type: ti.rt,
|
||||
Err: errors.Wrap(err, structFieldName(sf)),
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if sf.IsExported() {
|
||||
log.Printf("%s.%s: [%v]: %q %s (%s)",
|
||||
"reflection", "NewTypeInfo",
|
||||
i, structFieldName(sf), sf.Type, sf.Type.Kind())
|
||||
|
||||
fields = append(fields, TypeInfoField{
|
||||
sf: sf,
|
||||
tags: tags,
|
||||
})
|
||||
}
|
||||
}
|
||||
ti.fields = fields
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user