Issue1106

Title Support string parameters in option parser
Priority feature Status resolved
Superseder Nosy List florian, jendrik, malte, silvan, simon
Assigned To Keywords
Optional summary
PR: https://github.com/aibasel/downward/pull/170

Created on 2023-08-18.14:04:44 by florian, last changed by florian.

Summary
PR: https://github.com/aibasel/downward/pull/170
Messages
msg11494 (view) Author: florian Date: 2023-11-22.01:08:09
Thanks. This is merged and documented in the wiki now.
msg11491 (view) Author: malte Date: 2023-11-21.10:39:09
I don't need to look at it again.
msg11490 (view) Author: florian Date: 2023-11-20.22:51:06
Thanks. I did one more substantial change after your last comment and changed the timing of where the string is unescaped. I think this makes the code much nicer now and would propose to merge it like this. Do you want to have another look now that the code changed or are you still fine with merging?
msg11488 (view) Author: malte Date: 2023-11-20.15:18:18
Thanks! I left some comments. I didn't do a full code review because I don't have the time, but if you're happy to proceed without this I have no objections. The syntax we support would need to be documented on the wiki somewhere.
msg11487 (view) Author: florian Date: 2023-11-16.19:08:52
Thanks for the review. I updated the pull request to now also support escaped quotation marks.
msg11368 (view) Author: malte Date: 2023-09-11.17:32:26
I had a look and left one comment.
msg11282 (view) Author: florian Date: 2023-08-18.14:54:12
Since I wrote it down, I also got started with the implentation (PR linked in the summary). It only supports strings without nested quotation marks so far.
msg11281 (view) Author: florian Date: 2023-08-18.14:04:44
This came up in the last sprint planning and again on the Discord channel. We also already discussed this when we rewrote the option parser. Back then we decided against it as no string options are currently used in the planner. But it seems to be a feature that is used in several branches and forks, so I think YAGNI doesn't apply.

I'll copy my answer from Discord which has some hints of where to start:

---

We have this on our backlog as something that we want to do eventually (FD-9). One thing we have to work out there is how strings should be quoted and how quotation should be escaped. So far, we stuck closely to Python syntax (e.g., we use [] for lists, we use keyword args, etc.) so having a a string literal like "foo\"\\bar" or 'foo\'\\bar' to represent foo"\bar would make sense.

We then would need a separate TokenType for string literals (token_stream.h) and have to adapt the lexer to parse quoted strings correctly (construct_token_type_expressions and possibly split_tokens in lexical_analyzer.cc). With quotation, I don't think we'll have any issues with string tokens like "(" "infinity", "3.14", "true", "4k", "let", "SILENT", etc. which would otherwise clash with other types.

As the next step, the syntax analyzer should parse Tokens of the new type into LiteralNodes. I don't think we need a new class or any special handling here. We can just add the new type to the switch in parse_node to be treated in the same way as BOOLEAN, INTEGER, and FLOAT.

In LiteralNode::decorate(), we then have an additional case, where we have to create a DecoratedASTNode. I wouldn't abuse SymbolNode for this but rather create a new class StringLiteralNode analogous to BoolLiteralNode, IntLiteralNode, FloatLiteralNode.

In LiteralNode::get_type(), we have to return the correct type, which means we have to register it first (insert_basic_type<std::string>(); in TypeRegistry::TypeRegistry()).
I think this is it and the only complicated part in there is to parse quoted strings correctly. For a quick hack to just get this working for most cases, you could use something like this in construct_token_type_expressions (untested):
{TokenType::STRING_LITERAL, R"("[^"]*")"}

This would not allow " inside a string literal, and thus also no escaping but it would probably be fine for most cases.
History
Date User Action Args
2023-11-22 01:08:09floriansetstatus: chatting -> resolved
messages: + msg11494
2023-11-21 10:39:09maltesetmessages: + msg11491
2023-11-20 22:51:06floriansetmessages: + msg11490
2023-11-20 15:44:37simonsetnosy: + simon
2023-11-20 15:18:18maltesetmessages: + msg11488
2023-11-16 19:08:52floriansetmessages: + msg11487
2023-09-11 17:32:26maltesetmessages: + msg11368
2023-08-18 14:54:12floriansetstatus: unread -> chatting
messages: + msg11282
summary: PR: https://github.com/aibasel/downward/pull/170
2023-08-18 14:04:44floriancreate