Skip to content

Commit 5612dd0

Browse files
committed
implemented lexer in minifier, added test
1 parent 29f6e9b commit 5612dd0

5 files changed

Lines changed: 40 additions & 40 deletions

File tree

jSQL.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ function jSQLCreateQuery(){
10691069
function jSQLLexer(input) {
10701070
this.input = input;
10711071
this.pos = 0;
1072+
this.real_pos = 0;
10721073
this.tokens = [];
10731074
this.token_matches = [];
10741075
}
@@ -1097,28 +1098,25 @@ jSQLLexer.prototype.getTokens = function(){
10971098
var r = matches[type_id][match_index];
10981099
if(r.index !== this.pos) continue;
10991100
var token = new jSQLToken(this.pos, r[0], type_id);
1100-
if(throwaway.indexOf(token.type) === -1) this.tokens.push(token);
11011101
this.pos += token.length;
1102-
type_id=0;
1102+
if(throwaway.indexOf(token.type) === -1) this.tokens.push(token);
1103+
type_id=-1;
11031104
break;
11041105
}
11051106
}
1106-
if(this.pos !== this.input.length)
1107-
return _throw(new jSQL_Lexer_Error(this.pos, this.input));
1108-
1107+
if(this.pos !== this.input.length){
1108+
var pos;
1109+
if(this.tokens.length){
1110+
var lastToken = this.tokens[this.tokens.length-1];
1111+
pos = lastToken.input_pos + lastToken.length;
1112+
}else pos = 0;
1113+
return _throw(new jSQL_Lexer_Error(pos, this.input));
1114+
}
11091115
return this.tokens;
11101116
};
11111117

11121118
jSQLLexer.token_types = [
11131119

1114-
// WHITESPACE
1115-
{pattern: /[\n\r]/g,
1116-
type: 'WHITESPACE',
1117-
name: "LINEBREAK"},
1118-
{pattern: /[ \t]/g,
1119-
type: 'WHITESPACE',
1120-
name: "WHITESPACE"},
1121-
11221120
// STRINGs
11231121
{pattern: /"(?:[^"\\]|\\.)*"/g,
11241122
type: 'STRING',
@@ -1135,6 +1133,14 @@ jSQLLexer.token_types = [
11351133
type: 'COMMENT',
11361134
name: "MULTI LINE COMMENT"},
11371135

1136+
// WHITESPACE
1137+
{pattern: /\r?\n|\r/g,
1138+
type: 'WHITESPACE',
1139+
name: "LINEBREAK"},
1140+
{pattern: /[ \t]/g,
1141+
type: 'WHITESPACE',
1142+
name: "WHITESPACE"},
1143+
11381144
// NUMBERs
11391145
{pattern: /\d+/g,
11401146
type: 'NUMBER',

jSQL.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"example": "tests"
88
},
99
"scripts": {
10-
"test": "node ./tests/test1.js && node ./tests/test2.js && node ./tests/test3.js && node ./tests/test4.js && node ./tests/test5.js && node ./tests/test6.js"
10+
"test": "node ./tests/test1.js && node ./tests/test2.js && node ./tests/test3.js && node ./tests/test4.js && node ./tests/test5.js && node ./tests/test6.js && node ./tests/test7.js"
1111
},
1212
"repository": {
1313
"type": "git",

src/lexer/jSQLLexer.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
function jSQLLexer(input) {
44
this.input = input;
55
this.pos = 0;
6+
this.real_pos = 0;
67
this.tokens = [];
78
this.token_matches = [];
89
}
@@ -31,28 +32,25 @@ jSQLLexer.prototype.getTokens = function(){
3132
var r = matches[type_id][match_index];
3233
if(r.index !== this.pos) continue;
3334
var token = new jSQLToken(this.pos, r[0], type_id);
34-
if(throwaway.indexOf(token.type) === -1) this.tokens.push(token);
3535
this.pos += token.length;
36-
type_id=0;
36+
if(throwaway.indexOf(token.type) === -1) this.tokens.push(token);
37+
type_id=-1;
3738
break;
3839
}
3940
}
40-
if(this.pos !== this.input.length)
41-
return _throw(new jSQL_Lexer_Error(this.pos, this.input));
42-
41+
if(this.pos !== this.input.length){
42+
var pos;
43+
if(this.tokens.length){
44+
var lastToken = this.tokens[this.tokens.length-1];
45+
pos = lastToken.input_pos + lastToken.length;
46+
}else pos = 0;
47+
return _throw(new jSQL_Lexer_Error(pos, this.input));
48+
}
4349
return this.tokens;
4450
};
4551

4652
jSQLLexer.token_types = [
4753

48-
// WHITESPACE
49-
{pattern: /[\n\r]/g,
50-
type: 'WHITESPACE',
51-
name: "LINEBREAK"},
52-
{pattern: /[ \t]/g,
53-
type: 'WHITESPACE',
54-
name: "WHITESPACE"},
55-
5654
// STRINGs
5755
{pattern: /"(?:[^"\\]|\\.)*"/g,
5856
type: 'STRING',
@@ -69,6 +67,14 @@ jSQLLexer.token_types = [
6967
type: 'COMMENT',
7068
name: "MULTI LINE COMMENT"},
7169

70+
// WHITESPACE
71+
{pattern: /\r?\n|\r/g,
72+
type: 'WHITESPACE',
73+
name: "LINEBREAK"},
74+
{pattern: /[ \t]/g,
75+
type: 'WHITESPACE',
76+
name: "WHITESPACE"},
77+
7278
// NUMBERs
7379
{pattern: /\d+/g,
7480
type: 'NUMBER',

tests/test7.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,5 @@ jSQL.load(function(){
9090
}
9191
}
9292

93-
console.log(jSQL.minify("\
94-
\
95-
insert into `typetest` \
96-
values (\
97-
11, '{\
98-
\"greeting\":\"hello\"\
99-
}', 'function(){\
100-
console.log(\"hello world\")\
101-
}', true, 3, '72', 'rob', \
102-
'Mon, 25 Dec 1995 13:30:00 +0430', \
103-
'scooby doo', \"hello\")"));
104-
10593
console.log("\n\nsuccess");
10694
});

0 commit comments

Comments
 (0)