Previous Next Contents

3.2   Extension

An entry can be extended using the function ``Grammar.extend''. But its interface being quite complicated and, as it must be used with appropriate type constraints, the Camlp4 library provides a file, named ``pa_extend.cmo'', compatible with ``pa_o.cmo'' and ``pa_r.cmo'' which creates a new instruction doing this work.

This instruction is ``EXTEND'' which has the following format:

EXTEND
{ GLOBAL : global-list ; }
entry : { position } extension ;
...
entry : { position } extension ;
END

EXTEND, GLOBAL and END are keywords. There are some other keywords in this instruction, all in uppercase.

The entry is a name of an entry variable (simple identifier or prefixed by a module name). It is extended according to the rules given by extension. The optional position tells how to insert the extension among the entry precedence levels: by default, the extension is done at the first precedence level (creating this level if the entry is empty).

The optional global-list restricts the list of the global entries (separated by spaces) extended in the ``EXTEND'' instruction. The other entries are created locally. By default, all entries are global and must correspond to entry variables visible at the ``EXTEND'' instruction point.

Syntax of a position

A position can be: Only the case LEVEL extends already existing levels: the other cases create new levels.

Syntax of an extension

The syntax of an entry extension is:
[ { label } { associativity } level-rules  
| ...  
| { label } { associativity } level-rules ]

The optional label identifies the current level for possible future extensions. The optional associativity can be LEFTA, RIGHTA or NONA for respectively left, right and no associativity: the default is left associativity.

Syntax of a level-rules

The level-rules have the format:
[ { pattern = } symbol ; ... { pattern = } symbol { -> action }  
| ...  
| { pattern = } symbol ; ... { pattern = } symbol { -> action } ]

The pattern is a language simple pattern, limited to identifiers, wildcard (character ``underscore'') and tuples of patterns. If not present, it is equivalent to the wildcard.

The action is a language expression where the patterns are bound to the result of their corresponding symbol; in addition, the variable ``loc'' is bound to the source location of the rule. The action part is optional; by default it is the value ``()''.

A symbol is either:
A string
meaning: a keyword. If this keyword does not exist in the current grammar, it is added. The syntax of the keyword must respect the rules of the lexer. Its type is string.
An entry name
(simple identifier or prefixed by a module name) meaning a call to this entry. Its type is the entry result type.
The keyword SELF
meaning the entry being extended (first level).
The keyword NEXT
meaning the entry being extended at the next level.
A token pattern.
Tokens patterns are identifiers starting with an uppercase letter optionally followed by a string; they recognize tokens generated by the lexer. See for example, the constructors generated by the predefined lexer (section 6.4), lexing Ocaml normal and revised syntaxes.
A metasymbol
among:
A level-rules
meaning an anonymous entry with only one level.
A symbol between parentheses

Previous Next Contents