I have generated a parser using JISON
:
%lex
%x TEXT
%%
("Project"|"project") {return 'PROJECTCOMMAND';}
"-au" {return 'ADDUSER';}
"-n" {this.begin('TEXT'); return 'NAMEOPTION';}
"-k" {return 'KEYOPTION';}
"-desc" {return 'DESCRIPTION';}
("--add"|"-a") {return 'ADDOPTION';}
<TEXT>[-a-zA-Z0-9@\.]+ {this.popState(); return 'TEXT';}
<INITIAL,TEXT>\s+ // Ignore white space...
/lex
%%
line :
PROJECTCOMMAND ADDUSER
{
//Project Command of add user
var res = new Object();
res.value = "addUser Project";
return res;
}
| PROJECTCOMMAND ADDOPTION
{
//Project Command with no arguments
var res = new Object();
res.value = "addProject";
return res;
}
| PROJECTCOMMAND ADDOPTION NAMEOPTION TEXT
{
//Project command with project name as argument
var res = new Object();
res.value = "addProject name";
res.name = $4;
return res;
}
Above works fine If I give commands like:
project -a
project -au
project -a -n abc
...
But gives error If I type in a command like this:
project -a abcd
It throws an error.
Same way If I give a command as
project -a -n
Error:
Expecting 'TEXT' got `1'
One way to fix this was to write all possible error cases, but that would be never end because as commands increase possible error cases also increase, Is there anyway I can tell parser that If it does not satisfy any of above commands then throw a common error?
Thanks in Advance
I gess,
project
isPROJECTCOMMAND
,-a
isADDOPTION
,abcd
isNAMEOPTION
, and parser is aspected fourth node –TEXT
. You have alternatives for one, two and four nodes, but not for three.