question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to preserve whitespace separators?

See original GitHub issue

Issue type

  • Bug Report: no
  • Feature Request: no
  • Question: yes
  • Not an issue: no

Prerequisites

  • Can you reproduce the issue?: yes
  • Did you search the repository issues?: yes
  • Did you check the forums?: yes
  • Did you perform a web search (google, yahoo, etc)?: yes

I am struggling to make PEG.js parser keep original whitespaces of the equation.

Current behavior: 2 * 5 + SUM(1, 2, 3)

[
   "2",
   "*",
   "5",
   "+",
   "SUM",
   "(",
   [
      "1",
      ",",
      "2",
      ",",
      "3"
   ],
   ")"
]

Desired behaviour: 2 * 5 + SUM(1, 2, 3)

[
   "2",
   " ",
   "*",
   " ",
   "5",
   " ",
   "+",
   " ",
   "SUM",
   "(",
   [
      "1",
      ",",
      " ",
      "2",
      ",",
      " ",
      "3"
   ],
   ")"
]

Grammar to copy: https://pastebin.com/zpwqT6Uw PEG.js playground https://pegjs.org/online

What am I missing?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Seb35commented, Apr 2, 2020

@marek-baranowski Another gentle ping 😺

Also, I wrote a PEG.js plugin pegjs-syntactic-actions to facilitate debugging of grammars, and specifically see what characters are captured by what rule independently of the actions, which is probably your issue here as explained by @StoneCypher.

The reasoning of this plugin is: I find it is often/sometimes difficult to understand the global result when it is not what we expect, because it results from the combination of many small actions, and finding the action which behaves badly/stangely could be time-consuming. With this plugin, we see what rule captures what character, and it gives the name of the action to act on.

1reaction
StoneCyphercommented, Apr 2, 2020

oh wow this is really neat actually

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preserve whitespaces when using split() and join() in python
You want to use re.split() in that case, with a group: re.split(r'(\s+)', line). would return both the columns and the whitespace so you...
Read more >
Python | Split String and Keep Whitespace - Finxter
Summary: To split a string and keep the delimiters/separators, you can use one of the following methods: (i) Using the regex package and...
Read more >
white-space - CSS: Cascading Style Sheets - MDN Web Docs
Sequences of white space are preserved. Lines are broken at newline characters, at <br> , and as necessary to fill line boxes. pre-line....
Read more >
Trimming Whitespace - Oracle Help Center
See "How the PRESERVE BLANKS Option Affects Whitespace Trimming" for details on how to prevent whitespace trimming. Table 10-5 Behavior Summary for Trimming ......
Read more >
Split a String Keeping the Whitespace in JavaScript - Linux Hint
The string can be split keeping the whitespaces in JavaScript by using the “split()” method in combination with the following approaches: “join()” method....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found