Next: Various attributes and functions
Up: Statements
Previous: Modifying the fortran code
Unlike fortran statements and expressions, there exist no specific node variant
to describe comments. A comment is attached to a statement; not as a child but
as an attribute. Let us consider the comment in following fortran statement:
print *,1
C I am fortran comment
i=1
print *,2
The AST attribute applied to the assignment generates the following AST structure:
ass(name i, int_cst 1){^
prefix comment_s[comment I am fortran comment]}
So, comments need some specific functionalities in TSF.
-
s.NBCOMMENT
-
The expression returns the number of comments associated
to the AST statement s.
-
s.COMMENT
-
The expression returns the string concatenation of all
comments associated to the AST statement s.
-
s.COMMENT(n)
-
The expression returns the string of the
comment of rank n associated to the AST statement s. The rank
of the first comment is 1. The strings "first" and "last" can be used to
access to the first and the last comment.
-
s.PUREINST
-
The expression returns a copy of the AST statement s
buy without the comments associated to its root node. This attribute does
not remove comments in the body of an iterative or conditional statement
(see EXPURGATECOMMENT for an example)
-
s.EXPURGATECOMMENT
-
The expression returns a copy of the AST
s without any comment in any subpart of s.
Example 41:The following script removes the comments associated to a IF
statement and to the THEN part of the IF statement. The comments in the ELSE
clause are preserved.
SCRIPT RemoveIfComment(stat)
IF (stat.VARIANT=="struct_if") THEN
// Remove the comment of the IF statement
newstat := stat.PUREINST
//Remove the comments i n the THEN part
newstat.THENCLAUSE <- newstat.THENCLAUSE.EXPURGATECOMMENT
stat <- newstat
ENDIF
ENDSCRIPT
-
INSERTCOMMENT(s,c,op,p)
-
The statement adds the comment specified by the string
c to the statement s at the position p with the operation
op. The position p can be an integer index (i.e. the first
index is 1) or one of the string "first" or "last". The operation op
must be one of the following keywords (i.e. string):
- "insert" - The comment is inserted at the specified position.
- "replace" - The comment at the specified position is replaced by the
new comment. The position "all" can be used to replace all the
comments by the new one.
- "delete" - The comment at the specified position is deleted. The
position "all" can be used to delete all the comments. The argument
c is not used here.
Example 42:The following script replaces the comments of the selected
statement by the result of the AST attribute applied to the
statement. A comment "$$ AST" is added before this comment. If it is
found, then the new comment does not replace the last comment but it is
inserted after.
SCRIPT InsertASTComment ()
stat := $cstat
IF (stat) THEN
IF (stat.COMMENT(1)=="$$ AST") THEN
INSERTCOMMENT(stat," "+stat.AST,"insert","last")
ELSE
INSERTCOMMENT(stat," "+stat.AST,"replace","all")
INSERTCOMMENT(stat,"$$ AST","insert",1)
ENDIF
ENDIF
ENDSCRIPT
-
INSERTCOMMENT(s,c)
-
The statement adds the comment
specified by the string c to the statement s. This is a shortcut
to INSERTCOMMENT(s,c,"insert","last").
Next: Various attributes and functions
Up: Statements
Previous: Modifying the fortran code
Yann Mevel
1999-04-30