plsql.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. CodeMirror.defineMode("plsql", function(_config, parserConfig) {
  2. var keywords = parserConfig.keywords,
  3. functions = parserConfig.functions,
  4. types = parserConfig.types,
  5. sqlplus = parserConfig.sqlplus,
  6. multiLineStrings = parserConfig.multiLineStrings;
  7. var isOperatorChar = /[+\-*&%=<>!?:\/|]/;
  8. function chain(stream, state, f) {
  9. state.tokenize = f;
  10. return f(stream, state);
  11. }
  12. var type;
  13. function ret(tp, style) {
  14. type = tp;
  15. return style;
  16. }
  17. function tokenBase(stream, state) {
  18. var ch = stream.next();
  19. // start of string?
  20. if (ch == '"' || ch == "'")
  21. return chain(stream, state, tokenString(ch));
  22. // is it one of the special signs []{}().,;? Seperator?
  23. else if (/[\[\]{}\(\),;\.]/.test(ch))
  24. return ret(ch);
  25. // start of a number value?
  26. else if (/\d/.test(ch)) {
  27. stream.eatWhile(/[\w\.]/);
  28. return ret("number", "number");
  29. }
  30. // multi line comment or simple operator?
  31. else if (ch == "/") {
  32. if (stream.eat("*")) {
  33. return chain(stream, state, tokenComment);
  34. }
  35. else {
  36. stream.eatWhile(isOperatorChar);
  37. return ret("operator", "operator");
  38. }
  39. }
  40. // single line comment or simple operator?
  41. else if (ch == "-") {
  42. if (stream.eat("-")) {
  43. stream.skipToEnd();
  44. return ret("comment", "comment");
  45. }
  46. else {
  47. stream.eatWhile(isOperatorChar);
  48. return ret("operator", "operator");
  49. }
  50. }
  51. // pl/sql variable?
  52. else if (ch == "@" || ch == "$") {
  53. stream.eatWhile(/[\w\d\$_]/);
  54. return ret("word", "variable");
  55. }
  56. // is it a operator?
  57. else if (isOperatorChar.test(ch)) {
  58. stream.eatWhile(isOperatorChar);
  59. return ret("operator", "operator");
  60. }
  61. else {
  62. // get the whole word
  63. stream.eatWhile(/[\w\$_]/);
  64. // is it one of the listed keywords?
  65. if (keywords && keywords.propertyIsEnumerable(stream.current().toLowerCase())) return ret("keyword", "keyword");
  66. // is it one of the listed functions?
  67. if (functions && functions.propertyIsEnumerable(stream.current().toLowerCase())) return ret("keyword", "builtin");
  68. // is it one of the listed types?
  69. if (types && types.propertyIsEnumerable(stream.current().toLowerCase())) return ret("keyword", "variable-2");
  70. // is it one of the listed sqlplus keywords?
  71. if (sqlplus && sqlplus.propertyIsEnumerable(stream.current().toLowerCase())) return ret("keyword", "variable-3");
  72. // default: just a "variable"
  73. return ret("word", "variable");
  74. }
  75. }
  76. function tokenString(quote) {
  77. return function(stream, state) {
  78. var escaped = false, next, end = false;
  79. while ((next = stream.next()) != null) {
  80. if (next == quote && !escaped) {end = true; break;}
  81. escaped = !escaped && next == "\\";
  82. }
  83. if (end || !(escaped || multiLineStrings))
  84. state.tokenize = tokenBase;
  85. return ret("string", "plsql-string");
  86. };
  87. }
  88. function tokenComment(stream, state) {
  89. var maybeEnd = false, ch;
  90. while (ch = stream.next()) {
  91. if (ch == "/" && maybeEnd) {
  92. state.tokenize = tokenBase;
  93. break;
  94. }
  95. maybeEnd = (ch == "*");
  96. }
  97. return ret("comment", "plsql-comment");
  98. }
  99. // Interface
  100. return {
  101. startState: function() {
  102. return {
  103. tokenize: tokenBase,
  104. startOfLine: true
  105. };
  106. },
  107. token: function(stream, state) {
  108. if (stream.eatSpace()) return null;
  109. var style = state.tokenize(stream, state);
  110. return style;
  111. }
  112. };
  113. });
  114. (function() {
  115. function keywords(str) {
  116. var obj = {}, words = str.split(" ");
  117. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  118. return obj;
  119. }
  120. var cKeywords = "abort accept access add all alter and any array arraylen as asc assert assign at attributes audit " +
  121. "authorization avg " +
  122. "base_table begin between binary_integer body boolean by " +
  123. "case cast char char_base check close cluster clusters colauth column comment commit compress connect " +
  124. "connected constant constraint crash create current currval cursor " +
  125. "data_base database date dba deallocate debugoff debugon decimal declare default definition delay delete " +
  126. "desc digits dispose distinct do drop " +
  127. "else elsif enable end entry escape exception exception_init exchange exclusive exists exit external " +
  128. "fast fetch file for force form from function " +
  129. "generic goto grant group " +
  130. "having " +
  131. "identified if immediate in increment index indexes indicator initial initrans insert interface intersect " +
  132. "into is " +
  133. "key " +
  134. "level library like limited local lock log logging long loop " +
  135. "master maxextents maxtrans member minextents minus mislabel mode modify multiset " +
  136. "new next no noaudit nocompress nologging noparallel not nowait number_base " +
  137. "object of off offline on online only open option or order out " +
  138. "package parallel partition pctfree pctincrease pctused pls_integer positive positiven pragma primary prior " +
  139. "private privileges procedure public " +
  140. "raise range raw read rebuild record ref references refresh release rename replace resource restrict return " +
  141. "returning reverse revoke rollback row rowid rowlabel rownum rows run " +
  142. "savepoint schema segment select separate session set share snapshot some space split sql start statement " +
  143. "storage subtype successful synonym " +
  144. "tabauth table tables tablespace task terminate then to trigger truncate type " +
  145. "union unique unlimited unrecoverable unusable update use using " +
  146. "validate value values variable view views " +
  147. "when whenever where while with work";
  148. var cFunctions = "abs acos add_months ascii asin atan atan2 average " +
  149. "bfilename " +
  150. "ceil chartorowid chr concat convert cos cosh count " +
  151. "decode deref dual dump dup_val_on_index " +
  152. "empty error exp " +
  153. "false floor found " +
  154. "glb greatest " +
  155. "hextoraw " +
  156. "initcap instr instrb isopen " +
  157. "last_day least lenght lenghtb ln lower lpad ltrim lub " +
  158. "make_ref max min mod months_between " +
  159. "new_time next_day nextval nls_charset_decl_len nls_charset_id nls_charset_name nls_initcap nls_lower " +
  160. "nls_sort nls_upper nlssort no_data_found notfound null nvl " +
  161. "others " +
  162. "power " +
  163. "rawtohex reftohex round rowcount rowidtochar rpad rtrim " +
  164. "sign sin sinh soundex sqlcode sqlerrm sqrt stddev substr substrb sum sysdate " +
  165. "tan tanh to_char to_date to_label to_multi_byte to_number to_single_byte translate true trunc " +
  166. "uid upper user userenv " +
  167. "variance vsize";
  168. var cTypes = "bfile blob " +
  169. "character clob " +
  170. "dec " +
  171. "float " +
  172. "int integer " +
  173. "mlslabel " +
  174. "natural naturaln nchar nclob number numeric nvarchar2 " +
  175. "real rowtype " +
  176. "signtype smallint string " +
  177. "varchar varchar2";
  178. var cSqlplus = "appinfo arraysize autocommit autoprint autorecovery autotrace " +
  179. "blockterminator break btitle " +
  180. "cmdsep colsep compatibility compute concat copycommit copytypecheck " +
  181. "define describe " +
  182. "echo editfile embedded escape exec execute " +
  183. "feedback flagger flush " +
  184. "heading headsep " +
  185. "instance " +
  186. "linesize lno loboffset logsource long longchunksize " +
  187. "markup " +
  188. "native newpage numformat numwidth " +
  189. "pagesize pause pno " +
  190. "recsep recsepchar release repfooter repheader " +
  191. "serveroutput shiftinout show showmode size spool sqlblanklines sqlcase sqlcode sqlcontinue sqlnumber " +
  192. "sqlpluscompatibility sqlprefix sqlprompt sqlterminator suffix " +
  193. "tab term termout time timing trimout trimspool ttitle " +
  194. "underline " +
  195. "verify version " +
  196. "wrap";
  197. CodeMirror.defineMIME("text/x-plsql", {
  198. name: "plsql",
  199. keywords: keywords(cKeywords),
  200. functions: keywords(cFunctions),
  201. types: keywords(cTypes),
  202. sqlplus: keywords(cSqlplus)
  203. });
  204. }());