HighDots Forums  

Remove trailing comments exercise

Javascript JavaScript language (comp.lang.javascript)


Discuss Remove trailing comments exercise in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Csaba Gabor
 
Posts: n/a

Default Remove trailing comments exercise - 11-04-2009 , 06:51 AM






I'm looking for a
function stripEndComments(code) {
// remove trailing comments and whitespace from
/* the end of code, which is presumed to be valid
// javascript */
... }


My previous post at
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/2aa9a60623eb5883/
may amount to more than just an exercise, so I am
slicing off part of it into an independent exercise
(and this one IS just an exercise).

Assume the use of the function
function checkSyntax(code) {
// returns false if code is not syntactically OK
// returns browser's (string) interpretation of the code if it's
OK,
// encapsulated in an anonymous function
try {
var f = new Function(code);
return f.toString(); }
catch (err) { return false; } } // syntax error


Some examples:
foo + bar // two comments /* or one? *//
=> foo + bar

"Foo" + "bar" /* three */ // lines
// of comments /* should all be
/* stripped off *////
=> "Foo" + "bar"


For the rambunctious: remove trailing empty statements, too:
code = "baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;"
=> baz/* junk */+borf; fubar


Csaba Gabor from Vienna

Reply With Quote
  #2  
Old   
SAM
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 07:11 AM






Le 11/4/09 12:51 PM, Csaba Gabor a crit :
Quote:
I'm looking for a
function stripEndComments(code) {
// remove trailing comments and whitespace from
/* the end of code, which is presumed to be valid
// javascript */
... }
(...)
For the rambunctious: remove trailing empty statements, too:
code = "baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;"
=> baz/* junk */+borf; fubar
I get,
Firefox.3 :
baz + borf;
fubar;
IE.5, 6 and 7 :
baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;

not yet finished ?

--
sm

Reply With Quote
  #3  
Old   
Csaba Gabor
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 07:37 AM



On Nov 4, 1:11*pm, SAM <stephanemoriaux.NoAd... (AT) wanadoo (DOT) fr.invalid>
wrote:
Quote:
Le 11/4/09 12:51 PM, Csaba Gabor a crit :

I'm looking for a
function stripEndComments(code) {
* // remove trailing comments and whitespace from
* /* the end of code, which is presumed to be valid
* // javascript */
* ... }
(...)
For the rambunctious: remove trailing empty statements, too:
code = "baz/* junk */+borf; fubar *; /* more junk */ ; ;; ;"
=> baz/* junk */+borf; fubar

I get,
Firefox.3 :
* * *baz + borf;
* * *fubar;
IE.5, 6 and 7 :
* * *baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;

not yet finished ?
Hi SAM, what you have shown is what FF/IE returns if
you put the mentioned strings into a function and then
do a .toString() on it. FF cleans all comments
whereas IE leaves them in.

However, in this exercise, I'd like to strip the TRAILING
comments only, in an as browser independent fashion
as possible (without recasting the code string into
a different form). The part to the right of the
=> above indicates the string that the desired
function, stripEndComments, should return.
Therefore, you can use checkSyntax as a false vs.
nonempty-string check, but I don't think you'll find
the actual nonempty string return values useful for the
purposes of this exercise.

Reply With Quote
  #4  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 07:37 AM



On Nov 4, 11:51 am, Csaba Gabor wrote:
Quote:
I'm looking for a
function stripEndComments(code) {
// remove trailing comments and whitespace from
/* the end of code, which is presumed to be valid
// javascript */
... }

My previous post at ...
may amount to more than just an exercise, so I am
slicing off part of it into an independent exercise
(and this one IS just an exercise).

Assume the use of the function
function checkSyntax(code) {
// returns false if code is not syntactically OK
// returns browser's (string) interpretation of the code if it's
OK,
// encapsulated in an anonymous function
try {
var f = new Function(code);
return f.toString(); }
catch (err) { return false; } } // syntax error

Some examples:
foo + bar // two comments /* or one? *//
=> foo + bar

"Foo" + "bar" /* three */ // lines
// of comments /* should all be
/* stripped off *////
=> "Foo" + "bar"

For the rambunctious: remove trailing empty statements, too:
code = "baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;"
=> baz/* junk */+borf; fubar
This problem includes the problem of not reacting to comment
delimiters whenever they appear in strings in the source code. For
example, stripping everything from the // to the end of the line in
the following would be disastrous:-

