Detecting if a script line has a line continuation character

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 194
Joined: Thu Apr 18, 2013 2:48 pm

Detecting if a script line has a line continuation character

Post by livecodeali » Wed Jul 13, 2016 9:45 am

I'm wondering if anyone has any clever ideas on how to check for a given line of script whether the parser will treat the following line as a continuation.

Currently the script editor uses

Code: Select all

function lineIsContinued pLine
   return word -1 of pLine is "\"
end lineIsContinued
which will work in a large number of cases. However, the following bugs are examples where it doesn't:

http://quality.livecode.com/show_bug.cgi?id=8228 (because the \ is followed by a comment)
http://quality.livecode.com/show_bug.cgi?id=8328 (because the last word of

Code: Select all

(" something ") \
is

Code: Select all

") \
Moreover it will not work in the following case

Code: Select all

put "a" &\
"b" into tVar
The latter two examples would be fixed using

Code: Select all

char -1 of word -1 of pLine
, but I'm wondering if there's any way that covers all cases.

The char method has issues with this:

Code: Select all

put "a" into tVar --don't continue this line! \
put "b" into tVar2

This is the best I can come up with:

Code: Select all

function lineIsContinued pLine
   local tTokens, tFirstChar
   put token 1 to -1 of pLine into tTokens
   put offset(tTokens, pLine) into tFirstChar
   put "get a" into char tFirstChar to tFirstChar + length(tTokens) of pLine
   put return & "& b" after pLine
   compileCheck pLine
   return the result
end lineIsContinued

on compileCheck pScript
   local tCompileTest, tCompileResult
   --checks to see if the message compiles
   lock messages
   create script only stack "compilationTest"
   put "on compileTest" & cr & pScript & cr & "end compileTest" into tCompileTest
   set the script of stack "compilationTest" to tCompileTest
   put the result into tCompileResult
   delete stack "compilationTest"
   unlock messages
   return tCompileResult is not empty
end compileCheck
Obviously this is rather heavyweight, and would fail in the case that the actual script occurred in comments:

Code: Select all

/* put "a" into */ put "a" into /* put "a" into */ /
tVar
That example seems to be immune to any further tweaks to finding the offset of the tokens in the original line. Any ideas?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10350
Joined: Wed May 06, 2009 2:28 pm

Re: Detecting if a script line has a line continuation chara

Post by dunbarx » Wed Jul 13, 2016 3:00 pm

Hi.

I get no errors or issues with your first few examples, all the way up to "this is the best I can come up with...". This is in v6.11.

And this:

Code: Select all

/* put "a" into */ put "a" into /* put "a" into */ /
tVar
really has nothing to do with the line continuation char.

Craig Newman

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Detecting if a script line has a line continuation chara

Post by [-hh] » Wed Jul 13, 2016 5:47 pm

My idea was wrong, sorry. It's too late in the evening.
Last edited by [-hh] on Wed Jul 13, 2016 6:26 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10350
Joined: Wed May 06, 2009 2:28 pm

Re: Detecting if a script line has a line continuation chara

Post by dunbarx » Wed Jul 13, 2016 6:17 pm

Ali.

I did not see it was you, and thought the post was about errors in using that character.

I have no idea what Hermann was upset about.

Anyway, couldn't you still use your "char -1 of word -1..." (I see why you have to check the last word) and also check to see if "--" is in pLine?

The line

Code: Select all

put "a" &\
contains three words if you simply analyze it as a string, and the last word in any line of line continued code will always have "\" as the last char, or else be improperly formed.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Detecting if a script line has a line continuation chara

Post by [-hh] » Wed Jul 13, 2016 6:31 pm

Hi Craig. Wasn't upset but had a wrong idea which fails if there are quoted comment chars in the line

Code: Select all

put "#//--" \
into b
shiftLock happens

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Detecting if a script line has a line continuation chara

Post by [-hh] » Wed Jul 13, 2016 8:13 pm

If comments after a continuation char "\", for example
  put "A" into a \ -- just an example
are NOT allowed then one could try the following function:

Code: Select all

function lineIsContinued pLine
  replace "\" with ">"&numToChar(1) in pLine
  return (token -1 of pLine is numToChar(1))
end lineIsContinued
At least all examples above are correctly handled by that.
shiftLock happens

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 194
Joined: Thu Apr 18, 2013 2:48 pm

Re: Detecting if a script line has a line continuation chara

Post by livecodeali » Thu Jul 14, 2016 10:48 am

dunbarx wrote: And this:

Code: Select all

/* put "a" into */ put "a" into /* put "a" into */ /
tVar
really has nothing to do with the line continuation char.
Oops, put a forward slash there instead of a backslash :-) Should have been:

Code: Select all

/* put "a" into */ put "a" into /* put "a" into */ \
tVar
[-hh] wrote:If comments after a continuation char "\", for example
put "A" into a \ -- just an example
are NOT allowed then one could try the following function:

Code: Select all

function lineIsContinued pLine
  replace "\" with ">"&numToChar(1) in pLine
  return (token -1 of pLine is numToChar(1))
end lineIsContinued
At least all examples above are correctly handled by that.
Comments after a continuation char are unfortunately allowed, and it would be breaking backwards compatibility to change that. However I believe the gist of your example could be the most practical solution. It would even work for the other example I just thought of,

Code: Select all

put "\"

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Detecting if a script line has a line continuation chara

Post by Thierry » Thu Jul 14, 2016 11:22 am

Hi Ali,

Spending half an hour on this topic,
I came up with this one:

Code: Select all

function lineIsContinued pLine
   return  matchText( pLine, Rx )
end lineIsContinued
The datas I used for testing are valid LC statements which compile fine.

Code: Select all

on totest
   put a \ 
         into b
   put a \ -- comment 
   into b
   put a & ("xxx")\
   into b
   put b into c
   put "a" &\
   "b" into t
   put a into t -- comment \ xxxx
   put a into t -- comment \ 
         get t
   /* put "a" into */ put "a" into /* put "a" into */  \
         t
   put aaaa /* -- what about this one? */ into \
         t
   put b  -- what about this one? */ into \
         get the date
   put ccc /* -- and this one?  into \
         t
   */
   put " -- not a comment" \
         into c
   
   put " \ -- not a comment"  into c
end totest
Applying a small test loop over these lines, here are my results:
( true means it's a continuedLine, false otherwise )

Code: Select all

true	   put a \ 
true	   put a \ -- comment 
true	   put a & ("xxx")\
false	   put b into c
true	   put "a" &\
false	   put a into t -- comment \ xxxx
false	   put a into t -- comment \ 
true	   /* put "a" into */ put "a" into /* put "a" into */  \
true	   put aaaa /* -- what about this one? */ into \
false	   put b  -- what about this one? */ into \
false	   put ccc /* -- and this one?  into \
true	   put " -- not a comment" \
false	   put " \ -- not a comment"  into c
Let me know if this suits you and if I missed any cases...

Kind regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Detecting if a script line has a line continuation chara

Post by [-hh] » Thu Jul 14, 2016 12:09 pm

Hi Thierry.
Thierry wrote:

Code: Select all

return  matchText( pLine, Rx )
What's Rx?

Hermann

@Ali.
If comments are allowed after "\" then my function returns correctly true.

Code: Select all

put "\#//--\" \ --> returns true
I thought it should return false then.
shiftLock happens

Post Reply