[NEXT] [PREV]    HANDBOOK / GENTLE PRIMER / Describing Syntax /

Source Coordinates

Gentle
Applications
Concepts
Examples
Handbook
Support
Download

Syntactic errors are detected while the source file is being read, hence the actual position is available and can be used as part of an error message (this does not need to be programmed by the user). Semantic errors are detected when processing abstract syntax terms. In order to be able to issue a user-friendly error message, the source coordinates of the program text that is represented by the term should be available.

Gentle code automatically computes the coordinate of the ``most significant'' terminal of a construct. This is defined as follows: If a rule body contains one or more terminals, the position of the leftmost terminal is taken. If the rule body does not contain any terminals but one or more nonterminals then the coordinate of the leftmost nonterminal is taken. If the rule body is empty then the actual source position is taken.

This means, for example, that for expressions the coordinate of its top operator is taken. The expression a*b+c*d gets the coordinate of +. The expression a-b*c-d gets the coordinate of the second -.

This coordinate is available via the built-in predicate @, which can appear at the beginning of a syntax rule or after a nonterminal or token. If it appears at the beginning of a rule, it delivers the most significant coordinate of the phrase recognized by that rule; if it appears after a symbol, it delivers the coordinate of that symbol. An invocation has the form


   @(-> P)
and the output parameter has the type POSITION.

The type POSITION need not be declared. Its representation is not defined by the Gentle language. A library provides functions that implement and process this type. Values normally contain an indication of the line and column of a coordinate. See the Library Reference Manual for details.

The position should be part of those abstract syntax terms that may be the subject of error messages. For example,


   'type' DECLARATION
      declare(VARIABLE, TYPE, POSITION)
   
   'nonterm' Declaration(-> DECLARATION)
      'rule' Declaration(-> declare(V,T,P)):
         "DECLARE" Variable(-> V) @(-> P) ":" Type(-> T) ";"
selects the coordinate of the Variable and stores it in the abstract syntax term for a declaration. This can then be used for semantic processing.



[NEXT] [PREV]