Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: remove awk #32

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
vendor/
.vscode/

/removecomments
/removecomments
/snake2camel
10 changes: 4 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ docs: dots parser/docs/minimal_types.png parser/docs/falco_types.png parser/docs

.PHONY: snake2camel
snake2camel:
@awk -i inplace '{ \
while ( match($$0, /(.*)([a-z]+[0-9]*)_([a-zA-Z0-9])(.*)/, cap) ) \
$$0 = cap[1] cap[2] toupper(cap[3]) cap[4]; \
print \
}' $(file)
@go build ./tools/snake2camel

.PHONY: removecomments
removecomments:
Expand Down Expand Up @@ -78,10 +74,12 @@ parser/machine.go: parser/machine.go.rl common/common.rl

parser/machine.go: removecomments

parser/machine.go: snake2camel

parser/machine.go:
$(RAGEL) -Z -G2 -e -o $@ $<
@./removecomments $@
$(MAKE) file=$@ snake2camel
@./snake2camel $@
$(GOFMT) $@

.PHONY: tests
Expand Down
72 changes: 72 additions & 0 deletions tools/snake2camel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2020- Leonardo Di Donato <leodidonato@gmail.com>
package main

import (
"fmt"
"os"
"regexp"
"strings"
)

func exitWithError(msg string) {
err := fmt.Sprintf("error: %s\n", msg)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) (string, int) {
result := ""
lastIndex := 0

for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
if v[i] == -1 || v[i+1] == -1 {
groups = append(groups, "")
} else {
groups = append(groups, str[v[i]:v[i+1]])
}
}

result += str[lastIndex:v[0]] + repl(groups)
lastIndex = v[1]
}

return result + str[lastIndex:], lastIndex
}

func snake2camel(content []byte) []byte {
re := regexp.MustCompile(`(.*)([a-z]+[0-9]*)_([a-zA-Z0-9])(.*)`)
res := string(content)
last := -1

for last != 0 {
res, last = replaceAllStringSubmatchFunc(re, res, func(groups []string) string {
return groups[1] + groups[2] + strings.ToUpper(groups[3]) + groups[4]
})
}

return []byte(res)
}

func snake2camelFile(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return err
}
updated := snake2camel(content)

return os.WriteFile(path, updated, 0)
}

func main() {
if len(os.Args) != 2 {
exitWithError("must be called with the file path as the only argument")
}
path := os.Args[1]
if err := snake2camelFile(path); err != nil {
exitWithError(err.Error())
}
}
Loading