var prefixToIRI = {
'xsd':'http://www.w3.org/2001/XMLSchema',
'env':'http://schemas.xmlsoap.org/soap/envelope/',
'xsi':'http://www.w3.org/2001/XMLSchema-instance',
'xml':'http://www.w3.org/XML/1998/namespace',
'xmlns':'http://www.w3.org/2000/xmlns'
};

So for this task it seems necessary to identify the string literals
within the source, which is getting towards tokenising the source.
Tokenising the source was already implied in the task of verifying the
syntax of the code (along with identifying comments) so maybe this
stage should not be separated from the previous task if you genuinely
want all the comments removed.

Richard.

Reply With Quote
  #5  
Old   
Csaba Gabor
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 08:01 AM



On Nov 4, 1:37*pm, Richard Cornford <Rich... (AT) litotes (DOT) demon.co.uk>
wrote:
Quote:
On Nov 4, 11:51 am, Csaba *Gabor wrote:

I'm looking for a
function stripEndComments(code) {
* // remove trailing comments and whitespace from
* /* the end of code, which is presumed to be valid
* // javascript */
* ... }

My previous post at ...
may amount to more than just an exercise, so I am
slicing off part of it into an independent exercise
(and this one IS just an exercise).

Assume the use of the function
function checkSyntax(code) {
* // returns false if code is not syntactically OK
* // returns browser's (string) interpretation of the code if it's
OK,
* // * * encapsulated in an anonymous function
* try {
* * var f = new Function(code);
* * return f.toString(); }
* catch (err) { return false; } *} // syntax error

Some examples:
foo + bar // two comments /* or one? *//
=> foo + bar

"Foo" + "bar" /* three */ * // lines
// of comments /* should all be
/* stripped off *////
=> "Foo" + "bar"

For the rambunctious: remove trailing empty statements, too:
code = "baz/* junk */+borf; fubar *; /* more junk */ ; ;; ;"
=> baz/* junk */+borf; fubar

This problem includes the problem of not reacting to comment
delimiters whenever they appear in strings in the source code. For
example, stripping everything from the // to the end of the line in
the following would be disastrous:-
I don't want to strip all comments, just those at the
very tail end of the code string (as the 3rd example suggests).
For example:
foo(); // comment1
bar(); // comment2
=>
foo(); // comment1
bar()

Quote:
var prefixToIRI = {
* * 'xsd':'http://www.w3.org/2001/XMLSchema',
* * 'env':'http://schemas.xmlsoap.org/soap/envelope/',
* * 'xsi':'http://www.w3.org/2001/XMLSchema-instance',
* * 'xml':'http://www.w3.org/XML/1998/namespace',
* * 'xmlns':'http://www.w3.org/2000/xmlns'

};

So for this task it seems necessary to identify the string literals
within the source, which is getting towards tokenising the source.
Hopefully, we can stay away from tokenising. If we do have to
enter the business of tokenising (in any substantive way) to
solve this problem, it would no longer be an exercise.
Perhaps it is better to use the browser's embedded parser to help out.

