Skip to content

Commit

Permalink
#106 - fix unput usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Dec 27, 2017
1 parent 1f61ddb commit c4479e7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 25 deletions.
22 changes: 11 additions & 11 deletions src/lexer/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ module.exports = {
ch = this.input();
// check if hexa
if (ch === 'x' || ch === 'X') {
this.input();
ch = this.input();
if (this.is_HEX()) {
return this.consume_HNUM();
} else {
this.unput(2);
this.unput(ch ? 2 : 1);
}
} else if (ch === 'b' || ch === 'B') {
ch = this.input();
if (ch === '0' || ch === '1') {
return this.consume_BNUM();
} else {
this.unput(2);
this.unput(ch ? 2 : 1);
}
} else if (!this.is_NUM()) {
this.unput(1);
if (ch) this.unput(1);
}
}

Expand All @@ -56,18 +56,18 @@ module.exports = {
this.consume_LNUM();
return this.tok.T_DNUMBER;
} else {
if (ch) this.unput(3);
this.unput(ch ? 3 : 2);
break;
}
} else if (this.is_NUM()) {
this.consume_LNUM();
return this.tok.T_DNUMBER;
} else {
if (ch) this.unput(2);
this.unput(ch ? 2 : 1);
break;
}
} else {
this.unput(1);
if (ch) this.unput(1);
break;
}
}
Expand All @@ -91,9 +91,9 @@ module.exports = {
// read hexa
consume_HNUM: function() {
while(this.offset < this.size) {
this.input();
var ch = this.input();
if (!this.is_HEX()) {
this.unput(1);
if (ch) this.unput(1);
break;
}
}
Expand All @@ -102,9 +102,9 @@ module.exports = {
// read a generic number
consume_LNUM: function() {
while(this.offset < this.size) {
this.input();
var ch = this.input();
if (!this.is_NUM()) {
this.unput(1);
if (ch) this.unput(1);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/scripting.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = {
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
continue;
}
this.unput(1);
if (ch) this.unput(1);
break;
}
return this.tok.T_WHITESPACE;
Expand Down
18 changes: 9 additions & 9 deletions src/lexer/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ module.exports = {
this.unput(2);
break;
}
this.unput(1);
if (ch) this.unput(1);
} else if (ch == '{') {
ch = this.input();
if (ch == '$') {
this.unput(2);
break;
}
this.unput(1);
if (ch) this.unput(1);
}
}
if (ch == '"') {
Expand Down Expand Up @@ -252,10 +252,10 @@ module.exports = {
} else {
this.unput(2);
}
} else {
this.unput(1);
}
return this.tok.T_VARIABLE;
} else {
if (ch) this.unput(1);
}
return this.tok.T_VARIABLE;
},
// HANDLES BACKQUOTES
matchST_BACKQUOTE: function() {
Expand Down Expand Up @@ -312,7 +312,7 @@ module.exports = {
return next;
}
}
this.unput(1);
continue;
} else if (ch === '{') {
ch = this.input();
if (ch === '$') {
Expand All @@ -327,7 +327,7 @@ module.exports = {
return this.tok.T_CURLY_OPEN;
}
}
this.unput(1);
continue;
}
ch = this.input();
}
Expand Down Expand Up @@ -389,7 +389,7 @@ module.exports = {
return next;
}
}
this.unput(1);
if (ch) this.unput(1);
} else if (ch === '{') {
ch = this.input();
if (ch === '$') {
Expand Down
8 changes: 4 additions & 4 deletions src/lexer/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ module.exports = {
// reads each char of the label
consume_LABEL: function() {
while(this.offset < this.size) {
this.input();
var ch = this.input();
if (!this.is_LABEL()) {
this.unput(1);
if (ch) this.unput(1);
break;
}
}
Expand All @@ -65,9 +65,9 @@ module.exports = {
// consume all whitespaces (excluding newlines)
consume_TABSPACE: function() {
while(this.offset < this.size) {
this.input();
var ch = this.input();
if (!this.is_TABSPACE()) {
this.unput(1);
if (ch) this.unput(1);
break;
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/lexerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@ describe('Test lexer', function() {
tokens[2][2].should.be.exactly(2);
tokens[3][2].should.be.exactly(2);
});
it('test unput on whitespace', function() {
var tokens = parser.tokenGetAll('<?php \r\n\t ');
tokens.length.should.be.exactly(2);
tokens[1][1].should.be.exactly('\r\n\t ');
});
it('test consume_VARIABLE unput', function() {
// @todo
});
});
65 changes: 65 additions & 0 deletions test/stringTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,71 @@ describe('Test strings', function() {
it('...', function() {
var ast = parser.parseEval('echo B"\\colors[1] contains >$colors[1]<\\n";');
});
it('check infinite on $', function() {
var ast = parser.parseEval('echo "$', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast.children[0].arguments[0].value[0].kind.should.be.exactly('string');
ast.children[0].arguments[0].value[0].value.should.be.exactly('$');

// @todo ...
ast = parser.parseEval('echo `$', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast = parser.parseEval('echo ` -> $', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});

});
it('check infinite on {', function() {
var ast = parser.parseEval('echo "{', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast.children[0].arguments[0].value[0].kind.should.be.exactly('string');
ast.children[0].arguments[0].value[0].value.should.be.exactly('{');
ast = parser.parseEval('echo `{', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast = parser.parseEval('echo ` -> {', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
});
it('check infinite on ${', function() {
var ast = parser.parseEval('echo "${', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast.children[0].arguments[0].value[0].kind.should.be.exactly('variable');
ast = parser.parseEval('echo `${', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast = parser.parseEval('echo ` -> ${', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
});
it('check infinite on {$', function() {
var ast = parser.parseEval('echo "{$', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast.children[0].arguments[0].value[0].kind.should.be.exactly('variable');
ast = parser.parseEval('echo `{$', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
ast = parser.parseEval('echo ` -> {$', {
parser: { suppressErrors: true, debug: false },
lexer: { debug: false },
});
});
it('binary cast', function() {
var ast = parser.parseEval('echo (binary)"\\colors[1] contains >$colors[1]<\\n";');
// @todo console.log(ast.children[0].arguments[0]);
Expand Down

0 comments on commit c4479e7

Please sign in to comment.