Skip to content

Commit

Permalink
fix: escape backlash in literal strings
Browse files Browse the repository at this point in the history
  • Loading branch information
NLKNguyen committed Apr 9, 2023
1 parent ca1723a commit e402ad5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ npx tape ./tests/helpers.js | npx tap-spec

Piping to `tap-spec` is optional, but it makes the output easier to read.

<!--
<!--
# Publish
```shell
Expand All @@ -152,9 +152,11 @@ npm publish --access public
# Mark release tag
Use IDE to mark tag. Then push to repos:
```shell
git push origin {tag_name}
```
git push {tag_name}
-->

# 🚀 Quick start
Expand Down Expand Up @@ -203,7 +205,7 @@ const ast = S.parse(`( 1 "a \\"b\\" c" true null d (e f ()) )`)

if (S.isExpression(ast)) {
console.log(`ast is an expression: ${JSON.stringify(ast)}`)
} else {
} else {
throw Error(`ast is not a valid expression`)
}

Expand All @@ -229,11 +231,11 @@ for (let e of ast) {
Output:

```clj
ast is an expression: [1,"\"a \"b\" c\"","true","null","d",["e","f",[]]]
ast is an expression: [1,"\"a \\\"b\\\" c\"","true","null","d",["e","f",[]]]
ast[0] is a number with value: 1
ast[1] is a string with value: "a \"b\" c"
ast[1] is a string with value: "a \\\"b\\\" c"
ast[2] is a boolean with value: true
ast[3] is a null with value: null
ast[3] is null: null
ast[4] is an atom with id: d
ast[5] is an expression: ["e","f",[]]
```
Expand Down Expand Up @@ -305,16 +307,16 @@ The project is [MIT License](https://github.com/NLKNguyen/code-formation/blob/ma

It is a simple permissive license with conditions only requiring the preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

<!--
<!--
This snippet is used to transform the generated API markdown to include a divider
It's idempotent, meaning that it can run multiple times but will not add more
It's idempotent, meaning that it can run multiple times but will not add more
dividers than necessary.
// $<:AddDivider (LANGUAGE "nodejs")
module.exports = async (context) => {
const regex = /(?<!---)(\r?\n\r?\n)(###\s)/gm
/*
/*
if matched, (\r?\n\r?\n) => $1
(###\s) => $2
*/
Expand Down Expand Up @@ -440,13 +442,13 @@ to LISP dialects such as CLIPS, Clojure, Scheme, Racket, etc.

#### Parameters

* `expression`
* `expression`
* `context` (optional, default `{}`)
* `state` (optional, default `{scoped:[],globals:{}}`)
* `entity` (optional, default `this.ROOT`)
* `E` **any**
* `E` **any**

Returns **any**
Returns **any**

---

Expand Down Expand Up @@ -487,7 +489,7 @@ Serialize an expression tree into an S-expression string
* `opts` **any** serializing options (optional, default `{includingRootParentheses:true}`)
* `level` (optional, default `0`)

Returns **any**
Returns **any**

---

Expand All @@ -497,7 +499,7 @@ Create an identifier symbol

#### Parameters

* `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
* `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**

#### Examples

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ class SExpr {
t[i] =
"'" +
ti
.replaceAll("\\", "\\\\")
.replaceAll("\r", "\\r")
.replaceAll("\n", "\\n")
.replaceAll("\t", "\\t")
.replaceAll("'", "\\'") +
"'"
if (i > 0 && ti != "]" && t[i - 1].trim() != "(") t.splice(i, 0, ",")
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s-expression.js",
"version": "0.6.2",
"version": "0.6.3",
"description": "S-Expression Parser, Serializer, and Tree Constructor / Walker Utilities in JavaScript for Browsers and Node.js",
"main": "index.js",
"files": [
Expand Down
24 changes: 13 additions & 11 deletions tests/interpret.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const test = require("tape")
const util = require("util")
const colorize = require("json-colorizer")
const _ = require("lodash")
const SExpr = require("../index")
Expand Down Expand Up @@ -136,14 +137,14 @@ test("interpret method", async function (t) {
},
{
note: "multiline string with escaped quotes",
input: `(a "multi\n line\n string \\"with \nquotes\\"")`,
input: '(a "multi\n line\n string \\"with \nquotes\\"")',
parsingOpts: { includedRootParentheses: true },
expect: {
[S.ROOT]: [
"[ ROOT ]": [
{
[S.ATOM]: "a",
"[ ATOM ]": "a",
},
'multi\n line\n string "with \nquotes"',
'multi\n line\n string \\"with \nquotes\\"',
],
},
},
Expand Down Expand Up @@ -196,20 +197,21 @@ test("interpret method", async function (t) {
// console.dir(ast)

const context = testCase.context || {}
if (testCase.context) {
console.dir({
"testCase.context": testCase.context,
context,
})
// process.exit()
}
// if (testCase.context) {
// console.dir({
// "testCase.context": testCase.context,
// context,
// })
// // process.exit()
// }
const output = await S.interpret(ast, context)
console.log(
"Output : " +
colorize(output, {
pretty: true,
})
)
// console.log(util.inspect(output, {showHidden: false, depth: null, colors: true}))

const expect = testCase.expect
console.log("Expect : " + JSON.stringify(expect))
Expand Down
9 changes: 4 additions & 5 deletions tests/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ test("parse method", function (t) {
const S = new SExpr()
let testCases = [
{
input: `( 1 "a \\"b\\" c" true null d (e f ()) )`,
expect: [1, '"a "b" c"', "true", "null", "d", ["e", "f", []]],
opts: { includedRootParentheses: true }
input: '( 1 "a \\"b\\" c" true null d (e f ()) )',
expect: [1, '"a \\"b\\" c"', "true", "null", "d", ["e", "f", []]],
opts: { includedRootParentheses: true },
},
]
t.plan(testCases.length)
Expand All @@ -35,7 +35,7 @@ test("parse method", function (t) {
)

console.log(`---`)

if (S.isExpression(ast)) {
console.log(`ast is an expression: ${colorize(JSON.stringify(ast))}`)
} else {
Expand Down Expand Up @@ -78,4 +78,3 @@ test("parse method", function (t) {
console.log()
}
})

0 comments on commit e402ad5

Please sign in to comment.