Skip to content

Commit

Permalink
Add formatted code and linting (#149)
Browse files Browse the repository at this point in the history
* Add gofumpt and golangci-lint

* Remove trailing spaces from links

* Replace interface{} with any
  • Loading branch information
meyermarcel authored Nov 30, 2024
1 parent 3c7184a commit bd929c4
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 121 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
branches: [ master ]

jobs:

build:
name: Build
runs-on: ubuntu-latest
Expand All @@ -21,5 +20,11 @@ jobs:
with:
go-version-file: 'go.mod'

- name: Lint
uses: golangci/golangci-lint-action@v6
with:
# Require: The version of golangci-lint to use.
version: latest

- name: Build and Test
run: go run build.go test
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters:
enable:
- revive
- gofumpt
- errorlint
- godot
- errname
28 changes: 14 additions & 14 deletions LINKS.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
https://www.microsoft.com/en-us/research/blog/fp2-fully-in-place-functional-programming-provides-memory-reuse-for-pure-functional-programs/
https://www.microsoft.com/en-us/research/blog/fp2-fully-in-place-functional-programming-provides-memory-reuse-for-pure-functional-programs/

https://medium.com/@octskyward/graal-truffle-134d8f28fb69#.jo3luf4dn
http://nez-peg.github.io/
https://en.wikipedia.org/wiki/DFA_minimization
https://medium.com/@octskyward/graal-truffle-134d8f28fb69#.jo3luf4dn
http://nez-peg.github.io/
https://en.wikipedia.org/wiki/DFA_minimization

https://news.ycombinator.com/item?id=14589173
http://jamey.thesharps.us/2017/06/search-based-compiler-code-generation.html
https://news.ycombinator.com/item?id=14589173
http://jamey.thesharps.us/2017/06/search-based-compiler-code-generation.html

https://news.ycombinator.com/item?id=15105119
https://en.wikipedia.org/wiki/Tree_transducer
https://news.ycombinator.com/item?id=15105119
https://en.wikipedia.org/wiki/Tree_transducer

# Type-Driven Program Synthesis
https://news.ycombinator.com/item?id=18251145
https://www.youtube.com/watch?v=HnOix9TFy1A
http://comcom.csail.mit.edu/comcom/#welcome
https://bitbucket.org/nadiapolikarpova/synquid
https://news.ycombinator.com/item?id=18251145
https://www.youtube.com/watch?v=HnOix9TFy1A
http://comcom.csail.mit.edu/comcom/#welcome
https://bitbucket.org/nadiapolikarpova/synquid

# Formality – An efficient programming language and proof assistant
https://news.ycombinator.com/item?id=18230148
https://github.com/maiavictor/formality
https://news.ycombinator.com/item?id=18230148
https://github.com/maiavictor/formality
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,30 @@ Will print out `"capture"`. The captured string is stored in `buffer[begin:end]`
Testing a grammar usually requires more than the average unit testing with multiple inputs and outputs. Grammars are also usually not for just one language implementation. Consider maintaining a list of inputs with expected outputs in a structured file format such as JSON or YAML and parsing it for testing or using one of the available options for Go such as Rob Muhlestein's [`tinout`](https://github.com/robmuh/tinout) package.
## Files
## Development
### Requirements
* [Golang](https://golang.org/doc/install), see [go.mod](go.mod) for version
* [golangci-lint latest version](https://github.com/golangci/golangci-lint#install)
* gofumpt
```
go install mvdan.cc/gofumpt@latest
```
### Lint code
```
golangci-lint run
```
### Format code
```
gofumpt -l -w .
```
### Files
* `bootstrap/main.go` - bootstrap syntax tree of peg
* `tree/peg.go` - syntax tree and code generator
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,11 @@ func main() {
})

filename := "bootstrap.peg.go"
out, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
out, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
fmt.Printf("%v: %v\n", filename, err)
return
}
defer out.Close()
t.Compile(filename, os.Args, out)
_ = t.Compile(filename, os.Args, out)
}
8 changes: 4 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ package main
const (
// VERSION is the version of peg
VERSION = "{{.Version}}"
VERSION = "{{.Version}}"
// BUILDTIME is the build time of peg
BUILDTIME = "{{.Buildtime}}"
// COMMIT is the commit hash of peg
COMMIT = "{{.Commit}}"
COMMIT = "{{.Commit}}"
// IS_TAGGED is there a version
IS_TAGGED = {{.IsTagged}}
)
Expand Down Expand Up @@ -114,7 +114,7 @@ func buildinfo() {

var processed = make(map[string]bool)

func done(file string, deps ...interface{}) bool {
func done(file string, deps ...any) bool {
fini := true
file = filepath.FromSlash(file)
info, err := os.Stat(file)
Expand Down Expand Up @@ -204,7 +204,7 @@ func command(name, inputFile, outputFile string, arg ...string) {
if err != nil {
panic(err)
}
err = os.WriteFile(outputFile, output, 0600)
err = os.WriteFile(outputFile, output, 0o600)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main

const (
// VERSION is the version of peg
VERSION = ""
VERSION = ""
// BUILDTIME is the build time of peg
BUILDTIME = "2024-11-27T19:39:27"
// COMMIT is the commit hash of peg
COMMIT = "f02924709a94d2f169ee1dd5f9cee0277aed4edd"
COMMIT = "f02924709a94d2f169ee1dd5f9cee0277aed4edd"
// IS_TAGGED is there a version
IS_TAGGED = false
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/pointlander/peg

go 1.16
go 1.23
12 changes: 10 additions & 2 deletions grammars/c/c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func noParseC_4t(t *testing.T, src string) {
}

func TestCParsing_Expressions1(t *testing.T) {
case1src :=
`int a() {
case1src := `int a() {
(es);
1++;
1+1;
Expand Down Expand Up @@ -82,19 +81,24 @@ return 0+(a);
func TestCParsing_Expressions4(t *testing.T) {
parseC_4t(t, `int a(){1+(a);}`)
}

func TestCParsing_Expressions5(t *testing.T) {
parseC_4t(t, `int a(){return (int)0;}`)
}

func TestCParsing_Expressions6(t *testing.T) {
parseC_4t(t, `int a(){return (in)0;}`)
}

func TestCParsing_Expressions7(t *testing.T) {
parseC_4t(t, `int a()
{ return (0); }`)
}

func TestCParsing_Cast0(t *testing.T) {
parseC_4t(t, `int a(){(cast)0;}`)
}

func TestCParsing_Cast1(t *testing.T) {
parseC_4t(t, `int a(){(m*)(rsp);}`)
parseC_4t(t, `int a(){(struct m*)(rsp);}`)
Expand All @@ -103,11 +107,13 @@ func TestCParsing_Cast1(t *testing.T) {
func TestCParsing_Empty(t *testing.T) {
parseC_4t(t, `/** empty is valid. */ `)
}

func TestCParsing_EmptyStruct(t *testing.T) {
parseC_4t(t, `struct empty{};`)
parseC_4t(t, `struct {} empty;`)
parseC_4t(t, `struct empty {} empty;`)
}

func TestCParsing_EmptyEmbeddedUnion(t *testing.T) {
parseC_4t(t, `struct empty{
union {
Expand All @@ -116,6 +122,7 @@ func TestCParsing_EmptyEmbeddedUnion(t *testing.T) {
};
};`)
}

func TestCParsing_ExtraSEMI(t *testing.T) {
parseC_4t(t, `int func(){}
;
Expand All @@ -127,6 +134,7 @@ int foo() {};;

noParseC_4t(t, `struct empty{}`)
}

func TestCParsing_ExtraSEMI2(t *testing.T) {
parseC_4t(t, `
struct a { int b; ; };
Expand Down
3 changes: 0 additions & 3 deletions grammars/calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build grammars
// +build grammars

package main

import (
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
//go:generate build peg

var (
inline = flag.Bool("inline", false, "parse rule inlining")
_switch = flag.Bool("switch", false, "replace if-else if-else like blocks with switch blocks")
print = flag.Bool("print", false, "directly dump the syntax tree")
inline = flag.Bool("inline", false, "parse rule inlining")
_switch = flag.Bool("switch", false, "replace if-else if-else like blocks with switch blocks")
// Avoid redefinition of built-in function print.
printFlag = flag.Bool("print", false, "directly dump the syntax tree")
syntax = flag.Bool("syntax", false, "print out the syntax tree")
noast = flag.Bool("noast", false, "disable AST")
strict = flag.Bool("strict", false, "treat compiler warnings as errors")
Expand Down Expand Up @@ -58,14 +59,14 @@ func main() {
}

p := &Peg{Tree: tree.New(*inline, *_switch, *noast), Buffer: string(buffer)}
p.Init(Pretty(true), Size(1<<15))
_ = p.Init(Pretty(true), Size(1<<15))
if err := p.Parse(); err != nil {
log.Fatal(err)
}

p.Execute()

if *print {
if *printFlag {
p.Print()
}
if *syntax {
Expand All @@ -75,7 +76,7 @@ func main() {
if *filename == "" {
*filename = file + ".go"
}
out, err := os.OpenFile(*filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
out, err := os.OpenFile(*filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
fmt.Printf("%v: %v\n", *filename, err)
return
Expand Down
26 changes: 13 additions & 13 deletions peg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type T Peg {}
Grammar <- !.
`
p := &Peg{Tree: tree.New(false, false, false), Buffer: buffer}
p.Init()
_ = p.Init()
err := p.Parse()
if err != nil {
t.Error(err)
}

p = &Peg{Tree: tree.New(false, false, false), Buffer: buffer}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
err = p.Parse()
if err != nil {
t.Error(err)
Expand All @@ -34,7 +34,7 @@ type T Peg {}
Grammar <- !.
`
p := &Peg{Tree: tree.New(false, false, false), Buffer: buffer}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
err := p.Parse()
if err == nil {
t.Error("packagenospace was parsed without error")
Expand All @@ -48,7 +48,7 @@ typenospace Peg {}
Grammar <- !.
`
p := &Peg{Tree: tree.New(false, false, false), Buffer: buffer}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
err := p.Parse()
if err == nil {
t.Error("typenospace was parsed without error")
Expand All @@ -62,15 +62,15 @@ func TestSame(t *testing.T) {
}

p := &Peg{Tree: tree.New(true, true, false), Buffer: string(buffer)}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
if err = p.Parse(); err != nil {
t.Error(err)
}

p.Execute()

out := &bytes.Buffer{}
p.Compile("peg.peg.go", []string{"./peg", "-inline", "-switch", "peg.peg"}, out)
_ = p.Compile("peg.peg.go", []string{"./peg", "-inline", "-switch", "peg.peg"}, out)

bootstrap, err := os.ReadFile("peg.peg.go")
if err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ Begin <- Begin 'x'

for i, buffer := range tt {
p := &Peg{Tree: tree.New(false, false, false), Buffer: buffer}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
if err := p.Parse(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ type DiceExprParser Peg {
Expr <- 'CJK' / '汉字' / 'test'
`
p := &Peg{Tree: tree.New(false, true, false), Buffer: buffer}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
err := p.Parse()
if err != nil {
t.Fatal("cjk character test failed")
Expand Down Expand Up @@ -173,7 +173,7 @@ func BenchmarkInitOnly(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, peg := range pegs {
p := &Peg{Tree: tree.New(true, true, false), Buffer: peg}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
}
}
}
Expand All @@ -187,7 +187,7 @@ func BenchmarkParse(b *testing.B) {
}

p := &Peg{Tree: tree.New(true, true, false), Buffer: string(input)}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
pegs[i] = p
}

Expand All @@ -213,7 +213,7 @@ func BenchmarkResetAndParse(b *testing.B) {
}

p := &Peg{Tree: tree.New(true, true, false), Buffer: string(input)}
p.Init(Size(1 << 15))
_ = p.Init(Size(1 << 15))
pegs[i] = p
}

Expand Down Expand Up @@ -242,7 +242,7 @@ func BenchmarkInitAndParse(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, str := range strs {
peg := &Peg{Tree: tree.New(true, true, false), Buffer: str}
peg.Init(Size(1 << 15))
_ = peg.Init(Size(1 << 15))
if err := peg.Parse(); err != nil {
b.Error(err)
}
Expand All @@ -264,7 +264,7 @@ func BenchmarkInitResetAndParse(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, str := range strs {
peg := &Peg{Tree: tree.New(true, true, false), Buffer: str}
peg.Init(Size(1 << 15))
_ = peg.Init(Size(1 << 15))
if err := peg.Parse(); err != nil {
b.Error(err)
}
Expand Down
Loading

0 comments on commit bd929c4

Please sign in to comment.