1

I have a grammar where I want to have some whitespace (including newlines) in between two terms. There should be some whitespace, i.e. it should fail if the two terms are touching, however there can be as much whitespace as desired. The issue I'm coming across is that whitespace and newlines are different tokens. I can't work out how to generally make "at least one" in nearley.

4

2 回答 2

2

我设法用 EBNF 修饰符解决了这个问题:

ws -> %WS | %NL

# At least one whitespace
someWS -> ws:+

# none or some whitespace
manyWS -> ws:*
于 2020-02-17T17:26:46.510 回答
1

如果您使用moo.js, 则可以使用正则表达式预定义空格。

@{%
  const moo = require('moo')
  let lexer = moo.compile({
    space: {match: /\s+/, lineBreaks: true}
    // other rules
  });
%}

@lexer lexer

_ -> null | %space {% d => null %} // any amount of white space or none
__ -> %space {% d => " " %} // at least one white space token

于 2021-09-16T22:59:44.387 回答