PageRenderTime 51ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/helper/parse.lisp

https://gitlab.com/eql/EQL5
Lisp | 348 lines | 331 code | 12 blank | 5 comment | 0 complexity | e17002ea30a29ca82674b485f3aec2fb MD5 | raw file
  1. ;;; copyright (c) Polos Ruetz
  2. (load "../src/lisp/x")
  3. (load "share")
  4. (load "load-modules")
  5. (defvar *skip*
  6. (list "(preliminary)"
  7. "(deprecated)"
  8. "(obsolete)"
  9. "(volatile "
  10. "connect("
  11. "const * const *"
  12. "<Attribute>"
  13. "<FormatRange>"
  14. "<WizardButton>"
  15. "<WritingSystem>"
  16. "[4][4]"
  17. "const char * const[]"
  18. "defaultAction"
  19. "float *"
  20. "int *"
  21. "iterator"
  22. "macEvent"
  23. "nativeArguments"
  24. "setNativeArguments"
  25. "operator"
  26. "placeholderText"
  27. "printerSelectionOption"
  28. "qreal *"
  29. "setAsDockMenu"
  30. "setItemRoleNames"
  31. "setPrinterSelectionOption"
  32. "setupUi"
  33. "singleShot"
  34. "toAscii"
  35. "versionFunctions"
  36. "NSDate"
  37. "NSMenu"
  38. "NSURL"
  39. "qintptr"
  40. "quintptr"
  41. "quint8 *"
  42. "qwsSet"
  43. "sockaddr"
  44. "uchar *"
  45. "void *"
  46. "winPage"
  47. "x11"
  48. "CFDate"
  49. "CFbsBitmap"
  50. "CFURL"
  51. "CGImageRef"
  52. "DefaultAction"
  53. "EditFocus"
  54. "ExecutionEngine"
  55. "QIconEngine"
  56. "QPaintEngine"
  57. "QPrintEngine"
  58. "QSharedPointer"
  59. "FILE"
  60. "FT_Face"
  61. "GLfloat *"
  62. "GLfloat["
  63. "GLint *"
  64. "GLuint *"
  65. "GUID"
  66. "Handle"
  67. "HANDLE"
  68. "HBITMAP"
  69. "HCURSOR"
  70. "HDC"
  71. "HICON"
  72. "KeyValue"
  73. "NavigationMode"
  74. " Margins "
  75. "Margins margins"
  76. "MSG"
  77. "OffsetData"
  78. "PaperSources"
  79. "PlaceholderText"
  80. "PointerToMemberFunction"
  81. "RawHeaderPair"
  82. "RenderFlags"
  83. "RSgImage"
  84. "SearchHit"
  85. "T "
  86. "T "
  87. "Q_PID"
  88. "Q_IPV6ADDR"
  89. "QAbstractOpenGLFunctions"
  90. "QAccessible::State"
  91. "QPair"
  92. "QDataStream"
  93. "QDecoration"
  94. "QGenericArgument"
  95. "QGenericMatrix"
  96. "QGraphicsTransform *"
  97. "QIODevice"
  98. "QList<Country>"
  99. "QList<T>"
  100. "QList<Tab>"
  101. "QList<QVariant>"
  102. "QByteArray *"
  103. "QByteArray & buffer ()"
  104. "QMatrix2x2"
  105. "QMatrix2x3"
  106. "QMatrix2x4"
  107. "QMatrix3x2"
  108. "QMatrix3x3"
  109. "QMatrix3x4"
  110. "QMatrix4x2"
  111. "QMatrix4x3"
  112. "QMatrix4x4 *"
  113. "QMap"
  114. "QMetaClassInfo"
  115. "QMetaEnum"
  116. "QMetaMethod"
  117. "QMetaProperty"
  118. "QPrinterInfo"
  119. "QuotationStyle"
  120. "QSet<"
  121. "QSymbianEvent"
  122. "QTextObjectInterface"
  123. "QVariantMap"
  124. "QVector2D *"
  125. "QVector3D *"
  126. "QVector4D *"
  127. "QWebChannel"
  128. "QWebEngineContextMenuData"
  129. "QWebEngineHistory"
  130. "QWebHistory"
  131. "QWebEngineScriptCollection & "
  132. "QWebNetworkRequest"
  133. "QWSEvent"
  134. "QXmlStreamReader"
  135. "WinPage"
  136. "X11"
  137. "XEvent"
  138. "**"
  139. "&&"
  140. "nativeArguments("
  141. "setNativeArguments("
  142. "viewportAttributesForSize"
  143. "const QGraphicsObject *"
  144. "swap(QDns"
  145. "swap(QHttp"
  146. "swap(QNetwork"
  147. "swap(QStorage"
  148. "::Attribute"
  149. "::AttributeSet"
  150. "::ColoredPoint2D"
  151. "::Point2D"
  152. "::Renderer"
  153. "::TexturePoint2D"
  154. "Attribute *"
  155. "AttributeSet "
  156. "ColoredPoint2D *"
  157. "Point2D *"
  158. "Renderer *"
  159. "TexturePoint2D *"
  160. ;; exclude what won't compile with Qt 5.5.1:
  161. "QAudio::Role" ; 5.6
  162. "textureFactoryForImage(" ; 5.6
  163. " moveMedia(" ; 5.7
  164. ))
  165. (defvar *check* nil)
  166. (defvar *2-newlines* (format nil "~%~%"))
  167. (defvar *var-name-trim* "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY1234567890_")
  168. (defun simplify (str &optional (join t))
  169. (let ((list (remove-if 'x:empty-string (x:split (string-trim '(#\Space #\Tab) str)))))
  170. (if join
  171. (x:join list)
  172. list)))
  173. (defun one-space (str)
  174. (with-output-to-string (s)
  175. (let (ex)
  176. (x:do-string (ch (string-trim " " str))
  177. (when (char= #\Tab ch)
  178. (setf ch #\Space))
  179. (if (char= #\Space ch)
  180. (unless (eql #\Space ex)
  181. (write-char #\Space s))
  182. (write-char ch s))
  183. (setf ex ch)))))
  184. (defun space-trim (x)
  185. (string-trim " " x))
  186. (defun var-name-trim (x)
  187. (if (find #\= x)
  188. (let ((xy (x:split x #\=)))
  189. (format nil "~A =~A"
  190. (var-name-trim (string-right-trim " " (first xy)))
  191. (second xy)))
  192. (if (find (char x (1- (length x))) "&*>")
  193. x
  194. (if (find-if (lambda (ch) (find ch "&*>")) x)
  195. (string-right-trim *var-name-trim* x)
  196. (subseq x 0 (position #\Space x :from-end t))))))
  197. (defun tokenize (str)
  198. (let* ((a (position #\( str))
  199. (b (position #\) str :from-end t))
  200. (begin (one-space (subseq str 0 a)))
  201. (end (one-space (subseq str (1+ b))))
  202. (args (mapcar 'space-trim (x:split (one-space (subseq str (1+ a) b)) #\,))))
  203. (format nil "~A ~A~A"
  204. begin
  205. (if args (format nil "( ~A )" (x:join (mapcar 'var-name-trim args) " , ")) "()")
  206. (if (x:empty-string end) "" (format nil " ~A" end)))))
  207. (let (text)
  208. (defun read-text (class)
  209. (let ((path (doc-file class)))
  210. (if (probe-file path)
  211. (with-open-file (s path :direction :input :external-format :latin-1)
  212. (setf text (make-string (file-length s)))
  213. (read-sequence text s))
  214. (progn
  215. (incf *not-found*)
  216. (x:d :not-found path)))))
  217. (defun super-names (class &optional (word "Inherits:"))
  218. (x:when-it (search* word text)
  219. (let* ((line (subseq text (+ x:it (length word)) (position #\Newline text :start x:it)))
  220. (names (simplify line nil)))
  221. (unless names
  222. (fresh-line)
  223. (error "No super class found for: ~S" class))
  224. names)))
  225. (defun super-class (class)
  226. (let ((super (remove "and" (super-names class) :test 'string=)))
  227. (when (and (second super)
  228. (not (find #\< (first super)))) ; template
  229. (format *check* "~A: ~A and ~A~%"
  230. class
  231. (first super)
  232. (second super)))
  233. (first super)))
  234. (defun parse (type class s so no-new)
  235. ;; "bool QPainter::begin ( QPaintDevice * )": multiple inheritance problem
  236. (let* ((pub (string= "public functions" type))
  237. (qpainter (and pub (string= "QPainter" class)))
  238. (qvariant (and pub (string= "QVariant" class))))
  239. (cond (qpainter
  240. (dolist (device (list "QImage" "QPdfWriter" "QPicture" "QPixmap" "QPrinter" "QWidget"))
  241. (format s "~% \"new QPainter ( ~A * )\"~
  242. ~% \"bool begin ( ~A * )\""
  243. device device)))
  244. (qvariant
  245. (format s "~% \"new QVariant ( const QCursor & )\"")))
  246. (let ((static (x:starts-with "static" type))
  247. (protected (search "protected" type))
  248. (p (search* (format nil "~%~A~%~%" type) text)))
  249. (when p
  250. (let* ((a (+ 2 (search *2-newlines* text :start2 p)))
  251. (b (search *2-newlines* text :start2 a))
  252. (in (make-string-input-stream (subseq text a b))))
  253. (x:while-it (read-line in nil nil)
  254. (unless (or (dolist (skip *skip*)
  255. (when (search skip x:it)
  256. (return t)))
  257. (and (search "QHash<" x:it)
  258. (not (search "QHash<int, QByteArray>" x:it))) ; needed in QAbstractItemModel
  259. (and (search "Functor" x:it)
  260. (not (search "FunctorOrLambda" x:it)))) ; needed for QWebEngine
  261. (when (search "QByteArray const" x:it)
  262. (setf x:it (x:string-substitute "const QByteArray" "QByteArray const" x:it)))
  263. (when (search "QHash<int, QByteArray>" x:it)
  264. (setf x:it (x:string-substitute "QHashIntQByteArray" "QHash<int, QByteArray>" x:it)))
  265. (when (find #\( x:it)
  266. ;; special default values
  267. (x:when-it* (search " = QRect( QPoint( 0, 0 ), QSize( -1, -1 ) )" x:it)
  268. (setf x:it (x:cc (subseq x:it 0 x:it*) " = QRect_DEFAULT)")))
  269. (x:when-it* (search " = QMarginsF( 0, 0, 0, 0 )" x:it)
  270. (setf x:it (x:cc (subseq x:it 0 x:it*) " = QMarginsF_DEFAULT)")))
  271. (x:when-it* (search " = QPageLayout( QPageSize( QPageSize::A4 ), QPageLayout::Portrait, QMarginsF() )" x:it)
  272. (setf x:it (x:cc (subseq x:it 0 x:it*) " = QPageLayout_DEFAULT)")))
  273. (let* ((fun (tokenize x:it))
  274. (new (and (not static)
  275. (x:starts-with (format nil "~A (" class) fun)))
  276. (virtual (x:starts-with "virtual" fun)))
  277. (unless (or (and qpainter (search "QPaintDevice" fun :test 'string=))
  278. (and new no-new)
  279. (and new protected)
  280. (find #\~ fun) ; destructor
  281. ;; template problem
  282. (and (string= "QVariant" class)
  283. (string= "bool canConvert () const" fun)))
  284. (when virtual
  285. (format so "~% \"~A\"" fun))
  286. (unless (and virtual protected)
  287. (format s "~% \"~A~A\"" (cond (new "new ") ; constructor
  288. (protected "protected ")
  289. (static "static ")
  290. (t ""))
  291. fun)))))))))))))
  292. (defun parse-classes (classes s so non)
  293. (dolist (class classes)
  294. (let* ((no-new (x:starts-with "//" class))
  295. (class* (string-left-trim "/" class))
  296. (file class*))
  297. (x:when-it (search "::" class*)
  298. (setf file (subseq class* 0 x:it)
  299. class* (subseq class* (+ 2 x:it))))
  300. (read-text file)
  301. (format t "~%parsing ~A" class*)
  302. (let ((super (super-class class)))
  303. (format s " ((~S . ~S)" class* super)
  304. (format so " ((~S . ~S)" class* super))
  305. (dolist (type (list "public functions"
  306. "protected functions"
  307. "reimplemented public functions"
  308. "reimplemented protected functions"
  309. "static public members"
  310. "static protected members"))
  311. (parse type class* s so no-new)
  312. (write-char #\.))
  313. (format s ")~%")
  314. (format so ")~%")))
  315. (format s "))~%")
  316. (format so "))~%"))
  317. (defun start ()
  318. (with-open-file (*check* "multiple-inheritance.txt" :direction :output :if-exists :supersede)
  319. (mapc (lambda (names non)
  320. (let ((pre (if non #\n #\q)))
  321. (with-open-file (s (format nil "parsed/~C-methods.lisp" pre) :direction :output :if-exists :supersede)
  322. (with-open-file (so (format nil "parsed/~C-override.lisp" pre) :direction :output :if-exists :supersede)
  323. (format so "(defparameter *~C-override* '(~%" pre)
  324. (format s "(defparameter *~C-methods* '(~%" pre)
  325. (parse-classes (mapcar (lambda (name)
  326. (string-trim "= " (x:if-it (position #\( name)
  327. (subseq name 0 x:it)
  328. name)))
  329. names)
  330. s so non)))))
  331. (list *q-names* *n-names*)
  332. (list nil :non)))
  333. (if (zerop *not-found*)
  334. (format t "~%OK~%~%")
  335. (warn (format nil "Text files not found: ~D" *not-found*))))
  336. (start)