From bd929c49c994be66457bbf012b12f7a47837cfde Mon Sep 17 00:00:00 2001 From: meyermarcel Date: Sat, 30 Nov 2024 03:08:10 +0100 Subject: [PATCH] Add formatted code and linting (#149) * Add gofumpt and golangci-lint * Remove trailing spaces from links * Replace interface{} with any --- .github/workflows/go.yml | 7 +- .golangci.yml | 7 ++ LINKS.md | 28 +++---- README.md | 25 ++++++- bootstrap/main.go | 4 +- build.go | 8 +- buildinfo.go | 4 +- go.mod | 2 +- grammars/c/c_test.go | 12 ++- grammars/calculator/calculator.go | 3 - main.go | 13 ++-- peg_test.go | 26 +++---- set/set.go | 26 +++---- tree/peg.go | 118 +++++++++++++++--------------- 14 files changed, 162 insertions(+), 121 deletions(-) create mode 100644 .golangci.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index df36a66..9239cfe 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -7,7 +7,6 @@ on: branches: [ master ] jobs: - build: name: Build runs-on: ubuntu-latest @@ -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 diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..9da9caa --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,7 @@ +linters: + enable: + - revive + - gofumpt + - errorlint + - godot + - errname diff --git a/LINKS.md b/LINKS.md index 7efdaee..edf287c 100644 --- a/LINKS.md +++ b/LINKS.md @@ -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 diff --git a/README.md b/README.md index 00a4734..54b46ea 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bootstrap/main.go b/bootstrap/main.go index 08e9f4f..5599d38 100644 --- a/bootstrap/main.go +++ b/bootstrap/main.go @@ -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) } diff --git a/build.go b/build.go index de4079c..d6f3ec6 100644 --- a/build.go +++ b/build.go @@ -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}} ) @@ -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) @@ -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) } diff --git a/buildinfo.go b/buildinfo.go index 3857def..3783cf9 100644 --- a/buildinfo.go +++ b/buildinfo.go @@ -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 ) diff --git a/go.mod b/go.mod index c507a57..f927cd9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/pointlander/peg -go 1.16 +go 1.23 diff --git a/grammars/c/c_test.go b/grammars/c/c_test.go index d049594..1920b7d 100644 --- a/grammars/c/c_test.go +++ b/grammars/c/c_test.go @@ -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; @@ -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);}`) @@ -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 { @@ -116,6 +122,7 @@ func TestCParsing_EmptyEmbeddedUnion(t *testing.T) { }; };`) } + func TestCParsing_ExtraSEMI(t *testing.T) { parseC_4t(t, `int func(){} ; @@ -127,6 +134,7 @@ int foo() {};; noParseC_4t(t, `struct empty{}`) } + func TestCParsing_ExtraSEMI2(t *testing.T) { parseC_4t(t, ` struct a { int b; ; }; diff --git a/grammars/calculator/calculator.go b/grammars/calculator/calculator.go index ecf047d..8969470 100644 --- a/grammars/calculator/calculator.go +++ b/grammars/calculator/calculator.go @@ -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 ( diff --git a/main.go b/main.go index 0f6adcd..1b2f6bd 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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 { @@ -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 diff --git a/peg_test.go b/peg_test.go index ec8c004..7d5fcdb 100644 --- a/peg_test.go +++ b/peg_test.go @@ -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) @@ -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") @@ -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") @@ -62,7 +62,7 @@ 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) } @@ -70,7 +70,7 @@ func TestSame(t *testing.T) { 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 { @@ -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) } @@ -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") @@ -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)) } } } @@ -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 } @@ -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 } @@ -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) } @@ -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) } diff --git a/set/set.go b/set/set.go index 1d07154..b729da7 100644 --- a/set/set.go +++ b/set/set.go @@ -9,7 +9,7 @@ import ( "math" ) -// Node is a node +// Node is a node. type Node struct { Forward *Node Backward *Node @@ -17,13 +17,13 @@ type Node struct { End rune } -// Set is a set +// Set is a set. type Set struct { Head Node Tail Node } -// NewSet returns a new set +// NewSet returns a new set. func NewSet() *Set { return &Set{ Head: Node{ @@ -32,7 +32,7 @@ func NewSet() *Set { } } -// String returns the string of a set +// String returns the string of a set. func (s *Set) String() string { codes, space := "[", "" node := s.Head.Forward @@ -46,7 +46,7 @@ func (s *Set) String() string { return codes + "]" } -// Copy copies a set +// Copy copies a set. func (s *Set) Copy() *Set { set := NewSet() if s.Head.Forward == nil { @@ -68,12 +68,12 @@ func (s *Set) Copy() *Set { return set } -// Add adds a symbol to the set +// Add adds a symbol to the set. func (s *Set) Add(a rune) { s.AddRange(a, a) } -// AddRange adds to a set +// AddRange adds to a set. func (s *Set) AddRange(begin, end rune) { beginNode := &s.Head for beginNode.Forward != nil && begin > beginNode.Forward.End { @@ -150,7 +150,7 @@ func (s *Set) AddRange(begin, end rune) { } } -// Has tests if a set has a rune +// Has tests if a set has a rune. func (s *Set) Has(begin rune) bool { beginNode := &s.Head for beginNode.Forward != nil && begin > beginNode.Forward.End { @@ -162,7 +162,7 @@ func (s *Set) Has(begin rune) bool { return begin >= beginNode.Forward.Begin } -// Complement computes the complement of a set +// Complement computes the complement of a set. func (s *Set) Complement(endSymbol rune) *Set { set := NewSet() if s.Len() == 0 { @@ -215,7 +215,7 @@ func (s *Set) Complement(endSymbol rune) *Set { return set } -// Union is the union of two sets +// Union is the union of two sets. func (s *Set) Union(a *Set) *Set { set := s.Copy() node := a.Head.Forward @@ -229,7 +229,7 @@ func (s *Set) Union(a *Set) *Set { return set } -// Intersects returns true if two sets intersect +// Intersects returns true if two sets intersect. func (s *Set) Intersects(b *Set) bool { x := s.Head.Forward if x == nil { @@ -272,7 +272,7 @@ func (s *Set) Intersects(b *Set) bool { return false } -// Equal returns true if two sets are equal +// Equal returns true if two sets are equal. func (s *Set) Equal(a *Set) bool { lens, lena := s.Len(), a.Len() if lens != lena { @@ -293,7 +293,7 @@ func (s *Set) Equal(a *Set) bool { return true } -// Len returns the size of the set +// Len returns the size of the set. func (s *Set) Len() int { size := 0 if s.Head.Forward == nil { diff --git a/tree/peg.go b/tree/peg.go index e9605de..0eaf2a3 100644 --- a/tree/peg.go +++ b/tree/peg.go @@ -16,6 +16,7 @@ import ( "sort" "strconv" "strings" + "sync" "text/template" "unicode" @@ -261,7 +262,7 @@ func (p *{{.StructName}}) Execute() { begin, end = int(token.begin), int(token.end) text = string(_buffer[begin:end]) {{end}} - {{range .Actions}}case ruleAction{{.GetId}}: + {{range .Actions}}case ruleAction{{.GetID}}: {{.String}} {{end}} } @@ -507,7 +508,8 @@ var TypeMap = [...]string{ "TypePush", "TypeImplicitPush", "TypeNil", - "TypeLast"} + "TypeLast", +} func (t Type) GetType() Type { return t @@ -523,8 +525,8 @@ type Node interface { GetType() Type SetType(t Type) - GetId() int - SetId(id int) + GetID() int + SetID(id int) Init() Front() *node @@ -581,11 +583,11 @@ func (n *node) SetType(t Type) { n.Type = t } -func (n *node) GetId() int { +func (n *node) GetID() int { return n.id } -func (n *node) SetId(id int) { +func (n *node) SetID(id int) { n.id = id } @@ -729,15 +731,18 @@ func (t *Tree) AddDot() { t.PushFront(&node{Type: TypeDot, string: "."}) } func (t *Tree) AddCharacter(text string) { t.PushFront(&node{Type: TypeCharacter, string: text}) } + func (t *Tree) AddDoubleCharacter(text string) { t.PushFront(&node{Type: TypeCharacter, string: strings.ToLower(text)}) t.PushFront(&node{Type: TypeCharacter, string: strings.ToUpper(text)}) t.AddAlternate() } + func (t *Tree) AddHexaCharacter(text string) { hexa, _ := strconv.ParseInt(text, 16, 32) t.PushFront(&node{Type: TypeCharacter, string: string(rune(hexa))}) } + func (t *Tree) AddOctalCharacter(text string) { octal, _ := strconv.ParseInt(text, 8, 8) t.PushFront(&node{Type: TypeCharacter, string: string(rune(octal))}) @@ -800,13 +805,15 @@ func (t *Tree) AddPush() { t.addFix(TypePush) } func (t *Tree) AddPeg(text string) { t.PushFront(&node{Type: TypePeg, string: text}) } func join(tasks []func()) { - length := len(tasks) - done := make(chan int, length) + wg := sync.WaitGroup{} + wg.Add(len(tasks)) for _, task := range tasks { - go func(task func()) { task(); done <- 1 }(task) - } - for d := <-done; d < length; d += <-done { + go func(task func()) { + task() + wg.Done() + }(task) } + wg.Wait() } func escape(c string) string { @@ -838,9 +845,9 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { var werr error warn := func(e error) { if werr == nil { - werr = fmt.Errorf("warning: %s.", e) + werr = fmt.Errorf("warning: %w", e) } else { - werr = fmt.Errorf("%s\nwarning: %s", werr, e) + werr = fmt.Errorf("%w\nwarning: %w", werr, e) } } @@ -856,18 +863,18 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { countsForRule[nodeType]++ switch nodeType { case TypeAction: - n.SetId(int(id)) - copy, name := n.Copy(), fmt.Sprintf("Action%v", id) - t.Actions = append(t.Actions, copy) + n.SetID(int(id)) + cp, name := n.Copy(), fmt.Sprintf("Action%v", id) + t.Actions = append(t.Actions, cp) n.Init() n.SetType(TypeName) n.SetString(name) - n.SetId(t.RulesCount) + n.SetID(t.RulesCount) emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount} implicitPush := &node{Type: TypeImplicitPush} emptyRule.PushBack(implicitPush) - implicitPush.PushBack(copy) + implicitPush.PushBack(cp) implicitPush.PushBack(emptyRule.Copy()) t.PushBack(emptyRule) t.RulesCount++ @@ -891,8 +898,8 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { countsByRule = append(countsByRule, &[TypeLast]uint{}) } case TypePush: - copy, name := rule.Copy(), "PegText" - copy.SetString(name) + cp, name := rule.Copy(), "PegText" + cp.SetString(name) if _, ok := t.Rules[name]; !ok { emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount} emptyRule.PushBack(&node{Type: TypeNil, string: ""}) @@ -903,7 +910,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { t.RuleNames = append(t.RuleNames, emptyRule) countsByRule = append(countsByRule, &[TypeLast]uint{}) } - n.PushBack(copy) + n.PushBack(cp) fallthrough case TypeImplicitPush: link(countsForRule, n.Front()) @@ -927,10 +934,10 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { case TypeRule: if _, ok := t.Rules[node.String()]; !ok { expression := node.Front() - copy := expression.Copy() + cp := expression.Copy() expression.Init() expression.SetType(TypeImplicitPush) - expression.PushBack(copy) + expression.PushBack(cp) expression.PushBack(node.Copy()) t.Rules[node.String()] = node @@ -946,7 +953,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { if node.GetType() == TypeRule { rule = node counts := [TypeLast]uint{} - countsByRule[node.GetId()] = &counts + countsByRule[node.GetID()] = &counts link(&counts, node) } } @@ -960,7 +967,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { countRules = func(node Node) { switch node.GetType() { case TypeRule: - name, id := node.String(), node.GetId() + name, id := node.String(), node.GetID() if count, ok := t.rulesCount[name]; ok { t.rulesCount[name] = count + 1 } else { @@ -1002,7 +1009,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { checkRecursion = func(node Node) bool { switch node.GetType() { case TypeRule: - id := node.GetId() + id := node.GetID() if ruleReached[id] { warn(fmt.Errorf("possible infinite left recursion in rule '%v'", node)) return false @@ -1040,7 +1047,8 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { checkRecursion(node) } } - }}) + }, + }) if t._switch { var optimizeAlternates func(node Node) (consumes bool, s *set.Set) @@ -1056,7 +1064,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { /*n.debug()*/ switch n.GetType() { case TypeRule: - cache := &cache[n.GetId()] + cache := &cache[n.GetID()] if cache.reached { consumes, s = cache.consumes, cache.s return @@ -1084,17 +1092,15 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { s.AddRange(lower, upper) case TypeAlternate: consumes = true - mconsumes, properties, c := - consumes, make([]struct { - intersects bool - s *set.Set - }, n.Len()), 0 + properties, c := make([]struct { + intersects bool + s *set.Set + }, n.Len()), 0 for i := range properties { properties[i].s = set.NewSet() } for _, element := range n.Slice() { - mconsumes, properties[c].s = optimizeAlternates(element) - consumes = consumes && mconsumes + consumes, properties[c].s = optimizeAlternates(element) s = s.Union(properties[c].s) c++ } @@ -1118,8 +1124,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { break } - c, unordered, ordered, max := - 0, &node{Type: TypeUnorderedAlternate}, &node{Type: TypeAlternate}, 0 + c, unordered, ordered, maxVal := 0, &node{Type: TypeUnorderedAlternate}, &node{Type: TypeAlternate}, 0 for _, element := range n.Slice() { if properties[c].intersects { ordered.PushBack(element.Copy()) @@ -1131,8 +1136,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { } } - sequence, predicate, length := - &node{Type: TypeSequence}, &node{Type: TypePeekFor}, properties[c].s.Len() + sequence, predicate, length := &node{Type: TypeSequence}, &node{Type: TypePeekFor}, properties[c].s.Len() if length == 0 { class.PushBack(&node{Type: TypeNil, string: ""}) } @@ -1142,9 +1146,9 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { if element.GetType() == TypeNil { unordered.PushBack(sequence) - } else if length > max { + } else if length > maxVal { unordered.PushBack(sequence) - max = length + maxVal = length } else { unordered.PushFront(sequence) } @@ -1164,10 +1168,9 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { n.PushBack(unordered) } case TypeSequence: - classes, elements := - make([]struct { - s *set.Set - }, n.Len()), n.Slice() + classes, elements := make([]struct { + s *set.Set + }, n.Len()), n.Slice() for i := range classes { classes[i].s = set.NewSet() } @@ -1193,7 +1196,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { case TypePlus, TypePush, TypeImplicitPush: consumes, s = optimizeAlternates(n.Front()) case TypeAction, TypeNil: - //empty + // empty } return } @@ -1232,21 +1235,18 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { fileSet := token.NewFileSet() code, err := parser.ParseFile(fileSet, file, &buffer, parser.ParseComments) if err != nil { - buffer.WriteTo(out) - err = fmt.Errorf("%v: %v", file, err) + _, _ = buffer.WriteTo(out) return } formatter := printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8} err = formatter.Fprint(out, fileSet, code) if err != nil { - buffer.WriteTo(out) - err = fmt.Errorf("%v: %v", file, err) + _, _ = buffer.WriteTo(out) return } - }() - _print := func(format string, a ...interface{}) { fmt.Fprintf(&buffer, format, a...) } + _print := func(format string, a ...any) { _, _ = fmt.Fprintf(&buffer, format, a...) } printSave := func(n uint) { _print("\n position%d, tokenIndex%d := position, tokenIndex", n, n) } printRestore := func(n uint) { _print("\n position, tokenIndex = position%d, tokenIndex%d", n, n) } printMemoSave := func(rule int, n uint, ret bool) { @@ -1391,7 +1391,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { compile(element, ko) return } - _print("\n if !_rules[rule%v]() {", name /*rule.GetId()*/) + _print("\n if !_rules[rule%v]() {", name /*rule.GetID()*/) printJump(ko) _print("}") case TypeRange: @@ -1601,7 +1601,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { } /* lets figure out which jump labels are going to be used with this dry compile */ - printTemp, _print := _print, func(format string, a ...interface{}) {} + printTemp, _print := _print, func(_ string, _ ...any) {} for _, element := range t.Slice() { if element.GetType() != TypeRule { continue @@ -1649,7 +1649,7 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { } ko := label label++ - _print("\n /* %v ", element.GetId()) + _print("\n /* %v ", element.GetID()) printRule(element) _print(" */") if count, ok := t.rulesCount[element.String()]; !ok { @@ -1662,21 +1662,21 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) { } _print("\n func() bool {") if t.Ast { - printMemoCheck(element.GetId()) + printMemoCheck(element.GetID()) } if t.Ast || labels[ko] { printSave(ko) } compile(expression, ko) - //print("\n fmt.Printf(\"%v\\n\")", element.String()) + // print("\n fmt.Printf(\"%v\\n\")", element.String()) if t.Ast { - printMemoSave(element.GetId(), ko, true) + printMemoSave(element.GetID(), ko, true) } _print("\n return true") if labels[ko] { printLabel(ko) if t.Ast { - printMemoSave(element.GetId(), ko, false) + printMemoSave(element.GetID(), ko, false) } printRestore(ko) _print("\n return false")