通过textmate语言语法中的空白缩进进行匹配

时间:2017-05-09 04:23:15

标签: regex parsing grammar textmate

我有一个DSL,我试图写一个textmate语法,所以我可以有语法高亮(在VS代码而不是textmate,但它使用他们的语法)

DSL代表树节点,是空格缩进,并映射到JSON

大部分内容非常简单,但它有一些奇怪的结构,我不确定如何与textmate语法相匹配。 DSL的一个例子如下。

我可以匹配"标签"和简单的标量类型"属性"和单行"阵列"很好,但我在多线阵列和对象上有点迷失 - 我不知道如何在缩进改变时停止匹配它们的内容。我认为当我点击一个已知模式时我可以停止匹配,但它在数组最后一行的一个项目和一个"标记"之间是不明确的。除了他们的缩进程度

有没有办法根据缩进单独匹配这些?

示例DSL文件:

fooTag
  foo An unquoted string
  bar 42
  baz "42"
  qux true
  barTag
    qux{}
      foo A string called foo on the qux object
      bar null
    foo[] in arrays each space separated token is a separate string "unless quoted"
    bar[]
      some arrays of space separated tokens look like this when it is a long
      list with lots of items
    baz[]
      arrays can be mixed types like this one which contains the number 42 and
      the constants true false and null and the rest of the tokens are all
      strings and even nested arrays and objects as follows
      [] 1 2 3
      []
        indented array of tokens
      {}
        foo 42
        bar "42"
    some
    more
    tags

示例JSON输出:

[
  {
    "name": "fooTag",
    "model": {
      "foo": "An unquoted string",
      "bar": 42,
      "baz": "42",
      "qux": true
    }
  },
  [
    {
      "name": "barTag",
      "model": {
        "qux": {
          "foo": "A string called foo on the qux object",
          "bar": null
        },
        "foo": [
          "each", "space", "separated", "token", "is", "a", "separate",
          "string", "unless quoted"
        ],
        "bar": [
          "some", "arrays", "of", "space", "separated", "tokens", "look",
          "like", "this", "when", "it", "is", "a", "long", "list", "with",
          "lots", "of", "items"
        ],
        "baz": [
          "arrays", "can", "be", "mixed", "types", "like", "this", "one",
          "which", "contains", "the", "number", 42 "and", "the", "constants",
          true, false, "and", null, "and", "the", "rest", "of", "the", "tokens",
          "are", "all", "strings", "and", "even", "nested", "arrays", "and",
          "objects", "as", "follows",
          [ 1, 2, 3 ],
          [ "indented", "array", "of", "tokens" ],
          {
            "foo": 42,
            "bar": "42"
          }
        ]
      }
    },
    [
      {
        "name": "some",
        "model": {}
      }
    ],
    [
      {
        "name": "more",
        "model": {}
      }
    ],
    [
      {
        "name": "tags",
        "model": {}
      }
    ]
  ]
]

0 个答案:

没有答案