comparison lisp/progmodes/verilog-mode.el @ 93066:f35f15ba549f

(verilog-syntax-ppss): New function. (electric-verilog-terminate-line, verilog-in-struct-region-p) (verilog-backward-ws&directives, verilog-forward-ws&directives) (verilog-in-comment-p, verilog-in-star-comment-p) (verilog-in-slash-comment-p, verilog-skip-backward-comments) (verilog-parenthesis-depth, verilog-skip-backward-comment-or-string) (verilog-skip-forward-comment-or-string, verilog-in-paren) (verilog-skip-forward-comment-p): Use it,
author Stefan Monnier <monnier@iro.umontreal.ca>
date Wed, 19 Mar 2008 14:29:35 +0000
parents d999f30304bc
children c67e7bd43423
comparison
equal deleted inserted replaced
93065:2de7dc87905b 93066:f35f15ba549f
271 (defun verilog-booleanp (value) 271 (defun verilog-booleanp (value)
272 "Return t if VALUE is boolean. 272 "Return t if VALUE is boolean.
273 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs. 273 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
274 This function may be removed when Emacs 21 is no longer supported." 274 This function may be removed when Emacs 21 is no longer supported."
275 (or (equal value t) (equal value nil))) 275 (or (equal value t) (equal value nil)))
276
277 (defalias 'verilog-syntax-ppss
278 (if (fboundp 'syntax-ppss) 'syntax-ppss
279 (lambda (&optional pos) (parse-partial-sexp (point-min) (or pos (point))))))
276 280
277 (defgroup verilog-mode nil 281 (defgroup verilog-mode nil
278 "Facilitates easy editing of Verilog source text." 282 "Facilitates easy editing of Verilog source text."
279 :version "22.2" 283 :version "22.2"
280 :group 'languages) 284 :group 'languages)
1740 (progn 1744 (progn
1741 ;; XEmacs (formerly Lucid) has the best implementation 1745 ;; XEmacs (formerly Lucid) has the best implementation
1742 (modify-syntax-entry ?/ ". 1456" table) 1746 (modify-syntax-entry ?/ ". 1456" table)
1743 (modify-syntax-entry ?* ". 23" table) 1747 (modify-syntax-entry ?* ". 23" table)
1744 (modify-syntax-entry ?\n "> b" table)) 1748 (modify-syntax-entry ?\n "> b" table))
1745 ;; Emacs 19 does things differently, but we can work with it 1749 ;; Emacs does things differently, but we can work with it
1746 (modify-syntax-entry ?/ ". 124b" table) 1750 (modify-syntax-entry ?/ ". 124b" table)
1747 (modify-syntax-entry ?* ". 23" table) 1751 (modify-syntax-entry ?* ". 23" table)
1748 (modify-syntax-entry ?\n "> b" table)) 1752 (modify-syntax-entry ?\n "> b" table))
1749 table) 1753 table)
1750 "Syntax table used in Verilog mode buffers.") 1754 "Syntax table used in Verilog mode buffers.")
2379 (when (featurep 'xemacs) 2383 (when (featurep 'xemacs)
2380 (easy-menu-add verilog-stmt-menu) 2384 (easy-menu-add verilog-stmt-menu)
2381 (easy-menu-add verilog-menu) 2385 (easy-menu-add verilog-menu)
2382 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu))) 2386 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
2383 2387
2384 ;; Stuff for GNU emacs 2388 ;; Stuff for GNU Emacs
2385 (set (make-local-variable 'font-lock-defaults) 2389 (set (make-local-variable 'font-lock-defaults)
2386 '((verilog-font-lock-keywords verilog-font-lock-keywords-1 2390 '((verilog-font-lock-keywords verilog-font-lock-keywords-1
2387 verilog-font-lock-keywords-2 2391 verilog-font-lock-keywords-2
2388 verilog-font-lock-keywords-3) 2392 verilog-font-lock-keywords-3)
2389 nil nil nil verilog-beg-of-defun)) 2393 nil nil nil verilog-beg-of-defun))
2390 ;;------------------------------------------------------------ 2394 ;;------------------------------------------------------------
2391 ;; now hook in 'verilog-colorize-include-files (eldo-mode.el&spice-mode.el) 2395 ;; now hook in 'verilog-colorize-include-files (eldo-mode.el&spice-mode.el)
2392 ;; all buffer local: 2396 ;; all buffer local:
2393 (when (featurep 'xemacs) 2397 (when (featurep 'xemacs)
2394 (make-local-hook 'font-lock-mode-hook) 2398 (make-local-hook 'font-lock-mode-hook)
2395 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in emacs 20 2399 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
2396 (make-local-hook 'after-change-functions)) 2400 (make-local-hook 'after-change-functions))
2397 (add-hook 'font-lock-mode-hook 'verilog-colorize-include-files-buffer t t) 2401 (add-hook 'font-lock-mode-hook 'verilog-colorize-include-files-buffer t t)
2398 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-colorize-include-files-buffer t t) ; not in emacs 20 2402 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-colorize-include-files-buffer t t) ; not in Emacs
2399 (add-hook 'after-change-functions 'verilog-colorize-include-files t t) 2403 (add-hook 'after-change-functions 'verilog-colorize-include-files t t)
2400 2404
2401 ;; Tell imenu how to handle Verilog. 2405 ;; Tell imenu how to handle Verilog.
2402 (make-local-variable 'imenu-generic-expression) 2406 (make-local-variable 'imenu-generic-expression)
2403 (setq imenu-generic-expression verilog-imenu-generic-expression) 2407 (setq imenu-generic-expression verilog-imenu-generic-expression)
2421 (defun electric-verilog-terminate-line (&optional arg) 2425 (defun electric-verilog-terminate-line (&optional arg)
2422 "Terminate line and indent next line. 2426 "Terminate line and indent next line.
2423 With optional ARG, remove existing end of line comments." 2427 With optional ARG, remove existing end of line comments."
2424 (interactive) 2428 (interactive)
2425 ;; before that see if we are in a comment 2429 ;; before that see if we are in a comment
2426 (let ((state 2430 (let ((state (save-excursion (verilog-syntax-ppss))))
2427 (save-excursion
2428 (parse-partial-sexp (point-min) (point)))))
2429 (cond 2431 (cond
2430 ((nth 7 state) ; Inside // comment 2432 ((nth 7 state) ; Inside // comment
2431 (if (eolp) 2433 (if (eolp)
2432 (progn 2434 (progn
2433 (delete-horizontal-space) 2435 (delete-horizontal-space)
2934 (defun verilog-in-struct-region-p () 2936 (defun verilog-in-struct-region-p ()
2935 "Return true if in a struct region. 2937 "Return true if in a struct region.
2936 More specifically, in a list after a struct|union keyword." 2938 More specifically, in a list after a struct|union keyword."
2937 (interactive) 2939 (interactive)
2938 (save-excursion 2940 (save-excursion
2939 (let* ((state (parse-partial-sexp (point-min) (point))) 2941 (let* ((state (verilog-syntax-ppss))
2940 (depth (nth 0 state))) 2942 (depth (nth 0 state)))
2941 (if depth 2943 (if depth
2942 (progn (backward-up-list depth) 2944 (progn (backward-up-list depth)
2943 (verilog-beg-of-statement) 2945 (verilog-beg-of-statement)
2944 (looking-at "\\<typedef\\>?\\s-*\\<struct\\|union\\>")))))) 2946 (looking-at "\\<typedef\\>?\\s-*\\<struct\\|union\\>"))))))
3690 This lets programs calling batch mode to easily extract error messages." 3692 This lets programs calling batch mode to easily extract error messages."
3691 `(condition-case err 3693 `(condition-case err
3692 (progn ,@body) 3694 (progn ,@body)
3693 (error 3695 (error
3694 (error "%%Error: %s%s" (error-message-string err) 3696 (error "%%Error: %s%s" (error-message-string err)
3695 (if (featurep 'xemacs) "\n" ""))))) ;; xemacs forgets to add a newline 3697 (if (featurep 'xemacs) "\n" ""))))) ;; XEmacs forgets to add a newline
3696 3698
3697 (defun verilog-batch-execute-func (funref) 3699 (defun verilog-batch-execute-func (funref)
3698 "Internal processing of a batch command, running FUNREF on all command arguments." 3700 "Internal processing of a batch command, running FUNREF on all command arguments."
3699 (verilog-batch-error-wrapper 3701 (verilog-batch-error-wrapper
3700 ;; General globals needed 3702 ;; General globals needed
4271 (let* ((bound (or bound (point-min))) 4273 (let* ((bound (or bound (point-min)))
4272 (here bound) 4274 (here bound)
4273 (p nil) ) 4275 (p nil) )
4274 (if (< bound (point)) 4276 (if (< bound (point))
4275 (progn 4277 (progn
4276 (let ((state 4278 (let ((state (save-excursion (verilog-syntax-ppss))))
4277 (save-excursion
4278 (parse-partial-sexp (point-min) (point)))))
4279 (cond 4279 (cond
4280 ((nth 7 state) ;; in // comment 4280 ((nth 7 state) ;; in // comment
4281 (verilog-re-search-backward "//" nil 'move) 4281 (verilog-re-search-backward "//" nil 'move)
4282 (skip-chars-backward "/")) 4282 (skip-chars-backward "/"))
4283 ((nth 4 state) ;; in /* */ comment 4283 ((nth 4 state) ;; in /* */ comment
4305 (let* ((bound (or bound (point-max))) 4305 (let* ((bound (or bound (point-max)))
4306 (here bound) 4306 (here bound)
4307 jump) 4307 jump)
4308 (if (> bound (point)) 4308 (if (> bound (point))
4309 (progn 4309 (progn
4310 (let ((state 4310 (let ((state (save-excursion (verilog-syntax-ppss))))
4311 (save-excursion
4312 (parse-partial-sexp (point-min) (point)))))
4313 (cond 4311 (cond
4314 ((nth 7 state) ;; in // comment 4312 ((nth 7 state) ;; in // comment
4315 (verilog-re-search-forward "//" nil 'move)) 4313 (verilog-re-search-forward "//" nil 'move))
4316 ((nth 4 state) ;; in /* */ comment 4314 ((nth 4 state) ;; in /* */ comment
4317 (verilog-re-search-forward "/\*" nil 'move)))) 4315 (verilog-re-search-forward "/\*" nil 'move))))
4327 (if jump 4325 (if jump
4328 (beginning-of-line 2)))))))) 4326 (beginning-of-line 2))))))))
4329 4327
4330 (defun verilog-in-comment-p () 4328 (defun verilog-in-comment-p ()
4331 "Return true if in a star or // comment." 4329 "Return true if in a star or // comment."
4332 (let ((state 4330 (let ((state (save-excursion (verilog-syntax-ppss))))
4333 (save-excursion
4334 (parse-partial-sexp (point-min) (point)))))
4335 (or (nth 4 state) (nth 7 state)))) 4331 (or (nth 4 state) (nth 7 state))))
4336 4332
4337 (defun verilog-in-star-comment-p () 4333 (defun verilog-in-star-comment-p ()
4338 "Return true if in a star comment." 4334 "Return true if in a star comment."
4339 (let ((state 4335 (let ((state (save-excursion (verilog-syntax-ppss))))
4340 (save-excursion
4341 (parse-partial-sexp (point-min) (point)))))
4342 (and 4336 (and
4343 (nth 4 state) ; t if in a comment of style a // or b /**/ 4337 (nth 4 state) ; t if in a comment of style a // or b /**/
4344 (not 4338 (not
4345 (nth 7 state) ; t if in a comment of style b /**/ 4339 (nth 7 state) ; t if in a comment of style b /**/
4346 )))) 4340 ))))
4347 4341
4348 (defun verilog-in-slash-comment-p () 4342 (defun verilog-in-slash-comment-p ()
4349 "Return true if in a slash comment." 4343 "Return true if in a slash comment."
4350 (let ((state 4344 (let ((state (save-excursion (verilog-syntax-ppss))))
4351 (save-excursion
4352 (parse-partial-sexp (point-min) (point)))))
4353 (nth 7 state))) 4345 (nth 7 state)))
4354 4346
4355 (defun verilog-in-comment-or-string-p () 4347 (defun verilog-in-comment-or-string-p ()
4356 "Return true if in a string or comment." 4348 "Return true if in a string or comment."
4357 (let ((state 4349 (let ((state (save-excursion (verilog-syntax-ppss))))
4358 (save-excursion
4359 (parse-partial-sexp (point-min) (point)))))
4360 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment) 4350 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
4361 4351
4362 (defun verilog-in-escaped-name-p () 4352 (defun verilog-in-escaped-name-p ()
4363 "Return true if in an escaped name." 4353 "Return true if in an escaped name."
4364 (save-excursion 4354 (save-excursion
4368 t 4358 t
4369 nil))) 4359 nil)))
4370 4360
4371 (defun verilog-in-paren () 4361 (defun verilog-in-paren ()
4372 "Return true if in a parenthetical expression." 4362 "Return true if in a parenthetical expression."
4373 (let ((state 4363 (let ((state (save-excursion (verilog-syntax-ppss))))
4374 (save-excursion
4375 (parse-partial-sexp (point-min) (point)))))
4376 (> (nth 0 state) 0 ))) 4364 (> (nth 0 state) 0 )))
4377 4365
4378 (defun verilog-in-coverage () 4366 (defun verilog-in-coverage ()
4379 "Return true if in a constraint or coverpoint expression." 4367 "Return true if in a constraint or coverpoint expression."
4380 (interactive) 4368 (interactive)
4411 ;; not 4399 ;; not
4412 nil)) 4400 nil))
4413 4401
4414 (defun verilog-parenthesis-depth () 4402 (defun verilog-parenthesis-depth ()
4415 "Return non zero if in parenthetical-expression." 4403 "Return non zero if in parenthetical-expression."
4416 (save-excursion 4404 (save-excursion (nth 1 (verilog-syntax-ppss))))
4417 (nth 1 (parse-partial-sexp (point-min) (point)))))
4418 4405
4419 4406
4420 (defun verilog-skip-forward-comment-or-string () 4407 (defun verilog-skip-forward-comment-or-string ()
4421 "Return true if in a string or comment." 4408 "Return true if in a string or comment."
4422 (let ((state 4409 (let ((state (save-excursion (verilog-syntax-ppss))))
4423 (save-excursion
4424 (parse-partial-sexp (point-min) (point)))))
4425 (cond 4410 (cond
4426 ((nth 3 state) ;Inside string 4411 ((nth 3 state) ;Inside string
4427 (goto-char (nth 3 state)) 4412 (goto-char (nth 3 state))
4428 t) 4413 t)
4429 ((nth 7 state) ;Inside // comment 4414 ((nth 7 state) ;Inside // comment
4434 (t 4419 (t
4435 nil)))) 4420 nil))))
4436 4421
4437 (defun verilog-skip-backward-comment-or-string () 4422 (defun verilog-skip-backward-comment-or-string ()
4438 "Return true if in a string or comment." 4423 "Return true if in a string or comment."
4439 (let ((state 4424 (let ((state (save-excursion (verilog-syntax-ppss))))
4440 (save-excursion
4441 (parse-partial-sexp (point-min) (point)))))
4442 (cond 4425 (cond
4443 ((nth 3 state) ;Inside string 4426 ((nth 3 state) ;Inside string
4444 (search-backward "\"") 4427 (search-backward "\"")
4445 t) 4428 t)
4446 ((nth 7 state) ;Inside // comment 4429 ((nth 7 state) ;Inside // comment
4456 (defun verilog-skip-backward-comments () 4439 (defun verilog-skip-backward-comments ()
4457 "Return true if a comment was skipped." 4440 "Return true if a comment was skipped."
4458 (let ((more t)) 4441 (let ((more t))
4459 (while more 4442 (while more
4460 (setq more 4443 (setq more
4461 (let ((state 4444 (let ((state (save-excursion (verilog-syntax-ppss))))
4462 (save-excursion
4463 (parse-partial-sexp (point-min) (point)))))
4464 (cond 4445 (cond
4465 ((nth 7 state) ;Inside // comment 4446 ((nth 7 state) ;Inside // comment
4466 (search-backward "//") 4447 (search-backward "//")
4467 (skip-chars-backward "/") 4448 (skip-chars-backward "/")
4468 (skip-chars-backward " \t\n\f") 4449 (skip-chars-backward " \t\n\f")
4482 4463
4483 (defun verilog-skip-forward-comment-p () 4464 (defun verilog-skip-forward-comment-p ()
4484 "If in comment, move to end and return true." 4465 "If in comment, move to end and return true."
4485 (let (state) 4466 (let (state)
4486 (progn 4467 (progn
4487 (setq state 4468 (setq state (save-excursion (verilog-syntax-ppss)))
4488 (save-excursion
4489 (parse-partial-sexp (point-min) (point))))
4490 (cond 4469 (cond
4491 ((nth 3 state) 4470 ((nth 3 state)
4492 t) 4471 t)
4493 ((nth 7 state) ;Inside // comment 4472 ((nth 7 state) ;Inside // comment
4494 (end-of-line) 4473 (end-of-line)
5933 (when (looking-at ")") 5912 (when (looking-at ")")
5934 (verilog-backward-open-paren) 5913 (verilog-backward-open-paren)
5935 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil)) 5914 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil))
5936 (skip-chars-backward "a-zA-Z0-9'_$") 5915 (skip-chars-backward "a-zA-Z0-9'_$")
5937 (looking-at "[a-zA-Z0-9`_\$]+") 5916 (looking-at "[a-zA-Z0-9`_\$]+")
5938 ;; Important: don't use match string, this must work with emacs 19 font-lock on 5917 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
5939 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))) 5918 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
5940 5919
5941 (defun verilog-read-inst-name () 5920 (defun verilog-read-inst-name ()
5942 "Return instance_name when point is inside instantiation." 5921 "Return instance_name when point is inside instantiation."
5943 (save-excursion 5922 (save-excursion
5944 (verilog-read-inst-backward-name) 5923 (verilog-read-inst-backward-name)
5945 (looking-at "[a-zA-Z0-9`_\$]+") 5924 (looking-at "[a-zA-Z0-9`_\$]+")
5946 ;; Important: don't use match string, this must work with emacs 19 font-lock on 5925 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
5947 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))) 5926 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
5948 5927
5949 (defun verilog-read-module-name () 5928 (defun verilog-read-module-name ()
5950 "Return module name when after its ( or ;." 5929 "Return module name when after its ( or ;."
5951 (save-excursion 5930 (save-excursion
5952 (re-search-backward "[(;]") 5931 (re-search-backward "[(;]")
5953 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil) 5932 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil)
5954 (skip-chars-backward "a-zA-Z0-9`_$") 5933 (skip-chars-backward "a-zA-Z0-9`_$")
5955 (looking-at "[a-zA-Z0-9`_\$]+") 5934 (looking-at "[a-zA-Z0-9`_\$]+")
5956 ;; Important: don't use match string, this must work with emacs 19 font-lock on 5935 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
5957 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))) 5936 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
5958 5937
5959 (defun verilog-read-auto-params (num-param &optional max-param) 5938 (defun verilog-read-auto-params (num-param &optional max-param)
5960 "Return parameter list inside auto. 5939 "Return parameter list inside auto.
5961 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters." 5940 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
6592 templateno lineno) 6571 templateno lineno)
6593 tpl-sig-list)) 6572 tpl-sig-list))
6594 (goto-char (match-end 0))) 6573 (goto-char (match-end 0)))
6595 ;; Regexp form?? 6574 ;; Regexp form??
6596 ((looking-at 6575 ((looking-at
6597 ;; Regexp bug in xemacs disallows ][ inside [], and wants + last 6576 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
6598 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)") 6577 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
6599 (setq rep (match-string-no-properties 3)) 6578 (setq rep (match-string-no-properties 3))
6600 (goto-char (match-end 0)) 6579 (goto-char (match-end 0))
6601 (setq tpl-wild-list 6580 (setq tpl-wild-list
6602 (cons (list 6581 (cons (list
7188 (cond (fass 7167 (cond (fass
7189 ;; Found 7168 ;; Found
7190 (setq func-returns (nth 3 fass))) 7169 (setq func-returns (nth 3 fass)))
7191 (t 7170 (t
7192 ;; Read from file 7171 ;; Read from file
7193 ;; Clear then restore any hilighting to make emacs19 happy 7172 ;; Clear then restore any hilighting to make Emacs 19 happy
7194 (let ((fontlocked (when (and (boundp 'font-lock-mode) 7173 (let ((fontlocked (when (and (boundp 'font-lock-mode)
7195 font-lock-mode) 7174 font-lock-mode)
7196 (font-lock-mode nil) 7175 (font-lock-mode nil)
7197 t))) 7176 t)))
7198 (setq func-returns (funcall function)) 7177 (setq func-returns (funcall function))