Debian Codegraph @

src/util/gen-symbol-table.lisp

raw
1 ;;; This file is used to generate symbols_list.h. Go to the last page to see
2 ;;; an ad-hoc parser for the old symbols_list.h from which the current
3 ;;; definitions were created. Keep in mind that it is only a helper to
4 ;;; generate the cl_symbols table in a desired format, some manual curation is
5 ;;; also needed (i.e modification of the structure etc).
6
7
8 ;;; Utilities
9
10 (defvar *pkg+kind-mapping*
11 `(("CL_ORDINARY" :cl :ordinary)
12 ("CL_SPECIAL" :cl :special)
13 ("CL_CONSTANT" :cl :constant)
14 ("CL_FORM" :cl :form)
15 ("SI_ORDINARY" :si :ordinary)
16 ("SI_SPECIAL" :si :special)
17 ("SI_CONSTANT" :si :constant)
18 ("EXT_ORDINARY" :ext :ordinary)
19 ("EXT_SPECIAL" :ext :special)
20 ("EXT_CONSTANT" :ext :constant)
21 ("EXT_FORM" :ext :form)
22 ("MP_ORDINARY" :mp :ordinary)
23 ("MP_SPECIAL" :mp :special)
24 ("MP_CONSTANT" :mp :constant)
25 ("CLOS_ORDINARY" :clos :ordinary)
26 ("CLOS_SPECIAL" :clos :special)
27 ("CLOS_ORDINARY | PRIVATE" :clos :private)
28 ("KEYWORD" :keyword :constant)
29 ("GRAY_ORDINARY" :gray :ordinary)
30 ("FFI_ORDINARY" :ffi :ordinary)
31 ("FFI_CONSTANT" :ffi :constant)))
32
33 (defun pkg+kind->type (pkg kind)
34 (car (find (list pkg kind) *pkg+kind-mapping* :key #'cdr :test #'equal)))
35
36 (defun type->pkg+kind (type)
37 (cdr (find type *pkg+kind-mapping* :key #'car :test #'equal)))
38
39 (defun split-sequence (char string &aux acc)
40 (do ((line string (subseq line (1+ (position char line)))))
41 ((null (position char line))
42 (push line acc)
43 (nreverse (mapcar (lambda (str)
44 (string-trim '(#\space) str))
45 acc)))
46 (push (subseq line 0 (position char line)) acc)))
47
48
49 ;;; Parser returns a list of "lines" in a preprocessed form:
50 ;;;
51 ;;; (line-type ,@arguments) ; or
52 ;;; (pack name kind value cfun narg)
53 ;;;
54 ;;; LINE-TYPE is either :macro, :comment, :newline, :end-tag or :entry. PACK
55 ;;; is one of core packages (i.e :ffi), NAME is the symbol name etc. CFUN is
56 ;;; the name of the corresponding C function.
57 ;;;
58 ;;; Entry parsers have versions so it is possible to parse and generate older
59 ;;; versions of the table.
60 ;;;
61 ;;; Example usage:
62 ;;;
63 #+(or)
64 (let ((symbols-list (parse-symbols-list "path/to/symbols_list.h" #'v1-parse-entry)))
65 (generate-symbols-list symbols_list #'v1-generate-entry))
66
67 (defun parse-symbols-list (path parser)
68 (with-open-file (s path)
69 (do ((line (read-line s nil :eof)
70 (read-line s nil :eof))
71 results)
72 ((eq line :eof) (nreverse results))
73 (push
74 (cond ((zerop (length line))
75 `(:newline))
76 ((search "}};" line)
77 `(:end-tag ,(funcall parser line)))
78 ((case (char line 0)
79 (#\{ `(:entry ,(funcall parser line)))
80 (#\# `(:macro ,line))
81 (otherwise (if (zerop (length line))
82 `(:newline)
83 `(:comment ,line))))))
84 results))))
85
86 (defun generate-symbols-list (results generator)
87 (loop for line in results
88 do (case (first line)
89 (:macro (format t "~a~%" (second line)))
90 (:comment (format t "~a~%" (second line)))
91 (:newline (terpri))
92 (:end-tag (format t "~a};~%" (apply generator (second line))))
93 (:entry (format t "~a,~%" (apply generator (second line)))))))
94
95 (defun v1-parse-entry (line)
96 (destructuring-bind (name package+kind cfun narg value)
97 (split-sequence #\, (subseq line
98 (1+ (position #\{ line))
99 (position #\} line)))
100 (let* ((pkg-kind (type->pkg+kind package+kind))
101 (pack (first pkg-kind))
102 (kind (second pkg-kind))
103 (narg (parse-integer narg)))
104 (list pack name kind value cfun narg))))
105
106 (defun v1-generate-entry (package name kind value cfun narg)
107 (let ((type (pkg+kind->type package kind))
108 (narg (or narg -1)))
109 (format nil "{~a, ~a, ~a, ~d, ~a}"
110 name type cfun narg value)))
111
112 ;;; V2 is defined to unify symbols_list.h files between ECL core and DPP. Also
113 ;;; it gives the all_symbols.d module an insight for the predefined symbol
114 ;;; name mappings (see #534).
115 (defun v2-parse-entry (line)
116 (let* ((fun-pos (search "ECL_FUN" line))
117 (var-pos (search "ECL_VAR" line))
118 (end-pos (position #\) line :from-end t))
119 (name (subseq line 1 (1- fun-pos)))
120 (fun-arg (split-sequence #\, (subseq line
121 (+ fun-pos 8)
122 (- var-pos 2))))
123 (var-arg (split-sequence #\, (subseq line
124 (+ var-pos 8)
125 end-pos))))
126 (destructuring-bind (fun-name cfun narg) fun-arg
127 (declare (ignore fun-name))
128 (destructuring-bind (type value) var-arg
129 (let* ((pkg-kind (type->pkg+kind type))
130 (pack (first pkg-kind))
131 (kind (second pkg-kind))
132 (narg (parse-integer narg)))
133 (list pack name kind value cfun narg))))))
134
135 (defun v2-generate-entry (package name kind value cfun narg)
136 (let ((type (pkg+kind->type package kind))
137 (narg (or narg -1))
138 (fun-name (let* ((start (position #\( cfun :from-end t))
139 (end (position #\) cfun))
140 ;; Find the "innermost" symbol, i.e for
141 ;; IF_DFFI(ECL_NAME(foobar)) it is foobar
142 (fname (if start
143 (subseq cfun
144 (1+ start)
145 end)
146 cfun)))
147 ;; If it is NULL, we want to specify NULL, otherwise we
148 ;; wrap the symbol in quotes to have a string.
149 (if (string= fname "NULL")
150 "NULL"
151 (format nil "~s" fname)))))
152 (format nil "{~a ECL_FUN(~a, ~a, ~a) ECL_VAR(~a, ~a)}"
153 name fun-name cfun narg type value)))