View Single Post
Old 11 July 2015, 14:48   #45
BigFan
Registered User
 
BigFan's Avatar
 
Join Date: Feb 2014
Location: Germany
Posts: 261
Parsing arguments

Parsing

One of the best features of ARexx is its parser. It is not restricted to command
line or function arguments. In fact you may parse anything from one of its input
channels. The entire argument is treated as being one long string. If the template
has more than one target, the parser looks for substrings seperated by white space.
If templatecontains strings, they get temporarily deleted from argument string.
The parser then continues with the next substring.
Values in [] are optional.

Syntax
Parse [upper] source template [,template] [,more templates] [.]

upper - is optional and translates the input to upper case

source - is one of
  • ARG - read command or function arguments
  • VAR name - use variable "name" as input (must be initialized before)
  • Pull - use 'STDIN' as input (console window mostly)

template - is a list of symbols (uninitialized variables) to assign values to.
A template consists of markers (a position) and targets (the variable to extract).
',' - seperates multiple templates when pulling (use without quotes)
'.' - is a signal dot that marks the end of line to stop parsing and tokenization.
The last value will be taken from input but assigned nowhere (use without quotes)

Examples:

Argument was "file"
Arg type
=>
type = FILE
Arg is similar to Parse Upper Arg

Argument was "5 6"
Arg i
=>
i="5 6"
i is a string now, can't be used for math

Arg i j
=>
i="5"
j=" 6"
i and j are strings of numeric type, useable in arithmetic operations now
Arg i j k
=>
i="5"
j="6"
k=""
(k is now an existing but uninitialized symbol, a LITERAL, use Symbol() to check)

Argument was "drinks food each $1"
Arg d f 'each' '$'p
=>
d="DRINKS"
f="EACH $1"
p=
Arg is shorthand Parse UPPER Arg. Either use Parse Arg or upper case in template

Argument was "Soda £1.50 Coffee £1.50 all prizes incl vat"
Parse Arg i j k l
=>
i=Soda
j=£1.5
k=Coffee
l=£1.5 all prizes incl vat
Parse Arg i j k l .
=>
i=Soda
j=£1.5
k=Coffee
l=£1.5
the remaining substring is not assigned to anything
Parse Arg i "£"j k "£"l .
=>
i=Soda
j=1.5
k=Coffee
l=1.5
currency symbol has been taken from argument string while parsing

Argument was "Soda £1.50 Coffee £1.50"

Parse Arg i "£"j k "£"l
=>
i=Soda
j=1.5
k=Coffee
l=" 1.5"
<== pay attention, the last gets it all including the white space

TIPP: Whenever possible, close the template with a dot to avoid unsolisticated
strings or white spaces.



Using markers

Markers are evaluated left to right in ascending order. If the next marker has
lower value than the previous, the parser rewinds to that position.
An operator like + or - steps from current position in right(+) or left()- direction.

Argument was "Me and Susan had a bad day!"

Parse Arg 1 me 8 su 1 we +17 "bad " -2 day
=>
me="Me and
su=Susan had a bad day!"
we="Me and Susan had a"
day="a day!"
The substring "bad " is hidden from input so -2 jumps to the position of "a" before
"bad" not "d" from "bad".

Please notice, the input string is not altered in any way. It is still the same.
The parser is doing modifications temporarily. You may parse it again with different
markers and targets.

Multiple templates
Multiple templates can be used when pulling from a console. Each template requires
a new input. Use comma to seperate templates.

Parse Pull last first, street, town
This requires the user to send 3 times his input using return key.
Assume input was
"Clause Santa"
"there is no street or road"
"why the heck should i tell you"
=>
first="Santa"
last="Clause"
street="there is no street or road"
town="why the heck should i tell you"

If we'd put commas in quotes, the parser would have tried to read all in once,
leaving street and town uninitialized.

Arguments can be parsed in functions by the user manually.
To do it yourself see following example

Code:
/* Summarize with variable argument length */
Say Sum( 5, 4, 3, 2)
Exit

Sum:procedure
sum=0
Do i=1 to Arg()
  sum = sum + Arg(i)
End
Return sum
This does not work with arguments passed to a program from outside (cli). A program
is considered being a command, not a function. Arguments are passed to commands
as one string. In our small example, Arg() will report 1 and Arg(1) is "5, 4, 3, 2"
if this is fetched from command line.
Invoking with :> rx sum 5 4 3 2

Example of erroneous code
Code:
/* Summarize with variable argument length */
sum=0
Do i=1 to Arg()           /* Arg() is 1*/
  sum = sum + Arg(i)      /* arithmetic error, sum is a number, 
                             Arg(1) is a string */
End
Return sum

Last edited by BigFan; 12 July 2015 at 10:15.
BigFan is offline  
 
Page generated in 0.04561 seconds with 11 queries