如何解析 Stylus 错误消息输出

How to parse Stylus error message output

我正在尝试解析 Stylus binary, for instance this example error from the documentation:

生成的错误消息
  ParseError: test.styl:3:16
    1| body
    2|    form input
    3|      == padding 5px
  ---------------------^
    4|


  illegal unary "==", missing left-hand operand

我基本上是在获取文件名、行和列以及最后的错误消息之后。由于似乎没有选项可以控制输出的冗长程度,因此我需要忽略所有以空格(后跟数字)或破折号开头的行。

这是我目前拥有的:.*Error: (.+):(\d+):(\d+)\n(?:\W+.*\n)+(.*\n)

虽然这可能不是理想的模式,但真正的问题是手写笔可能会输出多行,从而破坏模式。

illegal unary "==", missing left-hand operand

    at Parser.error (/usr/local/lib/node_modules/stylus/lib/parser.js:259:11)
    at Parser.equality (/usr/local/lib/node_modules/stylus/lib/parser.js:1905:23)
    at Parser.typecheck (/usr/local/lib/node_modules/stylus/lib/parser.js:1886:21)
    at Parser.logical (/usr/local/lib/node_modules/stylus/lib/parser.js:1873:21)
    at Parser.ternary (/usr/local/lib/node_modules/stylus/lib/parser.js:1857:21)
    at Parser.negation (/usr/local/lib/node_modules/stylus/lib/parser.js:1849:17)
    at Parser.expression (/usr/local/lib/node_modules/stylus/lib/parser.js:1828:24)
    at Parser.stmt (/usr/local/lib/node_modules/stylus/lib/parser.js:818:25)
    at Parser.statement (/usr/local/lib/node_modules/stylus/lib/parser.js:685:21)
    at Parser.block (/usr/local/lib/node_modules/stylus/lib/parser.js:865:21)

上面的模式将正确捕获文件名、行和列,但始终使用错误日志中的最后一行,而不是我之后的错误消息。

如何可靠地捕获错误消息?

利用这个特定的输出结构,您可以使用编号代码行之后的单换行符(?:\n)+

那么你有:

((.*Error): (.+):(\d+):(\d+))\n(?:\ +.*\n)+(?:\n)+(.*)

解释:

# First line with matching error, file name, line, column
((.*Error): (.+):(\d+):(\d+))\n   

# Non-matching group for lines starting with whitespace and multiple characters after
(?:\ +.*\n)+                      

# Non-matching group for at least one newline
(?:\n)+                           

# Lastly, match single line at the end
(.*)                     

示例: https://regex101.com/r/uQ7dM5/1