-
-
Notifications
You must be signed in to change notification settings - Fork 281
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
More feature-full format_description macro #428
Comments
Well… |
AFAIU What's needed is a form of nesting. Something like
Note that when recursing, whitespace inside brackets is significant, otherwise This can get a bit verbose, so
or
or
Actually, looking at that last example I realized that I initially tough
|
You're assuming literals will always be one character. This certainly isn't the case. The only way around this, realistically, is to have it be quoted. This isn't ideal because now we're forcing people to either escape the quotes or use raw strings. We'd also have to handle escaping within the literal ourselves.
|
Yes that's what I meant,
I did think about multi-character :) The "significant whitespace" rule just avoids some corner-cases and makes recursion easier.
The only in-litteral escaping needed is for I'm not sure how you feel about adding sigils to the grammar ? They enable shorter descriptions and look nicer / more readable, but they make the grammar more complex, aren't as future-proof and don't seem to work well for multi-bracket |
Where would a user need to explicitly state that something is I think that if you want to have either a Anything outside of brackets is a no-go for compatibility reasons. That includes using parenthesis, braces, and angle brackets, as well as prefix or postfix sigils outside of brackets. I also don't think sigils are more readable — the current format description arose because the previous version used too many without clear meaning or purpose. |
My initial suggestions needed an explicit
|
That is my understanding as well. Note that the output from the parser isn't guaranteed, only the behavior, so the optimization noted is not mandatory. For the recursive part, I still need to think about escaping. The left bracket is simple — double it as is the case for top-level items. The right bracket does not have this capability right now, as the grammar was designed without thinking of nesting. I will need to investigate whether it is possible to permit escaping the right bracket by doubling it in a backwards-compatible manner. My immediate concern is that consecutive right brackets will be quite common, and forcing a space between them isn't ideal. |
Somehow I never considered this before now, but in order to output these values from the runtime parser, owned format descriptions (#429) are necessary, as I can't emit a slice to a newly-created object. With that said, though, the implementation isn't too difficult now that I've rewritten things. |
Mixed news on this front. I was able to implement option items fully with the aforementioned syntax, such that this test passes: assert_eq!(
format_description::parse_owned("[optional [:[year]]]"),
Ok(OwnedFormatItem::Compound(Box::new([
OwnedFormatItem::Optional(Box::new(OwnedFormatItem::Compound(Box::new([
OwnedFormatItem::Literal(Box::new(*b":")),
OwnedFormatItem::Component(Component::Year(Default::default()))
]))))
])))
); Unfortunately, removing the colon results in something unexpected (at first glance). Because I might be able to work around this by carving out an edge case for nested format descriptions specifically ("optional" and "first"), but I will need to be careful in doing so. I still need to think about how to permit a literal Edit: I've successfully worked around that issue. The only outstanding issue for parsing optional items is the escaping of |
This has been implemented for the runtime parser on main. The escape character for a right bracket is a backslash. The only remaining step (implementation-wise) is to port it to the macro parser, which should be relatively simple. Then I need to actually document this… It doesn't matter publicly, but the optimization of @vincentdephily Please give this a test run! If you run into any bugs, let me know. |
It works, thanks. I only tested my specific format and
|
|
It's not pretty, but we can probably live with those escapes for now. I find I'll give the updated docs and macro a look when they're ready. |
The reason You did give me a thought, though. As there is It would be ideal if Rust supported default type parameters for functions, as I could then have marker structs to indicate the desired behavior (current, borrowed, or owned). This is not the case, however. Right now I think the wishlist for 0.4 is very small, so an issue isn't necessary. |
Alright, round two! I have pushed a change that introduces backslash for escapes everywhere. You should be able to do For backwards compatibility, I have made this change by introducing versioning internally. |
I have just pushed a commit that permits "optional" and "first" items in the The commit also fixes a bug where The only remaining implementation issue is being able to set the version when using |
Alright. I've changed it from Please give it a test! The only remaining issue is documentation. |
This has been released as part of |
It seems |
Whoops! I neglected to publish |
CI will do its thing, but I have just released it. |
I'm parsing a rfc3339-ish format with some flexibility (and UTC/Local offset selected out of band). I've ended up with the following incantation:
That does the job, but I'd prefer using
time::macros::format_description
for readability (and maybe compile-time goodness, I'm not sure). But AFAICT it's missing support foroptional
,first
, andcompound
.Would it be possible to extend the grammar ?
The text was updated successfully, but these errors were encountered: