I am currently developing an application in Javascript in which I will allow the users to add "rules" to the application by the use of a small self-declared programming language. In order to achieve this functionality, I need to be able to parse strings and extract the necessary information. Here are some examples of my language:
Example 1:
SET backgroundColor
MODULE someModule
TO red
WHEN someVariable == 1
Example 2:
SET textSize
TO someObject.size
WHEN 5 + 5 == 10
Example 3:
REMOVE backgroundColor
MODULE someModule
ID 5
Note that although I am making use of newlines in my examples, these rules can also just be formatted to one long string without any newlines.
As you can observe, it is an SQL-like language in which I make use of capitalized keywords. There are several combinations of keywords possible, just like SQL, but it is definetly not a huge language. After each keyword, the user can just write any simple Javascript expressions. This is important. I know one should usually write a parser, but in this case I do not think it is appropriate to reinvent the wheel and write a parser that can parse Javascript. Especially because, apart from these Javascript expressions, the language is rather simple/limited, it would be ideal if there is a more simple approach to tackling this problem.
I have already implemented functions that will take the necessary information as their parameters and add the rule to my system. What is left is to fill the gap. How do I efficiently verify a valid syntax and extract all the required information from the string I receive such that I can fill in a function like this:
addRuleToTheSystem('backgroundColor', 'someModule', 'red', 'someVariable == 1')