all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
27
Task/Strip-block-comments/0DESCRIPTION
Normal file
27
Task/Strip-block-comments/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
A block comment begins with a ''beginning delimiter'' and ends with a ''ending delimiter'', including the delimiters. These delimiters are often multi-character sequences.
|
||||
|
||||
'''Task:''' Strip block comments from program text (of a programming language much like classic [[C]]). Your demos should at least handle simple, non-nested and multiline block comment delimiters. The beginning delimiter is the two-character sequence “<tt>/*</tt>” and the ending delimiter is “<tt>*/</tt>”.
|
||||
|
||||
Sample text for stripping:
|
||||
<pre>
|
||||
/**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}
|
||||
</pre>
|
||||
|
||||
'''Extra credit:''' Ensure that the stripping code is not hard-coded to the particular delimiters described above, but instead allows the caller to specify them. (If your language supports them, [[Optional parameters|optional parameters]] may be useful for this.)
|
||||
|
||||
C.f: [[Strip comments from a string]]
|
||||
4
Task/Strip-block-comments/1META.yaml
Normal file
4
Task/Strip-block-comments/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- String manipulation
|
||||
note: Text processing
|
||||
93
Task/Strip-block-comments/Ada/strip-block-comments.ada
Normal file
93
Task/Strip-block-comments/Ada/strip-block-comments.ada
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
with Ada.Strings.Fixed;
|
||||
with Ada.Strings.Unbounded;
|
||||
with Ada.Text_IO;
|
||||
with Ada.Command_Line;
|
||||
|
||||
procedure Strip is
|
||||
use Ada.Strings.Unbounded;
|
||||
procedure Print_Usage is
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("Usage:");
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line (" strip <file> [<opening> [<closing>]]");
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line (" file: file to strip");
|
||||
Ada.Text_IO.Put_Line (" opening: string for opening comment");
|
||||
Ada.Text_IO.Put_Line (" closing: string for closing comment");
|
||||
Ada.Text_IO.New_Line;
|
||||
end Print_Usage;
|
||||
|
||||
Opening_Pattern : Unbounded_String := To_Unbounded_String ("/*");
|
||||
Closing_Pattern : Unbounded_String := To_Unbounded_String ("*/");
|
||||
Inside_Comment : Boolean := False;
|
||||
|
||||
function Strip_Comments (From : String) return String is
|
||||
use Ada.Strings.Fixed;
|
||||
Opening_Index : Natural;
|
||||
Closing_Index : Natural;
|
||||
Start_Index : Natural := From'First;
|
||||
begin
|
||||
if Inside_Comment then
|
||||
Start_Index :=
|
||||
Index (Source => From, Pattern => To_String (Closing_Pattern));
|
||||
if Start_Index < From'First then
|
||||
return "";
|
||||
end if;
|
||||
Inside_Comment := False;
|
||||
Start_Index := Start_Index + Length (Closing_Pattern);
|
||||
end if;
|
||||
Opening_Index :=
|
||||
Index
|
||||
(Source => From,
|
||||
Pattern => To_String (Opening_Pattern),
|
||||
From => Start_Index);
|
||||
if Opening_Index < From'First then
|
||||
return From (Start_Index .. From'Last);
|
||||
else
|
||||
Closing_Index :=
|
||||
Index
|
||||
(Source => From,
|
||||
Pattern => To_String (Closing_Pattern),
|
||||
From => Opening_Index + Length (Opening_Pattern));
|
||||
if Closing_Index > 0 then
|
||||
return From (Start_Index .. Opening_Index - 1) &
|
||||
Strip_Comments
|
||||
(From (
|
||||
Closing_Index + Length (Closing_Pattern) .. From'Last));
|
||||
else
|
||||
Inside_Comment := True;
|
||||
return From (Start_Index .. Opening_Index - 1);
|
||||
end if;
|
||||
end if;
|
||||
end Strip_Comments;
|
||||
|
||||
File : Ada.Text_IO.File_Type;
|
||||
begin
|
||||
if Ada.Command_Line.Argument_Count < 1
|
||||
or else Ada.Command_Line.Argument_Count > 3
|
||||
then
|
||||
Print_Usage;
|
||||
return;
|
||||
end if;
|
||||
if Ada.Command_Line.Argument_Count > 1 then
|
||||
Opening_Pattern := To_Unbounded_String (Ada.Command_Line.Argument (2));
|
||||
if Ada.Command_Line.Argument_Count > 2 then
|
||||
Closing_Pattern :=
|
||||
To_Unbounded_String (Ada.Command_Line.Argument (3));
|
||||
else
|
||||
Closing_Pattern := Opening_Pattern;
|
||||
end if;
|
||||
end if;
|
||||
Ada.Text_IO.Open
|
||||
(File => File,
|
||||
Mode => Ada.Text_IO.In_File,
|
||||
Name => Ada.Command_Line.Argument (1));
|
||||
while not Ada.Text_IO.End_Of_File (File => File) loop
|
||||
declare
|
||||
Line : constant String := Ada.Text_IO.Get_Line (File);
|
||||
begin
|
||||
Ada.Text_IO.Put_Line (Strip_Comments (Line));
|
||||
end;
|
||||
end loop;
|
||||
Ada.Text_IO.Close (File => File);
|
||||
end Strip;
|
||||
38
Task/Strip-block-comments/BBC-BASIC/strip-block-comments.bbc
Normal file
38
Task/Strip-block-comments/BBC-BASIC/strip-block-comments.bbc
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
infile$ = "C:\sample.c"
|
||||
outfile$ = "C:\stripped.c"
|
||||
|
||||
PROCstripblockcomments(infile$, outfile$, "/*", "*/")
|
||||
END
|
||||
|
||||
DEF PROCstripblockcomments(infile$, outfile$, start$, finish$)
|
||||
LOCAL infile%, outfile%, comment%, test%, A$
|
||||
|
||||
infile% = OPENIN(infile$)
|
||||
IF infile%=0 ERROR 100, "Could not open input file"
|
||||
outfile% = OPENOUT(outfile$)
|
||||
IF outfile%=0 ERROR 100, "Could not open output file"
|
||||
|
||||
WHILE NOT EOF#infile%
|
||||
A$ = GET$#infile% TO 10
|
||||
REPEAT
|
||||
IF comment% THEN
|
||||
test% = INSTR(A$, finish$)
|
||||
IF test% THEN
|
||||
A$ = MID$(A$, test% + LEN(finish$))
|
||||
comment% = FALSE
|
||||
ENDIF
|
||||
ELSE
|
||||
test% = INSTR(A$, start$)
|
||||
IF test% THEN
|
||||
BPUT#outfile%, LEFT$(A$, test%-1);
|
||||
A$ = MID$(A$, test% + LEN(start$))
|
||||
comment% = TRUE
|
||||
ENDIF
|
||||
ENDIF
|
||||
UNTIL test%=0
|
||||
IF NOT comment% BPUT#outfile%, A$
|
||||
ENDWHILE
|
||||
|
||||
CLOSE #infile%
|
||||
CLOSE #outfile%
|
||||
ENDPROC
|
||||
24
Task/Strip-block-comments/C++/strip-block-comments.cpp
Normal file
24
Task/Strip-block-comments/C++/strip-block-comments.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <fstream>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
int main( ) {
|
||||
std::ifstream codeFile( "samplecode.txt" ) ;
|
||||
if ( codeFile ) {
|
||||
boost::regex commentre( "/\\*.*?\\*/" ) ;//comment start and end, and as few characters in between as possible
|
||||
std::string my_erase( "" ) ; //erase them
|
||||
std::string stripped ;
|
||||
std::string code( (std::istreambuf_iterator<char>( codeFile ) ) ,
|
||||
std::istreambuf_iterator<char>( ) ) ;
|
||||
codeFile.close( ) ;
|
||||
stripped = boost::regex_replace( code , commentre , my_erase ) ;
|
||||
std::cout << "Code unstripped:\n" << stripped << std::endl ;
|
||||
return 0 ;
|
||||
}
|
||||
else {
|
||||
std::cout << "Could not find code file!" << std::endl ;
|
||||
return 1 ;
|
||||
}
|
||||
}
|
||||
53
Task/Strip-block-comments/C/strip-block-comments.c
Normal file
53
Task/Strip-block-comments/C/strip-block-comments.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
const char *ca = "/*", *cb = "*/";
|
||||
int al = 2, bl = 2;
|
||||
|
||||
char *loadfile(const char *fn) {
|
||||
FILE *f = fopen(fn, "rb");
|
||||
int l;
|
||||
char *s;
|
||||
|
||||
if (f != NULL) {
|
||||
fseek(f, 0, SEEK_END);
|
||||
l = ftell(f);
|
||||
s = malloc(l+1);
|
||||
rewind(f);
|
||||
if (s)
|
||||
fread(s, 1, l, f);
|
||||
fclose(f);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void stripcomments(char *s) {
|
||||
char *a, *b;
|
||||
int len = strlen(s) + 1;
|
||||
|
||||
while ((a = strstr(s, ca)) != NULL) {
|
||||
b = strstr(a+al, cb);
|
||||
if (b == NULL)
|
||||
break;
|
||||
b += bl;
|
||||
memmove(a, b, len-(b-a));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const char *fn = "input.txt";
|
||||
char *s;
|
||||
|
||||
if (argc >= 2)
|
||||
fn = argv[1];
|
||||
s = loadfile(fn);
|
||||
if (argc == 4) {
|
||||
al = strlen(ca = argv[2]);
|
||||
bl = strlen(cb = argv[3]);
|
||||
}
|
||||
stripcomments(s);
|
||||
puts(s);
|
||||
free(s);
|
||||
return 0;
|
||||
}
|
||||
12
Task/Strip-block-comments/Clojure/strip-block-comments.clj
Normal file
12
Task/Strip-block-comments/Clojure/strip-block-comments.clj
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(defn comment-strip [txt & args]
|
||||
(let [args (conj {:delim ["/*" "*/"]} (apply hash-map args)) ; This is the standard way of doing keyword/optional arguments in Clojure
|
||||
[opener closer] (:delim args)]
|
||||
(loop [out "", txt txt, delim-count 0] ; delim-count is needed to handle nested comments
|
||||
(let [[hdtxt resttxt] (split-at (count opener) txt)] ; This splits "/* blah blah */" into hdtxt="/*" and restxt="blah blah */"
|
||||
(printf "hdtxt=%8s resttxt=%8s out=%8s txt=%16s delim-count=%s\n" (apply str hdtxt) (apply str resttxt) out (apply str txt) delim-count)
|
||||
(cond
|
||||
(empty? hdtxt) (str out (apply str txt))
|
||||
(= (apply str hdtxt) opener) (recur out resttxt (inc delim-count))
|
||||
(= (apply str hdtxt) closer) (recur out resttxt (dec delim-count))
|
||||
(= delim-count 0)(recur (str out (first txt)) (rest txt) delim-count)
|
||||
true (recur out (rest txt) delim-count))))))
|
||||
89
Task/Strip-block-comments/D/strip-block-comments.d
Normal file
89
Task/Strip-block-comments/D/strip-block-comments.d
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import std.algorithm, std.regex;
|
||||
|
||||
string[2] separateComments(in string txt,
|
||||
in string cpat0, in string cpat1) {
|
||||
int[2] plen; // to handle /*/
|
||||
int i, j; // cursors
|
||||
bool inside; // is inside comment?
|
||||
|
||||
// pre-compute regex here if desired
|
||||
//auto r0 = regex(cpat0);
|
||||
//auto r1 = regex(cpat1);
|
||||
//enum rct = ctRegex!(r"\n|\r");
|
||||
|
||||
bool advCursor() {
|
||||
auto mo = match(txt[i .. $], inside ? cpat1 : cpat0);
|
||||
if (mo.empty)
|
||||
return false;
|
||||
plen[inside] = max(0, plen[inside], mo.front[0].length);
|
||||
j = i + mo.pre.length; // got comment head
|
||||
if (inside)
|
||||
j += mo.front[0].length; // or comment tail
|
||||
|
||||
// special adjust for \n\r
|
||||
if (!match(mo.front[0], r"\n|\r").empty)
|
||||
j--;
|
||||
return true;
|
||||
}
|
||||
|
||||
string[2] result;
|
||||
while (true) {
|
||||
if (!advCursor())
|
||||
break;
|
||||
result[inside] ~= txt[i .. j]; // save slice of result
|
||||
|
||||
// handle /*/ pattern
|
||||
if (inside && (j - i < plen[0] + plen[1])) {
|
||||
i = j;
|
||||
if (!advCursor())
|
||||
break;
|
||||
result[inside] ~= txt[i .. j]; // save result again
|
||||
}
|
||||
|
||||
i = j; // advance cursor
|
||||
inside = !inside; // toggle search type
|
||||
}
|
||||
|
||||
if (inside)
|
||||
throw new Exception("Mismatched Comment");
|
||||
result[inside] ~= txt[i .. $]; // save rest(non-comment)
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
static void showResults(in string e, in string[2] pair) {
|
||||
writeln("===Original text:\n", e);
|
||||
writeln("\n\n===Text without comments:\n", pair[0]);
|
||||
writeln("\n\n===The stripped comments:\n", pair[1]);
|
||||
}
|
||||
|
||||
// First example ------------------------------
|
||||
immutable ex1 = ` /**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}`;
|
||||
|
||||
showResults(ex1, separateComments(ex1, `/\*`, `\*/`));
|
||||
|
||||
// Second example ------------------------------
|
||||
writeln("\n");
|
||||
immutable ex2 = "apples, pears # and bananas
|
||||
apples, pears; and bananas "; // test for line comment
|
||||
|
||||
showResults(ex2, separateComments(ex2, `#|;`, `[\n\r]|$`));
|
||||
}
|
||||
54
Task/Strip-block-comments/Go/strip-block-comments.go
Normal file
54
Task/Strip-block-comments/Go/strip-block-comments.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// idiomatic to name a function newX that allocates an object, initializes it,
|
||||
// and returns it ready to use. the object in this case is a closure.
|
||||
func newStripper(start, end string) func(string) string {
|
||||
// default to c-style block comments
|
||||
if start == "" || end == "" {
|
||||
start, end = "/*", "*/"
|
||||
}
|
||||
// closes on variables start, end.
|
||||
return func(source string) string {
|
||||
for {
|
||||
cs := strings.Index(source, start)
|
||||
if cs < 0 {
|
||||
break
|
||||
}
|
||||
ce := strings.Index(source[cs+2:], end)
|
||||
if ce < 0 {
|
||||
break
|
||||
}
|
||||
source = source[:cs] + source[cs+ce+4:]
|
||||
}
|
||||
return source
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// idiomatic is that zero values indicate to use meaningful defaults
|
||||
stripC := newStripper("", "")
|
||||
|
||||
// strip function now defined and can be called any number of times
|
||||
// without respecifying delimiters
|
||||
fmt.Println(stripC(` /**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}`))
|
||||
}
|
||||
20
Task/Strip-block-comments/Groovy/strip-block-comments.groovy
Normal file
20
Task/Strip-block-comments/Groovy/strip-block-comments.groovy
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
def code = """
|
||||
/**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}
|
||||
"""
|
||||
|
||||
println ((code =~ "(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)").replaceAll(''))
|
||||
16
Task/Strip-block-comments/Haskell/strip-block-comments.hs
Normal file
16
Task/Strip-block-comments/Haskell/strip-block-comments.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import Data.List
|
||||
|
||||
stripComments :: String -> String -> String -> String
|
||||
stripComments start end = notComment
|
||||
where notComment :: String -> String
|
||||
notComment "" = ""
|
||||
notComment xs
|
||||
| start `isPrefixOf` xs = inComment $ drop (length start) xs
|
||||
| otherwise = head xs:(notComment $ tail xs)
|
||||
inComment :: String -> String
|
||||
inComment "" = ""
|
||||
inComment xs
|
||||
| end `isPrefixOf` xs = notComment $ drop (length end) xs
|
||||
| otherwise = inComment $ tail xs
|
||||
|
||||
main = interact (stripComments "/*" "*/")
|
||||
16
Task/Strip-block-comments/Icon/strip-block-comments-1.icon
Normal file
16
Task/Strip-block-comments/Icon/strip-block-comments-1.icon
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
procedure main()
|
||||
every (unstripped := "") ||:= !&input || "\n" # Load file as one string
|
||||
write(stripBlockComment(unstripped,"/*","*/"))
|
||||
end
|
||||
|
||||
procedure stripBlockComment(s1,s2,s3) #: strip comments between s2-s3 from s1
|
||||
result := ""
|
||||
s1 ? {
|
||||
while result ||:= tab(find(s2)) do {
|
||||
move(*s2)
|
||||
tab(find(s3)|0) # or end of string
|
||||
move(*s3)
|
||||
}
|
||||
return result || tab(0)
|
||||
}
|
||||
end
|
||||
14
Task/Strip-block-comments/Icon/strip-block-comments-2.icon
Normal file
14
Task/Strip-block-comments/Icon/strip-block-comments-2.icon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
procedure main()
|
||||
every writes(stripBlockComment(!&input,"/*","*/"))
|
||||
end
|
||||
|
||||
procedure stripBlockComment(s,s2,s3)
|
||||
static inC # non-null when inside comment
|
||||
(s||"\n") ? while not pos(0) do {
|
||||
if /inC then
|
||||
if inC := 1(tab(find(s2))\1, move(*s2)) then suspend inC
|
||||
else return tab(0)
|
||||
else if (tab(find(s3))\1,move(*s3)) then inC := &null
|
||||
else fail
|
||||
}
|
||||
end
|
||||
6
Task/Strip-block-comments/J/strip-block-comments-1.j
Normal file
6
Task/Strip-block-comments/J/strip-block-comments-1.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
strip=:#~1 0 _1*./@:(|."0 1)2>4{"1(5;(0,"0~".;._2]0 :0);'/*'i.a.)&;:
|
||||
1 0 0
|
||||
0 2 0
|
||||
2 3 2
|
||||
0 2 2
|
||||
)
|
||||
18
Task/Strip-block-comments/J/strip-block-comments-2.j
Normal file
18
Task/Strip-block-comments/J/strip-block-comments-2.j
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
example=: 0 :0
|
||||
/**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}
|
||||
)
|
||||
10
Task/Strip-block-comments/J/strip-block-comments-3.j
Normal file
10
Task/Strip-block-comments/J/strip-block-comments-3.j
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
strip example
|
||||
|
||||
function subroutine() {
|
||||
a = b + c ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function something() {
|
||||
}
|
||||
7
Task/Strip-block-comments/J/strip-block-comments-4.j
Normal file
7
Task/Strip-block-comments/J/strip-block-comments-4.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
stripp=:3 :0
|
||||
('/*';'*/') stripp y
|
||||
:
|
||||
'open close'=. x
|
||||
marks=. (+./(-i._1+#open,close)|."0 1 open E. y) - close E.&.|. y
|
||||
y #~ -. (+._1&|.) (1 <. 0 >. +)/\.&.|. marks
|
||||
)
|
||||
50
Task/Strip-block-comments/Java/strip-block-comments.java
Normal file
50
Task/Strip-block-comments/Java/strip-block-comments.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import java.io.*;
|
||||
|
||||
public class StripBlockComments{
|
||||
public static String readFile(String filename) {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(filename));
|
||||
try {
|
||||
StringBuilder fileContents = new StringBuilder();
|
||||
char[] buffer = new char[4096];
|
||||
while (reader.read(buffer, 0, 4096) > 0) {
|
||||
fileContents.append(buffer);
|
||||
}
|
||||
return fileContents.toString();
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static String stripComments(String beginToken, String endToken,
|
||||
String input) {
|
||||
StringBuilder output = new StringBuilder();
|
||||
while (true) {
|
||||
int begin = input.indexOf(beginToken);
|
||||
int end = input.indexOf(endToken, begin+beginToken.length());
|
||||
if (begin == -1 || end == -1) {
|
||||
output.append(input);
|
||||
return output.toString();
|
||||
}
|
||||
output.append(input.substring(0, begin));
|
||||
input = input.substring(end + endToken.length());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length < 3) {
|
||||
System.out.println("Usage: BeginToken EndToken FileToProcess");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
String begin = args[0];
|
||||
String end = args[1];
|
||||
String input = args[2];
|
||||
|
||||
try {
|
||||
System.out.println(stripComments(begin, end, readFile(input)));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
global CRLF$
|
||||
CRLF$ =chr$( 13) +chr$( 10)
|
||||
|
||||
sample$ =" /**"+CRLF$+_
|
||||
" * Some comments"+CRLF$+_
|
||||
" * longer comments here that we can parse."+CRLF$+_
|
||||
" *"+CRLF$+_
|
||||
" * Rahoo "+CRLF$+_
|
||||
" */"+CRLF$+_
|
||||
" function subroutine() {"+CRLF$+_
|
||||
" a = /* inline comment */ b + c ;"+CRLF$+_
|
||||
" }"+CRLF$+_
|
||||
" /*/ <-- tricky comments */"+CRLF$+_
|
||||
""+CRLF$+_
|
||||
" /**"+CRLF$+_
|
||||
" * Another comment."+CRLF$+_
|
||||
" */"+CRLF$+_
|
||||
" function something() {"+CRLF$+_
|
||||
" }"+CRLF$
|
||||
|
||||
startDelim$ ="/*"
|
||||
finishDelim$ ="*/"
|
||||
|
||||
print "________________________________"
|
||||
print sample$
|
||||
print "________________________________"
|
||||
print blockStripped$( sample$, startDelim$, finishDelim$)
|
||||
print "________________________________"
|
||||
|
||||
end
|
||||
|
||||
function blockStripped$( in$, s$, f$)
|
||||
for i =1 to len( in$) -len( s$)
|
||||
if mid$( in$, i, len( s$)) =s$ then
|
||||
i =i +len( s$)
|
||||
do
|
||||
if mid$( in$, i, 2) =CRLF$ then blockStripped$ =blockStripped$ +CRLF$
|
||||
i =i +1
|
||||
loop until ( mid$( in$, i, len( f$)) =f$) or ( i =len( in$) -len( f$))
|
||||
i =i +len( f$) -1
|
||||
else
|
||||
blockStripped$ =blockStripped$ +mid$( in$, i, 1)
|
||||
end if
|
||||
next i
|
||||
end function
|
||||
8
Task/Strip-block-comments/Lua/strip-block-comments.lua
Normal file
8
Task/Strip-block-comments/Lua/strip-block-comments.lua
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
filename = "Text1.txt"
|
||||
|
||||
fp = io.open( filename, "r" )
|
||||
str = fp:read( "*all" )
|
||||
fp:close()
|
||||
|
||||
stripped = string.gsub( str, "/%*.-%*/", "" )
|
||||
print( stripped )
|
||||
13
Task/Strip-block-comments/MATLAB/strip-block-comments.m
Normal file
13
Task/Strip-block-comments/MATLAB/strip-block-comments.m
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function str = stripblockcomment(str,startmarker,endmarker)
|
||||
while(1)
|
||||
ix1 = strfind(str, startmarker);
|
||||
if isempty(ix1) return; end;
|
||||
ix2 = strfind(str(ix1+length(startmarker):end),endmarker);
|
||||
if isempty(ix2)
|
||||
str = str(1:ix1(1)-1);
|
||||
return;
|
||||
else
|
||||
str = [str(1:ix1(1)-1),str(ix1(1)+ix2(1)+length(endmarker)+1:end)];
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
StringReplace[a,"/*"~~Shortest[___]~~"*/" -> ""]
|
||||
|
||||
->
|
||||
function subroutine() {
|
||||
a = b + c ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function something() {
|
||||
}
|
||||
34
Task/Strip-block-comments/PL-I/strip-block-comments.pli
Normal file
34
Task/Strip-block-comments/PL-I/strip-block-comments.pli
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* A program to remove comments from text. */
|
||||
strip: procedure options (main); /* 8/1/2011 */
|
||||
declare text character (80) varying;
|
||||
declare (j, k) fixed binary;
|
||||
|
||||
on endfile (sysin) stop;
|
||||
|
||||
do forever;
|
||||
get edit (text) (L);
|
||||
do until (k = 0);
|
||||
k = index(text, '/*');
|
||||
if k > 0 then /* we have a start of comment. */
|
||||
do;
|
||||
/* Look for end of comment. */
|
||||
j = index(text, '*/', k+2);
|
||||
if j > 0 then
|
||||
do;
|
||||
text = substr(text, 1, k-1) ||
|
||||
substr(text, j+2, length(text)-(j+2)+1);
|
||||
end;
|
||||
else
|
||||
do; /* The comment continues onto the next line. */
|
||||
put skip list ( substr(text, 1, k-1) );
|
||||
more: get edit (text) (L);
|
||||
j = index(text, '*/');
|
||||
if j = 0 then do; put skip; go to more; end;
|
||||
text = substr(text, j+2, length(text) - (j+2) + 1);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
put skip list (text);
|
||||
end;
|
||||
|
||||
end strip;
|
||||
20
Task/Strip-block-comments/Perl-6/strip-block-comments.pl6
Normal file
20
Task/Strip-block-comments/Perl-6/strip-block-comments.pl6
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
sample().split(/ '/*' .+? '*/' /).print;
|
||||
|
||||
sub sample {
|
||||
' /**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}
|
||||
'}
|
||||
13
Task/Strip-block-comments/Perl/strip-block-comments.pl
Normal file
13
Task/Strip-block-comments/Perl/strip-block-comments.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#!/usr/bin/perl -w
|
||||
use strict ;
|
||||
use warnings ;
|
||||
|
||||
open( FH , "<" , "samplecode.txt" ) or die "Can't open file!$!\n" ;
|
||||
my $code = "" ;
|
||||
{
|
||||
local $/ ;
|
||||
$code = <FH> ; #slurp mode
|
||||
}
|
||||
close FH ;
|
||||
$code =~ s,/\*.*?\*/,,sg ;
|
||||
print $code . "\n" ;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(in "sample.txt"
|
||||
(while (echo "/*")
|
||||
(out "/dev/null" (echo "*/")) ) )
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
Procedure.s escapeChars(text.s)
|
||||
Static specialChars.s = "[\^$.|?*+()"
|
||||
Protected output.s, nextChar.s, i, countChar = Len(text)
|
||||
For i = 1 To countChar
|
||||
nextChar = Mid(text, i, 1)
|
||||
If FindString(specialChars, nextChar, 1)
|
||||
output + "\" + nextChar
|
||||
Else
|
||||
output + nextChar
|
||||
EndIf
|
||||
Next
|
||||
ProcedureReturn output
|
||||
EndProcedure
|
||||
|
||||
Procedure.s stripBlocks(text.s, first.s, last.s)
|
||||
Protected delimter_1.s = escapeChars(first), delimter_2.s = escapeChars(last)
|
||||
Protected expNum = CreateRegularExpression(#PB_Any, delimter_1 + ".*?" + delimter_2, #PB_RegularExpression_DotAll)
|
||||
Protected output.s = ReplaceRegularExpression(expNum, text, "")
|
||||
FreeRegularExpression(expNum)
|
||||
ProcedureReturn output
|
||||
EndProcedure
|
||||
|
||||
Define source.s
|
||||
source.s = " /**" + #CRLF$
|
||||
source.s + " * Some comments" + #CRLF$
|
||||
source.s + " * longer comments here that we can parse." + #CRLF$
|
||||
source.s + " *" + #CRLF$
|
||||
source.s + " * Rahoo " + #CRLF$
|
||||
source.s + " */" + #CRLF$
|
||||
source.s + " function subroutine() {" + #CRLF$
|
||||
source.s + " a = /* inline comment */ b + c ;" + #CRLF$
|
||||
source.s + " }" + #CRLF$
|
||||
source.s + " /*/ <-- tricky comments */" + #CRLF$
|
||||
source.s + "" + #CRLF$
|
||||
source.s + " /**" + #CRLF$
|
||||
source.s + " * Another comment." + #CRLF$
|
||||
source.s + " */" + #CRLF$
|
||||
source.s + " function something() {" + #CRLF$
|
||||
source.s + " }" + #CRLF$
|
||||
|
||||
If OpenConsole()
|
||||
PrintN("--- source ---")
|
||||
PrintN(source)
|
||||
PrintN("--- source with block comments between '/*' and '*/' removed ---")
|
||||
PrintN(stripBlocks(source, "/*", "*/"))
|
||||
PrintN("--- source with block comments between '*' and '*' removed ---")
|
||||
PrintN(stripBlocks(source, "*", "*"))
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
24
Task/Strip-block-comments/Python/strip-block-comments-1.py
Normal file
24
Task/Strip-block-comments/Python/strip-block-comments-1.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def _commentstripper(txt, delim):
|
||||
'Strips first nest of block comments'
|
||||
|
||||
deliml, delimr = delim
|
||||
out = ''
|
||||
if deliml in txt:
|
||||
indx = txt.index(deliml)
|
||||
out += txt[:indx]
|
||||
txt = txt[indx+len(deliml):]
|
||||
txt = _commentstripper(txt, delim)
|
||||
assert delimr in txt, 'Cannot find closing comment delimiter in ' + txt
|
||||
indx = txt.index(delimr)
|
||||
out += txt[(indx+len(delimr)):]
|
||||
else:
|
||||
out = txt
|
||||
return out
|
||||
|
||||
def commentstripper(txt, delim=('/*', '*/')):
|
||||
'Strips nests of block comments'
|
||||
|
||||
deliml, delimr = delim
|
||||
while deliml in txt:
|
||||
txt = _commentstripper(txt, delim)
|
||||
return txt
|
||||
41
Task/Strip-block-comments/Python/strip-block-comments-2.py
Normal file
41
Task/Strip-block-comments/Python/strip-block-comments-2.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
def test():
|
||||
print('\nNON-NESTED BLOCK COMMENT EXAMPLE:')
|
||||
sample = ''' /**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}'''
|
||||
print(commentstripper(sample))
|
||||
|
||||
print('\nNESTED BLOCK COMMENT EXAMPLE:')
|
||||
sample = ''' /**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*//*
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
*/
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}'''
|
||||
print(commentstripper(sample))
|
||||
|
||||
if __name__ == '__main__':
|
||||
test()
|
||||
32
Task/Strip-block-comments/Ruby/strip-block-comments.rb
Normal file
32
Task/Strip-block-comments/Ruby/strip-block-comments.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
def remove_comments!(str, comment_start='/*', comment_end='*/')
|
||||
while start_idx = str.index(comment_start)
|
||||
end_idx = str.index(comment_end, start_idx + comment_start.length) + comment_end.length - 1
|
||||
str[start_idx .. end_idx] = ""
|
||||
end
|
||||
str
|
||||
end
|
||||
|
||||
def remove_comments(str, comment_start='/*', comment_end='*/')
|
||||
remove_comments!(str.dup, comment_start, comment_end)
|
||||
end
|
||||
|
||||
example = <<END_OF_STRING
|
||||
/**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}
|
||||
END_OF_STRING
|
||||
|
||||
puts remove_comments example
|
||||
24
Task/Strip-block-comments/Seed7/strip-block-comments.seed7
Normal file
24
Task/Strip-block-comments/Seed7/strip-block-comments.seed7
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
const string: stri is "\
|
||||
\ /**\n\
|
||||
\ * Some comments\n\
|
||||
\ * longer comments here that we can parse.\n\
|
||||
\ *\n\
|
||||
\ * Rahoo\n\
|
||||
\ */\n\
|
||||
\ function subroutine() {\n\
|
||||
\ a = /* inline comment */ b + c ;\n\
|
||||
\ }\n\
|
||||
\ /*/ <-- tricky comments */\n\
|
||||
\\n\
|
||||
\ /**\n\
|
||||
\ * Another comment.\n\
|
||||
\ */\n\
|
||||
\ function something() {\n\
|
||||
\ }";
|
||||
begin
|
||||
writeln(replace2(stri, "/*", "*/", " "));
|
||||
end func;
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
$$ MODE DATA
|
||||
$$ script=*
|
||||
/**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}
|
||||
$$ MODE TUSCRIPT
|
||||
ERROR/STOP CREATE ("testfile",SEQ-E,-std-)
|
||||
ERROR/STOP CREATE ("destfile",SEQ-E,-std-)
|
||||
FILE "testfile" = script
|
||||
BUILD S_TABLE commentbeg=":/*:"
|
||||
BUILD S_TABLE commentend=":*/:"
|
||||
|
||||
ACCESS t: READ/STREAM "testfile" s.z/u,a/commentbeg+t+e/commentend,typ
|
||||
ACCESS d: WRITE/STREAM "destfile" s.z/u,a+t+e
|
||||
LOOP
|
||||
READ/EXIT t
|
||||
IF (typ==3) CYCLE
|
||||
t=SQUEEZE(t)
|
||||
WRITE/ADJUST d
|
||||
ENDLOOP
|
||||
ENDACCESS/PRINT t
|
||||
ENDACCESS/PRINT d
|
||||
d=FILE("destfile")
|
||||
TRACE *d
|
||||
8
Task/Strip-block-comments/Tcl/strip-block-comments-1.tcl
Normal file
8
Task/Strip-block-comments/Tcl/strip-block-comments-1.tcl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
proc stripBlockComment {string {openDelimiter "/*"} {closeDelimiter "*/"}} {
|
||||
# Convert the delimiters to REs by backslashing all non-alnum characters
|
||||
set openAsRE [regsub -all {\W} $openDelimiter {\\&}]
|
||||
set closeAsRE [regsub -all {\W} $closeDelimiter {\\&}]
|
||||
|
||||
# Now remove the blocks using a dynamic non-greedy regular expression
|
||||
regsub -all "$openAsRE.*?$closeAsRE" $string ""
|
||||
}
|
||||
17
Task/Strip-block-comments/Tcl/strip-block-comments-2.tcl
Normal file
17
Task/Strip-block-comments/Tcl/strip-block-comments-2.tcl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
puts [stripBlockComment " /**
|
||||
* Some comments
|
||||
* longer comments here that we can parse.
|
||||
*
|
||||
* Rahoo
|
||||
*/
|
||||
function subroutine() {
|
||||
a = /* inline comment */ b + c ;
|
||||
}
|
||||
/*/ <-- tricky comments */
|
||||
|
||||
/**
|
||||
* Another comment.
|
||||
*/
|
||||
function something() {
|
||||
}
|
||||
"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue