aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/mode/pig/pig.js
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/mode/pig/pig.js')
-rw-r--r--imports/codemirror/mode/pig/pig.js172
1 files changed, 172 insertions, 0 deletions
diff --git a/imports/codemirror/mode/pig/pig.js b/imports/codemirror/mode/pig/pig.js
new file mode 100644
index 00000000..b7399b43
--- /dev/null
+++ b/imports/codemirror/mode/pig/pig.js
@@ -0,0 +1,172 @@
1/*
2 * Pig Latin Mode for CodeMirror 2
3 * @author Prasanth Jayachandran
4 * @link https://github.com/prasanthj/pig-codemirror-2
5 * This implementation is adapted from PL/SQL mode in CodeMirror 2.
6*/
7CodeMirror.defineMode("pig", function(config, parserConfig) {
8 var indentUnit = config.indentUnit,
9 keywords = parserConfig.keywords,
10 builtins = parserConfig.builtins,
11 types = parserConfig.types,
12 multiLineStrings = parserConfig.multiLineStrings;
13
14 var isOperatorChar = /[*+\-%<>=&?:\/!|]/;
15
16 function chain(stream, state, f) {
17 state.tokenize = f;
18 return f(stream, state);
19 }
20
21 var type;
22 function ret(tp, style) {
23 type = tp;
24 return style;
25 }
26
27 function tokenComment(stream, state) {
28 var isEnd = false;
29 var ch;
30 while(ch = stream.next()) {
31 if(ch == "/" && isEnd) {
32 state.tokenize = tokenBase;
33 break;
34 }
35 isEnd = (ch == "*");
36 }
37 return ret("comment", "comment");
38 }
39
40 function tokenString(quote) {
41 return function(stream, state) {
42 var escaped = false, next, end = false;
43 while((next = stream.next()) != null) {
44 if (next == quote && !escaped) {
45 end = true; break;
46 }
47 escaped = !escaped && next == "\\";
48 }
49 if (end || !(escaped || multiLineStrings))
50 state.tokenize = tokenBase;
51 return ret("string", "error");
52 };
53 }
54
55 function tokenBase(stream, state) {
56 var ch = stream.next();
57
58 // is a start of string?
59 if (ch == '"' || ch == "'")
60 return chain(stream, state, tokenString(ch));
61 // is it one of the special chars
62 else if(/[\[\]{}\(\),;\.]/.test(ch))
63 return ret(ch);
64 // is it a number?
65 else if(/\d/.test(ch)) {
66 stream.eatWhile(/[\w\.]/);
67 return ret("number", "number");
68 }
69 // multi line comment or operator
70 else if (ch == "/") {
71 if (stream.eat("*")) {
72 return chain(stream, state, tokenComment);
73 }
74 else {
75 stream.eatWhile(isOperatorChar);
76 return ret("operator", "operator");
77 }
78 }
79 // single line comment or operator
80 else if (ch=="-") {
81 if(stream.eat("-")){
82 stream.skipToEnd();
83 return ret("comment", "comment");
84 }
85 else {
86 stream.eatWhile(isOperatorChar);
87 return ret("operator", "operator");
88 }
89 }
90 // is it an operator
91 else if (isOperatorChar.test(ch)) {
92 stream.eatWhile(isOperatorChar);
93 return ret("operator", "operator");
94 }
95 else {
96 // get the while word
97 stream.eatWhile(/[\w\$_]/);
98 // is it one of the listed keywords?
99 if (keywords && keywords.propertyIsEnumerable(stream.current().toUpperCase())) {
100 if (stream.eat(")") || stream.eat(".")) {
101 //keywords can be used as variables like flatten(group), group.$0 etc..
102 }
103 else {
104 return ("keyword", "keyword");
105 }
106 }
107 // is it one of the builtin functions?
108 if (builtins && builtins.propertyIsEnumerable(stream.current().toUpperCase()))
109 {
110 return ("keyword", "variable-2")
111 }
112 // is it one of the listed types?
113 if (types && types.propertyIsEnumerable(stream.current().toUpperCase()))
114 return ("keyword", "variable-3")
115 // default is a 'word'
116 return ret("word", "pig-word");
117 }
118 }
119
120 // Interface
121 return {
122 startState: function(basecolumn) {
123 return {
124 tokenize: tokenBase,
125 startOfLine: true
126 };
127 },
128
129 token: function(stream, state) {
130 if(stream.eatSpace()) return null;
131 var style = state.tokenize(stream, state);
132 return style;
133 }
134 };
135});
136
137(function() {
138 function keywords(str) {
139 var obj = {}, words = str.split(" ");
140 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
141 return obj;
142 }
143
144 // builtin funcs taken from trunk revision 1303237
145 var pBuiltins = "ABS ACOS ARITY ASIN ATAN AVG BAGSIZE BINSTORAGE BLOOM BUILDBLOOM CBRT CEIL "
146 + "CONCAT COR COS COSH COUNT COUNT_STAR COV CONSTANTSIZE CUBEDIMENSIONS DIFF DISTINCT DOUBLEABS "
147 + "DOUBLEAVG DOUBLEBASE DOUBLEMAX DOUBLEMIN DOUBLEROUND DOUBLESUM EXP FLOOR FLOATABS FLOATAVG "
148 + "FLOATMAX FLOATMIN FLOATROUND FLOATSUM GENERICINVOKER INDEXOF INTABS INTAVG INTMAX INTMIN "
149 + "INTSUM INVOKEFORDOUBLE INVOKEFORFLOAT INVOKEFORINT INVOKEFORLONG INVOKEFORSTRING INVOKER "
150 + "ISEMPTY JSONLOADER JSONMETADATA JSONSTORAGE LAST_INDEX_OF LCFIRST LOG LOG10 LOWER LONGABS "
151 + "LONGAVG LONGMAX LONGMIN LONGSUM MAX MIN MAPSIZE MONITOREDUDF NONDETERMINISTIC OUTPUTSCHEMA "
152 + "PIGSTORAGE PIGSTREAMING RANDOM REGEX_EXTRACT REGEX_EXTRACT_ALL REPLACE ROUND SIN SINH SIZE "
153 + "SQRT STRSPLIT SUBSTRING SUM STRINGCONCAT STRINGMAX STRINGMIN STRINGSIZE TAN TANH TOBAG "
154 + "TOKENIZE TOMAP TOP TOTUPLE TRIM TEXTLOADER TUPLESIZE UCFIRST UPPER UTF8STORAGECONVERTER ";
155
156 // taken from QueryLexer.g
157 var pKeywords = "VOID IMPORT RETURNS DEFINE LOAD FILTER FOREACH ORDER CUBE DISTINCT COGROUP "
158 + "JOIN CROSS UNION SPLIT INTO IF OTHERWISE ALL AS BY USING INNER OUTER ONSCHEMA PARALLEL "
159 + "PARTITION GROUP AND OR NOT GENERATE FLATTEN ASC DESC IS STREAM THROUGH STORE MAPREDUCE "
160 + "SHIP CACHE INPUT OUTPUT STDERROR STDIN STDOUT LIMIT SAMPLE LEFT RIGHT FULL EQ GT LT GTE LTE "
161 + "NEQ MATCHES TRUE FALSE ";
162
163 // data types
164 var pTypes = "BOOLEAN INT LONG FLOAT DOUBLE CHARARRAY BYTEARRAY BAG TUPLE MAP "
165
166 CodeMirror.defineMIME("text/x-pig", {
167 name: "pig",
168 builtins: keywords(pBuiltins),
169 keywords: keywords(pKeywords),
170 types: keywords(pTypes)
171 });
172}());