forked from mkhan45/RustScript
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinter.java
58 lines (55 loc) · 1.5 KB
/
Linter.java
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
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import core.Parser;
public class Linter {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.out.println("Usage: Linter [file(s)]");
return;
}
run(Arrays.asList(args));
}
public static void run(List<String> files) {
System.out.println("Linting " + files.size() + " file(s)...");
int failed = 0;
for (String file : files) {
File f = new File(file);
if (!f.exists()) {
System.out.println("File '" + file + "' does not exist.");
return;
} else if (f.isDirectory()) {
System.out.println("File '" + file + "' is a directory.");
return;
} else if (!f.canRead()) {
System.out.println("File '" + file + "' is not readable.");
return;
}
String source;
try {
source = Files.readString(Paths.get(file), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
continue;
}
try {
Parser.parseExprs(source);
} catch (Exception e) {
System.out.println(String.format("""
Syntax error in '%s':
%s
""", file, e.getMessage()));
failed++;
}
}
if (failed == 0) {
System.out.println("All files passed!");
} else {
System.out.println("Found " + failed + " errors.");
}
}
}