-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror_checks.go
More file actions
53 lines (48 loc) · 1.36 KB
/
Copy patherror_checks.go
File metadata and controls
53 lines (48 loc) · 1.36 KB
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
package main
import (
"log"
"net/http"
"regexp"
)
// Checks if the caller is trying to write a package or main function
// Returns appropriate error if they are.
// This will change later to still allow writing a main function or package
func (p *Program) checkErrors(input Cell, w http.ResponseWriter) (ok bool) {
// If a main function exists return error
checkMain, err := regexp.MatchString(`\s*func\s+main\s*\(\s*\)\s*{`, input.Contents)
if err != nil {
log.Println(err)
}
if checkMain {
_, err := w.Write([]byte("exit status 3\nMain function is generated automatically. Please remove func main()"))
if err != nil {
log.Println(err)
}
return false
}
// If import statement exists return error
checkImport, err := regexp.MatchString(`\s*import\s+[("]`, input.Contents)
if err != nil {
log.Println(err)
}
if checkImport {
_, err := w.Write([]byte("exit status 3\nImports are done automatically. Please remove import statement"))
if err != nil {
log.Println(err)
}
return false
}
// Check if user declaring a package
checkPackage, err := regexp.MatchString(`\s*package\s+\w*\n"]`, input.Contents)
if err != nil {
log.Println(err)
}
if checkPackage {
_, err = w.Write([]byte("exit status 3\nAre package is generated automatically. Please remove package statement"))
if err != nil {
log.Println(err)
}
return false
}
return true
}