-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.java
More file actions
105 lines (87 loc) · 2.95 KB
/
Copy pathConsole.java
File metadata and controls
105 lines (87 loc) · 2.95 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
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
// Kimberley Ni and Eylul Oktay
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Console {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
Lexer lexer = new Lexer();
Parser parser = new Parser();
String input = cleanConsoleInput(); // see comment
while (!input.equalsIgnoreCase("exit")) {
ArrayList<String> tokens = lexer.tokenize(input);
tokens = Parser.preparser(tokens);
if (tokens.size() > 0) {
String output = "";
try {
Expression exp = parser.parse(tokens);
if (tokens.contains("=")) {
output = Parser.lastVariableAssignResult();
} else {
output = exp.toString();
}
} catch (Exception e) {
System.out.println("Unparsable expression, input was: \"" + input + "\"");
e.printStackTrace();
input = cleanConsoleInput();
continue;
}
System.out.println(output);
}
input = cleanConsoleInput();
}
System.out.println("Goodbye!");
}
/*
* Collects user input, and ...
* ... does a bit of raw string processing to (1) strip away comments,
* (2) remove the BOM character that appears in unicode strings in Windows,
* (3) turn all weird whitespace characters into spaces,
* and (4) replace all λs with backslashes.
*/
private static String cleanConsoleInput() {
System.out.print("> ");
String raw = in.nextLine();
String deBOMified = raw.replaceAll("\uFEFF", ""); // remove Byte Order Marker from UTF
String clean = removeWeirdWhitespace(deBOMified);
return clean.replaceAll("λ", "\\\\");
}
public static String removeWeirdWhitespace(String input) {
String whitespace_chars = "" // dummy empty string for homogeneity
+ "\\u0009" // CHARACTER TABULATION
+ "\\u000A" // LINE FEED (LF)
+ "\\u000B" // LINE TABULATION
+ "\\u000C" // FORM FEED (FF)
+ "\\u000D" // CARRIAGE RETURN (CR)
+ "\\u0020" // SPACE
+ "\\u0085" // NEXT LINE (NEL)
+ "\\u00A0" // NO-BREAK SPACE
+ "\\u1680" // OGHAM SPACE MARK
+ "\\u180E" // MONGOLIAN VOWEL SEPARATOR
+ "\\u2000" // EN QUAD
+ "\\u2001" // EM QUAD
+ "\\u2002" // EN SPACE
+ "\\u2003" // EM SPACE
+ "\\u2004" // THREE-PER-EM SPACE
+ "\\u2005" // FOUR-PER-EM SPACE
+ "\\u2006" // SIX-PER-EM SPACE
+ "\\u2007" // FIGURE SPACE
+ "\\u2008" // PUNCTUATION SPACE
+ "\\u2009" // THIN SPACE
+ "\\u200A" // HAIR SPACE
+ "\\u2028" // LINE SEPARATOR
+ "\\u2029" // PARAGRAPH SEPARATOR
+ "\\u202F" // NARROW NO-BREAK SPACE
+ "\\u205F" // MEDIUM MATHEMATICAL SPACE
+ "\\u3000"; // IDEOGRAPHIC SPACE
Pattern whitespace = Pattern.compile(whitespace_chars);
Matcher matcher = whitespace.matcher(input);
String result = input;
if (matcher.find()) {
result = matcher.replaceAll(" ");
}
return result;
}
}