parsing comments in scripts

MisterX b.xavier at internet.lu
Fri Dec 10 12:20:40 EST 2004


It may not be the fastest but its been quite reliable.
This is the comment stripper for Transcriptolator.
It removes the comments and THEN puts them back (optional...)

It works line be line...

hope it helps...

cheers
Xavier

--
----------------------------------------------------------------------------
-- Comments Handlers
-- a comment stripper rejoiner
-- made to prevent comments translation
-- left locally for local variable usage
----------------------------------------------------------------------------
--

local commentdata


on ClearLineComments
  put empty into commentdata["handler"]
  put empty into commentdata["comment"]
  put false into commentdata["has2ndcomment"]
  put empty into commentdata["comment2"]
  put true into commentdata["commentfinished"]
end ClearLineComments

function StripComments aline
  local a,b
  local has2ndcomment,acomment2
  ClearLineComments
  -- clean up aline
  repeat while char 1 of aline is " "
    delete char 1 of aline
  end repeat
  repeat while char -1 of aline is " "
    delete char -1 of aline
  end repeat
  put offset("/*",aline) into a
  put offset("*/",aline) into b
  if a > 0 then
    if b > 0 then
      put true into commentdata["commentfinished"]
      get char a to b+1 of aline
      delete char a to b+1 of aline
    else
      put false into commentdata["commentfinished"]
      get char a to -1 of aline
      delete char a to -1 of aline
    end if
    put it into acomment
     
  else if commentdata["commentfinished"] is false then
    if b<0 then
      -- middle of a comment
      put aline into acomment
      put empty into aline
    else
      -- end of comment
      if b>0 then
        put char 1 to b+1 of aline into acomment
        if length(aline)>b+1 then 
          put char b+2 to -1 of aline into aline
        end if
        delete char 1 to b+1 of aline
        put true into commentdata["commentfinished"]
      end if
    end if
  end if
   
  put offset("//",aline) into a
  if a > 0 and offset("*/",aline) < a then
    put true into commentdata["commentfinished"]
    get char a to -1 of aline
    delete char a to -1 of aline
     
    put true into has2ndcomment 
    -- finally
    if has2ndcomment is false then
      put it into acomment
      put false into has2ndcomment
    else 
      put it into acomment2
      put true into has2ndcomment
    end if
     
   -- this part is commented since this code was meant
   -- for not xtalk languages. untested...
    --  else 
    --    -- search for -- comments
    --     
    --    put offset("--",aline) into a
    --    if a > 0 then
    --      get char a to -1 of aline
    --      delete char a to -1 of aline
    --       
    --      put true into has2ndcomment 
    --      -- finally
    --      if has2ndcomment is false then
    --        put it into acomment
    --        put false into has2ndcomment
    --      else 
    --        put it into acomment2
    --        put true into has2ndcomment
    --      end if
    --    end if
  end if
   
  repeat while char 1 of aline is " "
    delete char 1 of aline
  end repeat
  repeat while char -1 of aline is " "
    delete char -1 of aline
  end repeat
  put aline into commentdata["handler"]
  repeat while char 1 of acomment is " "
    delete char 1 of acomment
  end repeat
  repeat while char -1 of acomment is " "
    delete char -1 of acomment
  end repeat
  put acomment into commentdata["comment"]
   
  get has2ndcomment is true
  repeat while char 1 of acomment2 is " "
    delete char 1 of acomment2
  end repeat
  repeat while char -1 of acomment2 is " "
    delete char -1 of acomment2
  end repeat
  put it into commentdata["has2ndcomment"]
  if it then put acomment2 into commentdata["comment2"]
   
  return aline
end StripComments
  
function linewocomments
  -- return statement without comments
  return commentdata["handler"]
end linewocomments

function LineComments
  get commentdata["has2ndcomment"]
  if it then
    get commentdata["comment"] && commentdata["comment2"]
  else 
    get commentdata["comment"]
  end if
  return it
end LineComments

function RestoreOrigComments
  local thisline,linecomment
  get commentdata["handler"] into thisline
  put LineComments() into linecomment
  if linecomment is empty then return thisline
  else return thisline && linecomment
  ClearLineComments
end RestoreOrigComments




More information about the use-livecode mailing list