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