Compare commits

..

8 Commits

3
.gitignore vendored

@ -1,2 +1 @@
re kg/kg

@ -5,9 +5,13 @@ fmt:
go fmt ./... go fmt ./...
vet: fmt vet: fmt
go vet ./... go vet ./...
buildLib: vet buildLibUnopt: vet
go build -gcflags="all=-N -l" ./... go build -gcflags="all=-N -l" ./...
buildCmd: buildLib unopt: buildLibUnopt
go build -C kg/ -gcflags="all=-N -l" -o kg ./... go build -C kg/ -gcflags="all=-N -l" -o kg ./...
buildLib: vet
go build ./...
buildCmd: buildLib
go build -C kg/ -o kg ./...
test: buildCmd test: buildCmd
go test -v ./... go test -v ./...

@ -15,3 +15,7 @@ It also includes features not present in regexp, such as lookarounds and backref
The syntax is, for the most part, a superset of Go's regexp. A full overview of the syntax can be found [here](https://pkg.go.dev/gitea.twomorecents.org/Rockingcool/kleingrep/regex#hdr-Syntax). The syntax is, for the most part, a superset of Go's regexp. A full overview of the syntax can be found [here](https://pkg.go.dev/gitea.twomorecents.org/Rockingcool/kleingrep/regex#hdr-Syntax).
__For more information, see https://pkg.go.dev/gitea.twomorecents.org/Rockingcool/kleingrep/regex__. __For more information, see https://pkg.go.dev/gitea.twomorecents.org/Rockingcool/kleingrep/regex__.
### How it works
I've written about the inner workings of the engine [on my blog](https://twomorecents.org/writing-regex-engine/index.html).

@ -1,6 +1,11 @@
package main package main
import "slices" import (
"bufio"
"os"
"slices"
"strings"
)
type character interface { type character interface {
int | rune int | rune
@ -25,3 +30,23 @@ func genRange[T character](start, end T) []T {
} }
return toRet return toRet
} }
// Returns whether or not the given file contains a NULL character
func fileContainsNullChar(filename string) (bool, error) {
file, err := os.Open(filename)
if err != nil {
return true, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "\000") {
return true, nil
}
}
if err := scanner.Err(); err != nil {
return true, err
}
return false, nil
}

@ -5,7 +5,9 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath"
"slices" "slices"
"github.com/fatih/color" "github.com/fatih/color"
@ -91,6 +93,7 @@ func main() {
inputFilenames := flag.Args()[1:] inputFilenames := flag.Args()[1:]
for _, inputFilename := range inputFilenames { for _, inputFilename := range inputFilenames {
inputFile, err := os.Open(inputFilename) inputFile, err := os.Open(inputFilename)
defer inputFile.Close()
if err != nil { if err != nil {
fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename) fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename)
} else { } else {
@ -100,7 +103,33 @@ func main() {
os.Exit(2) os.Exit(2)
} else { } else {
if fileStat.Mode().IsDir() { if fileStat.Mode().IsDir() {
if *recursiveFlag {
// Walk the directory and open every file in it. Add each file to the filelist.
filepath.WalkDir(inputFilename, func(filename string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
f, err := os.Open(filename)
if err != nil {
return err
}
inputFiles = append(inputFiles, f)
}
return nil
})
} else {
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename) fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
}
} else {
var nullCharPresent bool
if nullCharPresent, err = fileContainsNullChar(inputFilename); nullCharPresent {
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
} else {
fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename)
}
} else { } else {
inputFiles = append(inputFiles, inputFile) inputFiles = append(inputFiles, inputFile)
} }
@ -108,6 +137,10 @@ func main() {
} }
} }
} }
}
if len(inputFiles) == 0 { // No valid files given
os.Exit(2)
}
var test_str string var test_str string
var err error var err error

@ -131,13 +131,9 @@ func newEscapedNode(c rune, inCharClass bool) (postfixNode, error) {
case 'v': // Vertical tab case 'v': // Vertical tab
toReturn.nodetype = characterNode toReturn.nodetype = characterNode
toReturn.contents = append(toReturn.contents, rune(11)) toReturn.contents = append(toReturn.contents, rune(11))
case '-': // Literal hyphen - only in character class case '-': // Literal hyphen
if inCharClass {
toReturn.nodetype = characterNode toReturn.nodetype = characterNode
toReturn.contents = append(toReturn.contents, '-') toReturn.contents = append(toReturn.contents, '-')
} else {
return postfixNode{}, fmt.Errorf("invalid escape character")
}
default: // None of the above - append it as a regular character default: // None of the above - append it as a regular character
if isNormalChar(c) { // Normal characters cannot be escaped if isNormalChar(c) { // Normal characters cannot be escaped
return postfixNode{}, fmt.Errorf("invalid escape character") return postfixNode{}, fmt.Errorf("invalid escape character")

Loading…
Cancel
Save