Page 1 of 3

Missing 'on' does not cause error...

Posted: Thu Apr 10, 2014 5:05 am
by paulsr
Greetings:

I'm wondering, does anyone agree that the following is a Livecode bug?...

I've just spent a day trying to figure why a button had stopped working. The end of the story is that I had accidentally deleted the 'on mouseUp' statement. It's at the head of a fairly long script, just after a 'global' statement, the code was still indented, and so it was not obvious the line had disappeared.

I'm really surprised that on closing the script, I didn't see any error. If I delete an 'end' statement, LC complains, because the there is no 'end' to match the 'on'. But the opposite check seems not to be done. LC is quite happy if you have an 'end' without a matching 'on'.

Thoughts?...

--paul

[LC6.6.1]

Re: Missing 'on' does not cause error...

Posted: Thu Apr 10, 2014 1:59 pm
by dunbarx
Certainly if you lead LC on, in that you create a handler and do not close it, it will complain. But if you write a bunch of text that happens to finish with an "end whatever", then LC was never interested in what you wrote before that line.

I guess you might say that if you introduce what appears to be a formal handler, you better close it. But if you "close" a handler you never really "opened", no harm, no foul.

Craig Newman

Re: Missing 'on' does not cause error...

Posted: Fri Apr 11, 2014 10:08 am
by paulsr
Okay Craig, I understand. But if it's not a bug, it still seems sloppy to me. I would at least expect to get a warning to tell me I was ending a handler I hadn't started.

--paul

Re: Missing 'on' does not cause error...

Posted: Fri Apr 11, 2014 2:08 pm
by dunbarx
You are saying if you write a line in the script editor somewhere that begins with "end", then that editor ought to perk up and, what? Look upwards in the code and expect to find an "on"?

I suppose it could be implemented that way, but just wasn't. There is indeed a procedure that keeps an eye out for "on", and looks downward expecting to find an "end". But sloppy? I see it as just the way things are, though I fully understand why you were thrown a bit when you discovered the thing.

Craig

Re: Missing 'on' does not cause error...

Posted: Fri Apr 11, 2014 8:53 pm
by paul_gr
Hi Craig,
that doesn't make sense to me.

In the LC 6.6.1 IDE, I can now use comments without having to use comment tags outside of a handler.
This script works and it shouldn't.

---------- start script ----------------------

clicking on this button sends "whatever" message ( uncommented comment )

on mouseUp
put "whatever"
end mouseUp

---------- end script ------------------------

If the uncommented string is inside a handler LC detects it and complains.
i.e LC finds the error in the next example:

---------- start script ----------------------

on mouseUp
clicking on this button sends "whatever" message ( uncommented comment )
put "whatever"
end mouseUp

---------- end script ------------------------

code that the IDE does not understand should be flagged; like it is in any other programming environment.

Paul

Re: Missing 'on' does not cause error...

Posted: Sat Apr 12, 2014 2:56 am
by dunbarx
Trying to remember if HC allowed uncommented text outside of handlers. Not sure. Must test when I get to a computer that still has a crank on it. Oh yes, I have a dozen running at my office.

Loosey-goosey? I guess so. But that is part and parcel of the xTalk world. I would miss those homey attributes if they were banished. But again, I can see that this sort of thing would throw you if you were used to more formal languages.

Come to the dark side...

Craig

Re: Missing 'on' does not cause error...

Posted: Wed Apr 16, 2014 4:30 am
by dunbarx
For what it is worth. HC also allowed uncommented text to be sprinkled outside of handlers in object scripts.

Craig

Re: Missing 'on' does not cause error...

Posted: Wed Apr 16, 2014 10:03 am
by [-hh]
..........

Re: Missing 'on' does not cause error...

Posted: Wed Apr 16, 2014 1:48 pm
by dunbarx
It is so great to have Hermann back!!!

Re: Missing 'on' does not cause error...

Posted: Wed Apr 16, 2014 1:58 pm
by Thierry
Thanks a lot Craig and Hermann!

You gave me an idea which works perfectly :)

I've been missing the concept of HERE Documents in LC we can find in some other languages.

For instance, working with complex regex or applescript, the best solution so far was
to type the regex/applescript in a custom property or in a field which I didn't like that much as it splits
the whole code in two parts (hard to read and maintain)

So, here is my last hour testing a sort of HERE document with my applescript code:

First sample code is how I did it before,
second and third are 2 ways based on this strange Livecode feature :)

Code: Select all

private function doBBLivecode aFile, aTitle
   do format("tell application \"BBLivecode\" to edit script \"%s\" infos \"%s\" type %s autosave true", \
         aFile,aTitle, "Stack")  as applescript
   return the result
end doBBLivecode

on mouseUp
   local scripFile
   put  "/Users/x/Desktop/xyz.lcscript" into scripFile
   put the script of me into url  ("file:" & scripFile)
   put "button sunnyLCBBproperties" into BBEtitle
   
   put doBBLivecode( scripFile, BBEtitle)
end mouseUp

Code: Select all

tell application "BBLivecode"
     edit script "%s" infos "%s" type  %s  autosave %s
end tell
#

local AScript

on mouseUp
   local scriptFile, BBEtitle
   put  "/Users/x/Desktop/xyz.lcscript" into scriptFile
   put the script of me into url  ("file:" & scriptFile)
   put "button sunnyLCBBproperties" into BBEtitle
   
   do format( AScript, scriptFile, BBEtitle, "Stack", true ) as applescript
end mouseUp

on mouseDown
     set itemDelimiter to "#"
     put item 1 of the script of me into AScript
end mouseDown

Code: Select all

tell application "BBLivecode"
     edit script "_scriptAlias_"    infos "_BBEtitle_"     type _type_   autosave  "_autoSave_" 
end tell
#

local AScript

on mouseUp
   local scriptFile, BBEtitle
   put  "/Users/x/Desktop/xyz.lcscript" into scriptFile
   put the script of me into url  ("file:" & scriptFile)
   put "button sunnyLCBBproperties" into BBEtitle
 
   replace "_scriptAlias_" with scriptFile in AScript
   replace "_BBEtitle_" with BBEtitle in AScript
   replace "_type_" with "Stack" in AScript
   replace "_autoSave_" with true in AScript

   do AScript as applescript
end mouseUp

on mouseDown
     set itemDelimiter to "#"
     put item 1 of the script of me into AScript
end mouseDown
and below a screenshot of the same code in the AppleScript Editor!

So now, I can seriously write/debug any applescripts in the Applescript
Editor and when happy, copy/paste it in a Livecode script with almost no surprises.
At least, I can use this when coding and it will save me a lot of time
and still very easy to read!

Thierry

Re: Missing 'on' does not cause error...

Posted: Wed Apr 16, 2014 4:32 pm
by [-hh]
..........

Re: Missing 'on' does not cause error...

Posted: Thu Apr 17, 2014 5:55 am
by Simon
dunbarx wrote:It is so great to have Hermann back!!!
I think we have to call him Sheriff Hermann now...

Look at that shiny new badge!

Simon :)

Re: Missing 'on' does not cause error...

Posted: Thu Apr 17, 2014 8:00 am
by [-hh]
..........

Re: Missing 'on' does not cause error...

Posted: Thu Apr 17, 2014 8:23 am
by Simon
(at least I'm engraved in the LOB-forever-startup-splash)
Hey I'm there as well!!!
Wow didn't know that :)

Simon

Re: Missing 'on' does not cause error...

Posted: Thu Apr 17, 2014 11:00 am
by Thierry
[-hh] wrote: TWO.
At BOTTOM of a script I do things as Thierry describes now (and then use item -1 instead of item 1 and so on). It's better to do this at bottom, if you have, as I do, also other not recommended things in scripts, see next remark.

And never ever do (at work) what I described in remark three ...
Hermann,Simon,

Sorry to disturb your bagde conversation :)

This morning I push a bit more the idea of storing some data connected to a script in the script!

Re-reading my yesterday's code, I feel today that it's a bit dangerous and even if it works well today,
we never know which LC version will break the game (as it's not an official feature).
So, that said, I've slightly changed the code above and did comment my data,
e.g a whole button script:

Code: Select all

local AScript, CP

/*** CP=tdzAScript
tell application "BBLivecode"
   edit script "_scriptAlias_"    infos "_BBEtitle_"     type _type_   autosave  "_autoSave_" 
end tell
*/

on mouseUp
   local scriptFile, BBEtitle
   
   if AScript is  empty then put the tdzAScript of me into AScript
   put  "/Users/x/Desktop/xyz.lcscript" into scriptFile
   put the script of me into url  ("file:" & scriptFile)
   put "button sunnyLCBBproperties" into BBEtitle
   
   replace "_scriptAlias_" with scriptFile in AScript
   replace "_BBEtitle_" with BBEtitle in AScript
   replace "_type_" with "Stack" in AScript
   replace "_autoSave_" with true in AScript
   do AScript as applescript
end mouseUp

on mouseDown
   if the environment is "development" then  
      if matchText( the script of me, \
            "(?msi)^\s*/\*+\s+CP=([^\n]+)\n(.+?)\n\s*\*/", \
            CP, AScript) then  set the CP of me to AScript
   end if
end mouseDown
So now, if in development, I scan the commented part of the script,
split it in a custom property name and in a var containing the data (my applescript in this case)
and set the property which will be called in a standalone application only.

Still some room to improve things but the idea is there,
and I believe it's quite solid by now,
and it's a real pleasure to read an applescript/regex/whatever without any extra backslash or quotes..

Regards,

Thierry