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