Menu

[03a9b8]: / tools / Scripts / ada-to-wiki.el  Maximize  Restore  History

Download this file

622 lines (531 with data), 18.6 kB

  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
;;; ada-to-wiki.el --- print MediaWiki markup around Ada tokens
;; Copyright (C) 2005 Georg Bauhaus
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;; Usage:
;;; mark a region, then command M-x Ada-to-WiKi. Modifies the
;;; current buffer by replacing the text in the region.
(defconst atwk-version "20051105")
;; or whatever version tag, if any, should be in ATWK-VERSION
;; * Overview of Implementation *
;; Starting at the first character of region, the translation first
;; creates a list of simple text tokens. The lexical elements are found
;; through some regular expressions, and using a function for string
;; literals. The tokenizer looks at the text at the current position to
;; see whether a prefix matches an expression. The expressions must be
;; tried in a certain order.
;; When there is a simple token list, the next step looks at each pair
;; of consecutive tokens in the list. If a pair can be combined into
;; an Ada two-character delimiter or operator, this is done, and the
;; result wrapped in appropriate markup. This process creates another
;; list of "WiKi tokens". If the first of the original tokens is '-'
;; or ''', special processing for comments, characters literals, and
;; attributes takes place. If the tokens cannot be combined, or if there
;; is only one token left, a similar function operates on the single
;; token returning an element suitably wrapped in markup if needed.
;; Finally, the original region is killed, and the wiki tokens
;; concatenated to form the replacement text of the region.
;;; Known Bugs:
;; If the region starts in column 1, the one-space indentation needed
;; for MediaWiKi program text is only prefixed to subsequent lines
;; in the region.
;;; Data:
; reserved words copied from draft 13 of AARM Ada 2005
(defconst atwk-Ada-Keywords
;; important: words must be lower case on this list
(list "abort"
"abs"
"abstract"
"accept"
"access"
"aliased"
"all"
"and"
"array"
"at"
"begin"
"body"
"case"
"constant"
"declare"
"delay"
"delta"
"digits"
"do"
"else"
"elsif"
"end"
"entry"
"exception"
"exit"
"for"
"function"
"generic"
"goto"
"if"
"in"
"interface"
"is"
"limited"
"loop"
"mod"
"new"
"not"
"null"
"of"
"or"
"others"
"out"
"overriding"
"package"
"pragma"
"private"
"procedure"
"protected"
"raise"
"range"
"record"
"rem"
"renames"
"requeue"
"return"
"reverse"
"select"
"separate"
"subtype"
"synchronized"
"tagged"
"task"
"terminate"
"then"
"type"
"until"
"use"
"when"
"while"
"with"
"xor"))
(defconst atwk-Ada-Attributes
;; important: every name on this list matches as if (UPCASE-INITIALS
;; (DOWNCASE NAME)) had been applied
(list "Access"
"Address"
"Adjacent"
"Aft"
"Alignment"
"Base"
"Bit_Order"
"Body_Version"
"Callable"
"Caller"
"Ceiling"
"Class"
"Component_Size"
"Compose"
"Constrained"
"Copy_Sign"
"Count"
"Definite"
"Delta"
"Denorm"
"Digits"
"Exponent"
"External_Tag"
"First"
"Floor"
"Fore"
"Fraction"
"Identity"
"Image"
"Class"
"Input"
"Last"
"Leading_Part"
"Length"
"Machine"
"Machine_Emax"
"Machine_Emin"
"Machine_Mantissa"
"Machine_Overflows"
"Machine_Radix"
"Machine_Rounding"
"Machine_Rounds"
"Max"
"Max_Size_In_Storage_Elements"
"Min"
"Mod"
"Model"
"Model_Emin"
"Model_Epsilon"
"Model_Mantissa"
"Model_Small"
"Modulus"
"Output"
"Partition_Id"
"Pos"
"Pred"
"Priority"
"Range"
"Read"
"Remainder"
"Round"
"Rounding"
"Safe_First"
"Safe_Last"
"Scale"
"Scaling"
"Signed_Zeros"
"Size"
"Small"
"Storage_Pool"
"Storage_Size"
"Stream_Size"
"Succ"
"Tag"
"Terminated"
"Truncation"
"Unbiased_Rounding"
"Unchecked_Access"
"Val"
"Valid"
"Value"
"Version"
"Wide_Image"
"Wide_Value"
"Wide_Wide_Image"
"Wide_Wide_Value"
"Wide_Wide_Width"
"Wide_Width"
"Width"
"Write"))
; Pieces of syntax *sufficiently* *similar* to Ada's. (Some obsolescent
; parts excluded.) Note that numbers and identifiers are combined in a
; single pattern. This is possible and useful because they are treated
; the same when producing output. There is no markup around either of
; them.
(defconst atwk-letter (concat "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"€")
"Letters occuring in identifiers. Some of the non-ASCII
characters are included. The intent is to include at least
those that are used somewhere in wikibook-ada.")
(defconst atwk-digit "0123456789#aAbBcCdDeEfF"
"characters occuring in numeric literals")
(defconst atwk-identifier (concat "[" atwk-letter atwk-digit "_" "]+")
"a span of characters making up a number or an identfier")
; atwk-delimiter1 must include atwk-operator1
(defconst atwk-operator1 "[&*+/<=>-]"
"single character operators.")
(defconst atwk-operator2 (regexp-opt '("**" ">=" "<=" "/=")
t)
"two-character operators.")
(defconst atwk-delimiter1 "[&'()*+,./:;<=>|!-]"
"single character delimiters.")
(defconst atwk-delimiter2 (regexp-opt '(">=" ".." "**" ":="
"/=" ">=" "<="
"<<" ">>" "<>")
t)
"two-character delimiters.")
; not used:
; special '?@{}[]`$\^~'
(defconst atwk-white "[[:blank:]\n]+"
"tabs, spaces, and new line characters.")
; (? can/should this depend on the current syntax table and use [:space:]?)
(defconst atwk-attribute
(concat "'\\(" "[" atwk-letter "][" atwk-letter "_" "]+\\)")
"an apostrophe, followed by at least two letters, and possibly
underscores. Must produce a group from the attribute characters")
(defconst atwk-start-of-string "[%\"]"
"characters that may start a string literal.")
; NOTE: are the ".=." links broken in the Ada_Programming book? %3D
; See also ATWK-ADA-DELIMITER-MW and ATWK-ADA-OPERATOR-MW.
(defconst MW-long-link-names
'(("+" . "plus")
("<" . "less_than")
(">" . "greater_than")
("|" . "vertical_line")
(".." . "double_dot"))
"Due to way MediaWiKi sites organise their pages and links using files
and URLs, some names are not permitted. So some operators are not
rendered/linked correctly unless special care is taken. I'm not really
sure that I am doing the right thing here as regards the known and
documented MediaWiKi markup limitations.
MediaWiKi:
``The following characters are not allowed in page titles:
``# + < > [ ] | { }
``Colon ...''
``..'' doesn't work either, probably because it denotes a parent
directory in a URL. (Correct?)
")
(defconst atwk-regression-testing nil
"If t, the functions that produce output will be bound to an identity
function, enabling regression testing. The transformation then calls
identity functions instead of the functions that produce MediaWiKi
markup around tokens.")
;;; Funtions:
(defun atwk-upto-end-of-string (beg end &optional quotation-mark)
"Find a quotation-mark after BEG that ends a string literal and update match data.
BEG is assumend to be the position of the first character of the (sub)string,
after a quotation-mark. Doubled quotation marks are skipped."
(let* ((Q (or quotation-mark "\""))
(QQ (concat Q Q)))
(or (and (search-forward QQ end t)
(atwk-upto-end-of-string (point) end Q))
(and (search-forward Q end t)))))
(defun atwk-message (msg)
"Announce the current time followed by MSG."
(let* ((now (current-time))
(high (car now))
(low (car(cdr now)))
(micro (car(cdr(cdr now)))))
(message "Ada-to-WiKi, Time: %s/%d %s"
(substring (current-time-string now) 11 19)
;; (+ (* high (lsh 2 15)) low)
micro
msg)))
(defun Ada-to-WiKi (beg end)
"Tokenize region and insert MediaWiki markup around some Ada tokens.
The text between BEG and END is first tokenized and the results made a
list. Ada comments can contain any kind of character, they are passed
as two delimiters followed by the remaining text on the line. There are
similar special circuits for character literals, string literals, and
attributes, too.
Some elements of the token list are then wrapped in MediaWiki markup, notably
reserved words, delimiters, attributes, and comments. The results are
inserted into the buffer replacing the text of the region."
(interactive "r")
(goto-char beg)
;; (message "input text: [%s]" (atwk-buf-sub beg end))
(atwk-message "start")
(let ((simple-tokens-list (atwk-tokenize beg end)))
(atwk-message (format "%d simple tokens found..."
(length simple-tokens-list)))
(let ((wiki-text (atwk-to-wiki simple-tokens-list)))
(if wiki-text
(progn (atwk-message (format "Replacing %d tokens of region (%d, %d)"
(length wiki-text) beg end))
(kill-region beg end)
(insert (mapconcat (function identity) wiki-text ""))
(atwk-message "Done."))
;; (message "new text is [%s]" (mapconcat 'upcase wiki-text "\\\\")))
(error "Something is wrong, no WiKi text has arrived")))))
;; setup of either MediaWiki or identity functions (for regression testing)
;; There is a LAMBDA in ATWK-ADA-COMMENT because of the MediaWiki Ada
;; markup for comments which is {{Ada/comment comment text}}. This
;; markup omits the '-' characters from the original "-- comment text"
;; (3 tokens originally). Rendering will show the hyphens again. The
;; tokens are therefore not cons'd onto the result list. The lambda
;; reintroduces them for regression testing, as there is no automatic
;; rendering of the hyphens.
(defconst atwk-Ada-attribute (if atwk-regression-testing
(function concat)
(function atwk-Ada-attribute-MW)))
(defconst atwk-Ada-comment (if atwk-regression-testing
(lambda (c) (concat "--" c))
(function atwk-Ada-comment-MW)))
(defconst atwk-Ada-delimiter (if atwk-regression-testing
(function concat)
(function atwk-Ada-delimiter-MW)))
(defconst atwk-Ada-operator (if atwk-regression-testing
(function concat)
(function atwk-Ada-operator-MW)))
(defconst atwk-Ada-keyword (if atwk-regression-testing
(function concat)
(function atwk-Ada-keyword-MW)))
;; mute indirect invocation chant
(defsubst atwk-ATTR (tok) (funcall atwk-Ada-attribute tok))
(defsubst atwk-CMNT (tok) (funcall atwk-Ada-comment tok))
(defsubst atwk-DELIM (tok) (funcall atwk-Ada-delimiter tok))
(defsubst atwk-KW (tok) (funcall atwk-Ada-keyword tok))
(defsubst atwk-OP (tok) (funcall atwk-Ada-operator tok))
(defsubst atwk-matching-text (n)
"buffer text of match group N without text properties."
(buffer-substring-no-properties (match-beginning n) (match-end n)))
(defun atwk-pos-after-match (end)
"new scanning position provided match doesn't exceed END.
An error is signalled if it does."
(if (<= (match-end 0) end)
(match-end 0)
(error "atwk-tokenize: last token [%s] exceeds region in ...%s[REGION END]"
(atwk-matching-text 0)
(buffer-substring (match-beginning 0) end))))
(defun atwk-tokenize (p end)
"turn a region of program text into a list of text tokens.
Tokens include white space, string/character literals, ``identifiers'',
delimiters, comments, and attributes. The last token must end before
region's END, otherwise there is an ERROR."
(let ((result (list)))
(while (< p end)
(goto-char p)
(cond ((eolp)
;; (assert (< (point) end))
(beginning-of-line 2) ; next line
(setq result (cons " " ; for MediaWiKi program text
(cons "\n" result))
p (point)))
((looking-at atwk-white)
(setq result (cons (atwk-matching-text 0)
result)
p (atwk-pos-after-match end)))
((looking-at "--\\(.*\\)$")
(setq result (cons (atwk-matching-text 1)
(cons "-"
(cons "-"
result)))
p (atwk-pos-after-match end)))
;; character literals (any character, including ''')
((looking-at "'\\(.\\)'")
(setq result (cons "'"
(cons (atwk-matching-text 1)
(cons "'"
result)))
p (atwk-pos-after-match end)))
;; attributes
((looking-at atwk-attribute)
(setq result (cons (atwk-matching-text 1)
(cons "'"
result))
p (atwk-pos-after-match end)))
;; Whichever of the characters in ATWK-START-OF-STRING is found
;; first will be taken to be the quotation mark that both
;; brackets the string and may occur doubled in the string.
((and (looking-at atwk-start-of-string)
(let* ((saved-point (match-end 0))
(eol (progn (end-of-line) (point)))
(qm (atwk-matching-text 0)))
(goto-char saved-point)
(atwk-upto-end-of-string (match-end 0)
eol
qm)))
(setq result (cons (buffer-substring-no-properties p (match-end 0))
result)
p (atwk-pos-after-match end)))
((looking-at atwk-identifier)
(setq result (cons (atwk-matching-text 0)
result)
p (atwk-pos-after-match end)))
((looking-at atwk-delimiter1)
(setq result (cons (atwk-matching-text 0)
result)
p (atwk-pos-after-match end)))
(t
(let* ((eol (progn (end-of-line) (point)))
(remaining (buffer-substring p eol)))
(error "Looking at [%s], cannot parse" remaining)))))
(nreverse result)))
(defun atwk-to-wiki-1 (tok operator1-exactly delimiter1-exactly)
"Process a single token.
See ATWK-TO-WIKI."
(cond ((string-match operator1-exactly tok)
(atwk-OP tok))
((string-match delimiter1-exactly tok)
(atwk-DELIM tok))
((member (downcase tok) atwk-Ada-Keywords)
(atwk-KW tok))
(t ; anything else, including white space tokens
; Attributes come after ''', they are taken care of
; in the two-tokens conditional in ATWK-TO-WIKI.
tok)))
(defun atwk-to-wiki (ts)
"turn a list of text tokens into another list with markup around some tokens."
; (message "atwk-to-wiki: {%s}"
; (mapconcat 'upcase ts "//"))
(let ((result (list))
(operator1-exactly (concat "^" atwk-operator1 "$"))
(delimiter1-exactly (concat "^" atwk-delimiter1 "$"))
(operator2-exactly (concat "^" atwk-operator2 "$"))
(delimiter2-exactly (concat "^" atwk-delimiter2 "$")))
(while (not (null ts))
(let ((tok1 (car ts)))
(cond ((null (cdr ts))
;; tok1 is the final token
(setq result (cons (atwk-to-wiki-1 tok1
operator1-exactly
delimiter1-exactly)
result)
ts (cdr ts)))
(t ; another token ahead
(let* ((tok2 (car (cdr ts)))
(two-toks (concat tok1 tok2)))
; (message "two-toks: [%s]" two-toks)
(cond ((string-equal "--" two-toks)
(setq result (cons (atwk-CMNT (car (cdr(cdr ts))))
result)
ts (cdr(cdr(cdr ts)))))
((string-match operator2-exactly two-toks)
(setq result (cons (atwk-OP two-toks) result)
ts (cdr(cdr ts))))
((string-match delimiter2-exactly two-toks)
(setq result (cons (atwk-DELIM two-toks) result)
ts (cdr(cdr ts))))
;; attributes, character literals
;; be optimistic about correctness of input, i.e. not '<char>'x
((string-equal "'" tok1)
(cond ((and (> (length tok2) 1)
(member (upcase-initials (downcase tok2))
atwk-Ada-Attributes))
(setq result (cons (atwk-ATTR tok2)
(cons (atwk-DELIM tok1)
result))
ts (cdr(cdr ts))))
(t ; a character literal
(setq result (cons tok2
(cons (atwk-DELIM tok1)
result))
ts (cdr(cdr ts))))))
(t ; process TOK1 only and shift
(setq result (cons (atwk-to-wiki-1 tok1
operator1-exactly
delimiter1-exactly)
result)
ts (cdr ts)))))))))
(nreverse result)))
; wrap pieces of program text in MediaWiki markup
(defun atwk-Ada-attribute-MW (text)
"wrap TEXT in suitable MediaWiki markup. Note that the tick mark is not
automatically produced when the markup is rendered, it must be present in
the WiKi text before the attribute."
(concat "{{Ada/attribute|" text "}}"))
(defun atwk-Ada-comment-MW (text)
"wrap TEXT in suitable MediaWiki markup"
(concat "{{Ada/-- |" text "}}"))
; ":=", and "=>" MediaWiki markup don't work at this time (2005-10-31)
(defun atwk-Ada-delimiter-MW (text)
"wrap TEXT in suitable MediaWiki markup"
(cond ((or (equal text ":=")
(equal text "=>"))
text)
((assoc text MW-long-link-names)
(let ((text2
(concat " 2|" (cdr (assoc text MW-long-link-names))
"|" text)))
(concat "{{Ada/delimiter" text2 "}}")))
(t
(concat "{{Ada/delimiter|" text "}}"))))
(defun atwk-Ada-keyword-MW (text)
"wrap TEXT in suitable MediaWiki markup"
(concat "{{Ada/keyword|" text "}}"))
; "<=", ">=", and "=" MediaWiki markup don't work at this time (2005-10-31)
(defun atwk-Ada-operator-MW (text)
"wrap TEXT in suitable MediaWiki markup"
(cond ((or (equal text "=")
(equal text ">=")
(equal text "<="))
text)
((assoc text MW-long-link-names)
(let ((text2
(concat " 2|" (cdr (assoc text MW-long-link-names))
"|" text)))
(concat "{{Ada/operator" text2 "}}")))
(t
(concat "{{Ada/operator|" text "}}"))))
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.