-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasmibm-mode.el
1204 lines (950 loc) · 37.8 KB
/
asmibm-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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; asmibm-mode --- A major mode to handle Assembler/IBM for MVS or Z/OS JCL.
;;;; -*- Mode: Emacs-Lisp -*-
;;; asmibm-mode.el
;;
;; See the file COPYING for license and copyright information.
;;
;; Author: Marco Antoniotti <marcoxa [at] gmail.com>
;;
;; Created: December 2nd, 2020.
;;
;; Version: 2022-10-05.1
;;
;; Keywords: languages, operating systems.
;;; Commentary:
;;
;; A major mode to handle IBM Assemblers, mostly for MVS or z/OS.
;;; Code:
;;;; ASM IBM Mode Setup.
(defgroup asmibm nil
"The major mode to handle Assemblers for IBM 360/370 and z Series machines.
This mode is part of the IRON MAIN package."
:group 'languages)
(defcustom asmibm-mode-os-flavor "MVS 3.8j"
"The current flavor of MVS used.
The values of this variable are strings starting either with 'MVS' or
'z/OS'. Other variants are acceptable as long as the 'main' OS name
comes first.
The value 'MVS 3.8' is the default one, being the version of MVS
that IBM release in the public domain."
:group 'asmibm
:type 'string)
;;; Things to highlight.
;;; We inherit from standard asm-mode, so we just need things for
;;; comments at the end of a line.
(defvar asmibm-mode--strings
"'.*'"
"ASM IBM strings.")
(defvar asmibm-mode--names
"^\\([[:alpha:]&.][[:alnum:]@$#_&.]*\\|\\.[:alpha:][[:alnum:]@$#_&.]*\\)"
"ASM IBM names (or labels).
These are the 'names' of instructions.")
;; (defvar asmibm-names
;; "^\\([^* ][[:graph:]]+\\)"
;; "ASM IBM names.
;;
;; These are the 'names'of instructions.")
(defvar asmibm-mode--instructions
"^\\([[:alpha:]&.][[:alnum:]@$#_&.]*\\)?[[:blank:]]+\\([[:alpha:]@$#&._][[:alnum:]@$#&._]*\\)"
"ASM IBM instructions.
These are the instructions mnemonics.
The second regexp group contains the istruction name.")
;; (defvar asmibm-instructions
;; "^\\([^* ][[:graph:]]+\\| +\\) +\\([[:graph:]]+\\)"
;; "ASM IBM instructions.
;; These are the instructions mnemonics.")
(defvar asmibm-mode--16-white-columns
"^ \{16\}"
"ASM IBM continuation line prefix.
Continuation lines must start at column 16. This regexp matches 16
spaces (columns) at the beginning of line.")
(defvar asmibm-mode--registers
"R[0-9][0-5]?"
"ASM IBM register names.")
(defvar asmibm-mode--card-end-comments-0
"^\\.?\\*.*$"
"ASM IBM 'comment' card (single '*' is for regular assembler, '.*'
is for macros.
A '*' or a '.*' (for macros) at the beginning of the card (line) marks
a comment.")
(defvar asmibm-mode--card-end-comments-1
"^\\([[:alpha:]&.][[:alnum:]@$#_&.]*\\)?[[:blank:]]+[[:alpha:]@$#&._][[:alnum:]@$#&._]*[[:blank:]]+[-[:alnum:],*='()+]?\\([[:graph:]]*\\)$
"
"ASM IBM 'end of card' comments for 'full' cards.
Anything after the 'operands' in a card is a comment; this regexp
selects them.")
;; (defvar asmibm-card-end-comments-1
;; "^[^* ]+ +[[:graph:]]+ +[[:graph:]]+ +\\([[:graph:]].*\\)"
;; "ASM IBM 'end of card' comments for 'full' cards..
;; Anything after the 'operands' in a card is a comment; this regexp
;; selects them.")
(defvar asmibm-mode-card-end-comments-2
"^ +[[:graph:]]+ +\\([[:graph:]].*\\)"
"ASM IBM 'end of card' comments for 'continuation' cards.
Anything after the 'operands' in a card is a comment; this regexp
selects them in case of 'continuation' cards that do not have the
'name' and 'operation'.")
(defvar asmibm-mode--card-end-comments-3
"^ +[[:graph:]]+ +[[:graph:]]+ +\\([[:graph:]].*\\)"
"ASM IBM 'end of card' comments for 'no operands' cards.
Anything after the 'operands' in a card is a comment; this regexp
selects them in case of 'no operands' cards that do not have the
'name' and 'operands'; however, the 'operands' must be at least one
non blank character.")
;; asmibm-attribute-symbol
;; Note that the following does only attributed symbols and not
;; symbols and expressions as per IBM specs.
(defvar asmibm-mode--attributed-symbol
"\\([LKDINOST]'[[:alnum:]]+\\|L'\\*\\)"
"ASM IBM 'attributed terms.
This is typically the 'length' of something, e.g.,
L'FOOBAR
indicating the length of FOOBAR.
Other 'attributes' are introduced by the other letters.
See, e.g.: https://www.ibm.com/docs/en/zos/2.1.0?topic=terms-other-attribute-references
")
(defvar asmibm-mode--jcl
"^//.*$"
"Lines starting with '//' are assumed to be JCL which wraps the Assembler.")
;; asmibm-card-remarks
;;
;; IBM assembly is old, and it uses some messed up syntax, especially
;; when it comes to 'end of lined (or card) remarks' (i.e., comments)
;; and 'strings'.
;;
;; Hence the hairball below, to be used for font-lock.
;;
;; Notes:
;; This is the first attempt. But it looks like font-lock-mode (and
;; font-lock-fontifiy-buffer, alongside font-lock-defaults) keep
;; calling it expecting it to work several times in a row. In other
;; words it should always return t even is the match-data were empty:
;; this seems incostistent, although there could always be a match for
;; "^.*$".
;; This is the reason why we terminate a search with
;;
;; (re-search-forward ".*$" nil nil)
;;
;; at the end of line.
(cl-defun asmibm-mode--card-remarks (&optional (limit (line-end-position)))
;; When called programmatically `limit' is end of buffer.
;; (interactive)
(message "\nASMIBM CARD REMARK: entered with limit = %s @%s" limit (point))
(condition-case nil
(let* ((current-point (point))
(post-instr-point
(re-search-forward asmibm-mode--instructions nil nil))
;; Now point should be at the end of the instruction.
;; I can start skipping over the operands characters,
;; including strings and attributed symbols.
(skipped-ws (skip-chars-forward "[:blank:]" limit))
(first-operand-point (point))
(operand-pos first-operand-point)
(first-remark-point nil)
)
(message "ASMIBM CARD REMARK: asserting")
(cl-assert (not (char-equal (following-char) ?\ ))) ; Being paranoid.
(cl-assert (not (bolp))) ; Yep, very paranoid.
(message "ASMIBM CARD REMARK: asserted")
;; Now I am at the first character of the 'operands' of the
;; instruction. Note that, as per IBM documentation, to
;; have "remarks" that are comments for instructions that
;; use zero arguments, there should be a lone comma on the
;; line, surrounded my spaces, signifying "I am the operand
;; list".
(cl-labels ((finish-remark-parsing
()
(message "ASMIBM CARD REMARK: finishing")
;; We get here in one case.
;; We have a blank character at hand and we need
;; to see whether there is 'end of line/card'
;; comment.
;; If so we need to fix the match and return t,
;; otherwise we return nil.
(skip-chars-forward "[:blank:]" limit)
(message "ASMIBM CARD REMARK: char after blanks `%c'" (char-after))
(if (< (point) (line-end-position))
;; Something non blank found before end of line.
;; Fix match
(cl-return-from asmibm-mode--card-remarks
(let ((result (re-search-forward ".*$" limit t)))
(message "ASMIBM CARD REMARK: finishing match data \"%s\" %s"
(match-string 0)
(match-data))
result))
(cl-return-from asmibm-mode--card-remarks
(progn
(message "ASMIBM CARD REMARK: finishing with no match")
;; nil
(finish-eol-success)
)
))
)
(finish-eol-success
()
(progn
(message "ASMIBM CARD REMARK: finishing at eol %s %s"
(point)
(line-end-position))
(cl-return-from asmibm-mode--card-remarks
(re-search-forward ".*$" limit t))
)
)
(advance
()
(forward-char)
(setq operand-pos (point))
)
(in-string
()
;; We are just before a "'"
(message "ASMIBM CARD REMARK: in-string")
;; Just to be paranoid...
(cl-assert (char-equal ?' (char-after))
t
"in-string cl-label")
(forward-char)
(let ((string-end-pos
(search-forward "'"
(line-end-position)
t))
)
;; We should have skipped the string
;; now (in a dumb way for the time
;; being; there are probably some
;; corner cases not considered here.)
;; (if string-end-pos
;; (message "ASMIBM CARD REMARK: in-string after `%c'" (char-after))
;; (message "ASMIBM CARD REMARK: in-string ooooops."))
(if string-end-pos
(setq operand-pos string-end-pos) ; We are cool.
(setq operand-pos (point)) ; We pray.
))
)
) ; labels functions.
(message "ASMIBM CARD REMARK: in loop with operand-pos = %s, eol = %s"
operand-pos
(line-end-position))
(while (and operand-pos
(< operand-pos limit)
(< operand-pos (line-end-position)))
(let ((c (following-char)))
(message "ASMIBM CARD REMARK: in loop with c = `%c'" c)
(cond ((equal (char-syntax c) ?\ ) ; Found a space; skip to first non blank.
(skip-chars-forward "[:blank:]" limit)
(finish-remark-parsing)
)
((char-equal c ?\') ; Tricky part: string or attribute?
(let ((prev-char (preceding-char)))
(message "ASMIBM CARD REMARK: attribute with prev c = `%c'"
prev-char)
;; Is this an attributed expression
(if (cl-find prev-char "LKDINOST") ; Possible attribute.
(cl-case (char-before (1- (point)))
((?+ ?- ?/ ?* ?\( ?\) ?, ?\t ?\ )
;; Possible 'operators'. Last is space!
;; We have (almost surely) an attributed
;; expression.
;; Keep going without bothering to deal
;; with strings.
;; (message "ASMIBM CARD REMARK: attributed")
(advance)
)
(t
;; Something else? We are probably in
;; the middle of an operand and
;; starting a proper string.
(in-string)
)
) ; cl-case
;; else, we are in a string (we hope).
(in-string)
))
)
(t ; Any other character
(advance)
)
))
) ; while...
(message "ASMIBM CARD REMARK: no match")
(if (>= operand-pos limit)
nil ; We are out of the loop past the limit, we finish with NIL.
(finish-eol-success)
)
) ; cl-labels...
) ; let*...
;; condition-case catchers...
(search-failed (message "ASMIBM CARD REMARK: search failed.") nil)
))
;; asmibm-mode--parse-operand
;;
;; Trying to parse the lovely "expression".
;;
;; Notes:
;;
;; The parser is stupid and does not span lines. That is, if you open
;; a parenthesis you must close it on the same line.
(cl-defun asmibm-mode--parse-operands (&optional
(limit (line-end-position))
(*debug-parse-operands* t)
)
;; This function must be called with (point) at the start of an operand.
;; At the end of the call, the point is at the first space after the
;; operand (which must be a full expression; cfr., Assembler reference).
;;
;; When called programmatically `limit' may be end of buffer.
;;
;; Notes:
;;
;; TODO: Come back to munge the 'match'.
(interactive)
(cl-flet ((msg
(m &rest args)
(when *debug-parse-operands*
(apply 'message
(concat "ASMIBM PARSE OPERANDS: " m)
args)))
)
(message "\nASMIBM PARSE OPERANDS: entered with limit = %s @%s" limit (point))
(condition-case nil
(let ((operand-pos (point)))
(msg "asserting")
(cl-assert (not (char-equal (following-char) ?\ ))) ; Being paranoid.
(cl-assert (not (bolp))) ; Yep, very paranoid.
(msg "asserted")
;; Now I am at the first character of the 'operands' of the
;; instruction.
;; The CL-LABELS here are a hand-crafted recursive descent
;; parser.
;;
;; There are three different functions (plus a few utility
;; ones). (a) functions for "terminals" and functions for
;; "more terminals", as the parser assumes a somewhat
;; right-recursive structure.
;; Each "terminal" function takes as argument the "starting
;; character" of the thing being parsed and finishes with
;; (point) right after the parsing. Functions implementing
;; "more terminal" may check the first thing they see and do
;; something appropriate; they also finish at the end of the
;; last proper character.
;; Do not expect anything fancy, the parser is buit to skip
;; over the operands of an instruction. That's it.
;; Note that, as per IBM documentation, to have "remarks" that
;; are comments for instructions that use zero arguments,
;; there should be a lone comma on the line, surrounded my
;; spaces, signifying "I am the operand list".
(cl-labels
((advance
()
(forward-char)
(setq operand-pos (point))
) ; advance
(retreat
()
(backward-char)
(setq operand-pos (point))
) ; retreat
(is-space
(c)
(equal (char-syntax c) ?\ )
) ; is-space
(is-newline
(c)
(equal (char-syntax c) ?\n)
) ; is-newline
(is-comma
(c)
(char-equal c ?\,)
) ; is-comma
(is-term-character
(c)
(or (equal (char-syntax c) ?\W) ; Word constituent
(equal (char-syntax c) ?\_) ; Symbol constituent;
; check later that it contains
; all characters needed.
(char-equal c ?\.)
(char-equal c ?\#))
) ; is-term-character
(finish-operands
(c)
;; (message "XXXXXXXXXXXXXXXXXXXXXXXXXXXX")
(msg "finishing operands.")
;; We get here in one case.
;; We have a blank character terminating the operands
;; field or we are at the end of a line.
(cl-assert (or (eolp)
(equal (char-syntax c) ?\ )))
(msg "returning; point is @%d." (point))
(cl-return-from asmibm-mode--parse-operands (point))
) ; finish-operands
(finish-operand
(c)
(msg "finishing one operand; c = ?%c" c)
(cl-assert (char-equal c ?\,) t)
;; (advance)
(msg "finished one operand.")
t
) ; finish-operand
(parse-string
(c)
;; We are just before a "'"
(msg "parse-string")
;; Just to be paranoid...
(cl-assert (char-equal ?' c) t "in-string cl-label")
(forward-char)
(let ((string-end-pos
(search-forward "'"
(line-end-position)
t))
)
;; We should have skipped the string
;; now (in a dumb way for the time
;; being; there are probably some
;; corner cases not considered here.)
(if string-end-pos
(setq operand-pos string-end-pos) ; We are cool.
(setq operand-pos (point)) ; We pray.
))
) ; parse-string
(parse-operands
(c)
(msg "parse-operands; c = ?%c" c)
(parse-operand c)
(msg "parse-operands; called parse-operand fc = ?%c"
(following-char))
(parse-more-operands (following-char))
) ; parse-operands
(parse-operand
(c)
(msg "parse-operand; c = ?%c" c)
(cond ((is-comma c)
(msg "parse-operand; calling finish-operand pc = ?%c fc = ?%c"
(char-before)
(following-char))
(msg "parse-operand; if I get here, there may be a problem.")
;; Should not get here now that there is parse-more-operands.
;; (retreat)
(finish-operand (following-char)) ; Essentially a no-op.
)
((or (is-space c) (eolp))
(msg "parse-operand; if I get here, there's a problem.")
(finish-operands c)
)
(t
(parse-expressions c)
)
)
) ; parse-operands
(parse-more-operands
(c)
(msg "parse-more-operands; c = ?%c" c)
(cond ((or (eolp) (is-space c))
;; Found a space or a newline, end of operand(s).
(finish-operands c)
)
((is-comma c)
;; Proceed to parse next operand, unless the
;; next character is a blank or we are at the
;; end of the line, as in the case before.
(advance)
(if (or (eolp) (is-space (following-char)))
;; (finish-operands c)
(finish-operands (following-char))
(parse-operands (following-char)))
)
)
) ; parse-operand
(parse-expressions
(c)
(msg "parse-expressions; c = ?%c" c)
(parse-expression c)
(msg "parse-expressions; called parse-expressions fc = ?%c"
(following-char))
(parse-more-expressions (following-char))
) ; parse-operands
(parse-expression
(c)
(msg "parse-expression; c = ?%c" c)
(parse-term c)
(parse-more-expressions (following-char))
) ; parse-expression
(parse-more-expressions
(c)
(msg "parse-more-expressions; c = ?%c" c)
(cond ((char-equal c ?\))
;; Closing of expression
;; Should not get here.
(msg "parse-more-expressions; trouble with c = ?%c" c)
;; BUt, if we get here, we are really in a
;; parenthesized expression. Leave the closing
;; parenthesis to be handled by
;; parse-parenthesized-expression.
;; (advance)
t)
((char-equal c ?\()
(parse-parenthesized-expression c))
((cl-find c "=+-*/")
(advance)
(parse-expressions (following-char)))
((char-equal c ?\')
(parse-string c)
(parse-more-expressions (following-char)))
((is-comma c)
;; (parse-expression)
(msg "parse-more-expressions; am I here; c = ?%c" c)
;; (retreat)
t)
((is-term-character c)
(msg "parse-more-expressions; parse-expressions; c = ?%c" c)
(parse-expressions c))
((is-space c)
;; (retreat)
t)
((equal (char-syntax c) ?\n)
t)
)
) ; parse-more-expression
(parse-term
(c)
(msg "parse-term; c = ?%c" c)
(cond ((char-equal c ?\()
;; Beginning of expression.
(parse-parenthesized-expression c))
((and (cl-find c "DONSKILT")
(char-equal (char-after (1+ (point))) ?\'))
;; We have an attribute.
(msg "attribute with c = ?%c" c)
(advance)
(advance) ; Now we are on the first character after the ?'.
(msg "attribute first c = ?%c" (following-char))
(parse-term (following-char)))
((and (cl-find c "XBCG")
(char-equal (char-after (1+ (point))) ?\'))
;; We have a "self-defining term".
(msg "self-def with c = ?%c" c)
(advance)
;; Now we parse a string.
(parse-string (following-char)))
((char-equal c ?\*) ; Special case for location
; counter.
(advance)
t)
((char-equal c ?\=)
;; A literal
(msg "literal with c = ?%c" c)
(advance)
(parse-literal-or-symbol (following-char))
(parse-expression (following-char)))
((char-equal c ?\')
(parse-string c))
(t
(msg "calling parse-literal-or-symbol")
(parse-literal-or-symbol c))
)
) ; parse-term
(parse-literal-or-symbol
(c)
(msg "parse-literal-or-symbol; c = ?%c" c)
(cond ((is-literal-or-symbol-following-char c)
(msg "parse-literal-or-symbol; terminating ?%c"
(following-char))
t)
(t
(advance)
(parse-literal-or-symbol (following-char))
)
)
) ; parse-literal-or-symbol
(parse-parenthesized-expression
(c)
(msg "parse-parenthesized-expression c = ?%c" c)
(cl-assert (char-equal c ?\())
(advance)
(parse-expression (following-char))
(prog1 (parse-more-parenthesized-expression (following-char))
(cl-assert (char-equal (char-before) ?\))
;; t
;; "char before is %c." (following-char)
)
)
)
(parse-more-parenthesized-expression
(c)
(msg "parse-more-parenthesized-expression c = ?%c" c)
(cond ((char-equal c ?\))
;; Closing of epression
(msg "parse-more-parenthesized-expression; closing c = ?%c"
c)
(advance)
t)
((char-equal c ?\()
(parse-parenthesized-expression c))
((cl-find c "=+-*/")
(advance)
(parse-expression c))
((char-equal c ?\')
(parse-string c)
(parse-more-parenthesized-expression (following-char)))
((is-comma c)
;; Contrary to parse-more-expression here we are
;; in an "argument list", we must advance.
;; parse-more-expression would return and let the
;; operands parsing take over.
(advance)
(parse-expression (following-char))
(parse-more-parenthesized-expression (following-char))
t)
((is-space c)
;; Here we differ as we can have the 'A..'
;; macros.
(skip-syntax-forward " " limit)
(parse-literal-or-symbol (following-char))
;; Really a AND, OR, XOR...
(parse-more-parenthesized-expression (following-char))
)
((equal (char-syntax c) ?\n)
t)
(t ; Other char
(msg "parse-more-parenthesized-expression; ooops!")
;; (advance)
(parse-expression (following-char))
)
)
) ; parse-more-parenthesized-expression
(is-literal-or-symbol-following-char
(c)
(cond ((char-equal c ?\))
;; Closing parenthesized expression.
t)
((char-equal c ?\()
;; Opening of parenthesized expression,
;; possibly an argument list.
t)
((cl-find c "+-*/")
;; Operator in expression.
t)
((char-equal c ?\')
;; Beginning of string?
t
)
((is-comma c)
;; End of expression/operand?
t)
((is-space c)
t)
((equal (char-syntax c) ?\n)
t)
(t nil)
)
) ; is-literal-or-symbol-following-char
(start-parsing
()
(message "\n\nASMIBM PARSE OPERANDS: start-parsing (pos = %d lep = %d)."
operand-pos
(line-end-position))
(when (and operand-pos
(< operand-pos limit)
(< operand-pos (line-end-position)))
(let ((c (following-char)))
(parse-operands c)))
)
) ; labels functions.
(msg "in loop with operand-pos = %s, eol = %s"
operand-pos
(line-end-position))
(start-parsing)
) ; cl-labels...
) ; let*...
;; condition-case catchers...
(search-failed (msg "search failed.") nil)
(error (msg "parsing failed.") nil)
)))
;; asmibm-mode--parse-statement
;;
;; Let's bite the bullet. Parse an entire statement and set the match
;; accordingly.
(cl-defun asmibm-mode--parse-statement (&optional (limit (line-end-position)))
;; When called programmatically `limit' is end of buffer.
(interactive)
(message "\nASMIBM PARSE STMT: entered with limit = %s @%s" limit (point))
(if (asmibm-mode--continuation-line-p)
(asmibm-mode--parse-continuation-card limit)
(asmibm-mode--parse-statement-card limit)
))
(cl-defun asmibm-mode--continuation-line-p ()
(interactive)
(save-excursion
(message "\nASMIBM IS-CONT: line-number-at-pos = %s."
(line-number-at-pos))
(let ((n-line (forward-line -1)))
(cl-case n-line
(-1 (message "\nASMIBM IS-CONT: n-line = %s on first line." n-line)) ; Do nothing.
(0 (message "\nASMIBM IS-CONT: n-line = %s, current line = %s, (%s %s)."
n-line
(line-number-at-pos)
(line-beginning-position)
(line-end-position)))
)
(when (zerop n-line)
(let ((c71 (move-to-column 71)))
(message "ASMIBM IS-CONT: moved to column %s." c71)
(cond ((< c71 71) ; Line shortes than 72.
(message "\nASMIBM IS-CONT: card is only %s long." c71)
(cl-return-from asmibm-mode--continuation-line-p
(progn (message "ASMIBM IS-CONT: returning nil.") nil))
)
((and (= c71 71)
(= (char-syntax (char-after)) ?\ ))
;; Nothing on column 72.
;; Do nothing.
(message "\nASMIBM IS-CONT: card is 71 or more long, but C72 is a blank.")
(cl-return-from asmibm-mode--continuation-line-p
(progn (message "ASMIBM IS-CONT: returning nil.") nil))
)
((and (= c71 71) ; Paranoid.
(not (= (char-syntax (char-after)) ?\ ))) ; Paranoid.
;;
(message "\nASMIBM IS-CONT: card is 71 or more long, and C72 is ?\%c."
(char-after))
(cl-return-from asmibm-mode--continuation-line-p
(progn (message "ASMIBM IS-CONT: returning t.") t))
)
))
)
(message "ASMIBM IS-CONT: returning nil.")
nil
)))
(defvar asmibm-mode--bad-cont-line-regexp
(rx (group) ; Dummy name/label
(group) ; Dummy instruction
(group) ; Dummy operands
(group) ; Dummy remark
(group (zero-or-more any))
eol
))
(cl-defun asmibm-mode--parse-continuation-card (limit)
(beginning-of-line)
(let ((c16 (skip-chars-forward "[:space:]" limit)))
(cond ((/= c16 16)
;; Something wrong.
(message "\nASMIBM PARSE CONT: line is %d long, c16 is %s, char is ?\%c."
(- (line-end-position) (line-beginning-position))
c16
(char-after))
(cl-return-from asmibm-mode--parse-continuation-card
(re-search-forward asmibm-mode--bad-cont-line-regexp limit t))
)
((= c16 16)
(asmibm-mode--parse-operands limit)
)
(t
(message "\nASMIBM PARSE CONT: c16 is %s; something wrong is happening." c16)
(cl-return-from asmibm-mode--parse-continuation-card
nil)
)
)))
;;; ASM IBM faces.
(defcustom asmibm-mode-string-face 'font-lock-string-face
"The face used to fontify strings (single-quoted) in ASM IBM mode."
:group 'asmibm
:type 'symbol
)
(defcustom asmibm-mode-names-face 'font-lock-function-name-face
"The face used to fontify 'names' in ASM IBM mode."
:group 'asmibm
:type 'symbol
)
(defcustom asmibm-mode-operations-face 'font-lock-keyword-face
"The face used to fontify 'operations' in ASM IBM mode."
:group 'asmibm
:type 'symbol
)
(defcustom asmibm-mode-operands-face 'font-lock-type-face
"The face used to fontify 'operands' in ASM IBM mode."
:group 'asmibm
:type 'symbol
)
(defcustom asmibm-mode-operators-face 'font-lock-builtin-face
"The face used to fontify 'operators' in ASM IBM mode."
:group 'asmibm
:type 'symbol
)
;; Just a try...
(defface asmibm-mode-comment-face-red
'((t :foreground "red" :weight bold))
"Face to colorize comments in ASM IBM mode."
:group 'asmibm
)
(defcustom asmibm-mode-comment-face 'font-lock-comment-face ; 'asmibm-comment-face-red
"The face used to fontify 'comments' in ASM IBM mode."
:group 'asmibm
:type 'symbol
)
;; (defcustom asmibm-comment-face 'font-lock-comment-face
;; "The face used to fontify 'comments' in ASM IBM mode."
;; :group 'asmibm
;; :type 'symbol
;; )
(defcustom asmibm-mode-grey-face 'shadow
"The face used to 'fontify out' possible JCL when assembler is embedded."
:group 'asmibm
:type 'symbol
)
(defvar asmibm-mode--font-lock-keywords
`(
(,asmibm-mode--names . (1 asmibm-mode-names-face))
(,asmibm-mode--instructions . (2 asmibm-mode-operations-face))
;; (,asmibm-registers . ,asmibm-operators-face)
(,asmibm-mode--card-end-comments-0 . ,asmibm-mode-comment-face)
(asmibm-mode--card-remarks . ,asmibm-mode-comment-face)
;; (,asmibm-card-end-comments-1 . (2 ,asmibm-comment-face))
;; (,asmibm-card-end-comments-2 . (1 ,asmibm-comment-face))
;; (,asmibm-card-end-comments-3 . (1 ,asmibm-comment-face))
(,asmibm-mode--attributed-symbol . (1 ,asmibm-mode-operands-face))
;; (,asmibm-strings . (0 ,asmibm-string-face t))
(,asmibm-mode--strings . (0 ,asmibm-mode-string-face))
(,asmibm-mode--jcl . (0 asmibm-mode-grey-face t))
)
"The ASM IBM mode 'font-lock' 'keyword' specification."
)