forked from tshprecher/gospell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (159 loc) · 3.95 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"errors"
"flag"
"fmt"
"github.com/tshprecher/gospell/check"
"github.com/tshprecher/gospell/lang"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
defaultMinLength = 5
defaultMaxSwaps = 1
defaultMaxIns = 0
defaultMaxDel = 0
defaultMaxMods = 0
)
var (
minLength = flag.Uint("ml", defaultMinLength, "filter out words less than 'ml' characters")
maxSwaps = flag.Uint("ms", defaultMaxSwaps, "correct spelling up to 'ms' consecutive character swaps")
maxIns = flag.Uint("mi", defaultMaxIns, "correct spelling up to 'mi' character insertions")
maxDel = flag.Uint("md", defaultMaxDel, "correct spelling up to 'md' character deletions")
maxMods = flag.Uint("mm", defaultMaxMods, "correct spelling up to 'mm' character modifications")
// flags for other languages (comments only)
langC = flag.Bool("c", false, "process C files")
langCpp = flag.Bool("cpp", false, "process C++ files")
langJava = flag.Bool("java", false, "process Java files")
langScala = flag.Bool("scala", false, "process Scala files")
fileRegexp *regexp.Regexp
checker check.Checker =
check.And(
check.MinLengthChecker(defaultMinLength),
check.Or(
check.DeltaChecker{1, 0, 0, 0},
check.DeltaChecker{0, 1, 0, 0},
check.DeltaChecker{0, 0, 1, 0},
check.DeltaChecker{0, 0, 0, 1}))
proc processor = cStyleCommentProcessor{checker}
)
func checkError(err error) {
if err != nil {
log.Fatal("error: " + err.Error())
}
}
func isInputFile(finfo os.FileInfo) bool {
return !finfo.IsDir() &&
!strings.HasPrefix(finfo.Name(), ".") &&
fileRegexp.MatchString(finfo.Name())
}
func processFile(filename string, dict check.Dict) (*fileResult, error) {
src, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return proc.run(filename, src, dict)
}
func processDir(dir string, dict check.Dict) error {
var fileFound = false
var visit = func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if isInputFile(f) {
fileFound = true
res, err := processFile(path, dict)
if err != nil {
return err
}
fmt.Print(res)
}
return nil
}
err := filepath.Walk(dir, visit)
if err != nil {
return err
}
if !fileFound {
fmt.Println("no input files found.")
}
return nil
}
// handleFlags parses the command line flags and sets the processor
// on success, returns an error otherwise.
func handleFlags() error {
log.SetFlags(0) // do not prefix message with timestamp
flag.Parse()
if *minLength != defaultMinLength ||
*maxSwaps != defaultMaxSwaps ||
*maxIns != defaultMaxIns ||
*maxDel != defaultMaxDel ||
*maxMods != defaultMaxMods {
// if any of the default value are set, override
// the default checker
checker = check.And(
check.MinLengthChecker(*minLength),
check.DeltaChecker{
*maxIns,
*maxDel,
*maxSwaps,
*maxMods,
})
}
r, _ := regexp.Compile(".*\\.go$")
fileRegexp = r
var altLang uint8 = 0
var langs = []struct {
isSet bool
pattern string
}{
{*langC, ".*\\.c$"},
{*langCpp, "(.*\\.cc$)|(.*\\.cpp$)"},
{*langJava, ".*\\.java$"},
{*langScala, ".*\\.scala$"},
}
for _, l := range langs {
if l.isSet {
altLang++
r, _ := regexp.Compile(l.pattern)
fileRegexp = r
}
}
if altLang > 1 {
return errors.New("cannot specify multiple languages.")
}
if altLang > 0 {
proc = cStyleCommentProcessor{checker}
}
return nil
}
func main() {
checkError(handleFlags())
if flag.NArg() == 0 {
log.Fatal("error: no files found.")
}
for a := 0; a < flag.NArg(); a++ {
filename := flag.Arg(a)
fileInfo, err := os.Stat(filename)
checkError(err)
dict := check.NewTrie(lang.EnglishAlphabet)
for _, word := range lang.EnglishUsWords {
// disregard unpopular words
if word.Popularity >= 2 {
dict.Add(word.Word)
}
}
if fileInfo.IsDir() {
err = processDir(filename, dict)
checkError(err)
} else {
res, err := processFile(filename, dict)
checkError(err)
fmt.Print(res)
}
}
}