Quote:
Tokenising the source was already implied in the task of verifying the
syntax of the code (along with identifying comments) so maybe this
stage should not be separated from the previous task if you genuinely
want all the comments removed.
Removing all the comments would seem to be a messier
problem (which I haven't thought about in this context).
I've done this (removed all comments) in the past for
PHP code, and it was around 60 lines of somewhat
intricate code (in parsing the original code string).
But I do not advocate such approach for this exercise.

> Richard

Reply With Quote
  #6  
Old   
Stevo
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 08:23 AM



Csaba Gabor wrote:
Quote:
I'm looking for a
function stripEndComments(code) {
// remove trailing comments and whitespace from
/* the end of code, which is presumed to be valid
// javascript */
... }


My previous post at
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/2aa9a60623eb5883/
may amount to more than just an exercise, so I am
slicing off part of it into an independent exercise
(and this one IS just an exercise).
Why are you talking about this as an exercise all the time? Is that your
way of getting people to write your code for you? Pretend it's just
an abstract exercise for fun?

Reply With Quote
  #7  
Old   
SAM
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 08:58 AM



Le 11/4/09 1:37 PM, Csaba Gabor a crit :
Quote:
On Nov 4, 1:11 pm, SAM <stephanemoriaux.NoAd... (AT) wanadoo (DOT) fr.invalid
wrote:
Le 11/4/09 12:51 PM, Csaba Gabor a crit :

I'm looking for a
function stripEndComments(code) {
// remove trailing comments and whitespace from
/* the end of code, which is presumed to be valid
// javascript */
... }
(...)
For the rambunctious: remove trailing empty statements, too:
code = "baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;"
=> baz/* junk */+borf; fubar
I get,
Firefox.3 :
baz + borf;
fubar;
IE.5, 6 and 7 :
baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;

not yet finished ?

Hi SAM, what you have shown is what FF/IE returns if
you put the mentioned strings into a function and then
do a .toString() on it. FF cleans all comments
whereas IE leaves them in.
Yes (the function checkSyntax() you've given).

Quote:
However, in this exercise, I'd like to strip the TRAILING
comments only, in an as browser independent fashion
as possible (without recasting the code string into
a different form).
javascript:alert("baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;
;;".replace(/\/[/\*][^\*]+\*\/|\s+|\s*;(?=\s*/g,''))

==> baz+borf;fubar;;

can't remove the last ';'

Quote:
The part to the right of the
=> above indicates the string that the desired
function, stripEndComments, should return.
Therefore, you can use checkSyntax as a false vs.
nonempty-string check, but I don't think you'll find
the actual nonempty string return values useful for the
purposes of this exercise.
(not yet understood what is "the" purpose ... comments no ... but yes)

javascript:alert("baz/* junk */+borf; fubar ; /* more junk */ ; ;; ;
;;".replace(/\/[/\*][^\*]+\*\/(?=\s*|\s+|;(?=\s*/g,''))

==> baz/*junk*/+borf;fubar;;

--
sm

Reply With Quote
  #8  
Old   
abozhilov
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 12:59 PM



On 4 , 13:51, Csaba Gabor <dans... (AT) gmail (DOT) com> wrote:
Quote:
I'm looking for a
function stripEndComments(code) {
// remove trailing comments and whitespace from
/* the end of code, which is presumed to be valid
// javascript */
... }
Something like this?

code.replace(/\s*;[\s;]*/g, ';\n').replace(/^\/(?:\/[^\n]+|\*[^\/*]*?\*
\/)/gm, '');

Reply With Quote
  #9  
Old   
Csaba Gabor
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 03:06 PM



On Nov 4, 6:59*pm, abozhilov <fort... (AT) gmail (DOT) com> wrote:
Quote:
On 4 îÏÅÍ, 13:51, Csaba *Gabor <dans... (AT) gmail (DOT) com> wrote:

I'm looking for a
function stripEndComments(code) {
š // remove trailing comments and whitespace from
š /* the end of code, which is presumed to be valid
š // javascript */
š ... }

Something like this?

code.replace(/\s*;[\s;]*/g, ';\n').replace(/^\/(?:\/[^\n]+|\*[^\/*]*?\*
\/)/gm, '');
You might be able to figure out a way to do this
with regular expressions, but I'm thinking that
it will be VERY messy because you will have to
account for strings and regular expressions such as:
var code = "var messy='it was windy/*sunny*'+" and */cold/*"

The first part of your code fails on:
var code = "var semi=' ; ; ; '";

While the second replace fails on
var code = "var k=i + j /* // */";

Reply With Quote
  #10  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Remove trailing comments exercise - 11-04-2009 , 03:58 PM



Csaba Gabor wrote:

Quote:
abozhilov wrote:
Csaba Gabor wrote:
š // remove trailing comments and whitespace from
š /* the end of code, which is presumed to be valid
š // javascript */
š ... }

Something like this?

code.replace(/\s*;[\s;]*/g, ';\n').replace(/^\/(?:\/[^\n]+|\*[^\/*]*?\*
\/)/gm, '');

You might be able to figure out a way to do this
with regular expressions, but I'm thinking that
it will be VERY messy
How fortunate then that you don't know what you are talking about.
It is rather easy to do if you do it properly. For example:

code = code.replace(
/('(?:[^']|\\')*')|("(?:[^"]|\\")*")|(\/\/.*)|(\s+$)/gm,
function(m, p1, p2, p3, p4) {
return (p3 || p4) ? "" : m;
});

Quote:
because you will have to
account for strings and regular expressions such as:
var code = "var messy='it was windy/*sunny*'+" and */cold/*"
The concatenation here is rather pointless. Any tokenizer or parser will
see this equivalent to

var code = "var messy='it was windy/*sunny*' and */cold/*"

And

var messy='it was windy/*sunny*' and */cold/*

is not syntactically correct to begin with. Which also points out that
there is not Regular Expression here.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.