re2-js-generator/example/lexme.js

65 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-01-30 11:16:22 +00:00
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 1/30/19.
*/
var Lexer = require("./lexer.js");
// See Allow Lexemes in ./lexer.js on top file
/**
* create Lexer and set string
*
* Here you can found next lexemes:
* - ID
* - EQ
* - STRING_LITERAL
* - AND
* - ID
* - GT
* - INTEGER
* @type {Lexer}
*/
var lexer = new Lexer("id == 'string' and a > 10");
var token;
while(token = lexer.next()){
/**
* token has next fields
* - error: number, // 0 if not error
* - lexeme: string, type of token (ID|EQ|STRING_LITERAL)
* - value: string, value what found between <start> and <end>
* - start: number,
* - end: number
*
* and if lexeme == "OID_LITERAL"
* - lsb - position left square bracket
* - rsb - position right square bracket
*/
switch (token.error) {
case 0:
console.log("TOKEN:", token.lexeme, token.value);
break;
case -2:
console.log("end of search");
break;
case -1:
console.log("Not found any lexemes or errors or anything else");
break;
case 1:
console.log("Found unknown symbol on position");
break;
case 2:
console.log("Not found close quote");
break;
case 3:
console.log("Unexpected symbol in oid structure");
break;
case 4:
console.log("Not found close bracket for Oid");
break;
}
}