-
Notifications
You must be signed in to change notification settings - Fork 113
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
Locally disable %whitespace #44
Comments
@olivren, thanks for the report. I tried to handle this situation without making any code change in peglib.h, and found the following solution.
The key of the solution is to use token operators effectively. The peglib ignores white-spaces in text surrounded by a token operator. Also I discovered that when we use nested token operators in a rule and call Please let me know if the above solution can work for you. Thanks! |
Thanks for your answer. I played a bit with the placement of the token operators in my grammar, and I found a combination that works for my case. I am not sure I understand exactly the logic of when whitespaces are ignored though. Here is the full code for reference. It may be useful for other users. It implements python-style string literals (no string prefix, no raw string, no \o \x \N \u escape codes, and no \ newline management)
rule["StrDblQuot"] = [](const SemanticValues& sv) -> string {
ostringstream ss;
for(string& e: sv.transform<string>())
ss << e;
return ss.str();
};
rule["StrEscape"] = [](const SemanticValues& sv) -> string {
string tok = sv.token();
assert (tok.size() == 2);
switch(tok.back()) {
case '\\': return "\\";
case '\'': return "'";
case '"': return "\"";
case 'a': return "\a";
case 'b': return "\b";
case 'f': return "\f";
case 'n': return "\n";
case 'r': return "\r";
case 't': return "\t";
case 'v': return "\v";
default: return tok;
}
};
rule["StrDblQuotChars"] = [](const SemanticValues& sv) -> string {
return sv.token();
};
|
Looks great! I labeled it as 'information', so that users could benefit from it. |
Hello, I have a similar problem with whitespaces. Not sure if I should contribute to this issue or create a new one. I apologize in advance if I chose the wrong option. I'm trying to write a grammar where the whitespace matters only in one point: between an identifier and a
I'd like to parse I believe that cpp-peglib's syntax works exactly like this as well. Is it possible to use Tokens or any other solution to say that no whitespace is allowed between |
@Beedeebee, thanks for the feedback. I think there is no easy solution for that unless you give up using A workaround that I can come up with this situation is as below: I know this is not a super beautiful solution, but it works with |
Thanks, the workaround you're suggesting works perfectly for me! :) |
I have a grammar for a programming language. It defines %whitespace, because whitespaces are not significant.
Now, I want to parse string literals with a rule like this:
StrEscape and StrChars both have rules that produces
std::string
, that I combine together in the rule of StrQuot. The problem is that the whitespaces in the strings are ignored, and thus the resulting string has all the whitespaces filtered out.Is there a way to deactivate locally the %whitespace rule?
The text was updated successfully, but these errors were encountered: