27 Commits

Author SHA1 Message Date
amery 6df387ffeb chore: update dependencies
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-03 15:05:00 +00:00
amery 123770119e parser: TextParser [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-02 22:04:41 +00:00
amery 667803fea5 reflective: IsZero()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-02 22:04:41 +00:00
amery a38f0c74e8 reflective: Add Reflection{} placeholder and the New() factory
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-02 16:18:56 +00:00
amery 7e0b7cfb47 reflective: introduce UnmarshalTypeError{}
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-02 16:14:11 +00:00
amery fd4f6439b8 reflective: introduce InvalidUnmarshalError{}
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-02 16:13:48 +00:00
amery f74745471d reflective: add placeholder for reflection assistant
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-02 15:48:08 +00:00
amery 2b5dcec64d lexer: add Hint to Error, which is expanded as "%s" instead of "%q"
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-31 20:11:58 +00:00
amery 4aa6233e4f lexer: refactor Error.Error()'s prefix generator
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-31 20:11:34 +00:00
amery c31cfb9244 Merge pull request 'README: add initial description of the package' (#11)
Reviewed-on: #11
2023-08-31 18:26:32 +02:00
amery 9fcc64347b README: add initial description of the package
and badges

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-31 15:18:26 +00:00
amery 8e4f759260 Merge pull request 'lexer: introduce NewIsInRunes() shortcut for NewIsIn()' (#10)
Reviewed-on: #10
2023-08-31 17:14:46 +02:00
amery 2ab838ea78 lexer: introduce NewIsInRunes() shortcut for NewIsIn()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-31 14:28:57 +00:00
amery 3bd2c58fc3 Merge pull request 'lexer: constants and helpers' (#9)
Reviewed-on: #9
2023-08-30 19:44:26 +02:00
amery f8f6ff9e11 Merge pull request 'lexer: Reader.Accept(), AcceptAll(), and StateFn' (#5)
Reviewed-on: #5
2023-08-30 19:02:15 +02:00
amery 05d504346e lexer: extend Run() to treat io.EOF as non-error termination
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 01:50:12 +01:00
amery eb9261b478 lexer: NewIsNot(), NewIsIn(), NewIsOneOf()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 23:30:51 +00:00
amery 0098052142 lexer: IsSpace()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 23:30:47 +00:00
amery a02326a0a6 lexer: ErrUnacceptableRune and ErrNotImplemented
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 23:30:44 +00:00
amery 93c58cdc26 Merge pull request 'lexer: introduce a Position (Line, Column) handler' (#8)
Reviewed-on: #8
2023-08-29 22:14:57 +02:00
amery 9425ba0f7c lexer: introduce a Position (Line, Column) handler
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 16:22:59 +00:00
amery 5a7b19bb06 Merge pull request 'build-sys: import build system from darvaza.org/core' (#7)
Reviewed-on: #7
2023-08-29 17:34:16 +02:00
amery 6c189fd87d build-sys: import build system from darvaza.org/core
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 15:23:25 +00:00
amery 1d62857e14 lexer: introduce StateFn and the basic state machine loop
v2: make the error break more explicit

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 15:11:51 +00:00
amery f7e13e0978 Merge pull request 'lexer: introduce Error{}' (#6)
Reviewed-on: #6
2023-08-29 17:00:09 +02:00
amery f67d8a2443 lexer: introduce Error{}
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 13:54:23 +00:00
amery d83b128c30 lexer: introduce Reader.Accept()/AcceptAll()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 13:26:49 +00:00
18 changed files with 705 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
.PHONY: all clean generate fmt
.PHONY: tidy get build test up
GO ?= go
GOFMT ?= gofmt
GOFMT_FLAGS = -w -l -s
GOGENERATE_FLAGS = -v
GOPATH ?= $(shell $(GO) env GOPATH)
GOBIN ?= $(GOPATH)/bin
TMPDIR ?= $(CURDIR)/.tmp
TOOLSDIR = $(CURDIR)/tools
REVIVE_CONF ?= $(TOOLSDIR)/revive.toml
REVIVE_RUN_ARGS ?= -config $(REVIVE_CONF) -formatter friendly
REVIVE ?= $(GO) run -v github.com/mgechev/revive
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell if [ "$$(tput colors 2> /dev/null || echo 0)" -ge 8 ]; then printf "\033[34;1m▶\033[0m"; else printf "▶"; fi)
all: get generate tidy build
clean: ; $(info $(M) cleaning)
rm -rf $(TMPDIR)
fmt: ; $(info $(M) reformatting sources)
$Q find . -name '*.go' | xargs -r $(GOFMT) $(GOFMT_FLAGS)
tidy: | fmt ; $(info $(M) tidying up)
$Q $(GO) mod tidy
$Q $(GO) vet ./...
$Q $(REVIVE) $(REVIVE_RUN_ARGS) ./...
get: ; $(info $(M) downloading dependencies)
$Q $(GO) get -v -tags tools ./...
build: ; $(info $(M) building)
$Q $(GO) build -v ./...
test: ; $(info $(M) building)
$Q $(GO) test ./...
up: ; $(info $(M) updating dependencies)
$Q $(GO) get -u -v ./...
$Q $(GO) mod tidy
generate: ; $(info $(M) generating data)
$Q git grep -l '^//go:generate' | sort -uV | xargs -r -n1 $(GO) generate $(GOGENERATE_FLAGS)
+77
View File
@@ -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)
+19
View File
@@ -1,3 +1,22 @@
module asciigoat.org/core
go 1.19
require github.com/mgechev/revive v1.3.3
require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/chavacava/garif v0.1.0 // 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.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // 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
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/tools v0.12.0 // indirect
)
+52
View File
@@ -0,0 +1,52 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
github.com/mgechev/revive v1.3.3 h1:GUWzV3g185agbHN4ZdaQvR6zrLVYTUSA2ktvIinivK0=
github.com/mgechev/revive v1.3.3/go.mod h1:NhpOtVtDbjYNDj697eDUBTobijCDHQKar4HDKc0TuTo=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+70
View File
@@ -0,0 +1,70 @@
package lexer
import (
"errors"
"fmt"
"strings"
)
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
Line int
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
prefix := err.prefix()
if prefix != "" {
s = append(s, prefix)
}
if err.Err != nil {
s = append(s, err.Err.Error())
}
if err.Content != "" {
s = append(s, fmt.Sprintf("%q", err.Content))
}
if err.Hint != "" {
s = append(s, err.Hint)
}
return strings.Join(s, ": ")
}
func (err Error) Unwrap() error {
return err.Err
}
+29
View File
@@ -1,2 +1,31 @@
// Package lexer provides basic helpers to implement parsers
package lexer
import (
"errors"
"io"
)
// StateFn is a State Function of the parser
type StateFn func() (StateFn, error)
// Run runs a state machine until the state function either
// returns nil or an error
func Run(fn StateFn) error {
for fn != nil {
var err error
fn, err = fn()
switch {
case errors.Is(err, io.EOF):
// EOF
return nil
case err != nil:
// failed
return err
}
}
// ended
return nil
}
+66
View File
@@ -0,0 +1,66 @@
package lexer
import "fmt"
// Position indicates a line and column pair on a file.
// Counting starts at 1.
type Position struct {
Line int
Column int
}
// String generates a pretty "(Line, Column)"" representation of the Position
func (p Position) String() string {
if p.Line == 0 {
p.Reset()
}
return fmt.Sprintf("(%v, %v)", p.Line, p.Column)
}
// GoString generates a string representation of the Position for %#v usage
func (p Position) GoString() string {
if p.Line == 0 {
p.Reset()
}
return fmt.Sprintf("lexer.Position{%v, %v}", p.Line, p.Column)
}
// Reset places a position at (1,1)
func (p *Position) Reset() {
p.Line, p.Column = 1, 1
}
// Step moves the column one place
func (p *Position) Step() {
if p.Line == 0 {
p.Reset()
}
p.Column++
}
// StepN moves the column N places forward
func (p *Position) StepN(n int) {
if p.Line == 0 {
p.Reset()
}
switch {
case n > 0:
p.Column += n
default:
panic(fmt.Errorf("invalid %v increment", n))
}
}
// StepLine moves position to the start of the next line
func (p *Position) StepLine() {
if p.Line == 0 {
p.Reset()
}
p.Line++
p.Column = 1
}
+35
View File
@@ -198,6 +198,41 @@ func (b *Reader) PeekRune() (rune, int, error) {
return r, l, err
}
// Accept consumes a rune from the source if it meets the condition.
// it returns true if the condition was met and false if it wasn't.
func (b *Reader) Accept(cond func(r rune) bool) bool {
r, _, err := b.ReadRune()
switch {
case err != nil:
return false
case cond(r):
return true
default:
_ = b.UnreadRune()
return false
}
}
// AcceptAll consumes runes from the source as long as they meet the
// condition. it returns true if the condition was met for at least one rune,
// and false if it wasn't.
func (b *Reader) AcceptAll(cond func(r rune) bool) bool {
var accepted bool
for {
r, _, err := b.ReadRune()
switch {
case err != nil:
return accepted
case cond(r):
accepted = true
default:
_ = b.UnreadRune()
return accepted
}
}
}
// NewReader creates a new runes [Reader] using the given [io.Reader]
func NewReader(r io.Reader) *Reader {
if r == nil {
+47
View File
@@ -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)
}
+2
View File
@@ -0,0 +1,2 @@
// Package parser implements some common logic of asciigoat based parsers
package parser
+72
View File
@@ -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
View File
@@ -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
View File
@@ -0,0 +1,2 @@
// Package reflective assists reflection-driven unmarshalling
package reflective
+69
View File
@@ -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
View File
@@ -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
View File
@@ -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
}
+35
View File
@@ -0,0 +1,35 @@
ignoreGeneratedHeader = false
severity = "error"
confidence = 0.8
errorCode = 1
warningCode = 0
enableAllRules = true
[rule.function-length]
arguments = [40,0]
severity = "warning"
[rule.function-result-limit]
arguments = [3]
[rule.argument-limit]
arguments = [5]
[rule.cognitive-complexity]
arguments = [7]
[rule.cyclomatic]
arguments = [10]
[rule.line-length-limit]
arguments = [100]
severity = "warning"
[rule.comment-spacings]
severity = "warning"
[rule.empty-lines]
severity = "warning"
# Disabled rules
[rule.max-public-structs]
disabled = true
[rule.file-header]
disabled = true
[rule.add-constant]
disabled = true
[rule.banned-characters]
disabled = true
+7
View File
@@ -0,0 +1,7 @@
//go:build tools
package tools
import (
_ "github.com/mgechev/revive"
)