1 (* mcComment.mod provides a module to remember the comments.
3 Copyright (C) 2015-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
22 IMPLEMENTATION MODULE mcComment
; (*!m2pim*)
24 FROM DynamicStrings
IMPORT String
, InitString
, ConCat
, RemoveWhitePrefix
, Mark
, KillString
, InitStringCharStar
, EqualCharStar
, Length
, Slice
, string
, char
;
25 FROM Storage
IMPORT ALLOCATE
;
26 FROM nameKey
IMPORT Name
, keyToCharStar
, lengthKey
, NulName
;
27 FROM mcDebug
IMPORT assert
;
28 FROM ASCII
IMPORT nl
;
29 FROM libc
IMPORT printf
;
33 commentType
= (unknown
, procedureHeading
, inBody
, afterStatement
) ;
35 commentDesc
= POINTER TO RECORD
45 isProcedureComment - returns TRUE if, cd, is a procedure comment.
48 PROCEDURE isProcedureComment (cd
: commentDesc
) : BOOLEAN ;
50 RETURN (cd #
NIL) AND (cd^.type
= procedureHeading
)
51 END isProcedureComment
;
55 isBodyComment - returns TRUE if, cd, is a body comment.
58 PROCEDURE isBodyComment (cd
: commentDesc
) : BOOLEAN ;
60 RETURN (cd #
NIL) AND (cd^.type
= inBody
)
65 isAfterComment - returns TRUE if, cd, is an after comment.
68 PROCEDURE isAfterComment (cd
: commentDesc
) : BOOLEAN ;
70 RETURN (cd #
NIL) AND (cd^.type
= afterStatement
)
75 initComment - the start of a new comment has been seen by the lexical analyser.
76 A new comment block is created and all addText contents are placed
77 in this block. onlySpaces indicates whether we have only seen
81 PROCEDURE initComment (onlySpaces
: BOOLEAN) : commentDesc
;
92 type
:= afterStatement
94 content
:= InitString ('') ;
103 addText - cs is a C string (null terminated) which contains comment text.
104 This is appended to the comment, cd.
107 PROCEDURE addText (cd
: commentDesc
; cs
: ADDRESS
) ;
111 cd^.content
:= ConCat (cd^.content
, InitStringCharStar (cs
))
117 Min - returns the lower of, a, and, b.
120 PROCEDURE Min (a
, b
: CARDINAL) : CARDINAL ;
135 PROCEDURE RemoveNewlines (s
: String
) : String
;
137 WHILE Length (s
) > 0 DO
140 s
:= RemoveWhitePrefix (Slice (s
, 1, 0))
142 RETURN RemoveWhitePrefix (s
)
150 seenProcedure - returns TRUE if the name, procName, appears as the first word
154 PROCEDURE seenProcedure (cd
: commentDesc
; procName
: Name
) : BOOLEAN ;
161 a
:= keyToCharStar (procName
) ;
162 s
:= RemoveNewlines (cd^.content
) ;
163 s
:= Slice (Mark (s
), 0, Min (Length (s
), lengthKey (procName
))) ;
164 res
:= EqualCharStar (s
, a
) ;
165 s
:= KillString (s
) ;
171 setProcedureComment - changes the type of comment, cd, to a
172 procedure heading comment,
173 providing it has the procname as the first word.
176 PROCEDURE setProcedureComment (cd
: commentDesc
; procname
: Name
) ;
180 IF seenProcedure (cd
, procname
)
182 cd^.type
:= procedureHeading
;
183 cd^.procName
:= procname
186 END setProcedureComment
;
190 getContent - returns the content of comment, cd.
193 PROCEDURE getContent (cd
: commentDesc
) : String
;
204 getCommentCharStar - returns the C string content of comment, cd.
207 PROCEDURE getCommentCharStar (cd
: commentDesc
) : ADDRESS
;
211 s
:= getContent (cd
) ;
218 END getCommentCharStar
;
222 getProcedureComment - returns the current procedure comment if available.
225 PROCEDURE getProcedureComment (cd
: commentDesc
) : String
;
227 IF (cd^.type
= procedureHeading
) AND (NOT cd^.used
)
233 END getProcedureComment
;
237 getAfterStatementComment - returns the current statement after comment if available.
240 PROCEDURE getAfterStatementComment (cd
: commentDesc
) : String
;
242 IF (cd^.type
= afterStatement
) AND (NOT cd^.used
)
248 END getAfterStatementComment
;
252 getInbodyStatementComment - returns the current statement after comment if available.
255 PROCEDURE getInbodyStatementComment (cd
: commentDesc
) : String
;
257 IF (cd^.type
= inBody
) AND (NOT cd^.used
)
263 END getInbodyStatementComment
;
270 PROCEDURE dumpComment (cd
: commentDesc
) ;
272 printf ("comment : ");
276 unknown
: printf ("unknown") |
277 procedureHeading
: printf ("procedureheading") |
278 inBody
: printf ("inbody") |
279 afterStatement
: printf ("afterstatement")
288 printf (" contents = %s\n", string (content
))