Specify a user-defined translation directive. |
#xtranslate <matchPattern> => <resultPattern> |
Name | Description |
---|---|
<matchPattern> | <matchPattern> is the pattern the input text should match. |
<resultPattern> | <resultPattern> is the text produced if a portion of input text matches the <matchPattern>. |
The => symbol between <matchPattern> and <resultPattern> is a literal part of the syntax that must be specified in a #xcommand directive. The symbol consists of an equal sign followed by a greater than symbol with no intervening spaces. Do not confuse the symbol with the >= or the <= comparison operators.
#xtranslate is a translation directive that define commands and pseudofunctions. Each directive specifies a translation rule. The rule consists of two portions: a match pattern and a result pattern. The match pattern matches a command specified in the program (.prw) file and saves portions of the command text (usually command arguments) for the result pattern to use. The result pattern then defines what will be written to the result text and how it will be written using the saved portions of the matching input text.
#xcommand and #xtranslate are similar, but differ in the circumstance under which their match patterns match input text. A #xcommand directive matches only if the input text is a complete statement, while #xtranslate matches input text that is not a complete statement. #xcommand defines a complete command and #xtranslate defines clauses and pseudofunctions that may not form a complete statement. In general, use #xcommand for most definitions and #xtranslate for special cases.
#xcommand and #xtranslate are similar to but more powerful than the #define directive.
#xcommand and #xtranslate directives have the same scope as the #define directive. The definition is valid only for the current program (.prw) file. If defined elsewhere, the definition is valid from the line where it is specified to the end of the program file.
When defining a command, there are several prerequisites to properly specifying the command definition. Many preprocessor commands require more than one #xcommand directive because mutually exclusive clauses contain a keyword or argument. For example, the @...GET command has mutually exclusive VALID and RANGE clauses and is defined with a different #xcommand rule to implement each clause.
This also occurs when a result pattern contains different expressions, functions, or parameter structures for different clauses specified for the same command (e.g., the @...SAY command). For example, there is a #xcommand rule for @...SAY specified with the PICTURE clause and another for @...SAY specified without the PICTURE clause. Each formulation of the command is translated into a different expression. Because directives are processed in stack order, when defining more than one rule for a command, place the most general case first, followed by the more specific ones. This ensures that the proper rule will match the command specified in the program (.prw) file.
The <matchPattern> portion of a translation directive is the pattern the input text must match. A match pattern is made from one or more of the following components, which the preprocessor tries to match against input text in a specific way:
Match markers are label and optional symbols delimited by angle brackets (<>) that provide a substitute (idMarker) to be used in the <resultPattern> and identify the clause for which it is a substitute. Marker names are identifiers and must, therefore, follow the identifier naming conventions. In short, the name must start with an alphabetic or underscore character, which may be followed by alphanumeric or underscore characters.
This table describes all match marker forms:
Match Marker | Name |
---|---|
<idMarker> | Regular match marker |
<idMarker, ...> | List match marker |
<idMarker:word list> | Restricted match marker |
<*idMarker*> | Wild match marker |
<(idMarker)> | Extended Expression match marker |
The <resultPattern> portion of a translation directive is the text the preprocessor will produce if a piece of input text matches the <matchPattern>. <resultPattern> is made from one or more of the following components:
Result markers: refer directly to a match marker name. Input text matched by the match marker is written to the result text via the result marker.
This table lists the Result marker forms:
Regular result marker | Name |
---|---|
<idMarker> | Regular result marker |
#<idMarker> | Dumb stringify result marker |
<"idMarker"> | Normal stringify result marker |
<(idMarker)> | Smart stringify result marker |
<{idMarker}> | Blockify result marker |
<.idMarker.> | Logify result marker |
Repeating result clauses are portions of the <resultPattern> enclosed by square brackets ("[" and "]"). The text within a repeating clause is written to the result text as many times as it has input text for any or all result markers within the clause. If there is no matching input text, the repeating clause is not written to the result text. Repeating clauses, however, cannot be nested. If you need to nest repeating clauses, you probably need an additional #xcommand rule for the current command.
Repeating clauses are the result pattern part of the #xcommand facility that create optional clauses which have arguments. You can match input text with any match marker other than the restricted match marker and write to the result text with any of the corresponding result markers.