Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions src/lexer/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ module.exports = {
if (ch === "x" || ch === "X") {
ch = this.input();
if (ch !== "_" && this.is_HEX()) {
return this.consume_HNUM();
return this.consume_hexadecimal();
} else {
this.unput(ch ? 2 : 1);
}
// check binary notation
} else if (ch === "b" || ch === "B") {
ch = this.input();
if ((ch !== "_" && ch === "0") || ch === "1") {
return this.consume_BNUM();
return this.consume_binary();
} else {
this.unput(ch ? 2 : 1);
}
} else if (ch === "o" || ch === "O") {
ch = this.input();
if (ch !== "_" && this.is_OCTAL()) {
return this.consume_ONUM();
return this.consume_octal();
} else {
this.unput(ch ? 2 : 1);
}
Expand Down Expand Up @@ -94,7 +94,7 @@ module.exports = {
ch = this.input();
}
if (this.is_NUM_START()) {
this.consume_LNUM();
this.consume_decimal_digits();
return this.tok.T_DNUMBER;
}
this.unput(ch ? undo : undo - 1); // keep only 1
Expand Down Expand Up @@ -123,49 +123,44 @@ module.exports = {
return this.tok.T_DNUMBER;
}
},
// read hexa
consume_HNUM() {
consume_prefixed_digits(isValid) {
let prev = this._input[this.offset - 1];
while (this.offset < this.size) {
const ch = this.input();
if (!this.is_HEX()) {
if (!isValid.call(this)) {
if (ch) this.unput(1);
break;
}
}
return this.tok.T_LNUMBER;
},
// read a generic number
consume_LNUM() {
while (this.offset < this.size) {
const ch = this.input();
if (!this.is_NUM()) {
if (ch) this.unput(1);
if (ch === "_" && prev === "_") {
this.unput(2);
prev = null;
break;
}
prev = ch;
}
if (prev === "_") this.unput(1);
return this.tok.T_LNUMBER;
},
// read binary
consume_BNUM() {
let ch;
while (this.offset < this.size) {
ch = this.input();
if (ch !== "0" && ch !== "1" && ch !== "_") {
if (ch) this.unput(1);
break;
}
}
return this.tok.T_LNUMBER;
consume_hexadecimal() {
return this.consume_prefixed_digits(this.is_HEX);
},
// read an octal number
consume_ONUM() {
consume_decimal_digits() {
while (this.offset < this.size) {
const ch = this.input();
if (!this.is_OCTAL()) {
if (!this.is_NUM()) {
if (ch) this.unput(1);
break;
}
}
return this.tok.T_LNUMBER;
},
consume_binary() {
return this.consume_prefixed_digits(function () {
const ch = this._input[this.offset - 1];
return ch === "0" || ch === "1" || ch === "_";
});
},
consume_octal() {
return this.consume_prefixed_digits(this.is_OCTAL);
},
};
Loading