Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing code #63

Open
drekka opened this issue Jan 18, 2024 · 1 comment
Open

Parsing code #63

drekka opened this issue Jan 18, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@drekka
Copy link

drekka commented Jan 18, 2024

Hi, I'm looking at Patterns to try and pass strings that contain small chunks of source code. I'm trying to work out if I can get it to pass a string that looks like this:

x = "abc"

The part I'm troubled with is the string declaration and I'm not sure if Patterns can handle a pattern where I need a double quote following by any number of characters and ending with a second double quote.

The value "abc" seems pretty straight forwards. But what if it contains a double quote. i.e "a\"bc".

@drekka drekka added the enhancement New feature or request label Jan 18, 2024
@wilhuff
Copy link

wilhuff commented Feb 29, 2024

A typical way you approach this problem (for nearly every parsing system) is to define a quoted string as consisting of a zero or more runs where each run is either

  • One or more characters other than " or \\
  • An escape sequence where you explicitly enumerate the escaped characters you accept, including \"

Something like:

let quotedStringChars = Capture(name: "chars", (!OneOf("\\\"")  OneOf(any))+)
let escapeSequence = "\\"  Capture(name: "escape", OneOf("bnrtv\""))
let quotedStringContent = quotedStringChars / escapeSequence
let quotedString = "\""  quotedStringContent*  "\""

Handling this requires combining the resulting series of captures, mapping the escape character code to its equivalent (e.g. "n" maps to "\n") before doing anything with the Match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants