![]() |
Remove trailing comments exercise
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....9a60623eb5883/ 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 |
Re: Remove trailing comments exercise
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 ? -- sm |
Re: Remove trailing comments exercise
On Nov 4, 1:11*pm, SAM <stephanemoriaux.NoAd...@wanadoo.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. 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. |
Re: Remove trailing comments exercise
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:- 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. |
Re: Remove trailing comments exercise
On Nov 4, 1:37*pm, Richard Cornford <Rich...@litotes.demon.co.uk>
wrote: > 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() > 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. > 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 |
Re: Remove trailing comments exercise
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 > http://groups.google.com/group/comp....9a60623eb5883/ > 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? |
Re: Remove trailing comments exercise
Le 11/4/09 1:37 PM, Csaba Gabor a crit :
> On Nov 4, 1:11 pm, SAM <stephanemoriaux.NoAd...@wanadoo.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). > 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 ';' > 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 |
Re: Remove trailing comments exercise
On 4 , 13:51, Csaba Gabor <dans...@gmail.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, ''); |
Re: Remove trailing comments exercise
On Nov 4, 6:59*pm, abozhilov <fort...@gmail.com> wrote:
> On 4 îÏÅÍ, 13:51, Csaba *Gabor <dans...@gmail.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 /* // */"; |
Re: Remove trailing comments exercise
Csaba Gabor wrote:
> 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; }); > 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 |
| All times are GMT. The time now is 01:03 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.