How to validate a pattern in lua - lua

I'm currently creating a search function in lua which basically just goes through a list of items and processes the items that match the input string in a specific way.
I use string.find(sourceString, inputString) to identify the items.
The function is called whenever a user types something into a text field so if he tries to enter a pattern it happens that when using sets or captures the function is called when the search string just contains a [ or a ( without the closing equivalent which of cause throws an error.
The best way to go around this problem I think is to validate the input as beeing a valid pattern, but I've got no idea how to do this. Lua itself doesn't seem to contain a method for this and I am a bit confused of how to check it in a more or less performant way myself. Thanks for your help and ideas in advance :)

You should wrap the call to string.find with pcall to capture the error.
local status, result = pcall(string.find, sourceString, inputString)
if not status then
-- bad pattern logic, error message is in result
else
-- good pattern logic, result contains the start index
end

See this for pattern escape function (taken from somewhere in Lua Users wiki, I think). You may convert it to the validation function if you need.

Related

lua attempt to call field 'createUDPSocket' (a nil value)

I am writing a LUA script, when I run the program, this result as below:
socket = net.createUDPSocket()
attempt to call field 'createUDPSocket' (a nil value)
I have been searching for many website, there seems to no much details about that. Could anyone help?
That simply means that the createUDPSocket function does not exist. Without seeing your code, that is the most anyone here is going to be able to help you with.

Can you prevent short-circuit evaluation in Lua?

I am working on a script that allows user entry of Lua (specifically, boolean conditionals), and I am trying to provide a feature that will perform a quick error check for the script that is entered.
It's easy enough to throw an error if loadstring() fails, so syntax issues are readily handled.
However, if variables are improperly entered, it is harder to detect a problem.
Let's say the user enters the following:
value.custom_value_1 == 1 and valse.custom_value_2 ~= 1
As far as the syntax goes, loadstring() is quite satisfied -- it adds 'return ' as a prefix to that string.
My next step is to check for errors when we execute what was entered by the user. The problem is, when value.custom_value_1 ~= 1, the logic will short-circuit since we're dealing with and - there's no point in going further, we already have false on the left-hand side.
What I would like is to find a way to cause Lua to completely evaluate the expression, rather than short-circuiting. If it did so, the spelling error valse would be caught (regardless of value.custom_value_1's value) and I can provide an error message to the end user.
Any thoughts?
My current best idea is to filter through the user input, sorting out every value entered (basically, whatever's not an a conditional or, and, ...) and just doing it manually. But I thought it's worth checking to see if there's a better way.
Preventing short-circuit evaluation is not something you would want to / could do. That's just the way it works.
What you could do instead is something like:
if(logic1 and logic2)
{
// code on success
}
else
// check what kind of error we have
{
if(~logic1)
{
// throw error type 1
}
if(~logic2)
{
// throw error type 2
}
}
If you don't mind parsing the string up-front you could use a library like https://github.com/stravant/LuaMinify/blob/master/ParseLua.lua to parse the source into an AST, modify the short-circuiting notes into function calls like
function my_and(x, y)
return x and y
end
Then you compile this AST to check for the errors.
Seems a lot hassle though just to catch trivial errors.
I'm not sure if anyone's answer was accurate, but the literal question was 'can you prevent short-circuit evals' and I was not able to find a way to do that.
I ultimately chopped up the user input and tested each component individually, gathering those errors and feeding them back to the user. Seems okay for now.

Some questions about building a network-accessible, multi-user, programmable, interactive environment

Introduction
I've been attempting to build this project for many weeks now, and trying multiple solutions that I can't get my head around. Let me describe the project a little. It's a text-based server, that players can login to (via telnet or a client), essentially like a MUD. They can then create and interact with 'objects', giving them 'verbs' and 'properties'.
The server is basically just a database of 'objects', each object has an ID, a name, a location (which is another object), a list of its contents (objects) and some other flags. Objects can have 'verbs' and 'properties'. Properties are just stored data (string, int, float, w/e). Verbs are methods/functions. Objects are interacted with using commands such as "put something in container". An old version of the server already exists, it's called LambdaMOO. I'm attempting to re-create it since it hasn't been updated in a very, very long time.
You can read more in-depth about how objects, verbs and properties should work at: http://bit.ly/17XIqjY
An Example
Let me describe what I'd like. Imagine we have an object. Object #256, it's called "Button". It has the property "count" along with all the default properties that are inherited from it's parent (i.e. 'description'). It has one "verb" on it, called "push". This verb contains this code:
this.count += 1;
this.description = "This button has been pushed " + this.count + " times.";
player.tell("You press the button and feel a chill run down your spine.");
When the player types 'push button' on the server, the 'push' verb will run and output
You press the button and feel a chill run down your spine.
If you then look at the button, you'll see it's updated description.
Note that player in the above script refers the object of the player executing the verb. tell is another verb, on the player object. However the tell verb has a flag saying it is executable from other verbs.
What language?
My main question is what languages can I use for the 'verbs'? I've tried using node.js and the 'vm' library. I've tried using C# to parse C#. I've tried using C# to parse JavaScript. The issue I keep getting is that I have no way of controlling the permissions of the verbs and properties. If I translate them to literal functions in JavaScript, I can't determine which object they are running on and what permissions it should have. If a user calls a function on another users object, I have no way of intercepting that call and stopping it if the permissions aren't correct. I'm not entirely fussed as to which language is used for the verb code it just needs to be "sandboxed". Properties need to be only readable/writeable when they are set to be so by the user, same with verbs. I imagine I could use a language with overloading (like PHP's __get, __set, __call).
I need to also be able to inject these variables into the verb: (mostly determined from the command typed, unless the verb is being called from another verb)
player (object) the player who typed the command
this (object) the object on which this verb was found
caller (object) this will be the same as ‘player’, unless another
verb calls the command in which case it is the object
containing that verb.
verb (string) the first word of the command
argstr (string) everything after the first word of the command
args (list of strings) a list of the words in ‘argstr’
dobjstr (string) the direct object string found during parsing
dobj (object) the direct object value found during matching
prepstr (string) the prepositional phrase found during parsing
iobjstr (string) the indirect object string
iobj (object) the indirect object value
I also need to be able to access any object from any other object (so long as the permissions work out).
// Object #128. Verb: multiply Prep: this none this Perms: +r +x
return (args[0] * args[1]);
// Object #256. Verb: square Prep: this none this Perms: +r +x
return #128:multiply(args[0], args[0]);
// Object #512. Verb: touch Prep: any any this Perms: +r
// Has a property (int) 'size' on it.
this.size = #256:square(this.size);
this.description = "It's a large button, it spans " + this.size + " metres.";
player:tell("You touch the button, it gets bigger.");
The user could then push button and the button object's size property would be squared.
Recommended Reading
I highly recommend you to read the document at http://bit.ly/17XIqjY for a more in-depth idea of how the system should work.
It is also recommended you read the following documents, as μMOO is based upon LambdaMOO and it’s methodology:
https://en.wikipedia.org/wiki/LambdaMOO
https://en.wikipedia.org/wiki/MOO
http://www.hayseed.net/MOO/manuals/ProgrammersManual_toc.html
http://www.moo.mud.org/
I take this question as asking for a language that could do what you need. That's what I'll try to answer.
First, this task is hopelessly unsuited to any mainstream or imperative language such as C# or Java. I wouldn't even think about it. Javascript is possible, but not what it's good at and nothing specific to recommend it.
Second, if you had the right skills, it would be an excellent opportunity to design an entirely new language and spend the next year or two getting it working. People really do that, but I don't recommend it unless you like that kind of masochistic experience. [I do.]
So my recommendation is that you widen your language experience until you find a match. Of the languages I know moderately well, Ruby is the best to try first. As soon as you said inject these variables into the verb you made me think of Ruby, because lots of Ruby software (including Rails) is built exactly like that. Forget Python, Perl and Javascript: I really don't think they will hack it.
Beyond Ruby you might contemplate Lua. I haven't used it much recently, and it may not suit, but it is widely used as a games scripting language.
Beyond that are the true functional languages. There is the most ancient of them all: Lisp. You can do absolutely anything in Lisp, including implementing the language you were looking for in the first place. Then there are Scala and Haskell, to name just two. They are mind-bending to learn, but well suited to the kind of problem you have.
Not much of an answer because it basically says: learn each of these languages in turn until you find one that works for you. [Happy to help further if I can. I have fond memories of Moo.]

How to return untranslated keys

For an API, I want to return the actual keyified string.
So:
User.errors.messages[:name]
#=> activerecord.errors.models.user.attrributes.blank
Instead of
Can't be blank
I know I can override this by creating an actual translation, or by setting custom errors in the validates methods in my Models, but I was wondering if there is a lower level, simpler way to make rails return the "keyified" string instead of parsing it through the translation-layers.
I answered some similar questions on SO, but could not find them right now...
I think that this is not possible right now because the ActiveModel::Errors::add method does not store the Key to the message, but just the derived message.
It's also not trivial to reverse get the key from translation files or the like.
I think it would be a valuable addition to rails to actually store the key of the error-message instead of just the message itself.

In transaction NACE, is it possible to find out the print program and form routine, if i know the name of the Smartform?

I want to find out the name of the Print Program, and the name of the subroutine (form) that prints my SmartForm.
Now luckily in NACE (Conditions for Output Control), one can "kinna" figure out the application and the output type by oneself, and then the print program is obviously written there.
So what about if one wouldn't know what application and Output Type, and would have to check all applicatios and output types manually? isn't there an easier way to do this?
The table TNAPR contains some print program -> script links, search for your smartform name in field SFORM. This is by no means an exhaustive list, however.
OR
Do a "where used" search for "SSF_FUNCTION_MODULE_NAME", which will give you a list of all smartform calling programs. You'd still need to go through it manually, but you might find a suitably matching program description or similar.
If you need to find out the actual printing program for a certain form and it is possible to get a print preview, you can always enter the debugger using /h and examine the call stack.

Resources