groupon / cson-parser

Simple & safe CSON parser
BSD 3-Clause "New" or "Revised" License
133 stars 27 forks source link

JSON 2 CSON conversion fails to escape '#{' pattern #81

Open msftrncs opened 5 years ago

msftrncs commented 5 years ago

Originally posted as https://github.com/bevry/cson/issues/91. Please excuse my unfamiliarity with CSON/Coffee at the time I posted this there. Since then, I have written my own crude CSON generator in PowerShell, https://github.com/msftrncs/PwshOutCSON, where as I had started with the JSON2CSON converter from https://github.com/bevry/cson.

Trying to convert a JSON tmLanguage file to a CSON format, I ran in to a issue where the tmLanguage parser in Atom didn't like the file. Turns out there must be some kind of construct using a #{…} (or maybe its #{(…)}) notation (subexpression?) and my tmLanguage file happens to have 5 such patterns in it. I'm showing the entire section below, but the issue is towards the middle of the begin property.

{
    beginCaptures:
        "1":
            name: "constant.numeric.hex.powershell"
        "2":
            name: "constant.language.powershell"
    begin: "(?>([+\\x{2013}-\\x{2015}-]?0(?i:x[0-9a-f_]+l?))((?i:[kmgtp]b)?))(?![^\\s#{(,;|)}<>.!+%*/=\\x{2013}-\\x{2015}-])"
    end: "$|(?![\\s#.+\\x{2013}-\\x{2015}-]|<#)"
    patterns: [
        {
            begin: "\\G(?=\\.(?!\\.)|::|\\[)"
            end: "(?=[^\\s\\p{L}]|\\n)"
            applyEndPatternLast: true
            patterns: [
                {
                    include: "#accessors"
                }
                {
                    match: "[^\\s\\W]+"
                }
            ]
        }
        {
            include: "#operators"
        }
        {
            include: "#commentBlock"
        }
        {
            include: "#commentLine"
        }
    ]
}

The value of the property is perfectly fine in JSON, but it needs the # (or the { following it) escaped in CSON in order to be acceptable to Atom.

I suspect its the CSON serializer that needs to catch the #{ pattern in strings and escape it with \#{.

jkrems commented 5 years ago

Another alternative would be to force ' as the delimiter whenever there's a # (or #{) in the string.