Nearley Postprocessing is removing valid whitespaces

133 views Asked by At

New to Nearley grammar, not sure what is going wrong here. Here is my grammar to get whatever is placed inside an opening and closing parenthesis.

@builtin "whitespace.ne"

with_in_brackets ->
    "(" _ args _ ")" {% d => ({
      type: 'inside_brackets',
      argumentstring: d[2]
    })%}
    
args -> .:* {% d => d[0].join("") %}
  • Test Input - (hello, " ", world)
  • Expected Result - {type: "inside_brackets", argumentstring: "hello, " ", world"}
  • Actual Result - {type: "inside_brackets", argumentstring: "hello, " ", world"}

Notice - the actual result is compacting 4 whitespaces to 1 whitespace. Not sure why this is happening.

Any thoughts? FYI - I am using Nearley Playground to test my grammer on Safari v15 browser.

1

There are 1 answers

2
BaseScript On

Your code works perfectly, and we can check it by sending the result into the console.

with_in_brackets ->
"(" _ args _ ")" {% d => {
console.log(d[2]);
return {
  type: 'inside_brackets',
  argumentstring: d[2]
}
}%}

As you can see, it is just the browsers' specific behavior of turning multiple spaces into one " ", unless it is the <pre> tag or CSS is involved. Also, with just whitespace between ( ) you will get multiple results because of _, so make sure to handle it.