summaryrefslogtreecommitdiff
path: root/skribe/src/bigloo/c.scm
blob: 07290ce82206cc963fb7bc99a0a5b3066cacbe7c (plain)
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
;*=====================================================================*/
;*    serrano/prgm/project/skribe/src/bigloo/c.scm                     */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Mon Sep  1 12:08:39 2003                          */
;*    Last change :  Thu May 27 10:11:24 2004 (serrano)                */
;*    Copyright   :  2003-04 Manuel Serrano                            */
;*    -------------------------------------------------------------    */
;*    C fontification                                                  */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribe_c

   (include "new.sch")
   
   (import  skribe_types
	    skribe_lib
	    skribe_resolve
	    skribe_eval
	    skribe_api
	    skribe_param
	    skribe_source)

   (export  C))

;*---------------------------------------------------------------------*/
;*    C stamps                                                         */
;*---------------------------------------------------------------------*/
(define *keyword* (gensym))
(define *cpp* (gensym))

;*---------------------------------------------------------------------*/
;*    C keywords                                                       */
;*---------------------------------------------------------------------*/
(for-each (lambda (symbol)
	     (putprop! symbol *keyword* #t))
	  '(for class template while return try catch break continue
		do if else typedef struct union goto switch case
		static extern default finally throw))
(let ((sharp (string->symbol "#")))
   (for-each (lambda (symbol)
		(putprop! (symbol-append sharp symbol) *cpp* #t))
	     '(include define if ifdef ifdef else endif)))

;*---------------------------------------------------------------------*/
;*    C ...                                                            */
;*---------------------------------------------------------------------*/
(define C 
   (new language
      (name "C")
      (fontifier c-fontifier)
      (extractor #f)))

;*---------------------------------------------------------------------*/
;*    c-fontifier ...                                                  */
;*---------------------------------------------------------------------*/
(define (c-fontifier s)
   (let ((g (regular-grammar ()
	       ((: "/*" (* (or (out #\*) (: (+ #\*) (out #\/ #\*))))
			(+ #\*) "/")
		;; bold comments
		(let ((str (split-string-newline (the-string))))
		   (append (map (lambda (s)
				   (if (eq? s 'eol)
				       "\n"
				       (new markup
					  (markup '&source-line-comment)
					  (body s))))
				str)
			   (ignore))))
	       ((: "//" (* all))
		;; italic comments
		(let ((c (new markup
			    (markup '&source-comment)
			    (body (the-string)))))
		   (cons c (ignore))))
	       ((+ (or #\Newline #\Space))
		;; separators
		(let ((str (the-string)))
		   (cons str (ignore))))
	       ((in "{}")
		;; brackets
		(let ((str (the-string)))
		   (let ((c (new markup
			       (markup '&source-bracket)
			       (body (the-string)))))
		      (cons c (ignore)))))
	       ((+ (out #\; #\Space #\Tab #\Newline #\( #\) #\{ #\} #\[ #\] #\" #\< #\> #\= #\! #\/ #\/ #\+ #\* #\-))
		;; keywords
		(let* ((string (the-string))
		       (symbol (the-symbol)))
		   (cond
		      ((getprop symbol *keyword*)
		       (let ((c (new markup
				   (markup '&source-keyword)
				   (ident (symbol->string (gensym)))
				   (body string))))
			  (cons c (ignore))))
		      ((getprop symbol *cpp*)
		       (let ((c (new markup
				   (markup '&source-module)
				   (ident (symbol->string (gensym)))
				   (body string))))
			  (cons c (ignore))))
		      (else
		       (cons string (ignore))))))
	       ((in "<>=!/\\+*-([])")
		;; regular text
		(let ((s (the-string)))
		   (cons s (ignore))))
	       ((: "\"" (* (or (out #a000 #\\ #\") (: #\\ all))) "\"")
		;; strings
		(let ((str (split-string-newline (the-string))))
		   (append (map (lambda (s)
				   (if (eq? s 'eol)
				       "\n"
				       (new markup
					  (markup '&source-string)
					  (body s))))
				str)
			   (ignore))))
	       ((+ (or #\; #\" #\# #\tab))
		(let ((str (the-string)))
		   (cons str (ignore))))
	       (else
		(let ((c (the-failure)))
		   (if (eof-object? c)
		       '()
		       (error "source(C)" "Unexpected character" c)))))))
      (read/rp g (open-input-string s))))