From 1346f9c9e107a98d999ab3ad1e4cb14730abcc97 Mon Sep 17 00:00:00 2001 From: Ben Hanna Date: Fri, 5 Apr 2019 18:20:42 +0000 Subject: [PATCH] Eliminated the use of classes. --- index.js | 57 +++++++++++++++++++++++---------------------------- test/index.js | 2 +- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 278c2b0..cf2fd32 100644 --- a/index.js +++ b/index.js @@ -1,32 +1,27 @@ -class Reader { - constructor(sexpr) { - this.i = 0; - this.len = sexpr.length; - this.sexpr = sexpr; - } - - getc() { - if (this.i == this.len) return null; - return this.sexpr[this.i++]; - } +module.exports = function (sexpr) { + var data = { + index: 0, + sexpr: sexpr + }; - ungetc() { - this.i--; - } + return parse(data); +}; - readValue(terminator) { - var c, str = ''; - while (!terminator(c = this.getc())) { - str += c; - } - return str; - } +function getChar(data) { + if (data.index == data.sexpr.length) return null; + return data.sexpr[data.index++]; } -module.exports = { - parse(sexpr) { - return parse(new Reader(sexpr)); +function readValue(data, isTerminator) { + var c, value = ''; + while (!isTerminator(c = getChar(data))) { + value += c; + } + return value; } + +function unGetChar(data) { + data.index--; } function isStringTerm(c) { @@ -50,20 +45,20 @@ function isSpace(c) { || c == '\v'; } -function parse(reader) { +function parse(data) { var c, list = []; - while (c = reader.getc()) { + while (c = getChar(data)) { if (c == ')') { break; } else if (c == '(') { - list.push(parse(reader)); + list.push(parse(data)); } else if (c == '"') { - list.push(reader.readValue(isStringTerm)); + list.push(readValue(data, isStringTerm)); } else if (!isSpace(c)) { - reader.ungetc(); - list.push(reader.readValue(isListTerm)); - reader.ungetc(); + unGetChar(data); + list.push(readValue(data, isListTerm)); + unGetChar(data); } } diff --git a/test/index.js b/test/index.js index 08aa75f..f80f6ac 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,6 @@ const assert = require('assert'); -const { parse } = require('../'); +const parse = require('../'); describe('Parser', function () {