-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
49 lines (41 loc) · 770 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/Willyham/gfp/fsm"
"github.com/Willyham/gfp/regex2fsm"
)
func main() {
pattern := os.Args[1]
filename := os.Args[2]
converter := regex2fsm.New()
machine, err := converter.Convert(pattern)
if err != nil {
log.Fatal(err)
}
searchInFile(filename, machine)
}
func searchInFile(filename string, machine *fsm.StateMachine) {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
tokens := strings.Split(line, "")
result := machine.Run(tokens)
if result {
fmt.Println(line)
}
machine.Reset()
}
err = scanner.Err()
if err != nil {
log.Fatal(err)
}
}