From 938f082a8d953d112de911e41b009a518ca5495a Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Sat, 18 Jan 2025 13:17:59 +0100 Subject: [PATCH] fix: escape single quotes in strings in generated Python code (#1312) Closes #1310 ### Summary of Changes Single quotes in strings are now escaped in the generated Python code. --- .../src/language/generation/python/safe-ds-python-generator.ts | 2 +- .../literals/generated/tests/generator/literals/gen_input.py | 1 + .../generated/tests/generator/literals/gen_input.py.map | 2 +- .../generation/python/expressions/literals/input.sdsdev | 3 +++ .../generated/tests/generator/templateString/gen_input.py | 1 + .../generated/tests/generator/templateString/gen_input.py.map | 2 +- .../generation/python/expressions/template string/input.sdsdev | 3 +++ 7 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/safe-ds-lang/src/language/generation/python/safe-ds-python-generator.ts b/packages/safe-ds-lang/src/language/generation/python/safe-ds-python-generator.ts index ea59a3aff..b2a1ec3eb 100644 --- a/packages/safe-ds-lang/src/language/generation/python/safe-ds-python-generator.ts +++ b/packages/safe-ds-lang/src/language/generation/python/safe-ds-python-generator.ts @@ -1289,7 +1289,7 @@ export class SafeDsPythonGenerator { } private formatStringSingleLine(value: string): string { - return value.replaceAll('\r\n', '\\n').replaceAll('\n', '\\n'); + return value.replaceAll('\r\n', '\\n').replaceAll('\n', '\\n').replaceAll("'", "\\'"); } } diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py b/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py index cae006660..5315a6f20 100644 --- a/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py @@ -12,3 +12,4 @@ def test(): f(None) f('') f('multi\nline') + f('\'') diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py.map b/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py.map index 24276afb1..64e5b6c72 100644 --- a/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py.map +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/generated/tests/generator/literals/gen_input.py.map @@ -1 +1 @@ -{"version":3,"sources":["input.sdsdev"],"names":["test","f","true","false","null"],"mappings":"AAAA;;;;;;AAIA,IAASA,IAAI;IACTC,CAAC,CAACC,IAAI;IACND,CAAC,CAACE,KAAK;IACPF,CAAC,CAAC,GAAG;IACLA,CAAC,CAAC,CAAC;IACHA,CAAC,CAACG,IAAI;IACNH,CAAC,CAAC,EAAE;IACJA,CAAC,CAAC","file":"gen_input.py"} \ No newline at end of file +{"version":3,"sources":["input.sdsdev"],"names":["test","f","true","false","null"],"mappings":"AAAA;;;;;;AAIA,IAASA,IAAI;IACTC,CAAC,CAACC,IAAI;IACND,CAAC,CAACE,KAAK;IACPF,CAAC,CAAC,GAAG;IACLA,CAAC,CAAC,CAAC;IACHA,CAAC,CAACG,IAAI;IACNH,CAAC,CAAC,EAAE;IACJA,CAAC,CAAC;IAIFA,CAAC,CAAC,IAAG","file":"gen_input.py"} \ No newline at end of file diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/input.sdsdev b/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/input.sdsdev index 33625ee46..d2cbb5105 100644 --- a/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/input.sdsdev +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/literals/input.sdsdev @@ -11,4 +11,7 @@ pipeline test { f(""); f("multi line"); + + // https://github.com/Safe-DS/DSL/issues/1310 + f("'"); } diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py b/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py index f407d7aaf..98252554c 100644 --- a/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py @@ -6,3 +6,4 @@ def test(): f(f'start\n{ g() }\ninner { g() }\nend') + f('\'') diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py.map b/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py.map index 2b4dc1ac0..45b285064 100644 --- a/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py.map +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/generated/tests/generator/templateString/gen_input.py.map @@ -1 +1 @@ -{"version":3,"sources":["input.sdsdev"],"names":["test","f","g"],"mappings":"AAAA;;;;;;AAMA,IAASA,IAAI;IACTC,CAAC,CAAC,EAAA,SACJC,CAAC,EAAG,YACEA,CAAC,EAAG","file":"gen_input.py"} \ No newline at end of file +{"version":3,"sources":["input.sdsdev"],"names":["test","f","g"],"mappings":"AAAA;;;;;;AAMA,IAASA,IAAI;IACTC,CAAC,CAAC,EAAA,SACJC,CAAC,EAAG,YACEA,CAAC,EAAG;IAIRD,CAAC,CAAC,IAAG","file":"gen_input.py"} \ No newline at end of file diff --git a/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/input.sdsdev b/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/input.sdsdev index 8e4c55575..d7dca101c 100644 --- a/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/input.sdsdev +++ b/packages/safe-ds-lang/tests/resources/generation/python/expressions/template string/input.sdsdev @@ -9,4 +9,7 @@ pipeline test { { g() } inner { g() } end`); + + // https://github.com/Safe-DS/DSL/issues/1310 + f(`'`); }