7 Commits

6 changed files with 33 additions and 7 deletions

View File

@@ -8,6 +8,6 @@ vet: fmt
buildLib: vet buildLib: vet
go build -gcflags="all=-N -l" ./... go build -gcflags="all=-N -l" ./...
buildCmd: buildLib buildCmd: buildLib
go build -C cmd/ -gcflags="all=-N -l" -o re ./... go build -C kg/ -gcflags="all=-N -l" -o kg ./...
test: buildCmd test: buildCmd
go test -v ./... go test -v ./...

View File

@@ -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).

View File

@@ -61,6 +61,11 @@ func main() {
panic("Invalid match number to print.") panic("Invalid match number to print.")
} }
// Enable lineFlag if lineNumFlag is enabled
if *lineNumFlag {
*lineFlag = true
}
// Process: // Process:
// 1. Convert regex into postfix notation (Shunting-Yard algorithm) // 1. Convert regex into postfix notation (Shunting-Yard algorithm)
// a. Add explicit concatenation operators to facilitate this // a. Add explicit concatenation operators to facilitate this
@@ -68,11 +73,11 @@ func main() {
// 3. Run the string against the NFA // 3. Run the string against the NFA
if len(flag.Args()) < 1 { // flag.Args() also strips out program name if len(flag.Args()) < 1 { // flag.Args() also strips out program name
fmt.Println("ERROR: Missing cmdline args") fmt.Printf("%s: ERROR: Missing cmdline args\n", os.Args[0])
os.Exit(22) os.Exit(22)
} }
if *recursiveFlag && len(flag.Args()) < 2 { // File/Directory must be provided with '-r' if *recursiveFlag && len(flag.Args()) < 2 { // File/Directory must be provided with '-r'
fmt.Println("ERROR: Missing cmdline args") fmt.Printf("%s: ERROR: Missing cmdline args\n", os.Args[0])
os.Exit(22) os.Exit(22)
} }
var re string var re string
@@ -87,10 +92,20 @@ func main() {
for _, inputFilename := range inputFilenames { for _, inputFilename := range inputFilenames {
inputFile, err := os.Open(inputFilename) inputFile, err := os.Open(inputFilename)
if err != nil { if err != nil {
fmt.Printf("%s: No such file or directory\n", flag.Args()[1]) fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename)
os.Exit(2) } else {
fileStat, err := inputFile.Stat()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
} else {
if fileStat.Mode().IsDir() {
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
} else {
inputFiles = append(inputFiles, inputFile)
}
}
} }
inputFiles = append(inputFiles, inputFile)
} }
} }
@@ -108,6 +123,7 @@ func main() {
} }
for _, inputFile := range inputFiles { for _, inputFile := range inputFiles {
lineNum = 0
reader := bufio.NewReader(inputFile) reader := bufio.NewReader(inputFile)
linesRead = false linesRead = false
for true { for true {
@@ -202,7 +218,12 @@ func main() {
if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 { if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 {
continue continue
} else { } else {
color.New(color.FgMagenta).Fprintf(out, "%s: ", inputFile.Name()) // Print filename if *recursiveFlag || len(flag.Args()) > 2 { // If we have 2 args, then we're only searching 1 file. We should only print the filename if there's more than 1 file.
color.New(color.FgMagenta).Fprintf(out, "%s:", inputFile.Name()) // Print filename
}
if *lineNumFlag {
color.New(color.FgGreen).Fprintf(out, "%d:", lineNum) // Print filename
}
} }
} }

View File

@@ -47,6 +47,7 @@ func (re *Reg) UnmarshalText(text []byte) error {
return err return err
} }
// Longest makes future searches prefer the longest branch of an alternation, as opposed to the leftmost branch.
func (re *Reg) Longest() { func (re *Reg) Longest() {
re.preferLongest = true re.preferLongest = true
} }