Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

example-or-fix-request: delimited-dsl where closing delimiter is also an escape leader #61

Closed
dancingbug opened this issue Apr 11, 2022 · 1 comment

Comments

@dancingbug
Copy link

dancingbug commented Apr 11, 2022

How to write a delimited-dsl closing delimiter that is the same as an escape leader?

suppose for sake of illustration that in the object language to be parsed, strings were delimited by %
Then I would want to match delimited strings as follows:

  • "%one two%" --> "one two"
  • "%one %% two%" --> "one % two"

In Pascal, for instance, the basic syntax of a string is delimited with single quotes.
If you want to escape a single quote inside such a string, you type it twice.

What is the recommended way to handle this?

here is a snippet that did not work

struct pct_quoted {
  static constexpr auto quote_sym = lexy::symbol_table<char>.map<'%'>('%');
  static constexpr auto rule = [] {
    auto delim = dsl::percent_sign;
    auto escape = dsl::escape(delim).symbol<quote_sym>();
    auto pct_q = dsl::delimited(delim, delim);
    return pct_q(dsl::ascii::character,escape);
  }();
};

void test_pct_quot() {
  auto input = lexy::zstring_input("%one%%two%");

  lexy::trace<pct_quoted>(stdout,input);
};
 1:  1: pct_quoted:
 1:  1: - literal: %
 1:  2: - token: one
 1:  5: - literal: %
 1:  6: - finish

Thank you for this nice library.

@foonathan
Copy link
Owner

A simple fix would be to change the closing delimiter to match % but not %% using dsl::not_followed_by: https://lexy.foonathan.net/playground/?id=xaazc4dMo&mode=tree.

Repository owner locked and limited conversation to collaborators Apr 11, 2022
@foonathan foonathan converted this issue into discussion #62 Apr 11, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants