Comments (3)
I think that is a good idea. It could simplify these tricky cases under a single "right way" to do things.
In general I think instance methods are also a good programmer experience. If you can stumble on a place to start, the available fields and methods are easy to find.
As for being unusual, maybe that is true. Usually I think parser combinator libraries emphasize functions over classes. On the other hand, functions are still exported in modules and it pretty much looks similar if you squint a bit.
On scope
Are there other combinators that could be built in steps like this? The manyXXX
"family" of combinators seem to fall under this quite nicely.
I looked at the combinators that are available now, and didn't notice any that would fit as well right now. However, you mentioned string casing, then there is also #98 that could have a similar interface (although I haven't read that with a lot of thought yet). I'm thinking this could possibly extend to creating parsers as well as combinators.
Ideally I think the best would be to have a similar experience for all of these in some way.
Tree shaking support
This is a bit of an open question. I'm not sure what the state of tree shaking is these days with new bundlers having popped up since the last time I looked at this a couple of years ago.
By default, I think at some point instance methods could not be eliminated, but this might very well have changed.
In fact, it might be a good idea for me to delve into this subject a bit - maybe the entire project could use some visibility on the status of this 🤔
from parjs.
Another note
@sp3ctum lol we posted at the same time
A lot of combinators such as manyTill
and manySepBy
can have their return value tweaked.
What if each parser returns several named values, and the thing you get when you call parse
is just the return value with the name value
? However, you can access the rest of the returns using a returns
combonator.
This combonator would let you remap value
using the other things parsers captured that aren’t the default return.
Like this:
const manyWithSeps = many().sep(",").returns(
dict => [...dict.value, ...dict.separators]
)
This enables a lot of versatility without complicating the interface. The extra captured values are there if you need them.
from parjs.
@sp3ctum
I'm glad you like the idea!
I do agree about instance methods, they really are nice to have and make exploring a library’s API much easier.
Looking at tree shaking sounds like an promising avenue for research. Honestly I’m not even sure if the library tree shakes properly. I tried to make it work and I'm pretty sure I tested it ages ago but like you said, bundlers have changed since then.
About creating parsers – you’re right! The number parsers could be created this way for example, instead of having to pass an object. Or maybe it’s just another option.
You could have one char
parser and one charCode
parser and just tweak them to filter the character you want to read.
Another idea is to be able to “extend” existing combinators or parsers with “more of the same”. For example:
const x_or_y = or("x", "y")
const or_z = x_or_y.or("z") // Parses "x" | "y" | "z"
const x_then_y = then("x", "y")
const xyz = z_then_y.then("z") // Returns ["x", "y", "z"]
I think it’s especially convenient with then
since then you can build a sequence incrementally. The current approach, to use flatten
, has always felt wrong with me.
One problem with wrapping stuff with functions all the time is the overhead. In Haskell you have crazy optimizations that inline function values, unroll recursion through magic, and get rid of allocations. But in most languages calling a function that isn't a full-on class method can never be inlined
So something like several nestings of then
will always have performance overhead since each nesting corresponds to a function that must be called. But this way users don't have to nest thens and can instead build them.
from parjs.
Related Issues (20)
- docs: add a friendly guide for users
- docs: create documentation explaining how to create new parsers
- refactor: replace overloaded functions with mapped types HOT 1
- Reconsider “quiet” parsers HOT 1
- refactor: Get rid of namespaces HOT 1
- Debugging feature: textual descriptions for parsers HOT 2
- Debugging feature: a zero key
- docs: add performance comparison to other parsing techniques
- test: assert parser positions after combinator failures
- test: add type level tests HOT 2
- feature: Case insensitive string parsing HOT 4
- Example of custom parser from `README.md` doesn't work HOT 4
- Releasing a new version is having challenges HOT 4
- Main documentation describes an upcoming version instead of the current one HOT 1
- More unicode support for character parsing: `anyUniCharOf` and `uniCharCodeWhere` HOT 1
- Proposal - venturing outside the Basic Multilingual Plane
- StringLen parser uses substring incorrectly, leading to incorrect results HOT 1
- Help: `or()` is not working HOT 6
- Can I throw error inside map combinator? HOT 1
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 parjs.