Skip to content

Commit e7d27bf

Browse files
committed
first commit
select parser
1 parent a8f2aa2 commit e7d27bf

File tree

9 files changed

+11183
-0
lines changed

9 files changed

+11183
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# lql-parser
2+
23
parser for my database project

Diff for: grammar/grammar.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Generated automatically by nearley, version 2.20.1
2+
// https://github.com/Hardmath123/nearley
3+
(function () {
4+
function id(x) { return x[0]; }
5+
var grammar = {
6+
Lexer: undefined,
7+
ParserRules: [
8+
{"name": "select_statement", "symbols": ["kw_select", {"literal":" "}, "select_what", {"literal":" "}, "kw_from", {"literal":" "}, "table_name"]},
9+
{"name": "select_what", "symbols": [{"literal":"*"}]},
10+
{"name": "select_what", "symbols": [{"literal":"["}, "select_what_option", {"literal":"]"}]},
11+
{"name": "select_what", "symbols": ["column_name"]},
12+
{"name": "select_what_option", "symbols": ["selection_array"]},
13+
{"name": "select_what_option", "symbols": []},
14+
{"name": "selection_array", "symbols": ["table_name"]},
15+
{"name": "selection_array$string$1", "symbols": [{"literal":","}, {"literal":" "}], "postprocess": function joiner(d) {return d.join('');}},
16+
{"name": "selection_array", "symbols": ["selection_array", "selection_array$string$1", "table_name"]},
17+
{"name": "table_name", "symbols": ["string"]},
18+
{"name": "column_name", "symbols": ["string"]},
19+
{"name": "string$ebnf$1", "symbols": [/[a-zA-Z0-9]/]},
20+
{"name": "string$ebnf$1", "symbols": ["string$ebnf$1", /[a-zA-Z0-9]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
21+
{"name": "string", "symbols": ["string$ebnf$1"]},
22+
{"name": "kw_from$string$1", "symbols": [{"literal":"f"}, {"literal":"r"}, {"literal":"o"}, {"literal":"m"}], "postprocess": function joiner(d) {return d.join('');}},
23+
{"name": "kw_from", "symbols": ["kw_from$string$1"]},
24+
{"name": "kw_from$string$2", "symbols": [{"literal":"F"}, {"literal":"R"}, {"literal":"O"}, {"literal":"M"}], "postprocess": function joiner(d) {return d.join('');}},
25+
{"name": "kw_from", "symbols": ["kw_from$string$2"]},
26+
{"name": "kw_select$string$1", "symbols": [{"literal":"S"}, {"literal":"E"}, {"literal":"L"}, {"literal":"E"}, {"literal":"C"}, {"literal":"T"}], "postprocess": function joiner(d) {return d.join('');}},
27+
{"name": "kw_select", "symbols": ["kw_select$string$1"]},
28+
{"name": "kw_select$string$2", "symbols": [{"literal":"s"}, {"literal":"e"}, {"literal":"l"}, {"literal":"e"}, {"literal":"c"}, {"literal":"t"}], "postprocess": function joiner(d) {return d.join('');}},
29+
{"name": "kw_select", "symbols": ["kw_select$string$2"]},
30+
{"name": "statement", "symbols": ["select_statement"]}
31+
]
32+
, ParserStart: "statement"
33+
}
34+
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
35+
module.exports = grammar;
36+
} else {
37+
window.grammar = grammar;
38+
}
39+
})();

Diff for: grammar/grammar.ne

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@include "select.ne"
2+
3+
statement -> select_statement

Diff for: grammar/select.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Generated automatically by nearley, version 2.20.1
2+
// https://github.com/Hardmath123/nearley
3+
(function () {
4+
function id(x) { return x[0]; }
5+
var grammar = {
6+
Lexer: undefined,
7+
ParserRules: [
8+
{"name": "select_statement", "symbols": ["kw_select", {"literal":" "}, "select_what", {"literal":" "}, "kw_from", {"literal":" "}, "table_name"]},
9+
{"name": "select_what", "symbols": [{"literal":"*"}]},
10+
{"name": "select_what", "symbols": [{"literal":"["}, "select_what_option", {"literal":"]"}]},
11+
{"name": "select_what", "symbols": ["column_name"]},
12+
{"name": "select_what_option", "symbols": ["selection_array"]},
13+
{"name": "select_what_option", "symbols": []},
14+
{"name": "selection_array", "symbols": ["table_name"]},
15+
{"name": "selection_array$string$1", "symbols": [{"literal":","}, {"literal":" "}], "postprocess": function joiner(d) {return d.join('');}},
16+
{"name": "selection_array", "symbols": ["selection_array", "selection_array$string$1", "table_name"]},
17+
{"name": "table_name", "symbols": ["string"], "postprocess": d => string},
18+
{"name": "column_name", "symbols": ["string"], "postprocess": d => string},
19+
{"name": "string$ebnf$1", "symbols": [/[a-zA-Z0-9]/]},
20+
{"name": "string$ebnf$1", "symbols": ["string$ebnf$1", /[a-zA-Z0-9]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
21+
{"name": "string", "symbols": ["string$ebnf$1"]},
22+
{"name": "kw_from$subexpression$1", "symbols": [/[fF]/, /[rR]/, /[oO]/, /[mM]/], "postprocess": function(d) {return d.join(""); }},
23+
{"name": "kw_from", "symbols": ["kw_from$subexpression$1"]},
24+
{"name": "kw_select$subexpression$1", "symbols": [/[sS]/, /[eE]/, /[lL]/, /[eE]/, /[cC]/, /[tT]/], "postprocess": function(d) {return d.join(""); }},
25+
{"name": "kw_select", "symbols": ["kw_select$subexpression$1"]}
26+
]
27+
, ParserStart: "select_statement"
28+
}
29+
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
30+
module.exports = grammar;
31+
} else {
32+
window.grammar = grammar;
33+
}
34+
})();

Diff for: grammar/select.ne

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
select_statement -> kw_select " " select_what " " kw_from " " table_name
2+
3+
select_what -> "*"
4+
select_what -> "[" select_what_option "]"
5+
select_what -> column_name
6+
7+
select_what_option -> selection_array
8+
select_what_option -> null
9+
10+
selection_array -> table_name
11+
selection_array -> selection_array ", " table_name
12+
13+
14+
15+
table_name -> string {% d => string %}
16+
17+
column_name -> string {% d => string %}
18+
19+
string -> [a-zA-Z0-9]:+
20+
21+
22+
kw_from -> "from"i
23+
24+
kw_select -> "select"i

Diff for: index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const nearley = require("nearley");
2+
const grammar = require("./grammar/grammar.js");
3+
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
4+
5+
const parseInput = function (input) {
6+
try {
7+
parser.feed(input);
8+
9+
return parser;
10+
} catch (error) {
11+
console.log(error);
12+
13+
return error;
14+
}
15+
};
16+
17+
module.exports = {
18+
parseInput,
19+
};

0 commit comments

Comments
 (0)