Re: A Puzzle in Eu -- what would you do?

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

Bob Elia wrote:
> 
<SNIP>
> >--Quark
> 
> }}}
<eucode>
> -- STRIPCMT.E  Sat, Aug  6 2005 12:29:12 am
> 
> include misc.e  -- FOR DEMO ONLY
> 
> with trace
> trace(1)
> function strip_comment(sequence line, sequence arg)
>      sequence s  integer inQuote, pos, ch
>      s = line  inQuote = 0
>      -- replace all escaped backslashes with spaces
>      pos = match("\\\\", s)
>      while pos do
>          s[pos..pos + 1] = "  "
>          pos = match("\\\\", s)
>      end while
>      -- replace all escaped double quotes with spaces
>      pos = match("\\\"", s)
>      while pos do
>          s[pos..pos + 1] = "  "
>          pos = match("\\\"", s)
>      end while
>      -- replace remaining quoted strings with spaces
>      for i = 1 to length(s) do
>          ch = s[i]
>          if ch = '"' then
>              if inQuote then
>                  inQuote = 0
>              else
>                  inQuote = 1
>              end if
>          elsif inQuote then
>              s[i] = ' '
>          end if
>      end for
>      -- look for comment
>      --pos = match("--", s)
>      -- look for argument
>      pos = match(arg, s)
>      if pos then
>          return line[1..pos - 1]
>      else
>          return line
>      end if
> end function
> 
> sequence line
> 
> line = "result =  x(\"--\", \"\\\\--\", \"----\\\"\\\"\") -- real comment --"
> 
> pretty_print(1, line, {2})  puts(1, "\n\n")
> 
> pretty_print(1, strip_comment(line, "--"), {2})  puts(1, "\n\n")
> 
> line = "line=InnocentRtn(\"--In Eu, a comment begins with 2 hyphens\") --A 
> real comment"
> pretty_print(1, line, {2})  puts(1, "\n\n")
> pretty_print(1, strip_comment(line, "--"), {2})  puts(1, "\n\n")
> 
> 
> <\eucode>
> 
> I've written a few of these using more complex logic but it was hard.  This 
> was almost dashed off.  Of course it could be improved but it's fast enough 
> for stripping comments from EU code. I ran a large program through it which 
> worked fine afterwards.  I hope this is what you meant.
> 
>                  Bob

Hi Bob,

Thanks for this post.  Your instincts are in the right direction for sure. 
Alas, I have just posted the Puzzle Report and Awards which sort of ends the
puzzle, but that doesn't lessen the value of your post.  It may well give people
interesting ideas, or an itch to borrow your work.

Also, I am authorized to award you a silver star and two blue ones.  They are
virtual ones, so you will have to go out and buy these if you want people to be
able to actually see them.

--Quark

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

Search



Quick Links

User menu

Not signed in.

Misc Menu