Continue running a script after finding an error - lua

One of the most annoying things about errors is that one simple syntax error will kill all of my program. For example, if I do this:
require 'moduleWithSyntaxError' --Has a syntax error
require 'fullyFunctioningModule' --No syntax errors
foo = faultyClass.new() --Has syntax error inside the class definition
bar = normalClass.new() --No syntax errors
Then if the program finds a syntax error in the faulty module, it quits, and if it finds a syntax error in the faulty class, it quits. This brings my to my question, is there any way I can detect whether there was a syntax error, and use that information to not call faultyClass.new(), in a similar syntax to exceptions? I'm looking for something like this (yes this is very similar to C++ exceptions):
try()
require 'moduleWithSyntaxError'
catch (exception)
print (exception.what())
end

Short and simple answer: pcall
It's exception handling in Lua.

If you are truly talking about syntax errors, then simply running luac on Lua each file in your project will tell you if you have errors. (You could automate that.)
You wouldn't need exception handling to detect syntax errors unless you don't control the source code for the modules that the project runs.

Related

flex yy_fatal_error exist just like that. I want handler back to application

flex yy_fatal_error exist just like that. But I want handler back to my application. How to avoid exist call? from yy_fatal_error. whether this problem addressed in any version? your suggestion is highly appreciated. help me on this issues.
You can override the function, by #defineing your own. Note that in the generated code there is
/* Report a fatal error. */
#ifndef YY_FATAL_ERROR
#define YY_FATAL_ERROR(msg) yy_fatal_error( msg )
#endif
If you #define the macro YY_FATAL_ERROR(msg) to call your own function, the lexer will call that function rather than the one from the template.
However, the lexer template is written to assume that this function does not return. You can make it do that by using setjmp and longjmp to prepare a predictable place to return in your application and jumping back (from your own yy_fatal_error function) to that when a "fatal" error is used.
vi like emacs does this for instance, because it uses lexers for syntax highlighting. If a fatal error is generated by the lexer, you would not want the editor to stop.
Here are a few links discussing setjmp and longjmp:
Practical usage of setjmp and longjmp in C
setjmp and longjmp - understanding with examples

Get error information if luaL_loadfile fails

If luaL_load function fails, then, according to the documentation (Lua5.1), the error message is pushed on the stack and can be retrieved with lua_tostring(L, -1), but if I want to print a customized error message, I don't think I can use lua_Debug (because that is for active function). Is there any other way (other than parsing the string) to get the file, line number and what error occured ?
--
Thanks.
The error code returned by luaL_loadfile tells you what kind of error happened. You're mostly likely only interested in LUA_ERRSYNTAX, in which case there is a detailed error message left on the stack. This is the only record of the error. If you need to reformat it, you need to parse it.
During a luaL_load(), there is nothing techinally on the stack that is relavant to the loading of the script. No functions are performed or executed in the script. Its just a compilation step, returning a function that encapsulates the whole file or the error found during compilation.
If you get a function back, you execute this to actually run the script, which sounds like what you are really interested in. In which case you can use a lua_pcall() and provide an error handler. In your handler you would then have your expected stack trace available via lua_Debug.

New to Lua, confused by error <eof>

I recently started taking an interest in Lua programming with a Minecraft addon called Computercraft, which involves console-based GUIs to control computers and other things with Lua. However, I seem to be randomly getting an error where the code requires something called an "eof". I have searched multiple manuals and how-tos, and none mention this particular error. In fact, I am having trouble finding anything with an error list. I am fairly new to programming, but have had basic Python experience. Could anyone explain what "eof" is?
An eof is do or then.
Usually eof means you have too many (or not enough) end statements. Paste code maybe?
Suppose I create a file with one too many end statements:
> edit test.lua
for i = 1, 10 do
print(i)
end
end
When I run it, and lua encounters that extra end statement on the last line where there is no code block still open, you'll get an error like:
> test.lua
bios: 337: [string "test.lua"]: 4: '<eof>' expected
(from a quick test in CCDesk pr7.1.1)
Problems with the basic structure of the blocks of lua code show up with bios on the left rather than your file name; the part of bios that loads lua files will usually tell you where it was at in the file when couldn't make sense of the code anymore (like line 4: here). Sometimes it might be a bit of a puzzle to work your way from the scene of the accident back to where things got off track. =)
Using ESPlorer there is a difference between "uploading" a script and "sending" a script. Use "Upload".
This is not the same issue as the one reported, but as searching landed me here...

Exceptions in LEX&YACC

I'm developing a lex/yacc c compiler.
In order to handle failures and parse errors, I want to deploy an exception system handler.
Actually only a "parse error" message is handled whatever the problem is.for example:
typedef struct , struct_name{...} this input will produce a parsing error because of the extra comma.
My purpose is to throw a contextual exception,giving us the possibility to focus exactly where the problem is.such as for this example :
"Invalid structure declaration "
I really need help to solve this problème.
This will go into your parser. As it runs, it gets tokens from the lexer. If the next token does not "fit" the current rule, then you have a problem. Luckily, there already exists a section for dealing with these situations! See bison error recovery for the gnu version of yacc and how to deal with this. It'll go through the concepts, and variables to deal with just the situation you have here.

How to import lua file to execute it?

I'm using Lua Mac 5.1.4 Compiler.
I'm trying to import lua file and run it.
I tried using this code:
% lua hello.lua
But I'm getting this error: stdin:1: unexpected symbol near '%'
Am I doing something wrong? This is my first day using lua so be easy on me.
Thank you.
The problem is probably that you saw this verbatim text in a tutorial:
% lua hello.lua
The '%' at the beginning of the line is not something you are supposed to type into your terminal, but is rather a generic prompt indicator. Sometimes you might see it written as '$' instead:
$ lua hello.lua
In either case, the first character is not something you type, but rather is a typographical convention to suggest that what follows is to be typed at a prompt. Your actual prompt might look something like this:
mo#macbook$
So you would type lua hello.lua but your screen would look like this:
mo#macbook$ lua hello.lua
So, try just entering lua hello.lua and see what happens.
Note that the error message you got regarding stdin:1 is likely from your shell (e.g. bash), and not from Lua (which never even started running due to the malformed command in the shell).
The error stdin:1: unexpected symbol near '%' suggests that you typed in % lua hello.lua while in an interactive lua session (or you executed a script containing it). Now that's something that you should type in in the commandline window.
Instead try something like print'Hello World!'
Lua provides two ways to call a file. One is the loadfile() and the other is the dofile() commands. Try using dofile("hello.lua").That should work.If it doesn't type the absolute path...:)

Resources