Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 7, 2025

Description

AlaSQL now processes standard escape sequences in string literals. Previously, \t, \n, \r, and similar sequences were treated as literal characters rather than their control character equivalents.

This implementation focuses on essential escape sequences while maintaining backward compatibility with existing quote escaping mechanisms.

Changes

  • Parser (src/alasqlparser.jison): Modified StringValue grammar rule to process escape sequences: \n, \t, \r, \\, \', \"
    • Uses specific regex /\\(n|t|r|\\|'|")/g to only match supported sequences
    • Removed support for \b, \f, \v to keep implementation conservative
    • Unsupported escape sequences are left as literal text (e.g., \x remains as \x)
  • JSON compilation (src/58json.js): Fixed JSONtoJS to escape strings properly when generating JavaScript code, preventing syntax errors with embedded quotes
  • Tests: Added test/test134-B.js with 8 test cases covering all supported escape sequences; updated test/test125.js to reflect new escape sequence behavior

Example

SELECT 'hello\tworld' AS result;
-- Returns: {result: 'hello	world'}

SELECT 'line1\nline2' AS result;  
-- Returns: {result: 'line1
-- line2'}

SELECT 'back\\slash' AS result;
-- Returns: {result: 'back\slash'}

SELECT * FROM CSV('data.csv', {quote:"\""});
-- Now correctly handles escaped quotes in parameters

Supported Escape Sequences

  • \n - newline
  • \t - tab
  • \r - carriage return
  • \\ - backslash
  • \' - single quote
  • \" - double quote

All other sequences (like \b, \f, \v, \x, etc.) are left as literal backslash + character.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add \ t and similar characters to strings</issue_title>
<issue_description>Now AlaSQL does not support \t and other similar special characters.
</issue_description>

<agent_instructions>Name the test file test/test134-B.js</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mathiasrw Have added this to feature requests so people can vote http://feathub.com/agershun/alasql/+27

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add support for \t and similar special characters in strings Add escape sequence support for string literals (\t, \n, \r, etc.) Dec 7, 2025
Copilot AI requested a review from mathiasrw December 7, 2025 07:00
Copy link
Member

@mathiasrw mathiasrw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove \b \f \v support

{ $$ = new yy.StringValue({value: $1.substr(1,$1.length-2).replace(/(\\\')/g,"'").replace(/(\'\')/g,"'")}); }
{
var str = $1.substr(1,$1.length-2).replace(/(\'\')/g,"'");
str = str.replace(/\\(.)/g, function(match, char) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont know if the first \ is escaped beforehand. You will need to find pairs like \ (regex as \\) or not pair (what you got now \(.))

Please see if you can place regex outside of the executing function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the regex to be more specific: /\\(n|t|r|\\|'|")/g instead of /\\(.)/g. This only matches the supported escape sequences and properly handles \\ without needing to worry about pairs. (57cfff3)

case 'b': return '\b';
case 'f': return '\f';
case 'v': return '\v';
case '\\': return '\\';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if this is being solved via #2335

- Removed support for \b, \f, \v escape sequences as requested
- Fixed regex to only match specific escape sequences: \n \t \r \\ \' \"
- Removed default case that was accepting any character after backslash
- Updated test134-B to remove tests for \b, \f, \v
- Updated test125 escape sequence assertions to work with new behavior

Co-authored-by: mathiasrw <[email protected]>
Copy link
Contributor Author

Copilot AI commented Dec 7, 2025

please remove \b \f \v support

Removed \b, \f, and \v support as requested. Now only supporting \n, \t, \r, \, ', and " escape sequences. (57cfff3)

Copilot AI requested a review from mathiasrw December 7, 2025 10:52
@mathiasrw mathiasrw added this to the Next major version bump milestone Dec 7, 2025
function JSONtoJS(obj, context, tableid, defcols) {
var s = '';
if (typeof obj == 'string') s = '"' + obj + '"';
if (typeof obj == 'string') s = '"' + escapeq(obj) + '"';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot

What is we have a string of data that contains \n, will it then get converted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is the correct behavior. When data contains actual newline characters (after SQL parsing converts \n to a newline), escapeq() converts them back to \n when generating JavaScript code. This ensures the generated JS code is syntactically valid.

Flow:

  1. SQL 'hello\nworld' → Parser converts to actual newline → Stored in memory as hello<LF>world
  2. When generating JS code → escapeq() converts back to \n → Generated code: "hello\nworld"
  3. JS runtime executes → Results in newline character again

Without escapeq(), strings with newlines/quotes would produce invalid JavaScript syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add \ t and similar characters to strings

2 participants