Comments (7)
sorted(strings, key=len, reverse=True)
And the name should indicate it works only for strings and that it is different from alt()
in supporting backtracking. With that in mind, wouldn't it be better to just add an optional backtrack
argument to alt()
?
Off-topic, but I see that now @jneen's repo is marked as a fork of this one; any better way to do that than contacting GitHub support?
from parsy.
@bugaevc - it would be difficult to add it into alt
, because the sorting logic only works for strings, and because we want to wrap the options in parsy.string
.
The motivations is that things like:
alt(string("a"), string("b"), string("c"))
alt(string("foo"), string("bar"))
are common enough to want a shortcut like:
one_of("abc")
one_of(["foo", "bar"])
and secondly, the backtracking issue of:
alt(string("thing", string("thing2"), string("thing3"))
which doesn't work as intended, can also benefit from something that gets the backtracking right.
For the first case, I think we'd want a more efficient implementation that doesn't use alt
etc. - what I've posted above covers the second case.
I'm not convinced we need to make it more verbose by adding something into the name to indicate the type, especially if we have two different types that it can accept. Parsimmon has something very similar, only for strings: https://github.com/jneen/parsimmon/blob/master/API.md#parsimmononeofstring
We could have two different functions, but then that would require two names, and I can't think of better ones. I think they are similar enough in purpose to benefit from being a single function.
Regarding changing the root repo - it seems the only way is to contact GitHub support, they just need confirmation from the relevant people.
from parsy.
Okay, so I've come the full circle from trying to write down the changes to alt()
I'd replace this with, to realizing it should be better done by the caller and to thinking of writing a simple wrapper around alt()
to do that.
But I'd still like a more descriptive name, how about string_from()
? And the docstring should mention backtracking (ex. "this function picks the longest of the passed strings that do match").
from parsy.
How about .keywords("thing", "thing2", ...)
?
from parsy.
@jneen that works well for multiple strings, not so good for one_of('0123456789')
.
@bugaevc similarly for string_from('0123456789')
which could be confusing.
one_of
seems pretty descriptive to me:
digit = one_of('0123456789')
boolean = one_of(["true", "false"])
Maybe we could have string_from
and char_from
, and just have two separate functions, if we want to have the data type in the name. I'm thinking of adding any_char
so maybe that would complement it.
from parsy.
I'd rather have the function accept *strings
so that you don't have to pack them in a list at the call site. If you want to pick a character from a string, call it like string_from(*'1234567890')
.
one_of
would be fine if it was the only way to alternate things (and indeed, it would make sense to name the current alt
one_of
), but it is specific to strings/keywords and the name should indicate that.
from parsy.
This is implemented now in PR #15
I implemented char_from
separately because:
string_from(*"abc")
is really pretty obscure for most Python users, and this is a really common casechar_from
implemented using thein
operator to test is many times faster. (5 times faster for 10 digits, 25 times faster for 62 chars (upper, lower, digits)).
from parsy.
Related Issues (20)
- >>= for bind HOT 14
- Missing documentation for eof parser? HOT 1
- Improve debugging: peek show next data in errors HOT 5
- Recompute line number for ParseError passed up from .bind
- Improve debugging ergonomics HOT 3
- Inline (explicit) and implicit tracing
- Missing seq import statement in tutorial HOT 1
- Help with parsing that "hangs" HOT 3
- Bug with backtracking and generate? HOT 4
- Parsy 1.3.0 fails to support 'group' keyword of `regex` function HOT 1
- async/await support in generate() HOT 6
- Release HOT 2
- Interested in a version of parsy with type annotations? HOT 3
- Allow providing a default to optional() HOT 2
- maintaining state while parsing HOT 3
- combine fails when nothing is produced by many HOT 2
- [bug] Parser.desc() causes loss of error information, simple fix HOT 2
- alt doesn't use fallback parsers when initial parser has .many()/.sep_by() HOT 1
- Processing list of tokens HOT 1
- Seeking Guidance on Implementing Parser Autocomplete HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parsy.