Next: , Previous: , Up: Style Sheets Semantics   [Contents][Index]


7.5.5 P-Rules

A P-rule (Pretty printing rule), or rule for short, is a structure which consists of two items:

lhs
left-hand side

its source string, with which the source file is compared;

rhs
right hand side

a list of faced strings which will replace the text matched in the pretty-printed output. A faced string is composed of

  • - a string, or a reference to a part of the source string (see Back-reference Operator in Regex manual)
  • - the face to use to print it

Just a short example: ‘(foo, bar, Keyword_strong)’ as a rule means that every input occurrence of ‘foo’ will be replaced by ‘bar’, written with the Keyword_strong face.

If the destination string is empty, then a2ps will use the source string. This is different from giving the source string as a destination string if the case is different. An example will make it fairly clear.

Let foobar be a case insensitive style sheet including the rules ‘(foo, "", Keyword)’ and ‘(bar, bar, Keyword)’. Then, on the input ‘FOO BAR’, a2ps will produce ‘FOO bar’ in Keyword.

a2ps implements two different ways to match a string. The difference comes from that some keywords are sensitive to the delimiters around them (such as ‘unsigned’ and ‘int’ in C, which are definitely not the same thing as ‘unsignedint’), and others not (in C, ‘!=’ is "different from" both in ‘a != b’ and ‘a!=b’).

The first ones are called keywords in a2ps jargon, and the seconds are operators. Operators are matched anywhere they appear, while keywords need to have separators around them (see Alphabets).

Let us give a more complicated example: that of the Yacc rules. A rule in Yacc is of the form:

a_rule : part1 part2 ;

Suppose you want to highlight these rules. To recognize them, you will write a regular expression specifying that:

  1. it must start at the beginning of the line,
  2. then there is string composed of symbols, which is what you want to highlight,
  3. and a colon, which can be preceded by blank characters.

The regexp you want is: ‘/^[a-zA-Z0-9_]*[\t ]*:/’. But with the rule

/^[a-zA-Z0-9_]*[\t ]*:/, "", Label_strong

the blanks and the colon are highlighted too. Hence you need to specify some parts in the regexp (see Back-reference Operator in Regex manual), and use a longer list of destination strings. The correct rule is

(/^([a-zA-Z0-9_]*)([\t ]*:)/, \1 Label_strong, \2 Plain)

Since it is a bit painful to read, regexps can be spread upon several lines. It is strongly suggested to break them by groups, and to document the group:

(/^([a-zA-Z0-9_]*)/    # \1. Name of the rule
 /([\t ]*:)/           # \2. Trailing space and colon
 \1 Label_strong, \2 Plain)

Next: Sequences, Previous: Case sensitivity, Up: Style Sheets Semantics   [Contents][Index]