Skip to content

Commit

Permalink
sanitise strings
Browse files Browse the repository at this point in the history
  • Loading branch information
TodePond committed Mar 6, 2021
1 parent 63233cb commit c3f2aae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
16 changes: 9 additions & 7 deletions build/habitat-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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([
Expand Down Expand Up @@ -994,7 +996,7 @@ Habitat.install = (global) => {
),
Term.string(`"`),
]),
([left, inner]) => `Term.string('${inner}')`
([left, inner]) => `Term.string(\`${inner}\`)`
)

scope.RegExp = Term.emit(
Expand Down
16 changes: 9 additions & 7 deletions build/habitat-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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([
Expand Down Expand Up @@ -994,7 +996,7 @@ Habitat.install = (global) => {
),
Term.string(`"`),
]),
([left, inner]) => `Term.string('${inner}')`
([left, inner]) => `Term.string(\`${inner}\`)`
)

scope.RegExp = Term.emit(
Expand Down
16 changes: 9 additions & 7 deletions source/mothertode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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([
Expand Down Expand Up @@ -577,7 +579,7 @@
),
Term.string(`"`),
]),
([left, inner]) => `Term.string('${inner}')`
([left, inner]) => `Term.string(\`${inner}\`)`
)

scope.RegExp = Term.emit(
Expand Down
8 changes: 8 additions & 0 deletions tinker/tinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c3f2aae

Please sign in to comment.