Previous: Example Backend File, Up: Parsing a language file


5.2 Tag Expansion

In any language with compound tag types, you will need to implement an expand function. Once written, assign it to this variable.

— Variable: semantic-tag-expand-function

Function used to expand a tag. It is passed each tag production, and must return a list of tags derived from it, or nil if it does not need to be expanded.

Languages with compound definitions should use this function to expand from one compound symbol into several. For example, in C or Java the following definition is easily parsed into one tag:

int a, b;

This function should take this compound tag and turn it into two tags, one for A, and the other for B.

Additionally, you can use the expand function in conjunction with your language for other types of compound statements. For example, in Common Lisp Object System, you can have a definition:

     (defclass classname nil
       (slots ...) ...)

This will create both the datatype classname and the functional constructor classname. Each slot may have a :accessor method as well.

You can create a special compounded tag in your rule, for example:

     classdef: LPAREN DEFCLASS name semantic-list semantic-list RPAREN
               (TAG "custom" 'compound-class
                    :value (list
                             (TYPE-TAG $3 "class" ...)
                             (FUNCTION-TAG $3 ...)
                             ))
             ;

and in your expand function, you would write:

     (defun my-tag-expand (tag)
       "Expand tags for my language."
       (when (semantic-tag-of-class-p tag 'compound-class)
          (remq nil
             (semantic-tag-get-attribute tag :value))))

This will cause the custom tag to be replaced by the tags created in the :value attribute of the specially constructed tag.