fix rules and literal
This commit is contained in:
parent
5e8c6bef71
commit
7f8ea12ffb
175
example/lexer.js
175
example/lexer.js
@ -1,4 +1,4 @@
|
||||
/* Generated by re2c 1.0.3 on Tue Jan 29 10:00:10 2019 */
|
||||
/* Generated by re2c 1.0.3 on Wed Jan 30 13:09:24 2019 */
|
||||
var types = [
|
||||
"SLASH",
|
||||
"LSB",
|
||||
@ -22,6 +22,7 @@ var types = [
|
||||
"NOT",
|
||||
"ADDRESS",
|
||||
"OID",
|
||||
"OID_LITERAL",
|
||||
"TIME",
|
||||
"TIMEDIFF",
|
||||
"INTEGER_LITERAL",
|
||||
@ -75,6 +76,26 @@ Lexer.prototype = {
|
||||
|
||||
console.log(print_f("Found unknown symbol on position: %s", this._yy_cursor));
|
||||
},
|
||||
_oidUnexpectedSymbol: function() {
|
||||
this._error = true;
|
||||
this._last_found_lexeme = {
|
||||
error: 3,
|
||||
start: this._yy_lex_start,
|
||||
end: this._yy_cursor
|
||||
};
|
||||
|
||||
console.log(print_f("Found unknown symbol in Oid on position: %s", this._yy_cursor));
|
||||
},
|
||||
_oidNotFoundCloseBracket: function() {
|
||||
this._error = true;
|
||||
this._last_found_lexeme = {
|
||||
error: 4,
|
||||
start: this._yy_lex_start,
|
||||
end: this._yy_cursor
|
||||
};
|
||||
|
||||
console.log(print_f("Not found close bracket for Oid"));
|
||||
},
|
||||
_foundLexeme: function(_lexeme) {
|
||||
console.log(print_f("found lex: %s; start: %s; end: %s; result => %s", _lexeme, this._yy_lex_start, this._yy_cursor, this._string.substring(this._yy_lex_start, this._yy_cursor)));
|
||||
this._last_found_lexeme = {
|
||||
@ -85,6 +106,18 @@ Lexer.prototype = {
|
||||
end: this._yy_cursor
|
||||
};
|
||||
},
|
||||
_foundOidLexeme: function(_lexeme, _lsb, _rsb) {
|
||||
console.log(print_f("found lex: %s; start: %s; end: %s; result => %s", _lexeme, this._yy_lex_start, this._yy_cursor, this._string.substring(this._yy_lex_start, this._yy_cursor)));
|
||||
this._last_found_lexeme = {
|
||||
error: 0,
|
||||
lexeme: _lexeme,
|
||||
value: this._string.substring(this._yy_lex_start, this._yy_cursor),
|
||||
start: this._yy_lex_start,
|
||||
end: this._yy_cursor,
|
||||
lsb: _lsb,
|
||||
rsb: _rsb
|
||||
};
|
||||
},
|
||||
_endOfString: function() {
|
||||
console.log(print_f("search end\n"));
|
||||
this._end = true;
|
||||
@ -131,6 +164,124 @@ Lexer.prototype = {
|
||||
|
||||
this._notFoundCloseQuote();
|
||||
},
|
||||
_searchOid: function() {
|
||||
var lsb, rsb;
|
||||
var state = 0;
|
||||
while (this._yy_cursor < this._string.length) {
|
||||
switch (state) {
|
||||
case 0:
|
||||
this._yy_char = this._string[this._yy_cursor];
|
||||
(function() {
|
||||
switch (this._yy_char) {
|
||||
case " ":
|
||||
state = 0;
|
||||
this._yy_cursor++;
|
||||
break;
|
||||
case "[":
|
||||
lsb = {
|
||||
start: this._yy_cursor,
|
||||
end: this._yy_cursor + 1
|
||||
};
|
||||
state = 1;
|
||||
break;
|
||||
default:
|
||||
state = 5;
|
||||
}
|
||||
}.bind(this))();
|
||||
break;
|
||||
case 1:
|
||||
this._yy_char = this._string[++this._yy_cursor];
|
||||
(function() {
|
||||
switch (this._yy_char) {
|
||||
case " ":
|
||||
state = 1;
|
||||
break;
|
||||
case "0":
|
||||
case "1":
|
||||
case "2":
|
||||
case "3":
|
||||
case "4":
|
||||
case "5":
|
||||
case "6":
|
||||
case "7":
|
||||
case "8":
|
||||
case "9":
|
||||
state = 2;
|
||||
break;
|
||||
case "]":
|
||||
state = 4;
|
||||
break;
|
||||
default:
|
||||
state = 5;
|
||||
}
|
||||
}.bind(this))();
|
||||
break;
|
||||
case 2:
|
||||
this._yy_char = this._string[++this._yy_cursor];
|
||||
(function() {
|
||||
switch (this._yy_char) {
|
||||
case " ":
|
||||
case "0":
|
||||
case "1":
|
||||
case "2":
|
||||
case "3":
|
||||
case "4":
|
||||
case "5":
|
||||
case "6":
|
||||
case "7":
|
||||
case "8":
|
||||
case "9":
|
||||
state = 2;
|
||||
break;
|
||||
case ".":
|
||||
state = 3;
|
||||
break;
|
||||
case "]":
|
||||
state = 4;
|
||||
break;
|
||||
default:
|
||||
state = 5;
|
||||
}
|
||||
}.bind(this))();
|
||||
break;
|
||||
case 3:
|
||||
this._yy_char = this._string[++this._yy_cursor];
|
||||
(function() {
|
||||
switch (this._yy_char) {
|
||||
case " ":
|
||||
case "0":
|
||||
case "1":
|
||||
case "2":
|
||||
case "3":
|
||||
case "4":
|
||||
case "5":
|
||||
case "6":
|
||||
case "7":
|
||||
case "8":
|
||||
case "9":
|
||||
state = 2;
|
||||
break;
|
||||
default:
|
||||
state = 5;
|
||||
}
|
||||
}.bind(this))();
|
||||
break;
|
||||
case 4:
|
||||
rsb = {
|
||||
start: this._yy_cursor,
|
||||
end: this._yy_cursor + 1
|
||||
};
|
||||
++this._yy_cursor;
|
||||
this._foundOidLexeme("OID_LITERAL", lsb, rsb);
|
||||
return;
|
||||
case 5:
|
||||
this._oidUnexpectedSymbol();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this._oidNotFoundCloseBracket();
|
||||
},
|
||||
_set_next: function() {
|
||||
this._yy_accept = 0;
|
||||
this._state = 1;
|
||||
@ -151,6 +302,8 @@ Lexer.prototype = {
|
||||
|
||||
while (true) {
|
||||
switch (this._state) {
|
||||
|
||||
|
||||
case 1:
|
||||
this._yy_char = this._string[this._yy_cursor];
|
||||
(function() {
|
||||
@ -1347,8 +1500,8 @@ Lexer.prototype = {
|
||||
break;
|
||||
case 84:
|
||||
{
|
||||
this._foundLexeme("OID");this._set_next();
|
||||
return;
|
||||
this._state = 100000001;
|
||||
break;
|
||||
}
|
||||
case 85:
|
||||
this._yy_char = this._string[++this._yy_cursor];
|
||||
@ -1995,17 +2148,27 @@ Lexer.prototype = {
|
||||
this._foundLexeme("TIMEDIFF");this._set_next();
|
||||
return;
|
||||
}
|
||||
|
||||
case 100000000:
|
||||
{
|
||||
this._searchString();this._set_next();
|
||||
this._searchString();
|
||||
this._set_next();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
case 100000001:
|
||||
this._searchOid();
|
||||
this._set_next();
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var print_f = function() {
|
||||
var r_str = "";
|
||||
var next = arguments[0];
|
||||
|
@ -346,48 +346,16 @@ literal(A) ::= address_literal(B) . {
|
||||
A = B;
|
||||
}
|
||||
|
||||
oid_literal_content(A) ::= id(B) . {
|
||||
A = new tokens.oid_literal_content({
|
||||
children: [B]
|
||||
});
|
||||
}
|
||||
|
||||
oid_literal_content(A) ::= oid_literal_content(B) DOT id(C) . {
|
||||
B.add(C);
|
||||
A = B;
|
||||
}
|
||||
|
||||
oid_literal_content_or_empty(A) ::= oid_literal_content(B) . {
|
||||
A = B;
|
||||
}
|
||||
|
||||
oid_literal_content_or_empty(A) ::= . {
|
||||
A = new tokens.oid_literal_content({
|
||||
children: []
|
||||
});
|
||||
}
|
||||
|
||||
oid_literal(A) ::= OID(B) LSB(C) oid_literal_content_or_empty(D) RSB(E) . {
|
||||
oid_literal(A) ::= OID_LITERAL(B) . {
|
||||
A = new tokens.oid_literal({
|
||||
children: D.children,
|
||||
keyword: new tokens.LEXEME({
|
||||
type: B.lexeme,
|
||||
value: B.value,
|
||||
start: B.start,
|
||||
end: B.end
|
||||
}),
|
||||
LSB: new tokens.LEXEME({
|
||||
type: C.lexeme,
|
||||
value: C.value,
|
||||
start: C.start,
|
||||
end: C.end
|
||||
}),
|
||||
RSB: new tokens.LEXEME({
|
||||
type: E.lexeme,
|
||||
value: E.value,
|
||||
start: E.start,
|
||||
end: E.end
|
||||
})
|
||||
LSB: B.lsb,
|
||||
RSB: B.rsb
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -501,14 +501,6 @@ var tokens = (function () {
|
||||
}
|
||||
});
|
||||
|
||||
var oid_literal_content = std.class([Rule], {
|
||||
constructor: function oid_literal_content(_options) {
|
||||
var base = tools.merge({}, _options);
|
||||
|
||||
Rule.call(this, base);
|
||||
}
|
||||
});
|
||||
|
||||
var oid_literal = std.class([Rule], {
|
||||
constructor: function oid_literal(_options) {
|
||||
var base = tools.merge({
|
||||
@ -524,9 +516,11 @@ var tokens = (function () {
|
||||
this.RSB = base.RSB;
|
||||
},
|
||||
position: function () {
|
||||
var first_child = this.children[0];
|
||||
|
||||
return {
|
||||
start: this.keyword.start,
|
||||
end: this.RSB.end
|
||||
start: first_child.start,
|
||||
end: first_child.end
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -629,8 +623,7 @@ var tokens = (function () {
|
||||
|
||||
// expr: expr,
|
||||
sub_expr: sub_expr,
|
||||
address_literal_content: address_literal_content,
|
||||
oid_literal_content: oid_literal_content,
|
||||
address_literal_content: address_literal_content
|
||||
}
|
||||
|
||||
})();
|
@ -21,23 +21,60 @@
|
||||
var token;
|
||||
var lexemes = [];
|
||||
while (token = lexer.next()) {
|
||||
if(_result.error) {
|
||||
return { success: false }
|
||||
}
|
||||
|
||||
if (token.error === 0) {
|
||||
switch(token.error){
|
||||
case 0:
|
||||
console.log("PARSE", token.lexeme);
|
||||
parser.parse(parser["TOKEN_" + token.lexeme], token);
|
||||
lexemes.push(token);
|
||||
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 }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -346,48 +346,16 @@ literal(A) ::= address_literal(B) . {
|
||||
A = B;
|
||||
}
|
||||
|
||||
oid_literal_content(A) ::= id(B) . {
|
||||
A = new tokens.oid_literal_content({
|
||||
children: [B]
|
||||
});
|
||||
}
|
||||
|
||||
oid_literal_content(A) ::= oid_literal_content(B) DOT id(C) . {
|
||||
B.add(C);
|
||||
A = B;
|
||||
}
|
||||
|
||||
oid_literal_content_or_empty(A) ::= oid_literal_content(B) . {
|
||||
A = B;
|
||||
}
|
||||
|
||||
oid_literal_content_or_empty(A) ::= . {
|
||||
A = new tokens.oid_literal_content({
|
||||
children: []
|
||||
});
|
||||
}
|
||||
|
||||
oid_literal(A) ::= OID(B) LSB(C) oid_literal_content_or_empty(D) RSB(E) . {
|
||||
oid_literal(A) ::= OID_LITERAL(B) . {
|
||||
A = new tokens.oid_literal({
|
||||
children: D.children,
|
||||
keyword: new tokens.LEXEME({
|
||||
type: B.lexeme,
|
||||
value: B.value,
|
||||
start: B.start,
|
||||
end: B.end
|
||||
}),
|
||||
LSB: new tokens.LEXEME({
|
||||
type: C.lexeme,
|
||||
value: C.value,
|
||||
start: C.start,
|
||||
end: C.end
|
||||
}),
|
||||
RSB: new tokens.LEXEME({
|
||||
type: E.lexeme,
|
||||
value: E.value,
|
||||
start: E.start,
|
||||
end: E.end
|
||||
})
|
||||
LSB: B.lsb,
|
||||
RSB: B.rsb
|
||||
});
|
||||
}
|
||||
|
||||
|
3
test.js
3
test.js
@ -47,7 +47,7 @@ var test = function() {
|
||||
var test_not = LemonJS('not cab == false').tree;
|
||||
fs.writeFileSync("./tests/test_not.json", JSON.stringify(test_not, true, 3));
|
||||
|
||||
var test_oid = LemonJS('abc == Oid [a.b.d]').tree;
|
||||
var test_oid = LemonJS('abc == Oid[1.2.3] and abd == Oid [ 1.2.3 ]').tree;
|
||||
fs.writeFileSync("./tests/test_oid.json", JSON.stringify(test_oid, true, 3));
|
||||
|
||||
var test_timediff = LemonJS('add == TimeDiff [17924 15:01:24 441000]').tree;
|
||||
@ -56,7 +56,6 @@ var test = function() {
|
||||
var test_time = LemonJS('add == Time [29/12/2019 15:01:24 441000]').tree;
|
||||
fs.writeFileSync("./tests/test_time.json", JSON.stringify(test_time, true, 3));
|
||||
|
||||
|
||||
var test_exp_1 = LemonJS('(add == Time [29/12/2019 15:01:24 441000]) and ddds == "sdfasdf" or a == 123 and v == 155').tree;
|
||||
fs.writeFileSync("./tests/test_exp_1.json", JSON.stringify(test_exp_1, true, 3));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user