; REMCPP
;
; Program for Proteus
;
; (C) 1998-2004 Simone Zanella Productions
;
; Remove "C++" remarks.
;
; Algorithm:
; - if "//" does not appear on the line, write it without modifications; otherwise,
; create a new line PL (empty);
; - if the line begins with a remark, append characters to PL until the
; remark is closed; then (or if the line begins outside a remark),
; consider only the last two characters; if they are "/*", append everything
; until the end of the line or until the token "*/" appears; if they are "//",
; ignore them until the end of the line. Print PL if it is not empty.
;
; Note: tokens "//", "/*" and "*/" cannot appear inside strings.
; Initialization
FUNCTION ONSTART()
IF STREQ(ARGV(3), "..")
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " source destination"
CONSOLELN ""
CONSOLELN "Purpose: remove \"C++\" remarks from source"
ABORT 0
FI
_InRem = 0
RETURN
; Find token "//"
Pos = STRSTR(L, "//")
; Not found
IF EQ(Pos, 0)
; Write line as is
PRINTLN L
; Set InRem according to C style remarks
P = STRRSTR(L, "/*")
M = STRRSTR(L, "*/")
; There is a "begin remark" token
IF NEQ(P, 0)
; There is an "end remark" token
IF NEQ(M, 0)
; We are inside a remark if P is greater than M
InRem = GT(P, M)
ELSE
; We are inside a remark (only begin remark was found)
InRem = 1
FI
ELSE
; End remark: set flag
IF NEQ(M, 0)
InRem = 0
FI
FI
ELSE
PL = ""
P = 1
M = STRLEN(L)
Prev = 0
Current = SUBSTR(L, P, 1)
; Line begins inside a C remark: copy until the end of line or end of remark
IF EQ(InRem, 1)
WHILE AND( AND(InRem, LE(P, M)), \
OR(STRNEQ(Prev, "*"), STRNEQ(Current, "/")) )
PL = PL Current
Prev = Current
INC(@P)
Current = SUBSTR(L, P, 1)
IF AND(STREQ(Prev, "*"), STREQ(Current, "/"))
InRem = 0
BREAK
FI
LOOP
FI
; Append characters until the token "//" is found
WHILE AND( LE(P, M), \
OR(STRNEQ(Prev, "/"), STRNEQ(Current, "/")) )
; Begin remark
IF AND(STREQ(Prev, "/"), STREQ(Current, "*"))
InRem = 1
; Copy until the end of line or the end of remark
WHILE AND( AND(InRem, LE(P, M)), \
OR(STRNEQ(Prev, "*"), STRNEQ(Current, "/")) )
PL = PL Current
Prev = Current
INC(@P)
Current = SUBSTR(L, P, 1)
IF AND(STREQ(Prev, "*"), STREQ(Current, "/"))
InRem = 0
BREAK
FI
LOOP
ELSE
; Append Current if it is not a token "//"
IF STRNEQ(SUBSTR(L, P, 2), "//")
PL = PL Current
FI
Prev = Current
INC(@P)
Current = SUBSTR(L, P, 1)
FI
LOOP
; If the modified line is not empty, write it
IF ISNOTEMPTY(PL)
PRINTLN PL
FI
FI