2018-09-30 23:01:58 +01:00
---
id: 59667989bf71cf555dd5d2ff
2020-11-27 19:02:05 +01:00
title: S-Expressions
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302303
2021-01-13 03:31:00 +01:00
dashedName: s-expressions
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
[S-Expressions ](https://en.wikipedia.org/wiki/S-Expression "wp: S-Expression" ) are one convenient way to parse and store data.
# --instructions--
2018-09-30 23:01:58 +01:00
2019-03-07 23:15:29 +09:00
Write a simple reader/parser for S-Expressions that handles quoted and unquoted strings, integers and floats.
2020-11-27 19:02:05 +01:00
2019-03-07 23:15:29 +09:00
The function should read a single but nested S-Expression from a string and return it as a (nested) array.
2020-11-27 19:02:05 +01:00
2019-03-07 23:15:29 +09:00
Newlines and other whitespace may be ignored unless contained within a quoted string.
2020-11-27 19:02:05 +01:00
"`()` " inside quoted strings are not interpreted, but treated as part of the string.
Handling escaped quotes inside a string is optional; thus "`(foo"bar)` " may be treated as a string "`foo"bar` ", or as an error.
2021-01-20 18:01:00 -08:00
For this, the reader need not recognize "< code > \\</ code > " for escaping, but should, in addition, recognize numbers if the language has appropriate data types.
2020-11-27 19:02:05 +01:00
2021-01-20 18:01:00 -08:00
Note that with the exception of "`()"` " ("< code > \\</ code > " if escaping is supported) and whitespace there are no special characters. Anything else is allowed without quotes.
2020-11-27 19:02:05 +01:00
2019-03-07 23:15:29 +09:00
The reader should be able to read the following input
2020-11-27 19:02:05 +01:00
< pre > ((data "quoted data" 123 4.5)
2019-03-07 23:15:29 +09:00
(data (!@# (4.5) "(more" "data)")))
< / pre >
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
and turn it into a native data structure. (See the [Pike ](https://rosettacode.org/wiki/S-Expressions#Pike "\#Pike" ), [Python ](https://rosettacode.org/wiki/S-Expressions#Python "\#Python" ) and [Ruby ](https://rosettacode.org/wiki/S-Expressions#Ruby "\#Ruby" ) implementations for examples of native data structures.)
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`parseSexpr` should be a function.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof parseSexpr === 'function');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`parseSexpr('(data1 data2 data3)')` should return `['data1', 'data2', 'data3']`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(parseSexpr(simpleSExpr), simpleSolution);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`parseSexpr('(data1 data2 data3)')` should return an array with 3 elements.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(parseSexpr(basicSExpr), basicSolution);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
const simpleSExpr = '(data1 data2 data3)';
const simpleSolution = ['data1', 'data2', 'data3'];
const basicSExpr = '((data "quoted data" 123 4.5) (data (!@# (4.5) "(more" "data)")))';
const basicSolution = [["data","\"quoted data\"",123,4.5],["data",["!@#",[4.5],"\"(more\"","\"data)\""]]];
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function parseSexpr(str) {
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
return true;
}
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function parseSexpr(str) {
const t = str.match(/\s*("[^"]*"|\(|\)|"|[^\s()"]+)/g);
for (var o, c = 0, i = t.length - 1; i >= 0; i--) {
var n,
ti = t[i].trim();
if (ti == '"') return;
else if (ti == '(') t[i] = '[', c += 1;
else if (ti == ')') t[i] = ']', c -= 1;
else if ((n = +ti) == ti) t[i] = n;
2018-10-20 21:02:47 +03:00
else t[i] = `'${ti.replace('\'', '\\\'')}'` ;
2018-09-30 23:01:58 +01:00
if (i > 0 & & ti != ']' & & t[i - 1].trim() != '(') t.splice(i, 0, ',');
if (!c) if (!o) o = true; else return;
}
2018-10-02 15:02:53 +01:00
return c ? undefined : eval(t.join(''));
2018-09-30 23:01:58 +01:00
}
```