🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.bug
Article #87975

hideif.el doesn't handle many types of constant expr in #if exprs

#87975
From: garyo
Date: Thu, 18 Aug 1994 22:39
183 lines
6456 bytes
In GNU Emacs 19.25.94.2 (sparc-sun-solaris2.3, X toolkit)
of Thu Aug 18 1994 on prometheus:

Here are some diffs to hideif.el (the latest version, released with
the 19.25.94 pretest), to make it parse #if a lot better.
A frequent Sunism, for instance, is:
  #if __STDC__ - 0 == 0
which gave a syntax error from hide-ifdefs before.

Basically I added +, -, == and != to the operators it already handled.
If there's a need, other operators could be handled similarly.

-- Gary Oberbrunner (garyo@avs.com)

*** /usr/local/lib/emacs/19.25.94/lisp/hideif.el	Fri Jul 29 15:59:06 1994
--- hideif.el	Thu Aug 18 17:56:43 1994
***************
*** 109,114 ****
--- 109,117 ----
  
  ;;; Change Log:
  ;;;
+ ;;; 94/08/09 garyo@avs.com
+ ;;; Added +, -, == and !=
+ ;;;
  ;;; Revision 1.7  88/02/16  03:12:58  liberte
  ;;; Fixed comments and doc strings.
  ;;; Added optional prefix arg for ifdef motion commands.
***************
*** 372,378 ****
    )
  
  ; pattern to match initial identifier, !, &&, ||, (, or ).
! (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)")
  (defconst hif-end-of-comment "\\*/")
  
  
--- 375,382 ----
    )
  
  ; pattern to match initial identifier, !, &&, ||, (, or ).
! ; Added ==, + and -: garyo@avs.com 8/9/94
! (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[!=]=\\|[()+-]\\|\\w+\\)")
  (defconst hif-end-of-comment "\\*/")
  
  
***************
*** 418,430 ****
  		       (cond
  			((string-equal token "||") 'or)
  			((string-equal token "&&") 'and)
  			((string-equal token "!")  'not)
  			((string-equal token "defined") 'hif-defined)
  			((string-equal token "(") 'lparen)
  			((string-equal token ")") 'rparen)
  			(t (intern token)))
  		       token-list))))
- 
  	     (t (error "Bad #if expression: %s" expr-string)))))
        (set-syntax-table current-syntax-table))
      (nreverse token-list)))
--- 422,437 ----
  		       (cond
  			((string-equal token "||") 'or)
  			((string-equal token "&&") 'and)
+ 			((string-equal token "==") 'equal)
+ 			((string-equal token "!=") 'hif-notequal)
  			((string-equal token "!")  'not)
  			((string-equal token "defined") 'hif-defined)
  			((string-equal token "(") 'lparen)
  			((string-equal token ")") 'rparen)
+ 			((string-equal token "+") 'hif-plus)
+ 			((string-equal token "-") 'hif-minus)
  			(t (intern token)))
  		       token-list))))
  	     (t (error "Bad #if expression: %s" expr-string)))))
        (set-syntax-table current-syntax-table))
      (nreverse token-list)))
***************
*** 432,437 ****
--- 439,445 ----
  ;;;-----------------------------------------------------------------
  ;;; Translate C preprocessor #if expressions using recursive descent.
  ;;; This parser is limited to the operators &&, ||, !, and "defined".
+ ;;; Added ==, !=, +, and -.  Gary Oberbrunner, garyo@avs.com, 8/9/94
  
  (defun hif-parse-if-exp (token-list)
    "Parse the TOKEN-LIST.  Return translated list in prefix form."
***************
*** 458,470 ****
  
  (defun hif-term ()
    "Parse a term of the form
!        term : factor | term '&&' factor."
!   (let ((result (hif-factor)))
      (while (eq token 'and)
        (hif-nexttoken)
!       (setq result (list 'and result (hif-factor))))
      result))
  
  (defun hif-factor ()
    "Parse a factor of the form
         factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
--- 466,500 ----
  
  (defun hif-term ()
    "Parse a term of the form
!        term : eq-expr | term '&&' eq-expr."
!   (let ((result (hif-eq-expr)))
      (while (eq token 'and)
        (hif-nexttoken)
!       (setq result (list 'and result (hif-eq-expr))))
      result))
  
+ (defun hif-eq-expr ()
+   "Parse a term of the form
+        eq-expr : math | eq-expr '=='|'!=' math."
+   (let ((result (hif-math))
+ 	(eq-token nil))
+     (while (or (eq token 'equal) (eq token 'hif-notequal))
+       (setq eq-token token)
+       (hif-nexttoken)
+       (setq result (list eq-token result (hif-math))))
+     result))
+ 
+ (defun hif-math ()
+   "Parse an expression of the form
+        math : factor | math '+|-' factor."
+   (let ((result (hif-factor))
+ 	(math-op nil))
+     (while (or (eq  token 'hif-plus) (eq token 'hif-minus))
+       (setq math-op token)
+       (hif-nexttoken)
+       (setq result (list math-op result (hif-factor))))
+   result))
+   
  (defun hif-factor ()
    "Parse a factor of the form
         factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
***************
*** 505,510 ****
--- 535,558 ----
  	))
      ))
  
+ (defun hif-mathify (val)
+   "Treat VAL as a number: if it's t or nil, use 1 or 0."
+   (cond ((eq val t)
+ 	 1)
+ 	((null val)
+ 	 0)
+ 	(t val)))
+ 
+ (defun hif-plus (a b)
+   "Like ordinary plus but treat t and nil as 1 and 0."
+   (+ (hif-mathify a) (hif-mathify b)))
+ (defun hif-minus (a b)
+   "Like ordinary minus but treat t and nil as 1 and 0."
+   (- (hif-mathify a) (hif-mathify b)))
+ (defun hif-notequal (a b)
+   "Like (not (equal A B)) but as one symbol."
+   (not (equal a b)))
+ 
  ;;;----------- end of parser -----------------------
  
  
***************
*** 898,904 ****
  (defun hide-ifdef-define (var)
    "Define a VAR so that #ifdef VAR would be included."
    (interactive "SDefine what? ")
!   (hif-set-var var t)
    (if hide-ifdef-hiding (hide-ifdefs)))
  
  (defun hide-ifdef-undef (var)
--- 946,952 ----
  (defun hide-ifdef-define (var)
    "Define a VAR so that #ifdef VAR would be included."
    (interactive "SDefine what? ")
!   (hif-set-var var 1)
    (if hide-ifdef-hiding (hide-ifdefs)))
  
  (defun hide-ifdef-undef (var)
Technical Details
Message-ID: <9408182209.AA13068@prometheus>
Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!uunet!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!math.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!avs.com!garyo

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fb2f1,d4b2e6b9953c85a5,start
X-Google-Attributes: gidfb2f1,public
X-Google-ArrivalTime: 1994-08-18 18:33:40 PST
Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!uunet!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!math.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!avs.com!garyo
From: garyo@avs.com
Newsgroups: gnu.emacs.bug
Subject: hideif.el doesn't handle many types of constant expr in #if exprs
Date: 18 Aug 1994 18:39:22 -0400
Organization: GNUs Not Usenet
Lines: 183
Sender: daemon@cis.ohio-state.edu
Approved: bug-gnu-emacs@prep.ai.mit.edu
Distribution: gnu
Message-ID: <9408182209.AA13068@prometheus>