-
Notifications
You must be signed in to change notification settings - Fork 2
/
pl1-mode.el
551 lines (440 loc) · 11.3 KB
/
pl1-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
;;; pl1-mode --- A major mode to handle (IBM) PL/I code.
;;; -*- Mode: Emacs-Lisp; lexical-binding: t; -*-
;;; pl1-mode.el
;;
;; See the file COPYING license and copyright information.
;;
;; Author: Marco Antoniotti <marcoxa [at] gmail.com>
;;
;; Created: April 2nd, 2021
;;
;; Version: 20210404.1
;;
;; Keywords: languages, operating systems.
;;; Commentary:
;;
;; A major mode to handle PL/I code.
;;
;; The code is very simple for the time being; mostly an excavation of
;; the IBM PLI/I Language Reference publication and a revisitation of
;; one of the two PL/I modes found online: the one by Stephen Merrony
;; at http://www.stephenmerrony.co.uk:8080 (which he kindly put in the
;; Public Domain). The second PL/I mode floating around is the
;; Multics Emacs one, whch is about 40 years old. It is interesting
;; per se, because it contains indentation code, however, it seems like
;; the Multics Emacs Lisp library seems "oldish", albeit a possible
;; subject of hermeneutical inquire.
;;; Code:
;;;; pl1 Mode Setup.
(defgroup pl1 nil
"The major mode to manipulate PL/I code.
This mode is part of the IRON MAIN package."
:group 'languages)
;;; The definitions of the syntax elements follow the "Language
;;; Reference" IBM document SC14-7285-00, First Edition (September
;;; 2010); in the following it is referred to as 'PL/I LR'.
;;, The reference 'Chapter X.' refer to PL/I LR.
;;; Things to highlight.
;; pl1-strings
;; The following is incomplete as the latest PL/I LR; strings bounded
;; by double quotes should be accepted.
;;
;; In any case, strings should be handled at this point by syntax
;; table entries (see below).
(defvar pl1-strings
"'.*'"
"PL/I strings regular expression.")
;; pl1-labels
;; Tricky one as now we have conditions to deal with.
(defvar pl1-labels
;; "^ *\\([[:alnum:]]* +\\)*\\([[:alnum:]]+\\):"
;; "^ *\\([A-Za-z_][A-Za-z0-9_]* +\\)*\\([A-Za-z_][A-Za-z0-9_]*\\):"
;; As above, but with optional spaces before colon.
"^ *\\([A-Za-z_][A-Za-z0-9_]* +\\)*\\([A-Za-z_][A-Za-z0-9_]*\\) *:"
)
;; pl1-statements
;; The list is incomplete.
;; The list is given as
;;
;; FULL-NAME [ABBR-NAME]
;;
;; The list relies on Emacs default case insensitive matching.
(defvar pl1-statements
'(
;; Chapter 6.
"PROCEDURE" "PROC"
"ENTRY"
"CALL"
"RETURN"
"PACKAGE"
"BEGIN"
"END"
;; Chapter 7.
"DEFINE"
;; Chapter 8.
"DECLARE" "DCL"
;; Chapter 9.
"ALLOCATE"
"BY"
"NAME"
"CLOSE"
"DELAY"
"DELETE"
"DETACH"
"DISPLAY"
"DO"
"EXIT"
"FETCH"
"FLUSH"
"FORMAT"
"FREE"
"GET"
"GO TO" "GO" "TO" "GOTO" ; Special case.
"IF"
"THEN"
"ELSE"
"ITERATE"
"LEAVE"
"LOCATE"
"ON"
"OPEN"
"OTHERWISE"
"PUT"
"READ"
"RELEASE"
"RESIGNAL"
"REVERT"
"REWRITE"
"SELECT"
"SIGNAL"
"STOP"
"WHEN"
"WRITE"
"WHILE"
"UNTIL"
"UPTHRU"
"DOWNTHRU"
"REPEAT"
;; Chapter 11.
"OPEN"
"CLOSE"
"FLUSH"
;; Chapter 12.
"READ"
"WRITE"
"REWRITE"
"LOCATE"
"DELETE"
;; Chapter 13.
"GET"
"PUT"
;; Chapter 16.
"ON"
"REVERT"
"SIGNAL"
"RESIGNAL"
;; Chapter 18.
"ATTACH"
"WAIT"
"DETACH"
)
"PL/I statements.
The set of PL/I 'statements'."
)
;; pl1-data-attributes
;; The list is incomplete.
;; The list is given as
;;
;; FULL-NAME [ABBR-NAME]
;;
;; The list relies on Emacs default case insensitive matching.
(defvar pl1-data-attributes
'(
;; Chapter 3.
;; Data attributes
"AREA"
"BINARY" "BIN"
"BIT"
"CHARACTER" "CHAR"
"COMPLEX" "CPLX"
"DECIMAL" "DEC"
"DIMENSION" "DIM"
"ENTRY"
"FILE"
"FIXED" "FIX"
"FLOAT"
"FORMAT"
"GRAPHIC" "G"
"HANDLE"
"LABEL"
"NONVARYING" "NONVAR"
"OFFSET"
"ORDINAL" "ORD"
"PICTURE" "PIC"
"POINTER" "PTR"
"PRECISION" "PREC"
"REAL"
"RETURNS"
"SIGNED"
"STRUCTURE" "STRUCT"
"TASK"
"TYPE"
"UNSIGNED"
"UNION"
"VARYING" "VAR"
"VARYINGZ" "VARZ"
"WIDECHAR"
;; Non data attributes.
"ABNORMAL"
"ALIGNED"
"ASSIGNABLE"
"AUTOMATIC" "AUTO"
"BASED"
"BIGENDIAN"
"BUFFERED"
"BUILTIN"
"BYADDR"
"BYVALUE"
"CONDITION"
"CONNECTED"
"CONTROLLED"
"DEFINED"
"DIRECT"
"ENVIRONMENT" "ENV"
"EXCLUSIVE"
"EXTERNAL"
"GENERIC"
"HEXADEC"
"IEEE"
"INITIAL" "INIT"
"INONLY"
"INOUT"
"INPUT"
;; "INTERMAL" This is a mistake in IBM docs.
"INTERNAL"
"KEYED"
"LIKE"
"LIST"
"LITTLEENDIAN"
"NONASSIGNABLE"
"NONCONNECTED"
"NORMAL"
"OPTIONAL"
"OPTIONS"
"OUTONLY"
"OUTPUT"
"PARAMETER" "PARAM"
"POSITION" "POS"
"PRINT"
"RECORD"
"SEQUENTIAL" "SEQ"
"STATIC"
"STREAM"
"UNALIGNED"
"UNBUFFERED"
"UPDATE"
"VALUE" "VAL"
"VARIABLE"
;; Chapter 6.
"EXPORTS"
"RESERVES"
"RECURSIVE"
"LIMITED"
;; Chapter 7.
;; Chapter 8.
"DEFAULT"
"DIMACROSS"
"NOINIT"
;; Chapter 9.
"FORMAT"
;; Chapter 10.
;; Chapter 11.
;; Chapter 12.
;; Chapter 13.
"FILE"
"LINE"
"PAGE"
"SKIP"
"STRING"
"EDIT"
"DATA"
;; Chapter 16.
;; Chapter 18.
)
"PL/I data attributes.
The set of names that can be used to qualify a PL/I piece of data.")
(defvar pl1-operators
'(
"->" "=>"
"+" "-" "*" "/" "**"
"&" "|" ; "^" ; not operator is not ASCII
"<" "<=" "=" ">=" ">"
;; Missing "^<" "^=" "^>" ; the '^' being the EBCDIC neg sign.
"||" ; Concatenation.
)
"PL/I 'operators'. A really minimal set.")
(defvar pl1-directives ; Not really that nice, but it
; is a first approximation.
'("%"
"%ACTIVATE" "% ACTIVATE"
"%DEACTIVATE" "% DEACTIVATE"
"%DECLARE" "% DECLARE"
"%DO" "% DO"
"%END" "% END"
"%GO TO" "% GO TO"
"%IF" "% IF"
"%THEN" "% THEN"
"%ELSE" "% ELSE"
"%INCLUDE" "% INCLUDE"
"%INSCAN" "% INSCAN"
"%ITERATE" "% ITERATE"
"%LEAVE" "% LEAVE"
"%NOTE" "% NOTE"
"%PROCEDURE" "% PROCEDURE"
"%PROCESS" "% PROCESS" "*PROCESS" ; Not really preprocessor.
"%REPLACE" "% REPLACE"
"%SELECT" "% SELECT"
"%XINCLUDE" "% XINCLUDE"
"%XINSCAN" "% XINSCAN"
)
"PL/I preprocessor 'directives' and macro statements. A minimal set.")
;;; PL/I faces.
(defcustom pl1-string-face 'font-lock-string-face
"The face used to fontify strings (single-quoted) in PL/I mode."
:group 'pl1
:type 'symbol
)
(defcustom pl1-labels-face 'font-lock-function-name-face
"The face used to fontify 'labels' (i.e., 'names') in PL/I mode."
:group 'pl1
:type 'symbol
)
(defcustom pl1-attributes-face 'font-lock-constant-face
"The face used to fontify 'attributes' in PL/I mode."
:group 'pl1
:type 'symbol
)
(defcustom pl1-operators-face 'font-lock-builtin-face
"The face used to fontify 'operators' in PL/I mode."
:group 'pl1
:type 'symbol
)
(defcustom pl1-statements-face 'font-lock-keyword-face ; 'font-lock-builtin-face
"The face used to fontify 'statements' in PL/I mode.
Statements are actually the 'commands' or 'verbs' defined by PL/I."
:group 'pl1
:type 'symbol
)
(defcustom pl1-directives-face 'font-lock-preprocessor-face
"The face used to fontify 'directives' in PL/I mode.
Directives are actually akin to the 'commands' or 'verbs' defined by PL/I."
:group 'pl1
:type 'symbol
)
(defcustom pl1-comment-face 'font-lock-comment-face
"The face used to fontify 'comments' in PL/I mode."
:group 'pl1
:type 'symbol
)
(defvar pl1-font-lock-keywords
`(
(,pl1-strings . ,pl1-string-face)
(,pl1-labels . (2 ,pl1-labels-face))
(,(regexp-opt pl1-statements 'symbols) . ,pl1-statements-face)
(,(regexp-opt pl1-operators nil) . ,pl1-operators-face)
(,(regexp-opt pl1-data-attributes 'symbols) . ,pl1-attributes-face)
(,(regexp-opt pl1-directives 'symbols) . ,pl1-directives-face)
;; These must be last.
;; (,pl1-card-end-comments-1 . (1 ,pl1-comment-face))
;; (,pl1-card-end-comments-2 . (1 ,pl1-comment-face))
;; (,pl1-comments . (0 ,pl1-comment-face t))
)
"The PL/I mode 'font-lock' 'keyword' specification."
)
(defvar pl1-font-lock-defaults
(list 'pl1-font-lock-keywords
nil ; Do syntax based processing.
t ; Be case insensitive.
)
"The PL/I mode 'font-lock' defaults specification."
)
;;; pl1-mode-syntax-table
(defvar pl1-mode-syntax-table
(let ((pl1-st (make-syntax-table)))
(modify-syntax-entry ?' "\"" pl1-st)
(modify-syntax-entry ?/ ". 14" pl1-st)
(modify-syntax-entry ?* ". 23" pl1-st)
;; (modify-syntax-entry ?\n "> " pl1-st)
pl1-st
)
"The PL/I mode syntax table."
)
;;; pl1-keymap
(defvar pl1-mode-map
(let ((km (make-sparse-keymap)))
(set-keymap-parent km prog-mode-map) ; Inherit from prog-mode-map!
km)
"The PL/I mode key map.")
;;; pl1-imenu-generic-expression
;;;
;;; 20210404: MA: ok, I lost an afternoon figuring out why the
;;; following was not working. Bottom line, you MUST match from the
;;; beginning of line.
(defvar pl1-imenu-generic-expression
'(("Packages"
"^ *\\([A-Za-z_][A-Za-z0-9_#]* +\\)*\\([A-Za-z_][A-Za-z0-9_]*\\): +PACKAGE" 2)
;; The above may be incorrect, as PLI/I LR seems to require a
;; colon after the condition.
("Procedures"
"^ *\\([A-Za-z_][A-Za-z0-9_#]* +\\)*\\([A-Za-z_][A-Za-z0-9_]*\\): +PROC" 2)
("Entries"
"^ *\\([A-Za-z_][A-Za-z0-9_#]* +\\)*\\([A-Za-z_][A-Za-z0-9_]*\\): +ENTRY" 2)
("Formats"
"^ *\\([A-Za-z_][A-Za-z0-9_#]* +\\)*\\([A-Za-z_][A-Za-z0-9_]*\\): +FORMAT" 2)
("Blocks"
"^ *\\([A-Za-z_][A-Za-z0-9_#]* +\\)*\\([A-Za-z_][A-Za-z0-9_]*\\): +BEGIN" 2)
)
"The PL/I Imenu regular expressions.")
;;; pl1-mode
(define-derived-mode pl1-mode prog-mode "PL/I"
"PL/I mode is a major mode to edit PL/I code."
:syntax-table pl1-mode-syntax-table
(setq-local font-lock-defaults pl1-font-lock-defaults)
;; (face-remap-add-relative pl1-comment-face :weight 'bold)
(face-remap-add-relative pl1-operators-face :weight 'bold
:foreground "Forest Green") ; This may be too much.
(face-remap-add-relative pl1-labels-face :weight 'bold)
;; Comments.
(setq-local comment-start "/\*")
(setq-local comment-start-skip "/\*+[ \t]*")
(setq-local comment-end "\*/")
(setq-local comment-end-skip "[ \t]*\*+/")
;; Set up the mode keymap.
(use-local-map pl1-mode-map)
;; Set up the menus.
;; (easy-menu-define pl1-mainframe-os-menu pl1-mode-map
;; "PL/I commands"
;; '("PL/I OS"
;; ["Submit" pl1-submit]
;; ["Submit PL/I File" pl1-submit-file])
;; )
(setq-local imenu-generic-expression
(reverse pl1-imenu-generic-expression))
;; (debug "About to enter ...")
(imenu-add-to-menubar "PL/I Code")
;; If defined, start the IRON MAIN minor mode, which sets up the
;; ruler and the "card" editing limits, plus the fill-column
;; indicator.
(when (fboundp 'iron-main-mode) (iron-main-mode))
'pl1-mode
)
;;;; Commands
;;;; ========
;;;; Epilogue
;;;; ========
(add-to-list 'auto-mode-alist '("\\.pl1\\'" . pl1-mode))
(add-to-list 'auto-mode-alist '("\\.pli\\'" . pl1-mode))
(add-to-list 'auto-mode-alist '("\\.inc\\'" . pl1-mode))
(add-to-list 'auto-mode-alist '("\\.cpy\\'" . pl1-mode))
(provide 'pl1-mode)
;;; pl1-mode.el ends here