Parse 

Parse


Literal

The input elements are processed as-is, without evaluation. A data type is assigned to each element according to the following rules:

In most cases, this is what you would intuitively expect to happen.

If you use the Literal or Evaluate option, then the option must be placed immediately after the Parse command. Literal and Evaluate are mutually exclusive options.

Example:

Global hello `Hello Peter !`
Parse Literal Value hello . name .
Say "Person greeted: {name}"

Here, we have to use Literal since the string can not be evaluated: for that, "Hello" would have to be a function, "Peter" an argument hence a Global variable, and the exclamation mark would have to have some algebraic meaning, which it doesn't.

[This is default.]


Evaluate

Requests evaluation of the input data as expressions in terms of Global variables. An error results if the syntax is not correct. This could for instance happen if you try to enter character input without quotes: an attempt would be made to evaluate the string as an expression in terms of the global variables.

If you use the Literal or Evaluate option, then the option must be placed immediately after the Parse command. Literal and Evaluate are mutually exclusive options.

Example:

Say "Open a metafile (yes/no) ?"
Global yes True
Global no False
Global y True
Global n False
Parse Evaluate Terminal metafile
If metafile Then
   Say "Opening metafile {input}.ps ..."
   !add meta type PostScript file-name "{input}.ps"
   !open meta
   !act meta
Else
   Say "No metafile opened."
Endif

The If statement expects a Logical as condition. We would however like to offer the user the possibility to reply with "yes" or "no". Therefore, we define Global variables by those names which have the values True and False. With the Evaluate option, a reply of "yes" therefore results in the assignment of Logical value True to the global variable METAFILE. With the Literal option, METAFILE would become a String with value `YES`.

[This is not default.]


Argument

Obtains the command line arguments while reading from the main input stream, or the arguments given after the file name when an external file is input, formats these as a string and uses this string as input for assigning values to the Global variables of the template.

Examples:

Garfield is started by issuing a command like:

garfield -arg "Hello !"
The double quotes around the argument string are needed only in case of special characters, such as the exclamation mark. Once the program is running, you can access the string "Hello !" with the Parse command:
Parse Arg greeting
Say {greeting}

Imagine you've an input file called factorial which looks like this:

Parse Arg n
If type(n)#`Number` Then
   Say "Please supply a numeric argument."
Elseif n<1 | abs(entier(n+0.5)-n)>1e-4 Then
   Say "Please supply a strictly positive, integer argument."
Else
   Global fac=1
   For i From 2 To n Do
      Global fac=fac*i
   Enddo
   Say "Factorial of {n} is {fac}."
Endif
One would run this file as follows:
<factorial 5

Global

The value of the Global variable named after Global is retrieved, formatted as a string and then used as input.

Parsing should not give surprising results with Globals that are of type Number or String. Globals of type Logical will result in the string `True` or `False` while variables of type Undefined yield the string `Nill`.

If the Global variable is an Histogram, then the resulting string will be `Histogram`, which is not useful.

Similarly, a Matrix with more than 1 dimension gives the string `n-Matrix` where n is the number of dimensions. Only for 1-dimensional matrices will the string contain a formatted portion of the matrix. This can be parsed, although not trivially:

Call book_matrix(a,4)
Parse Global a `(` a `,` b `,` c `,` d `)`

which is equivalent to:

Parse Value row(4) `(` a `,` b `,` c `,` d `)`

Input

A line of input is requested from the regular input stream and this line is compared with the template.

This format is only offered for completeness - the Terminal input format is probably more useful. See there for further details.


Terminal

A line of input is requested from the terminal, or the batch input file, as appropriate. This line is compared with the template.

The input line is treated like all other input, i.e. text outside single and reverse quotes is converted to upper case.

This is a frequently used format since it permits obtaining user input while a file is being input.

This format can also be used to pause execution of an input file, while waiting for the user to hit the return key:

&FIELD
grid 5
print ex,ey
Parse Terminal dummy
print v

The first table will be printed, then the program waits for a response from the user and the 2nd table is printed only after the return key has been pressed.


Value

The expression is evaluated in terms of Global variables, the result is formatted as a string and the result is compared with the template.

This format is similar to Parse Global, see there for examples and further comments.


template

The template is to be seen as a format for assigning bits and pieces of the input to a set of global variables.

A template can contain the following elements:

Element Purpose
`string` Locate string in the input, resume from there
var Global variable to which the field is assigned
. Ignore the corresponding input field.

All elements can be repeated many times.

Instead of reverse quotes (`), one can also use single and double quotes to delimit search strings. Keep in mind however that the quote is a syntax element - the quote must therefore still be visible after the Parse statement has been processed by the usual input routines.

If there are more variables in the template than elements in the input, then the variables to which no value can be assigned will be set to have type Undefined. Conversely, if there are more elements than variables, then all variables will together, with their separators, be assigned to the last variable. This operation is likely to fail in the Evaluate mode.

Examples with option Literal:

Input Template Result
1 2 3 x y z x=1, y=2, z=3
1 2 x+1 x y z x=1, y=2, z=`x+1`
1 2 3 4 x y . x=1, y=2
1 2 x y z x=1, y=2, z=Nill
1 2 3 x y x=1, y=`2 3`
1 2 A 3 x . `A` y x=1, y=3
1 2 A 3 x `A` y x=`1 2`, y=3
1 `abc` 2 x y . x=1, y=`abc`
1>2 z . z=`1>2`

Examples with option Evaluate:

Input Template Result
1 2 3 x y z x=1, y=2, z=3
1 2 x+1 x y z x=1, y=2, z=2
1 2 3 4 x y . x=1, y=2
1 2 x y z x=1, y=2, z=Nill
1 2 3 x y Error: y := 2 3 is incorrect syntax
1 2 A 3 x . `A` y x=1, y=3
1 2 A 3 x `A` y Error: x := 1 2 is incorrect syntax
1 `abc` 2 x y . x=1, y=`abc`
1>2 z . z=False

Go to the top level, to Parse, to the topic index, to the table of contents, or to the full text.

Formatted on 21/01/18 at 16:55.