Compare commits
7 Commits
e489dc4c27
...
v0.6.1
Author | SHA1 | Date | |
---|---|---|---|
fc0af1ccc5 | |||
980fb77114 | |||
4c4d747a9c | |||
595b86df60 | |||
5f9bab528a | |||
530564b920 | |||
02b3b469c4 |
2
Makefile
2
Makefile
@@ -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 ./...
|
||||||
|
@@ -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).
|
||||||
|
@@ -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,12 +92,22 @@ 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)
|
||||||
|
} else {
|
||||||
|
fileStat, err := inputFile.Stat()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%v\n", err)
|
||||||
os.Exit(2)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var test_str string
|
var test_str string
|
||||||
var err error
|
var err error
|
||||||
@@ -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,8 +218,13 @@ func main() {
|
|||||||
if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 {
|
if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 {
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
|
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
|
color.New(color.FgMagenta).Fprintf(out, "%s:", inputFile.Name()) // Print filename
|
||||||
}
|
}
|
||||||
|
if *lineNumFlag {
|
||||||
|
color.New(color.FgGreen).Fprintf(out, "%d:", lineNum) // Print filename
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we are substituting, we need a different behavior, as follows:
|
// If we are substituting, we need a different behavior, as follows:
|
@@ -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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user