groupon / cson-parser

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

Support RegExp #43

Closed phaux closed 8 years ago

phaux commented 9 years ago

Example:

syntax:
  identifier: /\b[a-z_][a-z_0-9]*\b/i
  operator: /// ^ (
    ?: [-=]>             # function
     | [-+*/%<>&|^!?=]=  # compound assign / compare
     | >>>=?             # zero-fill right shift
     | ([-+:])\1         # doubles
     | ([&|<>])\2=?      # logic / shift
     | \?\.              # soak access
     | \.{2,3}           # range or splat
  ) ///

JS Equivalent:

{
  syntax: {
    identifier: /\b[a-z_][a-z_0-9]*\b/i,
    operator: /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/
  }
}

I tried it while writing a custom atom editor syntax plugin and was surprised that it doesn't work.

ghost commented 9 years ago

Just like JSON is purely for data notation, I don't think CSON should support regular expressions. It's a form of executable code that could be misused.

In your case, you could put it in a string and pass it into new RegExp(pattern).

jkrems commented 9 years ago

I don't know, since it maps 1:1 to a string, it could arguably be called "data". Since we don't support unquoted strings, there shouldn't be a problem.

jkrems commented 9 years ago

@phaux Thanks for bringing this up, btw.

As for more arguments in favor of support: We already support static/const expressions for numeric operations. Which is even more similar to "executable code". But we'd have to figure out what we want to support in detail (e.g. coffee-script regex support is not just simple regex literals IIRC).

phaux commented 9 years ago

@khoomeister

There's a few problems with using strings to contain regexes:

The only downside I can think of is that it's not obvious what the JSON representation of v: /abc/gi should be. Should it just turn into string or something like

{
  "v": {
    "$regexp": "abc",
    "$flags": "gi"
  }
}

Or maybe it's not that important?

jkrems commented 9 years ago

There's no 1:1 mapping between CSON and JSON (see: a: 1/0 which can't be expressed in JSON), so I wouldn't be too concerned about how the JSON-equivalent. :)

jamen commented 9 years ago

I like the idea, but it doesn't feel right.

As CSON is a declarative language, if there were ever a standard for CSON and parsers made in other languages I would personally omit this aspect because Regex is represented many different ways throughout multiple languages, where as things like numbers, strings, etc. have some uniformity across languages.

texastoland commented 8 years ago

This is now blocking noseglid/atom-build#178 :crying_cat_face:

jkrems commented 8 years ago

@jamen I think that's not necessarily a concern. E.g. there's precedent in projects like https://github.com/ua-parser/uap-core/blob/master/regexes.yaml where the same set of regexes, in that case stored in a yaml file, is shared across many languages.

@dnalot I'll try to figure out how much it would take to add support.

jkrems commented 8 years ago

Added supported in v1.3.0