Page 1 of 1
Syntax check on the fly
Posted: Wed Apr 06, 2016 2:03 pm
by byrones15
My app allows the user to enter LiveCode code in order to control some of the screen elements and objects that are exposed. I have searched but can't find a way to validate the code before executing it. Is there a hook into the syntax checker that would allow me to determine and let the user know if they have entered invalid code? I often write the code snippets for them, but the users are typically engineers with programming expertise and they like to try writing code themselves occasionally.
Re: Syntax check on the fly
Posted: Wed Apr 06, 2016 2:21 pm
by Klaus
Hi byrones15,
1. welcome to the forum!
2. Not sure, but you could TRY to do the user entered script, depends on how you execute it?
Something like this:
Code: Select all
...
put fld "user script" into tScript
try
do tScript
## If an error occurs, this part of the handerl wil be executed!
## So you could catch any errors caused by invalid syntax.
catch errornum
answer "No valid synstax!"
end try
...
Best
Klaus
Re: Syntax check on the fly
Posted: Wed Apr 06, 2016 3:26 pm
by byrones15
Klaus,
Thanks for the idea! Unfortunately, the DO command does not execute or evaluate all of the code. For example, if I have a syntax error in the else section of an if/then/else statement and the if conditional evaluates as true the else section's code is not executed and a syntax error is not generated.
This partially solves the problem but I will keep looking. Also, I don't want to go down the path of stripping out all of the if, else, and end if statements from the script (that could get ugly). I guess I could run the script multiple times - once with the statements and once without. But I would also have to do the same with loops, etc. and it seems like a bottomless pit.
Byron
Re: Syntax check on the fly
Posted: Wed Apr 06, 2016 4:57 pm
by Klaus
How do you execute the user entered script(s) then?
Unfortunately I do not know of a way to "pre-compile" that text in a standalone as the script editor in the IDE does.
Re: Syntax check on the fly
Posted: Wed Apr 06, 2016 9:54 pm
by byrones15
I execute the script with the "do" command. I have been doing this for a couple years. It provides great flexibility for users to customize the system without me having to write custom apps. I just need to document the internal objects and screen controls.
The issue is that the engineer writes the script and does SOME testing and then end-users run the app and scripts on a daily basis. If there is a syntax error that happens to the end-user that wasn't caught by the engineer input data could be lost

. And the engineer then has to fix the problem and re-publish the script. It is trial and error process because they don't have access to the LiveCode IDE. It would be a lot easier to catch any syntax errors up front while they are writing the script. It isn't a showstopper, but it would improve the system.
What I would really like is to be able to submit a chunk of code to the syntax engine and have it tell me if it is valid. That would be a great enhancement to the system.
Thank you Klaus for your feedback and help.
Re: Syntax check on the fly
Posted: Wed Apr 06, 2016 10:18 pm
by FourthWorld
byrones15 wrote:The issue is that the engineer writes the script and does SOME testing and then end-users run the app and scripts on a daily basis. If there is a syntax error that happens to the end-user that wasn't caught by the engineer input data could be lost

. And the engineer then has to fix the problem and re-publish the script. It is trial and error process because they don't have access to the LiveCode IDE.
Why not? If they're coding in LC having the Dictionary and a script editor dedicated to the task would seem welcome, no? Coordination with your app could be done via a plugin.
Re: Syntax check on the fly
Posted: Thu Apr 07, 2016 3:22 am
by Thierry
byrones15 wrote:
What I would really like is to be able to submit a chunk of code to the syntax engine and have it tell me if it is valid. That would be a great enhancement to the system.
Ok, if I understand correctly,
this should make you happy:
Code: Select all
on mouseUp
local textScript
-- your user script
put fld "Fcode" into textScript
-- make this button invisible or off screen
set the script of button "B4try" to textScript
if the result is not empty then
-- syntax errors..
answer "Error in your script!" & cr& the result
else
-- should be fine (compil)
answer "Done!"
end if
end mouseUp
HTH,
Thierry
Re: Syntax check on the fly
Posted: Thu Apr 07, 2016 3:15 pm
by Klaus
Code: Select all
...
set the script of button "B4try" to textScript
if the result is not empty then
...
Ah, clever idea!

Re: Syntax check on the fly
Posted: Fri Apr 08, 2016 2:29 pm
by byrones15
Sweet! You guys rock! Thanks so much.