From c3f2aae898b711dce7a5275d1b2c1a37b29bae62 Mon Sep 17 00:00:00 2001 From: Luke Wilson Date: Sat, 6 Mar 2021 18:37:13 +0000 Subject: [PATCH] sanitise strings --- build/habitat-embed.js | 16 +++++++++------- build/habitat-import.js | 16 +++++++++------- source/mothertode.js | 16 +++++++++------- tinker/tinker.js | 8 ++++++++ 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/build/habitat-embed.js b/build/habitat-embed.js index 660d57f..158e84a 100644 --- a/build/habitat-embed.js +++ b/build/habitat-embed.js @@ -680,13 +680,15 @@ Habitat.install = (global) => { Term.term("ChainProperty", scope), ]) + const sanitise = (string) => string.split("`").join("\\`") + scope.MatchProperty = Term.emit( Term.list([ Term.string("::"), Term.maybe(Term.term("Gap", scope)), Term.except(Term.term("Term", scope), []), ]), - ([operator, gap, term = {}]) => `{match: \`${term.output}\`}, `, + ([operator, gap, term = {}]) => `{match: \`${sanitise(term.output)}\`}, `, ) scope.ChainProperty = Term.emit( @@ -695,7 +697,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.except(Term.term("Term", scope), []), ]), - ([operator, gap, term = {}]) => `{chain: \`${term.output}\`}, `, + ([operator, gap, term = {}]) => `{chain: \`${sanitise(term.output)}\`}, `, ) scope.EmitProperty = Term.emit( @@ -704,7 +706,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{emit: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{emit: \`${sanitise(term.output)}\`},`, ) scope.CheckProperty = Term.emit( @@ -713,7 +715,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{check: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{check: \`${sanitise(term.output)}\`},`, ) scope.ErrorProperty = Term.emit( @@ -722,7 +724,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{error: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{error: \`${sanitise(term.output)}\`},`, ) scope.ArgsProperty = Term.emit( @@ -731,7 +733,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{args: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{args: \`${sanitise(term.output)}\`},`, ) scope.JavaScript = Term.or([ @@ -994,7 +996,7 @@ Habitat.install = (global) => { ), Term.string(`"`), ]), - ([left, inner]) => `Term.string('${inner}')` + ([left, inner]) => `Term.string(\`${inner}\`)` ) scope.RegExp = Term.emit( diff --git a/build/habitat-import.js b/build/habitat-import.js index 54df7aa..03d964c 100644 --- a/build/habitat-import.js +++ b/build/habitat-import.js @@ -680,13 +680,15 @@ Habitat.install = (global) => { Term.term("ChainProperty", scope), ]) + const sanitise = (string) => string.split("`").join("\\`") + scope.MatchProperty = Term.emit( Term.list([ Term.string("::"), Term.maybe(Term.term("Gap", scope)), Term.except(Term.term("Term", scope), []), ]), - ([operator, gap, term = {}]) => `{match: \`${term.output}\`}, `, + ([operator, gap, term = {}]) => `{match: \`${sanitise(term.output)}\`}, `, ) scope.ChainProperty = Term.emit( @@ -695,7 +697,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.except(Term.term("Term", scope), []), ]), - ([operator, gap, term = {}]) => `{chain: \`${term.output}\`}, `, + ([operator, gap, term = {}]) => `{chain: \`${sanitise(term.output)}\`}, `, ) scope.EmitProperty = Term.emit( @@ -704,7 +706,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{emit: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{emit: \`${sanitise(term.output)}\`},`, ) scope.CheckProperty = Term.emit( @@ -713,7 +715,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{check: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{check: \`${sanitise(term.output)}\`},`, ) scope.ErrorProperty = Term.emit( @@ -722,7 +724,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{error: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{error: \`${sanitise(term.output)}\`},`, ) scope.ArgsProperty = Term.emit( @@ -731,7 +733,7 @@ Habitat.install = (global) => { Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{args: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{args: \`${sanitise(term.output)}\`},`, ) scope.JavaScript = Term.or([ @@ -994,7 +996,7 @@ Habitat.install = (global) => { ), Term.string(`"`), ]), - ([left, inner]) => `Term.string('${inner}')` + ([left, inner]) => `Term.string(\`${inner}\`)` ) scope.RegExp = Term.emit( diff --git a/source/mothertode.js b/source/mothertode.js index b7ae0a7..5f5c56d 100644 --- a/source/mothertode.js +++ b/source/mothertode.js @@ -263,13 +263,15 @@ Term.term("ChainProperty", scope), ]) + const sanitise = (string) => string.split("`").join("\\`") + scope.MatchProperty = Term.emit( Term.list([ Term.string("::"), Term.maybe(Term.term("Gap", scope)), Term.except(Term.term("Term", scope), []), ]), - ([operator, gap, term = {}]) => `{match: \`${term.output}\`}, `, + ([operator, gap, term = {}]) => `{match: \`${sanitise(term.output)}\`}, `, ) scope.ChainProperty = Term.emit( @@ -278,7 +280,7 @@ Term.maybe(Term.term("Gap", scope)), Term.except(Term.term("Term", scope), []), ]), - ([operator, gap, term = {}]) => `{chain: \`${term.output}\`}, `, + ([operator, gap, term = {}]) => `{chain: \`${sanitise(term.output)}\`}, `, ) scope.EmitProperty = Term.emit( @@ -287,7 +289,7 @@ Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{emit: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{emit: \`${sanitise(term.output)}\`},`, ) scope.CheckProperty = Term.emit( @@ -296,7 +298,7 @@ Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{check: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{check: \`${sanitise(term.output)}\`},`, ) scope.ErrorProperty = Term.emit( @@ -305,7 +307,7 @@ Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{error: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{error: \`${sanitise(term.output)}\`},`, ) scope.ArgsProperty = Term.emit( @@ -314,7 +316,7 @@ Term.maybe(Term.term("Gap", scope)), Term.term("JavaScript", scope), ]), - ([operator, gap, term = {}]) => `{args: \`${term.output}\`},`, + ([operator, gap, term = {}]) => `{args: \`${sanitise(term.output)}\`},`, ) scope.JavaScript = Term.or([ @@ -577,7 +579,7 @@ ), Term.string(`"`), ]), - ([left, inner]) => `Term.string('${inner}')` + ([left, inner]) => `Term.string(\`${inner}\`)` ) scope.RegExp = Term.emit( diff --git a/tinker/tinker.js b/tinker/tinker.js index a3c769d..e3b40c3 100644 --- a/tinker/tinker.js +++ b/tinker/tinker.js @@ -382,3 +382,11 @@ const whitespace = MotherTode ` Name :: /[a-z]/+ `.log() + +const EWAL = MotherTode ` + :: Instruction {"\n" Instruction} + Instruction :: "Hello" + Instruction :: "Yo" +` + +print(EWAL(`Hello`).output)