fix unexported code generation
This commit is contained in:
parent
ab17c3b195
commit
3474d3615b
25
Makefile
25
Makefile
@ -32,6 +32,17 @@ head: $(SOURCES) fmt
|
||||
syntaxhead.go \
|
||||
> head.go
|
||||
@gofmt -s -w head.go
|
||||
go run scripts/createhead.go --exported -- \
|
||||
char.go \
|
||||
sequence.go \
|
||||
choice.go \
|
||||
idset.go \
|
||||
results.go \
|
||||
context.go \
|
||||
nodehead.go \
|
||||
syntaxhead.go \
|
||||
> headexported.go
|
||||
@gofmt -s -w headexported.go
|
||||
|
||||
generate: $(SOURCES) $(PARSERS) fmt head install
|
||||
treerack generate -export -package-name self < syntax.treerack > self/self.go.next
|
||||
@ -48,8 +59,8 @@ regenerate: $(SOURCES) $(PARSERS) fmt head install
|
||||
|
||||
check-generate: $(SOURCES) $(PARSERS)
|
||||
@echo checking head
|
||||
@mv head.go head.go.backup
|
||||
@go run scripts/createhead.go -- \
|
||||
@mv headexported.go headexported.go.backup
|
||||
@go run scripts/createhead.go --exported -- \
|
||||
char.go \
|
||||
sequence.go \
|
||||
choice.go \
|
||||
@ -58,10 +69,10 @@ check-generate: $(SOURCES) $(PARSERS)
|
||||
context.go \
|
||||
nodehead.go \
|
||||
syntaxhead.go \
|
||||
> head.go
|
||||
@gofmt -s -w head.go
|
||||
@if ! diff head.go head.go.backup > /dev/null; then \
|
||||
mv head.go.backup head.go; \
|
||||
> headexported.go
|
||||
@gofmt -s -w headexported.go
|
||||
@if ! diff headexported.go headexported.go.backup > /dev/null; then \
|
||||
mv headexported.go.backup headexported.go; \
|
||||
echo head does not match; \
|
||||
false; \
|
||||
fi
|
||||
@ -77,7 +88,7 @@ check-generate: $(SOURCES) $(PARSERS)
|
||||
fi
|
||||
|
||||
@echo ok
|
||||
@mv head.go.backup head.go
|
||||
@mv headexported.go.backup headexported.go
|
||||
@mv self/self.go.backup self/self.go
|
||||
|
||||
check: build $(PARSERS)
|
||||
|
2
go.mod
2
go.mod
@ -5,6 +5,8 @@ go 1.24.6
|
||||
require golang.org/x/crypto v0.41.0
|
||||
|
||||
require (
|
||||
code.squareroundforest.org/arpio/notation v0.0.0-20241225183158-af3bd591a174 // indirect
|
||||
github.com/iancoleman/strcase v0.3.0 // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
golang.org/x/term v0.34.0 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -1,3 +1,7 @@
|
||||
code.squareroundforest.org/arpio/notation v0.0.0-20241225183158-af3bd591a174 h1:DKMSagVY3uyRhJ4ohiwQzNnR6CWdVKLkg97A8eQGxQU=
|
||||
code.squareroundforest.org/arpio/notation v0.0.0-20241225183158-af3bd591a174/go.mod h1:ait4Fvg9o0+bq5hlxi9dAcPL5a+/sr33qsZPNpToMLY=
|
||||
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
|
||||
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
|
||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||
|
4
headexported.go
Normal file
4
headexported.go
Normal file
File diff suppressed because one or more lines are too long
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/iancoleman/strcase"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
@ -11,11 +12,36 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func concatGo(w io.Writer, r ...io.Reader) error {
|
||||
var exportedSymbols = []string{
|
||||
"ErrInvalidUnicodeCharacter",
|
||||
"CommitType",
|
||||
"None",
|
||||
"Alias",
|
||||
"Whitespace",
|
||||
"NoWhitespace",
|
||||
"Keyword",
|
||||
"NoKeyword",
|
||||
"FailPass",
|
||||
"Root",
|
||||
"Node",
|
||||
"ParseError",
|
||||
}
|
||||
|
||||
func unexporter() map[string]*regexp.Regexp {
|
||||
m := make(map[string]*regexp.Regexp)
|
||||
for _, name := range exportedSymbols {
|
||||
m[name] = regexp.MustCompile(fmt.Sprintf("\\b%s\\b", name))
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func concatGo(w io.Writer, exported bool, r ...io.Reader) error {
|
||||
var others []ast.Decl
|
||||
imports := &ast.GenDecl{
|
||||
Tok: token.IMPORT,
|
||||
@ -74,8 +100,13 @@ func removePackage(gO string) string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
var packageName string
|
||||
var (
|
||||
packageName string
|
||||
exported bool
|
||||
)
|
||||
|
||||
flag.StringVar(&packageName, "package", "treerack", "package name of the generated code file")
|
||||
flag.BoolVar(&exported, "exported", false, "export required symbols")
|
||||
flag.Parse()
|
||||
|
||||
var files []io.Reader
|
||||
@ -90,13 +121,24 @@ func main() {
|
||||
}
|
||||
|
||||
var headCode bytes.Buffer
|
||||
if err := concatGo(&headCode, files...); err != nil {
|
||||
if err := concatGo(&headCode, exported, files...); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
code := headCode.String()
|
||||
code = removePackage(code)
|
||||
quotedCode := strconv.Quote(code)
|
||||
|
||||
fmt.Printf("package %s\n\n// generated with scripts/createhead.go\nconst headCode=%s", packageName, quotedCode)
|
||||
if !exported {
|
||||
unexp := unexporter()
|
||||
for name, exp := range unexp {
|
||||
code = exp.ReplaceAllString(code, strcase.ToLowerCamel(name))
|
||||
}
|
||||
}
|
||||
|
||||
quotedCode := strconv.Quote(code)
|
||||
varName := "headCode"
|
||||
if exported {
|
||||
varName = "headCodeExported"
|
||||
}
|
||||
|
||||
fmt.Printf("package %s\n\n// generated with scripts/createhead.go\nconst %s=%s", packageName, varName, quotedCode)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user