-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathCodePattern.java
More file actions
112 lines (104 loc) · 4.81 KB
/
Copy pathCodePattern.java
File metadata and controls
112 lines (104 loc) · 4.81 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
106
107
108
109
110
111
112
package org.qpython.qpy.utils;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Hmei
* 2018/4/19.
*/
public class CodePattern {
public static final int COLOR_ERROR = 0x80ff0000;
public static final int COLOR_NUMBER = 0xff7ba212;
public static final int COLOR_KEYWORD = 0xff7ba212;
public static final int COLOR_BUILTIN = 0xffd79e39;
public static final int COLOR_COMMENT = 0xff808080;
public static final int COLOR_QUOTE = 0xff399ed7;
public static final Pattern PATTERN_LINE = Pattern.compile(
".*\\n");
public static final Pattern PATTERN_NUMBER = Pattern.compile(
"\\b(\\d*[.]?\\d+)\\b");
public static final Pattern PATTERN_PY_KEYWORD = Pattern.compile(
"\\b(break|continue|del|" +
"except|exec|finally|" +
"pass|print|raise|" +
"return|try|with|" +
"global|assert|" +
"lambda|yield|" +
"def|class|self|" +
"for|while|" +
"if|elif|else|" +
"and|in|is|not|or|" +
"import|from|as)\\b");
public static final Pattern PATTERN_LUA_KEYWORD = Pattern.compile(
"\\b(and|break|do|" +
"else|elseif|end|" +
"for|function|" +
"goto|if|in|" +
"local|" +
"not|or|" +
"repeat|return|then|" +
"until|" +
"while)\\b");
public static final Pattern PATTERN_LUA_BUILD_IN = Pattern.compile(
"\\b(print|false|true|nil)\\b");
public static final Pattern PATTERN_PY_BUILD_IN = Pattern.compile(
"\\b(True|False|bool|enumerate|set|frozenset|help|" +
"reversed|sorted|sum|" +
"Ellipsis|None|NotImplemented|__import__|abs|" +
"apply|buffer|callable|chr|classmethod|cmp|" +
"coerce|compile|complex|delattr|dict|dir|divmod|" +
"eval|execfile|file|filter|float|getattr|globals|" +
"hasattr|hash|hex|id|input|int|intern|isinstance|" +
"issubclass|iter|len|list|locals|long|map|max|" +
"min|object|oct|open|ord|pow|property|range|" +
"raw_input|reduce|reload|repr|round|setattr|" +
"slice|staticmethod|str|super|tuple|type|unichr|" +
"unicode|vars|xrange|zip|" +
"ArithmeticError|AssertionError|AttributeError|" +
"DeprecationWarning|EOFError|EnvironmentError|" +
"Exception|FloatingPointError|IOError|" +
"ImportError|IndentationError|IndexError|" +
"KeyError|KeyboardInterrupt|LookupError|" +
"MemoryError|NameError|NotImplementedError|" +
"OSError|OverflowError|OverflowWarning|" +
"ReferenceError|RuntimeError|RuntimeWarning|" +
"StandardError|StopIteration|SyntaxError|" +
"SyntaxWarning|SystemError|SystemExit|TabError|" +
"TypeError|UnboundLocalError|UnicodeError|" +
"UnicodeEncodeError|UnicodeDecodeError|" +
"UnicodeTranslateError|" +
"UserWarning|ValueError|Warning|WindowsError|" +
"ZeroDivisionError)\\b");
public static final Pattern PATTERN_PY_COMMENT = Pattern.compile(
"/\\*(?:.|[\\n\\r])*?\\*/|" +
"#.*\n|" +
"\"\"\"(?:.|[\\n\\r])*?\"\"\"|" +
"\'\'\'(?:.|[\\n\\r])*?\'\'\'");
public static SpannableString formatPyCode(String code) {
SpannableString sStr = new SpannableString(code);
for (Matcher m = PATTERN_PY_KEYWORD.matcher(sStr); m.find(); ) {
sStr.setSpan(
new ForegroundColorSpan(COLOR_KEYWORD),
m.start(),
m.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
for (Matcher m = PATTERN_PY_BUILD_IN.matcher(sStr); m.find(); ) {
sStr.setSpan(
new ForegroundColorSpan(COLOR_BUILTIN),
m.start(),
m.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
for (Matcher m = PATTERN_PY_COMMENT.matcher(sStr); m.find(); ) {
sStr.setSpan(
new ForegroundColorSpan(COLOR_COMMENT),
m.start(),
m.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return sStr;
}
}