I am writing my language in Pegjs and as usual, my language has some keywords, like true
, false
, if
, else
and today
for instance. Now, I want to declare a variable, but apparently, the variable name cannot be one of the reserved keywords. It can be any alpha followed by an alpha-numeric, with the exception of the language keywords.
I did the following (testable in Pegjs Online):
variable = c:(alpha alphanum*)
{
var keywords = ["true", "false", "if", "else", "today"];
var res = c[0]
for (var i = 0; i<c[1].length; i++) {
res=res+c[1][i]
}
if(keywords.indexOf(res)>=0) {
return error('\'' + res + '\'' + ' is a keyword and cannot be used as a variable name.');
}
return { 'dataType' : 'variable', 'dataValue' : res };
}
alpha = [a-zA-Z]
alphanum = [a-zA-Z0-9_]
boolean = v: ("true" / "false")
{
return { 'dataType' : 'boolean', 'dataValue': v};
}
Now true
is illegal, but true1
is not. This is fine. However, since I have defined the boolean
structure somewhere else in my language, is it not possible to re-use that definition instead of manually re-defining the non-allowed keywords inside my variable
definition?
You can imagine why my solution is error-prone. I tried several things but they did not work.
Thanks for your help!