Skip to content

inconsistent escaping of string literals #7473

@krux02

Description

@krux02
import macros, strutils

const separator = repeat("=", 60)

macro foobar(arg: untyped): untyped =
  echo separator
  for x in arg:
    echo x.repr
    echo x.lispRepr
    echo x.treeRepr
    echo x.astGenRepr
    echo separator


foobar:
  "  \n  \r  \t  \\  "
  "  \x0A  \x0D  \x09  \\  "
  "  \L  \c  \t  \\  "
  """  \n  \r  \t  \\  """
  r"  \n  \r  \t  \\  "

this is the output:

============================================================
"  \L  \c  \t  \\  "
StrLit(  
  	  \  )
StrLit   
  	  \  
newLit("  \x0A  \x0D  \x09  \\  ")
============================================================
"  \L  \c  \t  \\  "
StrLit(  
  	  \  )
StrLit   
  	  \  
newLit("  \x0A  \x0D  \x09  \\  ")
============================================================
"  \L  \c  \t  \\  "
StrLit(  
  	  \  )
StrLit   
  	  \  
newLit("  \x0A  \x0D  \x09  \\  ")
============================================================
"""  \n  \r  \t  \\  """
TripleStrLit(  \n  \r  \t  \\  )
TripleStrLit   \n  \r  \t  \\  
newLit("  \\n  \\r  \\t  \\\\  ")
============================================================
r"  \n  \r  \t  \\  "
RStrLit(  \n  \r  \t  \\  )
RStrLit   \n  \r  \t  \\  
newLit("  \\n  \\r  \\t  \\\\  ")
============================================================

In my opinion the escaping works unnecessarily complicated. This looks like the escaping is implemented in several different locations in the source with code duplication, and then it diverged over time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions