work example

This commit is contained in:
Aleksey Chichenkov 2019-01-29 14:04:41 +03:00
parent f815210203
commit e114afb9d9
14 changed files with 95 additions and 1202 deletions

2046
example/lexer.js Normal file

File diff suppressed because it is too large Load diff

51
example/parser.y Normal file
View file

@ -0,0 +1,51 @@
%name Parser
%token_prefix TOKEN_
%left OR.
%left AND.
%right NOT.
%include {
// include something
}
%code {
&&REPLACER{example/test_code_environment.js}&&
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()) {
if(_result.error) {
return { success: false }
}
if (token.error === 0) {
console.log("PARSE", token.lexeme);
parser.parse(parser["TOKEN_" + token.lexeme], token);
lexemes.push(token);
}
}
parser.parse();
return {
success: true,
tree: _result.root_node,
lexemes: lexemes
};
};
}
%syntax_error {
_result.error = "true";
console.log("Syntax error");
}
&&REPLACER{example/rules.y}&&

462
example/rules.y Normal file
View file

@ -0,0 +1,462 @@
main ::= expr(A) . {
_result.root_node = A
}
main ::= literal(A) . {
_result.root_node = A
}
integer_literal(A) ::= INTEGER_LITERAL(B) . {
A = new tokens.integer_literal({
children: [
new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
})
]
});
}
literal(A) ::= integer_literal(B) . {
A = B;
}
float_literal(A) ::= FLOAT_LITERAL(B) . {
A = new tokens.float_literal({
children: [
new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
})
]
})
}
literal(A) ::= float_literal(B) . {
A = B;
}
bool_literal(A) ::= BOOL_LITERAL(B) . {
A = new tokens.bool_literal({
children: [
new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
})
]
})
}
literal(A) ::= bool_literal(B) . {
A = B;
}
string_literal(A) ::= STRING_LITERAL(B) . {
A = new tokens.string_literal({
children: [
new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
})
]
});
}
literal(A) ::= string_literal(B) . {
A = B;
}
id(A) ::= string_literal(B) . {
A = new tokens.id({
children: [B]
});
}
id(A) ::= ID(B) . {
A = new tokens.id({
children: [
new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
})
]
});
}
and(A) ::= expr(B) AND(C) expr(D) . {
A = new tokens.and({
lexpr: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
rexpr: D
})
}
or(A) ::= expr(B) OR(C) expr(D) . {
A = new tokens.or({
lexpr: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
rexpr: D
})
}
not(A) ::= NOT(C) expr(D) . {
A = new tokens.not({
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
rexpr: D
})
}
eq(A) ::= id(B) EQ(C) literal(D) . {
A = new tokens.eq({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
neq(A) ::= id(B) NEQ(C) literal(D) . {
A = new tokens.neq({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
gt(A) ::= id(B) GT(C) literal(D) . {
A = new tokens.gt({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
gte(A) ::= id(B) GTE(C) literal(D) . {
A = new tokens.gte({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
lt(A) ::= id(B) LT(C) literal(D) . {
A = new tokens.lt({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
lte(A) ::= id(B) LTE(C) literal(D) . {
A = new tokens.lte({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
like(A) ::= id(B) LIKE(C) literal(D) . {
A = new tokens.like({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
nlike(A) ::= id(B) NLIKE(C) literal(D) . {
A = new tokens.nlike({
id: B,
op: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
literal: D
});
}
expr(A) ::= and(B) . {
A = B;
}
expr(A) ::= or(B) . {
A = B;
}
expr(A) ::= not(B) . {
A = B;
}
expr(A) ::= eq(B) . {
A = B;
}
expr(A) ::= neq(B) . {
A = B;
}
expr(A) ::= gt(B) . {
A = B;
}
expr(A) ::= gte(B) . {
A = B;
}
expr(A) ::= lt(B) . {
A = B;
}
expr(A) ::= lte(B) . {
A = B;
}
expr(A) ::= like(B) . {
A = B;
}
expr(A) ::= nlike(B) . {
A = B;
}
expr(A) ::= LCB(B) expr(C) RCB(D) . {
A = new tokens.sub_expr({
LCB: new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
}),
expr: C,
RCB: new tokens.LEXEME({
type: D.lexeme,
value: D.value,
start: D.start,
end: D.end
})
});
}
address_literal_content(A) ::= string_literal(B) . {
A = new tokens.address_literal_content({
children: [B]
});
}
address_literal_content(A) ::= address_literal_content(B) COMMA string_literal(C) . {
B.add(C);
A = B;
}
address_literal_content_or_empty(A) ::= address_literal_content(B) . {
A = B;
}
address_literal_content_or_empty(A) ::= . {
A = new tokens.address_literal_content({
children: []
});
}
address_literal(A) ::= ADDRESS(B) LSB(C) address_literal_content_or_empty(D) RSB(E) . {
A = new tokens.address_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
})
});
}
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) . {
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
})
});
}
literal(A) ::= oid_literal(B) . {
A = B;
}
time_diff_literal(A) ::= TIMEDIFF(KWD) LSB(B) integer_literal(DAYS) integer_literal(HH) COLON integer_literal(MM) COLON integer_literal(SS) integer_literal(MS) RSB(C) . {
A = new tokens.time_diff_literal({
keyword: new tokens.LEXEME({
type: KWD.lexeme,
value: KWD.value,
start: KWD.start,
end: KWD.end
}),
LSB: new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
}),
RSB: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
days: DAYS,
hours: HH,
minutes: MM,
seconds: SS,
microseconds: MS,
});
}
literal(A) ::= time_diff_literal(B) . {
A = B;
}
time_literal(A) ::= TIME(KWD) LSB(B) integer_literal(DAY) SLASH integer_literal(MONTH) SLASH integer_literal(YEAR) integer_literal(HH) COLON integer_literal(MM) COLON integer_literal(SS) integer_literal(MS) RSB(C) . {
A = new tokens.time_literal({
keyword: new tokens.LEXEME({
type: KWD.lexeme,
value: KWD.value,
start: KWD.start,
end: KWD.end
}),
LSB: new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
}),
RSB: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
day: DAY,
month: MONTH,
year: YEAR,
hours: HH,
minutes: MM,
seconds: SS,
microseconds: MS,
});
}
literal(A) ::= time_literal(B) . {
A = B;
}

View file

@ -0,0 +1,636 @@
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 1/28/19.
*/
var fs = require("fs");
var Lexer = require('./lexer.js');
var std = (function () {
var protos = "__protos__";
var keys = "__keys__";
/**
* Return unique data
*
* @param {Object[]} _arr - prototypes of inheritance classes
* @param {Object} _main - prototype of resulting class
*
* @return {Object}
* */
var unique = function (_arr, _main) {
var result = Object.create(null);
var to_remove = [];
for (var i = 0, e = _arr.length; i != e; ++i) {
var item = _arr[i];
for (var key in item) {
if (key in result) {
to_remove.push(key);
continue;
}
result[key] = item[key];
}
if (keys in item) {
for (var ii = 0, ee = item[keys].length; ii != ee; ++ii) {
var key = item[keys][ii];
if (key in result) {
to_remove.push(key);
continue;
}
result[key] = item[key];
}
}
}
for (var i = 0; i != to_remove.length; ++i) {
delete result[to_remove[i]];
}
for (var key in _main) {
result[key] = _main[key];
}
return result;
};
/**
* Create OOP class
*
* @param {Function[]} _constrs - inheritance classes
* @param {Object} _proto - prototype of resulting class
* @param {Object?} _static - static data
*
* @return {Function}
* */
var class_creator = function (_constrs, _proto, _static) {
_constrs = _constrs || [];
_proto = _proto || [];
_static = _static || [];
var constr;
if (_proto && _proto.hasOwnProperty("constructor")) {
constr = _proto.constructor;
delete _proto.constructor;
} else {
constr = function () {
for (var i = 0; i != _constrs.length; ++i) {
_constrs[i].apply(this, arguments);
}
};
}
var proto = Object.create(null);
Object.defineProperty(proto, protos, {
"value": []
});
Object.defineProperty(proto, keys, {
"value": []
});
/************************FOR MEMBERS*******************************/
for (var i = 0, e = _constrs.length; i != e; ++i) {
proto[protos].push(_constrs[i].prototype);
}
var m_un = unique(proto[protos], _proto);
for (var key in m_un) {
proto[keys].push(key);
Object.defineProperty(proto, key, {
"value": m_un[key]
});
}
/************************FOR MEMBERS END***************************/
/************************FOR STATICS*******************************/
var s_un = unique(_constrs, _static);
for (var key in s_un) {
Object.defineProperty(constr, key, {
"value": s_un[key],
"enumerable": true
});
}
/************************FOR STATICS END***************************/
Object.defineProperties(constr, {
"pr": {
"value": proto
},
"prototype": {
"value": proto
}
});
Object.freeze(proto);
Object.freeze(constr);
return constr;
};
/**
* Check if target has prototype
*
* @param {Object} _target - checkable instance
* @param {Object} _proto - posible prototype
*
* */
var check = function (_target, _proto) {
for (var i = 0; i != _target[protos].length; ++i) {
var t_proto = _target[protos][i];
if (t_proto == _proto) {
return true;
}
if (t_proto[protos]) {
if (check(t_proto, _proto))
return true;
}
}
return false;
};
/**
* Check if target is instance of class
*
* @param {Object} _target - checkable instance
* @param {Function} _constr - posible constructor
*
* */
var class_check = function (_target, _constr) {
if (_target instanceof _constr) {
return true;
}
return check(_target, _constr.prototype);
};
return {
class: class_creator,
class_check: class_check
};
})();
var tools = {
merge: function (_obj) {
var target = Object.create(null);
var i = 0, e = arguments.length;
for (; i != e; ++i) {
var options = arguments[i];
for (var key in options) {
if (options[key] === undefined && target === options[key])
continue;
target[key] = options[key];
}
}
return target;
}
};
var tokens = (function () {
var Node = std.class([], {
constructor: function Node(_options) {
var base = tools.merge({
children: []
}, _options);
this.children = base.children;
},
add: function (_n) {
this.children.push(_n);
return this;
}
});
var Lexeme = std.class([Node], {
constructor: function Lexeme(_options) {
var base = tools.merge({
start: -1,
end: -1,
type: null,
value: null
}, _options);
Node.call(this, base);
this.start = base.start;
this.end = base.end;
this.type = base.type;
this.value = base.value;
}
});
var Rule = std.class([Node], {
constructor: function NonTerminal(_options) {
var base = tools.merge({}, _options);
Node.call(this, base);
}
});
var terminal_literal = std.class([Rule], {
constructor: function terminal_literal(_options) {
var base = tools.merge({}, _options);
Rule.call(this, base);
},
position: function () {
var first_child = this.children[0];
return {
start: first_child.start,
end: first_child.end,
}
}
});
var string_literal = std.class([terminal_literal], {
constructor: function string_literal(_options) {
var base = tools.merge({}, _options);
terminal_literal.call(this, base);
}
});
var integer_literal = std.class([terminal_literal], {
constructor: function integer_literal(_options) {
var base = tools.merge({}, _options);
terminal_literal.call(this, base);
}
});
var float_literal = std.class([terminal_literal], {
constructor: function float_literal(_options) {
var base = tools.merge({}, _options);
terminal_literal.call(this, base);
}
});
var bool_literal = std.class([terminal_literal], {
constructor: function bool_literal(_options) {
var base = tools.merge({}, _options);
terminal_literal.call(this, base);
}
});
var id = std.class([Rule], {
constructor: function id(_options) {
var base = tools.merge({}, _options);
Rule.call(this, base);
},
position: function () {
var first_child = this.children[0];
if(std.class_check(first_child, Lexeme)){
return {
start: first_child.start,
end: first_child.end,
}
} else {
return this.position();
}
}
});
var expr_compares = std.class([Rule], {
constructor: function expr_compares(_options) {
var base = tools.merge({
lexpr: null,
op: null,
rexpr: null
}, _options);
Rule.call(this, base);
this.lexpr = base.lexpr;
this.op = base.op;
this.rexpr = base.rexpr;
},
position: function () {
return {
start: this.lexpr.position().start,
end: this.rexpr.position().end,
}
}
});
var and = std.class([Rule], {
constructor: function and(_options) {
var base = tools.merge({}, _options);
expr_compares.call(this, base);
}
});
var or = std.class([Rule], {
constructor: function or(_options) {
var base = tools.merge({}, _options);
expr_compares.call(this, base);
}
});
var not = std.class([Rule], {
constructor: function not(_options) {
var base = tools.merge({
op: null,
rexpr: null
}, _options);
Rule.call(this, base);
this.op = base.op;
this.rexpr = base.rexpr;
},
position: function () {
return {
start: this.op.start,
end: this.rexpr.position().end,
}
}
});
var endpoint_compares = std.class([Rule], {
constructor: function endpoint_compares(_options) {
var base = tools.merge({
id: null,
op: null,
literal: null
}, _options);
Rule.call(this, base);
this.id = base.id;
this.op = base.op;
this.literal = base.literal;
},
position: function () {
return {
start: this.id.start,
end: this.literal.end
}
}
});
var eq = std.class([endpoint_compares], {
constructor: function eq(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
},
});
var neq = std.class([endpoint_compares], {
constructor: function neq(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
},
});
var gt = std.class([endpoint_compares], {
constructor: function gt(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
}
});
var gte = std.class([endpoint_compares], {
constructor: function gte(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
}
});
var lt = std.class([endpoint_compares], {
constructor: function lt(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
}
});
var lte = std.class([endpoint_compares], {
constructor: function lte(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
}
});
var like = std.class([endpoint_compares], {
constructor: function like(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
}
});
var nlike = std.class([endpoint_compares], {
constructor: function nlike(_options) {
var base = tools.merge({}, _options);
endpoint_compares.call(this, base);
}
});
var sub_expr = std.class([Rule], {
constructor: function expr(_options) {
var base = tools.merge({
LCB: null,
expr: null,
RCB: null,
}, _options);
Rule.call(this, base);
this.LCB = base.LCB;
this.expr = base.expr;
this.RCB = base.RCB;
},
position: function () {
return {
start: this.LCB.start,
end: this.RCB.end
}
}
});
var address_literal_content = std.class([Rule], {
constructor: function address_literal_content(_options) {
var base = tools.merge({}, _options);
Rule.call(this, base);
}
});
var address_literal = std.class([Rule], {
constructor: function address_literal(_options) {
var base = tools.merge({
keyword: null,
LSB: null,
RSB: null,
}, _options);
Rule.call(this, base);
this.keyword = base.keyword;
this.LSB = base.LSB;
this.RSB = base.RSB;
},
position: function () {
return {
start: this.keyword.start,
end: this.RSB.end
}
}
});
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({
keyword: null,
LSB: null,
RSB: null,
}, _options);
Rule.call(this, base);
this.keyword = base.keyword;
this.LSB = base.LSB;
this.RSB = base.RSB;
},
position: function () {
return {
start: this.keyword.start,
end: this.RSB.end
}
}
});
var time_diff_literal = std.class([Rule], {
constructor: function time_diff_literal(_options) {
var base = tools.merge({
keyword: null,
LSB: null,
RSB: null,
days: -1,
hours: -1,
minutes: -1,
seconds: -1,
microseconds: -1
}, _options);
Rule.call(this, base);
this.keyword = base.keyword;
this.LSB = base.LSB;
this.RSB = base.RSB;
this.days = base.days;
this.hours = base.hours;
this.minutes = base.minutes;
this.seconds = base.seconds;
this.microseconds = base.microseconds;
},
position: function () {
return {
start: this.keyword.start,
end: this.RSB.end
}
}
});
var time_literal = std.class([Rule], {
constructor: function time_literal(_options) {
var base = tools.merge({
keyword: null,
LSB: null,
RSB: null,
day: -1,
month: -1,
year: -1,
hours: -1,
minutes: -1,
seconds: -1,
microseconds: -1
}, _options);
Rule.call(this, base);
this.keyword = base.keyword;
this.LSB = base.LSB;
this.RSB = base.RSB;
this.day = base.day;
this.month = base.month;
this.year = base.year;
this.hours = base.hours;
this.minutes = base.minutes;
this.seconds = base.seconds;
this.microseconds = base.microseconds;
},
position: function () {
return {
start: this.keyword.start,
end: this.RSB.end
}
}
});
return {
// terminal
LEXEME: Lexeme,
// not terminal
id: id,
string_literal: string_literal,
integer_literal: integer_literal,
float_literal: float_literal,
bool_literal: bool_literal,
address_literal: address_literal,
oid_literal: oid_literal,
time_diff_literal: time_diff_literal,
time_literal: time_literal,
or: or,
and: and,
not: not,
eq: eq,
neq: neq,
gt: gt,
gte: gte,
lt: lt,
lte: lte,
like: like,
nlike: nlike,
// expr: expr,
sub_expr: sub_expr,
address_literal_content: address_literal_content,
oid_literal_content: oid_literal_content,
}
})();