LemonJS/parser_template/parser.y

94 lines
2.5 KiB
Plaintext

%name Parser
%token_prefix TOKEN_
%left OR.
%left AND.
%right NOT.
%include {
// include something
}
%code {
var _result = {
error: false
};
var LemonJS = function(_input) {
_result = Object.create(null);
var parser = new Parser();
var lexer = new Lexer(_input);
var token;
var lexemes = [];
while (token = lexer.next()) {
switch(token.error){
case 0:
console.log("PARSE", token.lexeme);
parser.parse(parser["TOKEN_" + token.lexeme], token);
lexemes.push(token);
break;
case -2:
// end of search
// do nothing
break;
case -1:
// empty string or not found any lexemes
// do nothing
break;
case 1:
return {
success: false,
message: "Found unknown symbol on position",
error: 1,
token: token
};
case 2:
return {
success: false,
message: "Not found close quote",
error: 2,
token: token
};
case 3:
return {
success: false,
message: "Unexpected symbol in oid structure",
error: 3,
token: token
};
case 4:
return {
success: false,
message: "Not found close bracket for Oid",
error: 3,
token: token
};
}
if(_result.error) {
return {
success: false,
message: "Syntax error",
error: 0
}
}
}
parser.parse();
if (_result.root_node !== undefined) {
return {
success: true,
tree: _result.root_node,
lexemes: lexemes
};
} else {
return { success: false }
}
};
}
%syntax_error {
_result.error = "true";
console.log("Syntax error");
}
&&REPLACER{rules.y}&&