Strip block comments: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added PicoLisp)
m (whitespace)
Line 80: Line 80:
writefln("==original:\n%s", s) ;
writefln("==original:\n%s", s) ;
auto t = sepComment(s, `/\*`, `\*/`) ;
auto t = sepComment(s, `/\*`, `\*/`) ;
writefln("==comment stripted:\n%s\n==stripted comment:\n%s", t[0], t[1]) ;
writefln("==comment stripped:\n%s\n==stripped comment:\n%s", t[0], t[1]) ;


s = "apples, pears # and bananas
s = "apples, pears # and bananas
Line 87: Line 87:
writefln("==original:\n%s", s) ;
writefln("==original:\n%s", s) ;
t = sepComment(s, `#|;`, `[\n\r]|$`) ;
t = sepComment(s, `#|;`, `[\n\r]|$`) ;
writefln("==comment stripted:\n%s\n==stripted comment:\n%s", t[0], t[1]) ;
writefln("==comment stripped:\n%s\n==stripped comment:\n%s", t[0], t[1]) ;
}</lang>
}</lang>
part of output:
part of output:
<pre>==comment stripted:
<pre>==comment stripped:


function subroutine() {
function subroutine() {
Line 100: Line 100:
function something() {
function something() {
}
}
==stripted comment:
==stripped comment:
/**
/**
* Some comments
* Some comments
Line 130: Line 130:
function something() {
function something() {
}
}
'}
'}</lang>
</lang>


Output:<pre>
Output:<pre>

Revision as of 09:34, 8 November 2010

Task
Strip block comments
You are encouraged to solve this task according to the task description, using any language you may know.

A block comment begins with a begining delimiter and ends with a ending delimiter, including the delimiters.

Task : Strips block comments from text (of codes). Your demos should at least handle simple, non-nested and multiline block comment delimiters.

Sample text for striping.

  /**
   * Some comments
   * longer comments here that we can parse.
   *
   * Rahoo 
   */
   function subroutine() {
    a = /* inline comment */ b + c ;
   }
   /*/ <-- triky comments */

   /**
    * Another comment.
    */
    function something() {
    }

D

<lang d>import std.stdio ; import std.regexp, std.algorithm ;

string[] sepComment(string s, string cpat[] ...) {

   assert(cpat.length == 2,
       "sepComment : 2 pattern arguments for comment begin & end") ;
   string[] res = new string[](2) ;
   int p = 0, q = 0 /* cursors */, ic = 0 ;     // inside comment?
   int[] plen = new int[](2) ;                  // this's for handling /*/
   bool advCursor() {
       auto m = std.regexp.search(s[p..$], cpat[ic]) ;
       if(m is null) return false ;
       plen[ic] = max(0, plen[ic], m[0].length) ;
       q = p + m.pre.length ;                   // got comment head
       if(ic) { q += m[0].length  ;    }        // or comment tail
       if(std.regexp.find(m[0], "\n|\r") != -1) // special adjust for \n\r
           q-- ;
       return true ;
   }
   while(true) {
       if(!advCursor()) break ;
       res[ic] ~= s[p..q] ;                     // save slice of result
       if( ic && (q - p < plen[0] + plen[1])) { // this handle /*/ pattern
           p = q ;
           if(!advCursor()) break ;
           res[ic] ~= s[p..q] ;                 // save result again
       }
       p = q ;                                  // advance cursor
       ic = 1 - ic ;                            // toggle search type
   }
   if(ic)
       throw new Exception("Mismatched Comment") ;
   res[ic] ~= s[p..$] ;                         // save rest(non-comment)
   return res ;

}

void main() {

   string s = `  /**
  * Some comments
  * longer comments here that we can parse.
  *
  * Rahoo
  */
  function subroutine() {
   a = /* inline comment */ b + c ;
  }
  /*/ <-- triky comments */
  /**
   * Another comment.
   */
   function something() {
   }` ;
   writefln("==original:\n%s", s) ;
   auto t = sepComment(s, `/\*`, `\*/`) ;
   writefln("==comment stripped:\n%s\n==stripped comment:\n%s", t[0], t[1]) ;
   s = "apples, pears # and bananas

apples, pears ; and bananas " ; // test for line comment

   writefln("==original:\n%s", s) ;
   t = sepComment(s, `#|;`, `[\n\r]|$`) ;
   writefln("==comment stripped:\n%s\n==stripped comment:\n%s", t[0], t[1]) ;

}</lang> part of output:

==comment stripped:

   function subroutine() {
    a =  b + c ;
   }



    function something() {
    }
==stripped comment:
/**
   * Some comments
   * longer comments here that we can parse.
   *
   * Rahoo
   *//* inline comment *//*/ <-- triky comments *//**
    * Another comment.
    */

Perl 6

<lang perl6>sample().split(/ '/*' .+? '*/' /).print;

sub sample { ' /**

   * Some comments
   * longer comments here that we can parse.
   *
   * Rahoo
   */
   function subroutine() {
    a = /* inline comment */ b + c ;
   }
   /*/ <-- triky comments */
   /**
    * Another comment.
    */
   function something() {
   }

'}</lang>

Output:

   
    function subroutine() {
     a =  b + c ;
    }
    

    
    function something() {
    }

PicoLisp

<lang PicoLisp>(in "sample.txt"

  (while (echo "/*")
     (out "/dev/null" (echo "*/")) ) )</lang>

Output:


   function subroutine() {
    a =  b + c ;
   }
   

   
    function something() {
    }