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