cobol.js 9.6 KB

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