Skip to content

Commit

Permalink
Lexer refactor; updated engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Dec 12, 2020
1 parent c4dd8bd commit 04a209d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
74 changes: 32 additions & 42 deletions lib/lexers.js
@@ -1,17 +1,17 @@

var TokenType = { Integer: 1, String: 2, Name: 3, Punctuation: 4, Operator: 5 };
const TokenType = { Integer: 1, String: 2, Name: 3, Punctuation: 4, Operator: 5 };

var punctuations = ".,()";
var operators = [ ">", "<", "=", ">=", "<=", "<>" ];
const punctuations = ".,()";
const operators = [ ">", "<", "=", ">=", "<=", "<>" ];

function Token(value, type) {
this.value = value;
this.type = type;
}

function Lexer(text) {
var position = 0,
nexts = [];
let position = 0;
const nexts = [];

function nextChar() {
return text[position++];
Expand Down Expand Up @@ -74,8 +74,8 @@ function Lexer(text) {
}

function nextName(letter) {
var name = letter,
ch;
let name = letter;
let ch;

for (ch = nextChar(); ch && (isLetter(ch) || isDigit(ch) || ch === '-'); ch = nextChar()) {
name += ch;
Expand All @@ -87,39 +87,38 @@ function Lexer(text) {
}

function nextOperator(letter) {
var ch = nextChar(),
value = letter + ch;
const ch = nextChar();
const value = letter + ch;

if (operators.indexOf(value) >= 0) {
if (operators.indexOf(value) >= 0)
return new Token(value, TokenType.Operator);
}

pushChar(ch);

return new Token(letter, TokenType.Operator);
}

function nextString() {
var name = '',
ch;
let name = '';
let ch;

for (ch = nextChar(); ch && ch !== '"'; ch = nextChar()) {
name += ch;

if (ch === '\\') {
name += nextChar();
}
}

if (!ch) {
if (!ch)
throw "unclosed string";
}

return new Token(name, TokenType.String);
}

function nextNumber(digit) {
var number = digit,
ch;
let number = digit;
let ch;

for (ch = nextChar(); ch && isDigit(ch); ch = nextChar()) {
number += ch;
Expand All @@ -131,11 +130,11 @@ function Lexer(text) {
}

this.nextPhrase = function () {
var lastchar = null,
lastposition = null,
l = text.length,
initial,
ch;
let lastchar = null;
let lastposition = null;
const l = text.length;
let initial;
let ch;

skipSpaces();

Expand All @@ -156,55 +155,46 @@ function Lexer(text) {
position++;
}

if (lastchar === '.') {
if (lastchar === '.')
return text.slice(initial, lastposition);
}

throw "unexpected end of input";
}

this.nextToken = function () {
if (nexts.length > 0) {
if (nexts.length > 0)
return nexts.pop();
}

var ch = nextFirstChar();
const ch = nextFirstChar();

if (ch === null) {
if (ch === null)
return null;
}

if (isLetter(ch)) {
if (isLetter(ch))
return nextName(ch);
}

if (isDigit(ch)) {
if (isDigit(ch))
return nextNumber(ch);
}

if (isOperator(ch)) {
if (isOperator(ch))
return nextOperator(ch);
}

if (ch === '"') {
if (ch === '"')
return nextString();
}

if (punctuations.indexOf(ch) >= 0) {
if (punctuations.indexOf(ch) >= 0)
return new Token(ch, TokenType.Punctuation);
}

throw "unexpected '" + ch + "'";
}

this.pushToken = function(token) {
if (token) {
if (token)
nexts.push(token);
}
}

this.peekToken = function () {
var token = this.nextToken();
const token = this.nextToken();

if (token)
this.pushToken(token);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -5,13 +5,13 @@
, "author": "Angel 'Java' Lopez <webmaster@ajlopez.com> (http://www.ajlopez.com)"
, "repository": { "type": "git", "url": "git://github.com/ajlopez/CobolScript.git" }
, "main": "./lib/cobolscript.js"
, "engines": { "node": ">= 0.6.0 && < 0.9.0" }
, "engines": { "node": ">= 8.0.0" }
, "scripts": {
"test": "simpleunit test"
}
, "dependencies": {
}
, "devDependencies": {
"simpleunit": "0.0.4"
"simpleunit": "0.0.9"
}
}

0 comments on commit 04a209d

Please sign in to comment.