You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cobol.js 10KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. /**
  4. * Author: Gautam Mehta
  5. * Branched from CodeMirror's Scheme mode
  6. */
  7. (function(mod) {
  8. if (typeof exports == "object" && typeof module == "object") // CommonJS
  9. mod(require("../../lib/codemirror"));
  10. else if (typeof define == "function" && define.amd) // AMD
  11. define(["../../lib/codemirror"], mod);
  12. else // Plain browser env
  13. mod(CodeMirror);
  14. })(function(CodeMirror) {
  15. "use strict";
  16. CodeMirror.defineMode("cobol", function () {
  17. var BUILTIN = "builtin", COMMENT = "comment", STRING = "string",
  18. ATOM = "atom", NUMBER = "number", KEYWORD = "keyword", MODTAG = "header",
  19. COBOLLINENUM = "def", PERIOD = "link";
  20. function makeKeywords(str) {
  21. var obj = {}, words = str.split(" ");
  22. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  23. return obj;
  24. }
  25. var atoms = makeKeywords("TRUE FALSE ZEROES ZEROS ZERO SPACES SPACE LOW-VALUE LOW-VALUES ");
  26. var keywords = makeKeywords(
  27. "ACCEPT ACCESS ACQUIRE ADD ADDRESS " +
  28. "ADVANCING AFTER ALIAS ALL ALPHABET " +
  29. "ALPHABETIC ALPHABETIC-LOWER ALPHABETIC-UPPER ALPHANUMERIC ALPHANUMERIC-EDITED " +
  30. "ALSO ALTER ALTERNATE AND ANY " +
  31. "ARE AREA AREAS ARITHMETIC ASCENDING " +
  32. "ASSIGN AT ATTRIBUTE AUTHOR AUTO " +
  33. "AUTO-SKIP AUTOMATIC B-AND B-EXOR B-LESS " +
  34. "B-NOT B-OR BACKGROUND-COLOR BACKGROUND-COLOUR BEEP " +
  35. "BEFORE BELL BINARY BIT BITS " +
  36. "BLANK BLINK BLOCK BOOLEAN BOTTOM " +
  37. "BY CALL CANCEL CD CF " +
  38. "CH CHARACTER CHARACTERS CLASS CLOCK-UNITS " +
  39. "CLOSE COBOL CODE CODE-SET COL " +
  40. "COLLATING COLUMN COMMA COMMIT COMMITMENT " +
  41. "COMMON COMMUNICATION COMP COMP-0 COMP-1 " +
  42. "COMP-2 COMP-3 COMP-4 COMP-5 COMP-6 " +
  43. "COMP-7 COMP-8 COMP-9 COMPUTATIONAL COMPUTATIONAL-0 " +
  44. "COMPUTATIONAL-1 COMPUTATIONAL-2 COMPUTATIONAL-3 COMPUTATIONAL-4 COMPUTATIONAL-5 " +
  45. "COMPUTATIONAL-6 COMPUTATIONAL-7 COMPUTATIONAL-8 COMPUTATIONAL-9 COMPUTE " +
  46. "CONFIGURATION CONNECT CONSOLE CONTAINED CONTAINS " +
  47. "CONTENT CONTINUE CONTROL CONTROL-AREA CONTROLS " +
  48. "CONVERTING COPY CORR CORRESPONDING COUNT " +
  49. "CRT CRT-UNDER CURRENCY CURRENT CURSOR " +
  50. "DATA DATE DATE-COMPILED DATE-WRITTEN DAY " +
  51. "DAY-OF-WEEK DB DB-ACCESS-CONTROL-KEY DB-DATA-NAME DB-EXCEPTION " +
  52. "DB-FORMAT-NAME DB-RECORD-NAME DB-SET-NAME DB-STATUS DBCS " +
  53. "DBCS-EDITED DE DEBUG-CONTENTS DEBUG-ITEM DEBUG-LINE " +
  54. "DEBUG-NAME DEBUG-SUB-1 DEBUG-SUB-2 DEBUG-SUB-3 DEBUGGING " +
  55. "DECIMAL-POINT DECLARATIVES DEFAULT DELETE DELIMITED " +
  56. "DELIMITER DEPENDING DESCENDING DESCRIBED DESTINATION " +
  57. "DETAIL DISABLE DISCONNECT DISPLAY DISPLAY-1 " +
  58. "DISPLAY-2 DISPLAY-3 DISPLAY-4 DISPLAY-5 DISPLAY-6 " +
  59. "DISPLAY-7 DISPLAY-8 DISPLAY-9 DIVIDE DIVISION " +
  60. "DOWN DROP DUPLICATE DUPLICATES DYNAMIC " +
  61. "EBCDIC EGI EJECT ELSE EMI " +
  62. "EMPTY EMPTY-CHECK ENABLE END END. END-ACCEPT END-ACCEPT. " +
  63. "END-ADD END-CALL END-COMPUTE END-DELETE END-DISPLAY " +
  64. "END-DIVIDE END-EVALUATE END-IF END-INVOKE END-MULTIPLY " +
  65. "END-OF-PAGE END-PERFORM END-READ END-RECEIVE END-RETURN " +
  66. "END-REWRITE END-SEARCH END-START END-STRING END-SUBTRACT " +
  67. "END-UNSTRING END-WRITE END-XML ENTER ENTRY " +
  68. "ENVIRONMENT EOP EQUAL EQUALS ERASE " +
  69. "ERROR ESI EVALUATE EVERY EXCEEDS " +
  70. "EXCEPTION EXCLUSIVE EXIT EXTEND EXTERNAL " +
  71. "EXTERNALLY-DESCRIBED-KEY FD FETCH FILE FILE-CONTROL " +
  72. "FILE-STREAM FILES FILLER FINAL FIND " +
  73. "FINISH FIRST FOOTING FOR FOREGROUND-COLOR " +
  74. "FOREGROUND-COLOUR FORMAT FREE FROM FULL " +
  75. "FUNCTION GENERATE GET GIVING GLOBAL " +
  76. "GO GOBACK GREATER GROUP HEADING " +
  77. "HIGH-VALUE HIGH-VALUES HIGHLIGHT I-O I-O-CONTROL " +
  78. "ID IDENTIFICATION IF IN INDEX " +
  79. "INDEX-1 INDEX-2 INDEX-3 INDEX-4 INDEX-5 " +
  80. "INDEX-6 INDEX-7 INDEX-8 INDEX-9 INDEXED " +
  81. "INDIC INDICATE INDICATOR INDICATORS INITIAL " +
  82. "INITIALIZE INITIATE INPUT INPUT-OUTPUT INSPECT " +
  83. "INSTALLATION INTO INVALID INVOKE IS " +
  84. "JUST JUSTIFIED KANJI KEEP KEY " +
  85. "LABEL LAST LD LEADING LEFT " +
  86. "LEFT-JUSTIFY LENGTH LENGTH-CHECK LESS LIBRARY " +
  87. "LIKE LIMIT LIMITS LINAGE LINAGE-COUNTER " +
  88. "LINE LINE-COUNTER LINES LINKAGE LOCAL-STORAGE " +
  89. "LOCALE LOCALLY LOCK " +
  90. "MEMBER MEMORY MERGE MESSAGE METACLASS " +
  91. "MODE MODIFIED MODIFY MODULES MOVE " +
  92. "MULTIPLE MULTIPLY NATIONAL NATIVE NEGATIVE " +
  93. "NEXT NO NO-ECHO NONE NOT " +
  94. "NULL NULL-KEY-MAP NULL-MAP NULLS NUMBER " +
  95. "NUMERIC NUMERIC-EDITED OBJECT OBJECT-COMPUTER OCCURS " +
  96. "OF OFF OMITTED ON ONLY " +
  97. "OPEN OPTIONAL OR ORDER ORGANIZATION " +
  98. "OTHER OUTPUT OVERFLOW OWNER PACKED-DECIMAL " +
  99. "PADDING PAGE PAGE-COUNTER PARSE PERFORM " +
  100. "PF PH PIC PICTURE PLUS " +
  101. "POINTER POSITION POSITIVE PREFIX PRESENT " +
  102. "PRINTING PRIOR PROCEDURE PROCEDURE-POINTER PROCEDURES " +
  103. "PROCEED PROCESS PROCESSING PROGRAM PROGRAM-ID " +
  104. "PROMPT PROTECTED PURGE QUEUE QUOTE " +
  105. "QUOTES RANDOM RD READ READY " +
  106. "REALM RECEIVE RECONNECT RECORD RECORD-NAME " +
  107. "RECORDS RECURSIVE REDEFINES REEL REFERENCE " +
  108. "REFERENCE-MONITOR REFERENCES RELATION RELATIVE RELEASE " +
  109. "REMAINDER REMOVAL RENAMES REPEATED REPLACE " +
  110. "REPLACING REPORT REPORTING REPORTS REPOSITORY " +
  111. "REQUIRED RERUN RESERVE RESET RETAINING " +
  112. "RETRIEVAL RETURN RETURN-CODE RETURNING REVERSE-VIDEO " +
  113. "REVERSED REWIND REWRITE RF RH " +
  114. "RIGHT RIGHT-JUSTIFY ROLLBACK ROLLING ROUNDED " +
  115. "RUN SAME SCREEN SD SEARCH " +
  116. "SECTION SECURE SECURITY SEGMENT SEGMENT-LIMIT " +
  117. "SELECT SEND SENTENCE SEPARATE SEQUENCE " +
  118. "SEQUENTIAL SET SHARED SIGN SIZE " +
  119. "SKIP1 SKIP2 SKIP3 SORT SORT-MERGE " +
  120. "SORT-RETURN SOURCE SOURCE-COMPUTER SPACE-FILL " +
  121. "SPECIAL-NAMES STANDARD STANDARD-1 STANDARD-2 " +
  122. "START STARTING STATUS STOP STORE " +
  123. "STRING SUB-QUEUE-1 SUB-QUEUE-2 SUB-QUEUE-3 SUB-SCHEMA " +
  124. "SUBFILE SUBSTITUTE SUBTRACT SUM SUPPRESS " +
  125. "SYMBOLIC SYNC SYNCHRONIZED SYSIN SYSOUT " +
  126. "TABLE TALLYING TAPE TENANT TERMINAL " +
  127. "TERMINATE TEST TEXT THAN THEN " +
  128. "THROUGH THRU TIME TIMES TITLE " +
  129. "TO TOP TRAILING TRAILING-SIGN TRANSACTION " +
  130. "TYPE TYPEDEF UNDERLINE UNEQUAL UNIT " +
  131. "UNSTRING UNTIL UP UPDATE UPON " +
  132. "USAGE USAGE-MODE USE USING VALID " +
  133. "VALIDATE VALUE VALUES VARYING VLR " +
  134. "WAIT WHEN WHEN-COMPILED WITH WITHIN " +
  135. "WORDS WORKING-STORAGE WRITE XML XML-CODE " +
  136. "XML-EVENT XML-NTEXT XML-TEXT ZERO ZERO-FILL " );
  137. var builtins = makeKeywords("- * ** / + < <= = > >= ");
  138. var tests = {
  139. digit: /\d/,
  140. digit_or_colon: /[\d:]/,
  141. hex: /[0-9a-f]/i,
  142. sign: /[+-]/,
  143. exponent: /e/i,
  144. keyword_char: /[^\s\(\[\;\)\]]/,
  145. symbol: /[\w*+\-]/
  146. };
  147. function isNumber(ch, stream){
  148. // hex
  149. if ( ch === '0' && stream.eat(/x/i) ) {
  150. stream.eatWhile(tests.hex);
  151. return true;
  152. }
  153. // leading sign
  154. if ( ( ch == '+' || ch == '-' ) && ( tests.digit.test(stream.peek()) ) ) {
  155. stream.eat(tests.sign);
  156. ch = stream.next();
  157. }
  158. if ( tests.digit.test(ch) ) {
  159. stream.eat(ch);
  160. stream.eatWhile(tests.digit);
  161. if ( '.' == stream.peek()) {
  162. stream.eat('.');
  163. stream.eatWhile(tests.digit);
  164. }
  165. if ( stream.eat(tests.exponent) ) {
  166. stream.eat(tests.sign);
  167. stream.eatWhile(tests.digit);
  168. }
  169. return true;
  170. }
  171. return false;
  172. }
  173. return {
  174. startState: function () {
  175. return {
  176. indentStack: null,
  177. indentation: 0,
  178. mode: false
  179. };
  180. },
  181. token: function (stream, state) {
  182. if (state.indentStack == null && stream.sol()) {
  183. // update indentation, but only if indentStack is empty
  184. state.indentation = 6 ; //stream.indentation();
  185. }
  186. // skip spaces
  187. if (stream.eatSpace()) {
  188. return null;
  189. }
  190. var returnType = null;
  191. switch(state.mode){
  192. case "string": // multi-line string parsing mode
  193. var next = false;
  194. while ((next = stream.next()) != null) {
  195. if ((next == "\"" || next == "\'") && !stream.match(/['"]/, false)) {
  196. state.mode = false;
  197. break;
  198. }
  199. }
  200. returnType = STRING; // continue on in string mode
  201. break;
  202. default: // default parsing mode
  203. var ch = stream.next();
  204. var col = stream.column();
  205. if (col >= 0 && col <= 5) {
  206. returnType = COBOLLINENUM;
  207. } else if (col >= 72 && col <= 79) {
  208. stream.skipToEnd();
  209. returnType = MODTAG;
  210. } else if (ch == "*" && col == 6) { // comment
  211. stream.skipToEnd(); // rest of the line is a comment
  212. returnType = COMMENT;
  213. } else if (ch == "\"" || ch == "\'") {
  214. state.mode = "string";
  215. returnType = STRING;
  216. } else if (ch == "'" && !( tests.digit_or_colon.test(stream.peek()) )) {
  217. returnType = ATOM;
  218. } else if (ch == ".") {
  219. returnType = PERIOD;
  220. } else if (isNumber(ch,stream)){
  221. returnType = NUMBER;
  222. } else {
  223. if (stream.current().match(tests.symbol)) {
  224. while (col < 71) {
  225. if (stream.eat(tests.symbol) === undefined) {
  226. break;
  227. } else {
  228. col++;
  229. }
  230. }
  231. }
  232. if (keywords && keywords.propertyIsEnumerable(stream.current().toUpperCase())) {
  233. returnType = KEYWORD;
  234. } else if (builtins && builtins.propertyIsEnumerable(stream.current().toUpperCase())) {
  235. returnType = BUILTIN;
  236. } else if (atoms && atoms.propertyIsEnumerable(stream.current().toUpperCase())) {
  237. returnType = ATOM;
  238. } else returnType = null;
  239. }
  240. }
  241. return returnType;
  242. },
  243. indent: function (state) {
  244. if (state.indentStack == null) return state.indentation;
  245. return state.indentStack.indent;
  246. }
  247. };
  248. });
  249. CodeMirror.defineMIME("text/x-cobol", "cobol");
  250. });