comment removal.

new topic     » topic index » view thread      » older message » newer message

I wanted to be able to remove line comments from various source code but also
keep the block style comments and headers intact.

Here is a snipet that i've adapted from my other code to do just that.
---------------------------------------------------------------
object  x     -- An object placeholder for various data types
integer i     -- An integer variable blink
constant

    COMMENT = "--",
    INPUT   = -- ADD FIELMANE HERE!
    OUTPUT  = -- ADD FILENAME HERE!


    constant Fn = open("INPUT" , "r")
    constant Tn = open("OUTPUT", "w")
    constant S  = COMMENT

    -- get line's ( inludes '\n' char ) for proccessing

    while 1 do
       x = gets(Fn)
    if atom(x) then            -- -1 is returned at EOF
        exit                   -- done!
    else
        i = match(S, x)        -- look for line comment
    end if

    -- if comment is found at end of line we remove it!

    if ( i > 1 ) then
        x = x[1..i - 1]        -- trims off the comment
    end if

    -- makes sure the line is terminated with '\n' char

    if x[$] != '\n' then       -- there's no '\n' char!
       x    &= '\n'            -- fix it up!
    end if

    -- Ok, writes the modified line out to the tempfile

    if equal(repeat(' ', length(x) - 1) & '\n', x)
    then
    --  just an empty sequence - so do nothing!
    else
        puts(Tn, x)
    end if
    end while


new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu