Velocity Reviews > Determining the last statement exercise

# Determining the last statement exercise

Csaba Gabor
Guest
Posts: n/a

 11-03-2009
Suppose you have some javascript statements in a string.
Can you determine whether the entire string is syntactically
valid, and if so, the starting position of the last statement?
In other words,

function lastStatementPos(code) {
// returns the starting position within code of the last
// javascript statement, and -1 if code is not syntactiaclly
// valid.

This came up in a different context today, and I thought
it would make an interesting exercise.

Csaba Gabor from Vienna

SAM
Guest
Posts: n/a

 11-03-2009
Le 11/3/09 4:21 PM, Csaba Gabor a écrit :
> Suppose you have some javascript statements in a string.

can you give an example of that ?

> Can you determine whether the entire string is syntactically
> valid, and if so, the starting position of the last statement?
> In other words,
>
> function lastStatementPos(code) {
> // returns the starting position within code of the last
> // javascript statement, and -1 if code is not syntactiaclly
> // valid.
>
>
> This came up in a different context today, and I thought
> it would make an interesting exercise.

eval(code); ?

--
sm

Evertjan.
Guest
Posts: n/a

 11-03-2009
Csaba Gabor wrote on 03 nov 2009 in comp.lang.javascript:

> Suppose you have some javascript statements in a string.
> Can you determine whether the entire string is syntactically
> valid, and if so, the starting position of the last statement?
> In other words,
>
> function lastStatementPos(code) {
> // returns the starting position within code of the last
> // javascript statement, and -1 if code is not syntactiaclly
> // valid.
>
>
> This came up in a different context today, and I thought
> it would make an interesting exercise.

A string like?

Would that be usefull?
I don't think it would.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Richard Cornford
Guest
Posts: n/a

 11-03-2009
On Nov 3, 3:21 pm, Csaba Gabor wrote:
> Suppose you have some javascript statements in a string.
> Can you determine whether the entire string is syntactically
> valid, and if so, the starting position of the last statement?

It would be possible to write a javascript tokeniser/parser with
javascript, and have that determine the syntactic correctness of the
source and expose the contained statements in a way that would allow
you to determine the starting position of the last.

> In other words,
>
> function lastStatementPos(code) {
> // returns the starting position within code of the last
> // javascript statement, and -1 if code is not syntactiaclly
> // valid.
>
> This came up in a different context today, and I thought
> it would make an interesting exercise.

Maybe a bit too big a task to be considered an 'exercise'. Quickest
results would probably start with something that was already doing
most of the job like Narcissus or JSLint.

<URL: http://mxr.mozilla.org/mozilla/source/js/narcissus/ >

Richard.

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 11-03-2009
Csaba Gabor wrote:

> Suppose you have some javascript statements in a string.

Define "javascript".

> Can you determine whether the entire string is syntactically
> valid,

Depends. Short of writing an ECMAScript parser, which would only be able
to cover specified syntax rules, where available you could try-catch the
SyntaxError that the eval() of the specific implementation would throw if
the code would not conform to its syntax rules. However, AFAICS that can
become quite a nuisance in Konqueror 4.3.2 as syntax errors cannot be
catched there (syntax errors caused Konqueror 3.5.x to crash, therefore I
had to modify the ECMAScript Support Matrix.)

> and if so, the starting position of the last statement?

Depends. What is, in your book, considered "the last statement" in

if (x)
y;
else
z;

(we are looking at the following production here:

/IfStatement/ :
if (/Expression/) /Statement/ else /Statement/

see ES3F, 12.5)

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Csaba Gabor
Guest
Posts: n/a

 11-04-2009
On Nov 3, 4:33*pm, "Evertjan." <(E-Mail Removed)> wrote:
> Csaba *Gabor wrote on 03 nov 2009 in comp.lang.javascript:
>
> > Suppose you have some javascript statements in a string.
> > Can you determine whether the entire string is syntactically
> > valid, and if so, the starting position of the last statement?
> > In other words,

>
> > function lastStatementPos(code) {
> > * // returns the starting position within code of the last
> > * // javascript statement, and -1 if code is not syntactiaclly
> > * // valid.

>
> > This came up in a different context today, and I thought
> > it would make an interesting exercise.

>
> A string like?
>
>
> Would that be usefull?
> I don't think it would.

On Nov 3, 4:33 pm, "Evertjan." <(E-Mail Removed)> wrote:
> Csaba Gabor wrote on 03 nov 2009 in comp.lang.javascript:
>
> > Suppose you have some javascript statements in a string.
> > Can you determine whether the entire string is syntactically
> > valid, and if so, the starting position of the last statement?
> > In other words,

>
> > function lastStatementPos(code) {
> > // returns the starting position within code of the last
> > // javascript statement, and -1 if code is not syntactiaclly
> > // valid.
> > }

>
> > This came up in a different context today, and I thought
> > it would make an interesting exercise.

>
> A string like?
>
>
> Would that be usefull?
> I don't think it would.

You've made a good example, and yes it can be useful.
But I think I better give context to my question
and recast it, especially in light of the fact that my
proposed solution does not work across browsers.

The question arose in the following context: The
user is asked to enter some javascript statements
to affect a value. I want to ensure that all the
statements are syntactically correct, AND to insert
a return before the last statement, if it doesn't
start with return, but does make syntactic sense.

The validation is fairly straightforward:

function syntax_check(code) {
// returns false is code is not syntactically OK
// returns browser's interpretation of the code if it's OK
try {
var f = new Function(code);
return f.toString(); }
catch (err) { return false; } // syntax error
}

On firefox, the returned string is cleaned of all
comments, and is recast into a 'standard form'.
I was thinking that if the penultimate line of
this returned string did not consist of "}",
then a return could be prefixed, if it wasn't
already there. Any exceptions to this?

However, with IE the returned string is not gussied up
into standard form and is left pretty much as is.

So the question, exercise, or problem is: given
a function which will tell you whether or not you
have a syntactically correct javascript, can you
semi reasonably isolate the last statement to
determine whether it should be prefixed with a
return, and to do so in such case.

Some examples:
code = "'Fred'" => return 'Fred';

code = "var i=7; i *= 9"; =>
var i=7;
return i *= 9;

code = "x='word'\nx+='s' // making a plural\n" +
" return x /* pluralizing a word */";
=> no change

Evertjan's code =>

code = '{a = 1; b = 2}' =>
a = 1;
return b = 2;

Two problem cases:
code = "var y=6;\n y*= 2 // difficult; hard case";
code = "var y=6;\n y*= 2; // difficult; hard case";

Evertjan.
Guest
Posts: n/a

 11-04-2009
Csaba Gabor wrote on 04 nov 2009 in comp.lang.javascript:

> Evertjan's code =>
>

Which is utter nonsense as

1 alert() does not have a return value

2 return is only sensible in a function

> The question arose in the following context:
> The user is asked to enter some javascript statements
> to affect a value.

Sorry, that is not only not very useful, but it could even be dangerous.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 11-04-2009
Csaba Gabor wrote:

> The validation is fairly straightforward:

AISB, it is not.

> function syntax_check(code) {
> // returns false is code is not syntactically OK

Based on which syntax rules? ECMAScript's, JavaScript's, JScripts or
others'?

> // returns browser's interpretation of the code if it's OK
> try {

And if this already constitutes a syntax error, the whole thing breaks.

> var f = new Function(code);

And if the Function constructor is not supported, the whole thing breaks.

> return f.toString(); }
> catch (err) { return false; } // syntax error
> }

Does not work reliably. You have failed to realize, among other things,
that syntax is context-sensitive. For example, `return' is allowed in a
function, it is not allowed elsewhere.

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

VK
Guest
Posts: n/a

 11-04-2009
Thomas 'PointedEars' Lahn wrote:
>
> Based on which syntax rules? *ECMAScript's, JavaScript's, JScripts or
> others'?
>
> And if the Function constructor is not supported, the whole thing breaks.

By trying get nasty do not get dorky

To OP: I am wondering if you are looking for a regexp solution
recreating parser rules - or some creative eval or eval-like approach.
Or it's up to respondents to find the most effective way?

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 11-04-2009
VK wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Based on which syntax rules? ECMAScript's, JavaScript's, JScripts or
>> others'?
>>
>> And if the Function constructor is not supported, the whole thing breaks.

>
> By trying get nasty do not get dorky

Please shut up until you know what you are talking about (suppose
that is ever going to happen). And lose the bogus smileys.

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