aadl-mode.el 9.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
;; aadl-mode
;; Copyright 2004-2006 Ecole nationale superieure des telecommunications
;; Laurent.Pautet@enst.fr, Thomas.Vergnaud@enst.fr, Bechir.Zalila@enst.fr

;; To load this file, just add the following line to your .emacs :
;; (load "/path/to/this/file.el")

(defvar aadl-mode-hook nil)

10
11
12
13
14
15
16
; Add support for compilation of AADL files directly from emacs
(require 'compile)
(add-hook 'aadl-mode-hook
	  (lambda ()
	    (set (make-local-variable 'compile-command)
		 (concat "ocarina -aadlv2 -f -p " buffer-file-name))))

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
(defvar aadl-mode-map
  (let ((aadl-mode-map (make-keymap)))
    (define-key aadl-mode-map "\C-j"  'new-line-and-indent)
    (define-key aadl-mode-map "\C-c;" 'comment-region)
    (define-key aadl-mode-map "\C-c:" 'uncomment-region)
    aadl-mode-map)
  "keymap for AADL major mode")

; We declare that the aadl mode should be used with .aadl files
(add-to-list 'auto-mode-alist '("\\.aadl\\'" . aadl-mode))

; The AADL syntax table
(defvar aadl-mode-syntax-table nil "syntax table for aadl-mode")
(setq aadl-mode-syntax-table (make-syntax-table))
(set-syntax-table aadl-mode-syntax-table)

; Modify the syntax table to be conformant with the AADL syntax

; The '_' character may belong to an AADL word
(modify-syntax-entry ?_ "w" aadl-mode-syntax-table)

; The '.' and the ':' characters are word separators
(modify-syntax-entry ?. "." aadl-mode-syntax-table)
(modify-syntax-entry ?: "." aadl-mode-syntax-table)

; There is only one comment style in AADL beginning with the "--" string
; and ending with a new line or a new page
(modify-syntax-entry ?- ". 12" aadl-mode-syntax-table)
(modify-syntax-entry ?\n ">" aadl-mode-syntax-table)
(modify-syntax-entry ?\f ">" aadl-mode-syntax-table)

; AADL syntactic coloration. Th order of the elements in the global list
; is important. The first element rules override the second element rules 
; which override the third...

(defconst aadl-font-lock-keywords
  (list
   ;; Keywords
   (list (concat
	  "\\<"
	  (regexp-opt
	   '("abstract" "access" "and" "applies" "binding" "calls" "classifier" 
	     "connections" "constant" "delta" "data" "enumeration" "event" 
60
	     "feature" "features" "flow" "flows" "group" "in" "inherit" "initial"
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
	     "inverse" "is" "list" "mode" "modes" "not" "out" "parameter" 
	     "path" "port" "private" "process" "processor" "prototypes" 
	     "properties" "with" "renames"
	     "property" "provides" "public" "range" "reference" "refined" 
	     "refines" "requires" "server" "set" "sink" "source" 
	     "subcomponents" "system" "thread" "to" "type" "units" "value"
	     "virtual") t)
	  "\\>") 
	 '(1 font-lock-keyword-face))

   ;; Three keywords followed by an identifier or a scoped name
   (list (concat
	  ;; 1st kw
	  (regexp-opt 
	   '("event" "port" "provides" "requires" "thread") t)
	  "[ \t]+"
	  ;; 2nd kw
	  (regexp-opt 
	   '("group" "data") t)
	  "[ \t]+"
	  ;; 3rd kw
	  (regexp-opt 
	   '("access" "implementation" "port") t)
	  "\\>[ \t]*"
	  "\\(\\sw+\\(\\(\\.\\|::\\)\\sw*\\)*\\)?")
	 '(1 font-lock-keyword-face) 
	 '(2 font-lock-keyword-face)
	 '(3 font-lock-keyword-face)
	 '(4 font-lock-function-name-face nil t))

   ;; Couple of keywords followed by an identifier or a scoped name
   (list (concat
	  ;; 1st kw
	  (regexp-opt 
	   '("bus" "data" "device" "end" "event" "memory" "port" "process" 
	     "processor" "property" "thread" "port" "subprogram" "system") t)
	  "[ \t]+"
	  ;; 2nd kw
	  (regexp-opt 
	   '("group" "implementation" "port" "set") t)
	  "\\>[ \t]*"
	  "\\(\\sw+\\(\\(\\.\\|::\\)\\sw*\\)*\\)?")
	 '(1 font-lock-keyword-face) 
	 '(2 font-lock-keyword-face)
	 '(3 font-lock-function-name-face nil t))

   ;; One single keyword followed by an identifier or a scoped name
   (list (concat
	  "\\<"
	  (regexp-opt
	   '("annex" "bus" "data" "device" "end" "extends" "memory" 
	     "package" "parameter" "process" "processor" "property" "port" 
113
	     "subprogram" "system" "thread" "with") t)
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
 	  "\\>[ \t]*"
 	  "\\(\\sw+\\(\\(\\.\\|::\\)\\sw*\\)*\\)?") 
 	 '(1 font-lock-keyword-face) 
	 '(2 font-lock-function-name-face nil t))
   
   ;; Identifier followed by a single colon ':'
   (list (concat
 	  "^[ \t]*\\<\\("
	  "\\sw+"
 	  "\\)\\>[ \t]*:[^:]") 
 	 '(1 font-lock-variable-name-face) 
	 '(2 font-lock-text-face nil t))

   ;; Constants
   (list (concat
	  "\\<"
	  (regexp-opt 
	   '("true" "false" "none" "all") t)
	  "\\>")
	 '(1 font-lock-constant-face))

   ;; Types
   (list (concat
	  "\\<"
	  (regexp-opt 
	   '("aadlboolean" "aadlinteger" "aadlreal" "aadlstring") t)
	  "\\>")
	 '(1 font-lock-type-face))
   "AADL Colors"))

;; specialized indentation functions

(defun aadl-indent-end-statement ()
  (forward-line -1)
  (cond 
   ((looking-at "^[ \t]*end")
    0)

   ((looking-at "^[ \t]*\\<\\(package\\|property[ \t]+set\\)\\> +\\sw+")
    0)
   
   ((looking-at "^[ \t]*\\<\\(bus\\|data\\|port\\)\\>[ \t]+\\<\\(access\\|port\\|group\\)\\>")
    (aadl-indent-end-statement))
   
   ((looking-at "^[ \t]*\\<\\(data\\|thread\\|subprogram\\|process\\|bus\\|processor\\|device\\|memory\\|system\\|port[ \t]+group\\)\\([ \t]+implementation\\)?\\>[ \t]+\\sw+")
    (current-indentation))
   
   ((bobp) 0)
   
   (t (aadl-indent-end-statement))
   )
  )

(defun aadl-indent-component-statement ()
  (forward-line -1)
  (cond 
   ((looking-at "^[ \t]*end")
    (current-indentation))
   
   ((looking-at "^[ \t]*\\<\\(private\\|public\\|is\\|package\\|property[ \t]+set\\)\\>")
    2)
   
   ((bobp) 0)
   
   (t (aadl-indent-component-statement))
   )
  )

(defun aadl-indent-subclause-statement ()
  (forward-line -1)
  (cond
   
   ((bobp) 10) ; this should not happen

   ((looking-at "^[ \t]*\\<\\(bus\\|data\\|port\\)\\>[ \t]+\\<\\(access\\|port\\|group\\)\\>")
    (aadl-indent-subclause-statement))
   
   ((looking-at "^[ \t]*\\<\\(data\\|thread\\|subprogram\\|process\\|bus\\|processor\\|device\\|memory\\|system\\|port +group\\)\\([ \t]+implementation\\)?\\>")
    (current-indentation))
   
   (t (aadl-indent-subclause-statement))
   )
  )

(defun aadl-indent-statement ()
  (forward-line -1)
  (cond 
   ((bobp) 0)

   ((looking-at "^[ \t]*\\<\\(package\\|property[ \t]+set\\)\\>[^;]*$")
    2)

   ((looking-at "^[ \t]*\\<\\(public\\|private\\|is\\)\\>[^;]*$")
    2)

   ((looking-at "^[ \t]*end")
    (current-indentation))

   ((looking-at "^[ \t]*\\<\\(bus\\|data\\|port\\)\\>[ \t]+\\<\\(access\\|port\\|group\\)\\>")
    (aadl-indent-statement))
   
   ((looking-at "^[ \t]*\\<\\(data\\|thread\\|subprogram\\|process\\|bus\\|processor\\|device\\|memory\\|port +group\\|features\\|subcomponents\\|properties\\|flows\\|connections\\|modes\\|calls\\)\\>[^;]*$")
    (+ (current-indentation) 2))
   
   ((looking-at "^[ \t]*$")
    (aadl-indent-statement))

   (t (current-indentation))
   )
  )

;; the main indentation function
(defun aadl-indent-line ()
  "indent current line"
  (interactive)
  ;; save the current point position and declare the indentation value variable
  (let ((aadl-current-column 
	 (max 0 (- (current-column) (current-indentation))))
	aadl-indent-value)
    (beginning-of-line)
    (setq 
     aadl-indent-value
     (max 
      0
      (save-excursion
	(cond 
	 ((bobp) 0)
	 
	 ((looking-at "^[ \t]*\\<\\(package\\|property +set\\|private\\|public\\|is\\)\\>") 0)
	 
	 ((looking-at "^[ \t]*end") 
	  (aadl-indent-end-statement))
	 
	 ((looking-at "^[ \t]*\\<\\(data\\|thread\\|subprogram\\|process\\|bus\\|processor\\|device\\|memory\\|system\\|port +group\\)[ \t]+\\(implementation\\)?\\>")
	  (aadl-indent-component-statement))
	 
	 ((looking-at "^[ \t]*\\<\\(features\\|modes\\|subcomponents\\|properties\\|flows\\|connections\\|calls\\)\\>")
	  (aadl-indent-subclause-statement))
	 
	 (t
	  (aadl-indent-statement))
	 ))))
    (indent-line-to aadl-indent-value)
    
    ;; calculate the new point position
    (setq aadl-current-column (+ aadl-current-column aadl-indent-value))
    (move-to-column aadl-current-column)))

(defun aadl-fill-paragraph (ARG)
  (back-to-indentation)
  (cond 
   ((looking-at comment-start)
    (re-search-forward "---*[ \t]*")
    (set-fill-prefix)
    (fill-paragraph ARG))
   
   (t (error "not in comment")))
  )

(defun aadl-mode ()
  "Major mode for editing AADL descriptions"
  (interactive)
  (kill-all-local-variables)
  (use-local-map aadl-mode-map)
  
  ;; register the syntax table
  (set-syntax-table aadl-mode-syntax-table)

  ;; register the keywords for syntax highlighting
  (set (make-local-variable 'font-lock-defaults) 
       '((aadl-font-lock-keywords) nil t))
     
  ;; register the indenting function
  (set (make-local-variable 'indent-line-function)
       'aadl-indent-line)

  ;; a few definitions necessary for aadl-fill-paragraph
  (set (make-local-variable 'comment-line-break-function)
       (lambda (&optional soft) (indent-new-comment-line)))
  (set (make-local-variable 'comment-start) "--")
  (set (make-local-variable 'comment-indent-function) 'aadl-indent-statement)
  
  ;;  Support for ispell : Check only comments
  (set (make-local-variable 'ispell-check-comments) 'exclusive)
  
  ;;  Support for indent-new-comment-line (Especially for XEmacs)
  (setq comment-multi-line nil)
   
  (set (make-local-variable 'use-hard-newlines) t)
  
  (set (make-local-variable 'fill-paragraph-function)
      'aadl-fill-paragraph)   
  
  (setq major-mode 'aadl-mode)
  (setq mode-name "AADL")
  (run-hooks 'aadl-mode-hook))

(provide 'aadl-mode)