previous next contentsEND(*LOOP*);"
while another likes "END;(*LOOP*)".However, with Depot4 there are two ways to tackle this problem:
PascalComment=
('(*'c:= 2; { !c#1;('*)'|any)}
|'{' c:= 2; {!c#1;('}'|any)})
-> SOURCETEXT.
and (one comment may be followed by another)
Comment= {PascalComment[i]} -> {PascalComment_[i]}
To suppress the default comment definition call
DefCom('$-$', '');.Comment must be inserted (and handled!) in the grammar at every
point where a comment is allowed. By this, the size of the grammar will easily be doubled - at least.
Skip(); in
front of alternatives, options and before the end of iterations. (Note, that cannot be done in
general because it prevents the correct operation of skipping suppression
<...>.)
The following example code illustrates this approach. You can experience the mentioned problem
by deleting the Skip(); in the last but one line.
ExampleKeepCom=
GLOBVAR USE coms: FLEX OF TXT; USE nrOfComs: INT;
DefCom('[*', '*] '); (* accept nested comments but do not
call ExampleKeepCom recursively *)
{ExampleKeepC1; INC(nrOfComs); coms[nrOfComs]:= ExampleKeepC1_; }
DefCom('$1-:ExampleKeepCom$[*', '*] '); (* re-activate ExampleKeepCom *).
ExampleKeepC1= {('*]' | any)!c#1; } !N>0;
-> '(* ' SOURCETEXT ' *)\n'.
ExampleKeepInsert= (* call this whenever you want to insert cumulated comments *)
GLOBVAR USE coms: FLEX OF TXT; USE nrOfComs: INT;
VAR nr: INT;
nr:= nrOfComs; nrOfComs:= 0;
->
{/..nr/ coms[i] }.
(*-------------------------------- DEMO ---------------------------------------*)
ExampleKeepComDemo= (* --- root of the demo --- *)
GLOBVAR DCL coms: FLEX OF TXT; DCL nrOfComs: INT;
nrOfComs:= 0;
DefCom('$1-:ExampleKeepCom$[*', '*] ');
'(*' { ExampleKeepComDemoElems[i] ExampleKeepInsert[i]}'*)'
-> { ExampleKeepComDemoElems_[i] ExampleKeepInsert_[i]}.
ExampleKeepComDemoElems=
Skip(); (* this is important to avoid multiplied comment texts - try it *)
(//e:id | e:str | e:num) -> e_ ':'.
In general, comments are handled by the second method, but at these fixed points the first one
is applied.
Example:
Insert a call to statementWithComment= statement <{' '} Comment>
-> statement_ '--' Comment_ '\n'.statementWithComment wherever you expect a comment of
this type.
(Note: This will append the Ada-like comment regardless whether there is a comment in the
source or not. This can be avoided if the new comment delimiters are added already within
Comment.)
IMPORTS Dp4StrBuf, Dp4Streams;
KeepCom=
GLOBVAR USE coms: FLEX OF TXT; USE nrOfComs: INT;
VAR bs: Dp4Streams.Stream;
DefCom('(*', '*) '); (* as above *)
KeepC1;
bs:= Dp4StrBuf.New('tmpstr'); (* make input stream *)
Dp4Streams.Tar2Strm(KeepC1_, bs); (* from accepted *)
From(Dp4Streams.StrmSrc(bs)); (* comment text *)
KeepC2 Back(); (* and process it *)
INC(nrOfComs); coms[nrOfComs]:= KeepC2_;
DefCom('$1-:KeepCom$(*', '*) '); (* re-activate KeepCom *).
KeepC1= {('*)' | any)!c#1; } !N>0;
-> SOURCETEXT .
KeepC2= {line[i]} (* prefix each line *)
-> '\n' {'//' line_[i] }.
Remarks:
Dp4StrBuf any other extension of Dp4Streams (
Files, Texts etc.) could be used.
(* don't use */ here *)" ).
previous next contents