July 2, 2013

Mapping the Pyson syntax to JSON

The problem with creating a new syntax like Pyson is that it's necessary to reimplement a parser in every programming you wish to use it in. So an alternative could be to map the Pyson syntax on to JSON and only keep the semantic concepts associated with it, i.e. the
$$\text{AST} \to \text{data}$$
step.

For example, the following could be a JSON representation of a Pyson document we have seen a few times already
{
  "DateTime": {
    "#type": " @Type",
    "#args": [
      "DateTime",
      ["year", "month", "day"]
    ],
    "defaults"=[0, 0, 0],
    "format"="{year:D4}/{month:D2}/{day:D2} {hour:D2}:{minute:D2}:{second:D2}"
  }
  "obj3": {
    "#type": "@DateTime",
    "#args": [2013, 6, 24]
  }
  "period": [
    "@obj3",
    {
      "#type": "@DateTime",
      "#args": [2014, 6, 1]
    }
  ],
  "simulation": {
    "start": "@obj3"
  }
}
Here we've used strings starting with '@' to denote references and dictionary keys starting with '#' for keys with special meaning. The JSON version is somewhat more verbose and less readable than the Pyson version, however it has the advantage of being immediately parsable in any language that already supports JSON. To render it usable in the same way as Pyson, a second pass is needed to link the references and unescape strings that really should start with '@', and viceversa.

No comments:

Post a Comment