File: api-generate.lisp

package info (click to toggle)
albert 0.4.10.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,556 kB
  • ctags: 2,014
  • sloc: lisp: 13,587; ansic: 7,729; xml: 843; makefile: 99; sh: 28
file content (163 lines) | stat: -rw-r--r-- 4,623 bytes parent folder | download | duplicates (2)
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
;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: APISPEC -*-

#|

DESC: apispec/api-generate.lisp - the program which generates/updates APIs
Copyright (c) 1998,1999 - Stig Erik Sand

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.

|#

(in-package :sds-api-apispec)

(defclass api-language ()
  ((name :initarg :name
	 :accessor lang.name)))

(defclass api-lang-lisp (api-language)
  ())

(defclass api-lang-cpp (api-language)
  ())

(defclass api-lang-java (api-language)
  ())

(defclass api-lang-python (api-language)
  ())

(defclass api-lang-html (api-language)
  ())

(defvar *supported-languages* (make-hash-table :test #'equal))

(defun add-language (name class-sym)
  (setf (gethash (string-upcase name) *supported-languages*) 
	#'(lambda () (make-instance class-sym :name name))))

(defun get-language (name)
  (let ((res (gethash (string-upcase name) *supported-languages*)))
    (unless res
      (warn "Unable to find language ~a" name)
      (return-from get-language nil))
    (funcall res)))

;;; added languages

(add-language "lisp" 'api-lang-lisp)
(add-language "cpp" 'api-lang-cpp)
(add-language "java" 'api-lang-java)
(add-language "python" 'api-lang-python)
(add-language "html" 'api-lang-html)

(defgeneric output-declarations (obj lang stream api-name)
  (:documentation "Outputs declaration to given stream"))

(defmethod output-declarations (obj lang stream api-name)
  (declare (ignore api-name))
  (warn "No sensible (~:@(~a ~a ~a ~a ..~))" 
	"output-declarations"
	(its-name obj)
	(its-name lang)
	(its-name stream)))

(defgeneric output-code (obj lang stream api-name)
  (:documentation "Outputs the 'code' to given stream"))

(defmethod output-code (obj lang stream api-name)
  (declare (ignore api-name))
  (warn "No sensible (~:@(~a ~a ~a ~a ..~))" 
	"output-code"
	(its-name obj)
	(its-name lang)
	(its-name stream)))

(defgeneric get-name-of-constant (lang const))
(defgeneric get-name-of-class (lang api-name obj))
(defgeneric get-the-actual-type (lang key))

(defun generate-api (spec-tree &key lang out-dir out-file decl-file src-file)
  "This function takes a tree as the main argument and a few rather important
keyword-parameters as arguments."

  (let ((lang-class (get-language lang)))
    (unless lang-class
      (warn "unable to find language ~a" lang)
      (return-from generate-api nil))
      
    (let ((*print-circle* nil))
      ;;(warn "Switching on ~a" lang-class)
      (cond
	((string-equal lang "cpp")
	     
	 ;; we're c++
	 ;; squeeze out declarations
	 (with-open-file (str (merge-pathnames (pathname decl-file))
			      :direction :output
			      :if-exists :supersede
			      :if-does-not-exist :create)
	   (dolist (some-api spec-tree)
	     (output-declarations some-api lang-class str "")))
	 
	 ;;squeeze out some code
	 (with-open-file (str (merge-pathnames (pathname src-file))
			      :direction :output
			      :if-exists :supersede
			      :if-does-not-exist :create)
	   (dolist (some-api spec-tree)
	     (output-code some-api lang-class str ""))))

	((string-equal lang "java")
	 ;; we're java
	 (dolist (some-api spec-tree)
	   (output-code some-api lang-class nil out-dir)))

	((string-equal lang "python")
	 ;; we are the python. resistance is 10 Ohm
	 (dolist (some-api spec-tree)
	       (output-code some-api lang-class nil out-dir)))
		
	((string-equal lang "lisp")
	 ;; we're lisp
	 ;;squeeze out some code
	 (with-open-file (str (merge-pathnames (pathname out-file))
			      :direction :output
			      :if-exists :supersede
			      :if-does-not-exist :create)
	   (dolist (some-api spec-tree)
	     (output-code some-api lang-class str ""))))
	
	((string-equal lang "html")
	 ;; we write HTML.. maybe it can be used in HTML-OS
	 (with-open-file (str (merge-pathnames (pathname out-file))
			      :direction :output
			      :if-exists :supersede
			      :if-does-not-exist :create)
	   (dolist (some-api spec-tree)
	     (output-code some-api lang-class str ""))))


	(t
	 (warn "Unknown language.. ~a" lang)))

      )))


(defun generate-api-from-file (spec-file &key lang out-dir out-file decl-file src-file)
  "parses the given file and then calls GENERATE-API"

  (let ((api-tree (parse-apispec-file spec-file)))
    
    (unless api-tree
      (return-from generate-api-from-file nil))
    
    (generate-api api-tree
		  :lang lang
		  :out-dir out-dir
		  :out-file out-file
		  :decl-file decl-file
		  :src-file src-file)))