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 = [
|
var types = [
|
||||||
"SLASH",
|
"SLASH",
|
||||||
"LSB",
|
"LSB",
|
||||||
@ -22,6 +22,7 @@ var types = [
|
|||||||
"NOT",
|
"NOT",
|
||||||
"ADDRESS",
|
"ADDRESS",
|
||||||
"OID",
|
"OID",
|
||||||
|
"OID_LITERAL",
|
||||||
"TIME",
|
"TIME",
|
||||||
"TIMEDIFF",
|
"TIMEDIFF",
|
||||||
"INTEGER_LITERAL",
|
"INTEGER_LITERAL",
|
||||||
@ -75,6 +76,26 @@ Lexer.prototype = {
|
|||||||
|
|
||||||
console.log(print_f("Found unknown symbol on position: %s", this._yy_cursor));
|
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) {
|
_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)));
|
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 = {
|
this._last_found_lexeme = {
|
||||||
@ -85,6 +106,18 @@ Lexer.prototype = {
|
|||||||
end: this._yy_cursor
|
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() {
|
_endOfString: function() {
|
||||||
console.log(print_f("search end\n"));
|
console.log(print_f("search end\n"));
|
||||||
this._end = true;
|
this._end = true;
|
||||||
@ -131,6 +164,124 @@ Lexer.prototype = {
|
|||||||
|
|
||||||
this._notFoundCloseQuote();
|
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() {
|
_set_next: function() {
|
||||||
this._yy_accept = 0;
|
this._yy_accept = 0;
|
||||||
this._state = 1;
|
this._state = 1;
|
||||||
@ -151,6 +302,8 @@ Lexer.prototype = {
|
|||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
switch (this._state) {
|
switch (this._state) {
|
||||||
|
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
this._yy_char = this._string[this._yy_cursor];
|
this._yy_char = this._string[this._yy_cursor];
|
||||||
(function() {
|
(function() {
|
||||||
@ -1347,8 +1500,8 @@ Lexer.prototype = {
|
|||||||
break;
|
break;
|
||||||
case 84:
|
case 84:
|
||||||
{
|
{
|
||||||
this._foundLexeme("OID");this._set_next();
|
this._state = 100000001;
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
case 85:
|
case 85:
|
||||||
this._yy_char = this._string[++this._yy_cursor];
|
this._yy_char = this._string[++this._yy_cursor];
|
||||||
@ -1995,17 +2148,27 @@ Lexer.prototype = {
|
|||||||
this._foundLexeme("TIMEDIFF");this._set_next();
|
this._foundLexeme("TIMEDIFF");this._set_next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 100000000:
|
case 100000000:
|
||||||
{
|
this._searchString();
|
||||||
this._searchString();this._set_next();
|
this._set_next();
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
|
case 100000001:
|
||||||
|
this._searchOid();
|
||||||
|
this._set_next();
|
||||||
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var print_f = function() {
|
var print_f = function() {
|
||||||
var r_str = "";
|
var r_str = "";
|
||||||
var next = arguments[0];
|
var next = arguments[0];
|
||||||
|
@ -346,48 +346,16 @@ literal(A) ::= address_literal(B) . {
|
|||||||
A = B;
|
A = B;
|
||||||
}
|
}
|
||||||
|
|
||||||
oid_literal_content(A) ::= id(B) . {
|
oid_literal(A) ::= OID_LITERAL(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) . {
|
|
||||||
A = new tokens.oid_literal({
|
A = new tokens.oid_literal({
|
||||||
children: D.children,
|
|
||||||
keyword: new tokens.LEXEME({
|
keyword: new tokens.LEXEME({
|
||||||
type: B.lexeme,
|
type: B.lexeme,
|
||||||
value: B.value,
|
value: B.value,
|
||||||
start: B.start,
|
start: B.start,
|
||||||
end: B.end
|
end: B.end
|
||||||
}),
|
}),
|
||||||
LSB: new tokens.LEXEME({
|
LSB: B.lsb,
|
||||||
type: C.lexeme,
|
RSB: B.rsb
|
||||||
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
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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], {
|
var oid_literal = std.class([Rule], {
|
||||||
constructor: function oid_literal(_options) {
|
constructor: function oid_literal(_options) {
|
||||||
var base = tools.merge({
|
var base = tools.merge({
|
||||||
@ -524,9 +516,11 @@ var tokens = (function () {
|
|||||||
this.RSB = base.RSB;
|
this.RSB = base.RSB;
|
||||||
},
|
},
|
||||||
position: function () {
|
position: function () {
|
||||||
|
var first_child = this.children[0];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
start: this.keyword.start,
|
start: first_child.start,
|
||||||
end: this.RSB.end
|
end: first_child.end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -629,8 +623,7 @@ var tokens = (function () {
|
|||||||
|
|
||||||
// expr: expr,
|
// expr: expr,
|
||||||
sub_expr: sub_expr,
|
sub_expr: sub_expr,
|
||||||
address_literal_content: address_literal_content,
|
address_literal_content: address_literal_content
|
||||||
oid_literal_content: oid_literal_content,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})();
|
})();
|
@ -21,23 +21,60 @@
|
|||||||
var token;
|
var token;
|
||||||
var lexemes = [];
|
var lexemes = [];
|
||||||
while (token = lexer.next()) {
|
while (token = lexer.next()) {
|
||||||
if(_result.error) {
|
switch(token.error){
|
||||||
return { success: false }
|
case 0:
|
||||||
}
|
|
||||||
|
|
||||||
if (token.error === 0) {
|
|
||||||
console.log("PARSE", token.lexeme);
|
console.log("PARSE", token.lexeme);
|
||||||
parser.parse(parser["TOKEN_" + token.lexeme], token);
|
parser.parse(parser["TOKEN_" + token.lexeme], token);
|
||||||
lexemes.push(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();
|
parser.parse();
|
||||||
|
if (_result.root_node !== undefined) {
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
tree: _result.root_node,
|
tree: _result.root_node,
|
||||||
lexemes: lexemes
|
lexemes: lexemes
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
return { success: false }
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -346,48 +346,16 @@ literal(A) ::= address_literal(B) . {
|
|||||||
A = B;
|
A = B;
|
||||||
}
|
}
|
||||||
|
|
||||||
oid_literal_content(A) ::= id(B) . {
|
oid_literal(A) ::= OID_LITERAL(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) . {
|
|
||||||
A = new tokens.oid_literal({
|
A = new tokens.oid_literal({
|
||||||
children: D.children,
|
|
||||||
keyword: new tokens.LEXEME({
|
keyword: new tokens.LEXEME({
|
||||||
type: B.lexeme,
|
type: B.lexeme,
|
||||||
value: B.value,
|
value: B.value,
|
||||||
start: B.start,
|
start: B.start,
|
||||||
end: B.end
|
end: B.end
|
||||||
}),
|
}),
|
||||||
LSB: new tokens.LEXEME({
|
LSB: B.lsb,
|
||||||
type: C.lexeme,
|
RSB: B.rsb
|
||||||
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
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
test.js
3
test.js
@ -47,7 +47,7 @@ var test = function() {
|
|||||||
var test_not = LemonJS('not cab == false').tree;
|
var test_not = LemonJS('not cab == false').tree;
|
||||||
fs.writeFileSync("./tests/test_not.json", JSON.stringify(test_not, true, 3));
|
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));
|
fs.writeFileSync("./tests/test_oid.json", JSON.stringify(test_oid, true, 3));
|
||||||
|
|
||||||
var test_timediff = LemonJS('add == TimeDiff [17924 15:01:24 441000]').tree;
|
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;
|
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));
|
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;
|
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));
|
fs.writeFileSync("./tests/test_exp_1.json", JSON.stringify(test_exp_1, true, 3));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user