Nearley at least one

102 Views Asked by At

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.

2

There are 2 best solutions below

2
On BEST ANSWER

If you are using moo.js, than you can predefine whitespace with regular expressions.

@{%
  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

0
On

I managed to solve this with EBNF modifiers:

ws -> %WS | %NL

# At least one whitespace
someWS -> ws:+

# none or some whitespace
manyWS -> ws:*