Macro expansion / Ultimate string parsing problem

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Macro expansion / Ultimate string parsing problem

Alex Rasker
Dear list and readers,

Macro expansion has been discussed in many ways before.
I have read a lot of the posts but am still at a loss for
the ultimate string parsing problem:

How does one parse a macro call (with arguments) within a macro?

Description of the problem:
/*
I have a macro workbook where each macro has a help text.
The help text can be invoked by calling the macro with
an optional argument 'help=Y'.
This works fine but is not very pleasing, esthetically.
Of course, there are many non-esthetical reasons for why one would
want the following feature to work.
In this example the idea is to create a macro (or in this case just a 'wrapper')
named '!helpme' that can be invoked like:

!helpme !macro1.

which in turn would call '!macro1' with a 'help=Y' argument.
*/.

The main part of the help macro should look something like:

DEFINE helpme (arg=!Tokens(1) )
!arg help=Y.
!ENDDEFINE.

unfortunately, the expansion properties of macros mess things up here.
I have tried to resolve this by using parenthesis
along with !UNQUOTE calls etc. but this has not had the
desired effect.

I hope somebody has an idea as to whether '!EVAL' or some form
of EXPAND=OFF/ON or something completely different
could possibly solve this by syntax-only methods.

Thanks, Alex
Reply | Threaded
Open this post in threaded view
|

Re: Macro expansion / Ultimate string parsing problem

Richard Ristow
At 06:56 AM 6/25/2006, Alex Rasker wrote:

>How does one parse a macro call (with arguments) within a macro?
>
>Description of the problem:
>*
>I have a macro workbook where each macro has a help text. The help
>text can be invoked by calling the macro with an optional argument
>'help=Y'.
>This works fine but is not very pleasing, esthetically.
>
>[I want] to create a macro (or in this case just a 'wrapper') named
>'!helpme' that can be invoked like:
>
>!helpme !macro1.
>
>which in turn would call '!macro1' with a 'help=Y' argument.

Well, I have no great confidence in knowing what macros will or won't
do, but the following is some test code, ending in the definition of
macro !HelpMe that is designed to do what you want, and appears to.
This is SPSS draft output:

*  This is the test macro .......................................... .

DEFINE !Mystery(Help=!DEFAULT(No) !TOKENS(1))

   !IF (!UPCASE(!HELP) !NE Y) !THEN
      ECHO 'Sorry, it is a mystery. There is no help for you'.
   !IFEND

   !IF (!UPCASE(!HELP) !EQ Y) !THEN
      ECHO 'Yes! You can be helped! Mystery is solved!'.
   !IFEND
*.
!ENDDEFINE.

*  Macro /* !MacEcho */ is used to display the macro expansion   .
DEFINE !MacEcho(!POS !NOEXPAND !CMDEND)
    ECHO  !QUOTE(!CONCAT('     Call  : ',!1)).
    ECHO  !QUOTE(!CONCAT('     Result: ',!EVAL(!1))).
!ENDDEFINE.

!MYSTERY .
Sorry, it is a mystery. There is no help for you

!MYSTERY Help=Y

Yes! You can be helped! Mystery is solved!
!MacEcho !Mystery  .
      Call  : !Mystery
      Result:  ECHO 'Sorry, it is a mystery. There is no help for you'
*

!MacEcho !MYSTERY Help=Y .
      Call  : !MYSTERY Help=Y
      Result:   ECHO 'Yes! You can be helped! Mystery is solved!'*



*  And this is /* !HelpMe */ that you wanted                     .
DEFINE !HelpMe (!POS !NOEXPAND !TOKENS(1))
. /*--  ECHO  !QUOTE(!CONCAT('Argument---: ',!1)).
        !LET !INVOKE = !CONCAT(!1,' Help=Y')
. /*--  ECHO  !QUOTE(!CONCAT('Will invoke: ',!INVOKE)).
        !EVAL(!INVOKE)
!ENDDEFINE.

. /*-- SET MPRINT ON.
!HelpMe !Mystery
Yes! You can be helped! Mystery is solved!
. /*-- SET MPRINT OFF.

* ....  End of syntax  .... .
Reply | Threaded
Open this post in threaded view
|

Re: Macro expansion / Ultimate string parsing problem

Raynald Levesque
In reply to this post by Alex Rasker
Hi Alex,

The following works:

DEFINE helpme (arg=!NOEXPAND !TOKENS(1) )
!arg help=YES .
!ENDDEFINE.

DEFINE !macro1 (help=!DEFAULT("NO") !TOKENS(1))
!IF (!UPCASE(!help)="YES") !THEN
ECHO "this is HELP for !macro1!"
!IFEND
!ENDDEFINE.

DEFINE !macro2 (help=!DEFAULT("NO") !TOKENS(1))
!IF (!UPCASE(!help)="YES") !THEN
ECHO "this is HELP for !macro2!"
!IFEND
!ENDDEFINE.


SET MPRINT=NO.
TITLE Get help for macro1.
helpme arg=!macro1.
TITLE now get help for macro2.
helpme arg=!macro2.

Regards

Raynald Levesque [hidden email]
Visit my SPSS site: http://www.spsstools.net


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Alex Rasker
Sent: June 25, 2006 6:56 AM
To: [hidden email]
Subject: Macro expansion / Ultimate string parsing problem

Dear list and readers,

Macro expansion has been discussed in many ways before.
I have read a lot of the posts but am still at a loss for the ultimate
string parsing problem:

How does one parse a macro call (with arguments) within a macro?

Description of the problem:
/*
I have a macro workbook where each macro has a help text.
The help text can be invoked by calling the macro with an optional argument
'help=Y'.
This works fine but is not very pleasing, esthetically.
Of course, there are many non-esthetical reasons for why one would want the
following feature to work.
In this example the idea is to create a macro (or in this case just a
'wrapper') named '!helpme' that can be invoked like:

!helpme !macro1.

which in turn would call '!macro1' with a 'help=Y' argument.
*/.

The main part of the help macro should look something like:

DEFINE helpme (arg=!Tokens(1) )
!arg help=Y.
!ENDDEFINE.

unfortunately, the expansion properties of macros mess things up here.
I have tried to resolve this by using parenthesis along with !UNQUOTE calls
etc. but this has not had the desired effect.

I hope somebody has an idea as to whether '!EVAL' or some form of
EXPAND=OFF/ON or something completely different could possibly solve this by
syntax-only methods.

Thanks, Alex