Comments (3)
Hey @farcaller
First of all, I would highly suggest using a pre-existing Markdown parser if at all possible. Markdown is very tricky to parse correctly. https://runkit.com/wavebeem/parsimmon-issues-314
It's a little hard to say what's wrong with the parser based on just this snippet.
I formatted your initial snippet a little bit, and it looks right to me at a glance.
x
.then(
notFollowedBy(string("*"))
.then(r.Inline)
.or(r.Strong)
)
.atLeast(1);
When in doubt, you might find P.alt(a, b)
easier to read than a.or(b)
from parsimmon.
I know markdown is tricky to parse but I'm in a situation where I need a PEG-based markdown parser and I'm up for the sufferingchallenge.
Apparently my issue was that I placed or
outside on the then
, it's indeed getting tricky to read the code.
FWIW, this is how the parser for my current case ended up looking:
function lookbehind(p, ofs) {
if (ofs === undefined) {
ofs = -1;
}
return Parser((input, i) => {
const charI = i - ofs;
if (charI < 0) {
return makeSuccess(i, '');
}
const res = p._(input, charI);
res.index = i;
return res;
});
}
function EmphasisUnderline(r) {
const lfdr = string('_')
.notFollowedBy(whitespace)
.then(alt(
notFollowedBy(r.PunctuationChar),
lookahead(r.PunctuationChar).then(lookbehind(alt(whitespace, r.PunctuationChar), 2))
));
const lfdrNotRfdr = lfdr
.then(
lookbehind(whitespace, 2)
.or(
lookbehind(r.PunctuationChar, 2)
.lookahead(alt(whitespace, eof, r.PunctuationChar))
)
);
const rfdr = string('_')
.notFollowedBy(lookbehind(whitespace, 2))
.then(alt(
notFollowedBy(lookbehind(r.PunctuationChar, 2)),
lookbehind(r.PunctuationChar, 2).lookahead(alt(whitespace, eof, r.PunctuationChar))
));
const rfdrNotLfdr = rfdr
.then(
lookahead(alt(whitespace, eof))
.or(
lookahead(r.PunctuationChar)
.then(lookbehind(alt(whitespace, r.PunctuationChar), 2)))
);
return (
lfdrNotRfdr
.then(
notFollowedBy(rfdrNotLfdr).then(r.Inline).atLeast(1)
)
.map(children => ({
type: 'emphasis',
children,
}))
.skip(rfdrNotLfdr)
);
}
Thanks for your help!
from parsimmon.
Apparently my issue was that I placed or outside on the then, it's indeed getting tricky to read the code.
Aha! I wasn't sure enough to suggest it, but I'm glad that my formatting made that clearer. I love using prettier to autoformat my code, but sometimes those lines can get a little dense, especially with Parsimmon.
Good luck on your Markdown parser! 👍🏻
from parsimmon.
Related Issues (20)
- Regex dotAll support (/s flag) HOT 2
- MS Edge 18 error HOT 9
- Question: What's wrong with my parser? HOT 3
- Question: How to parse html comment HOT 4
- Ignoring XML like tags when encountered inside XML like tags HOT 2
- The library doesn't include type definitions HOT 1
- Add parser methods wrapTimes, wrapAtLeast, wrapAtMost and wrapMany HOT 6
- Unneeded code dublication in formatGot HOT 4
- How can I parse the inside and outside of the parentheses? HOT 1
- Question HOT 9
- Question about parsing "define <name> [as <internalName>]" style grammar HOT 4
- Question: Is it possible to get consumed part of string from the parser result? HOT 1
- Import Parsimmon HOT 7
- Question: how to do an intersection of optional parsers? HOT 4
- Parsimmon.index yields incorrect position for tokens right before a newline ('\n') HOT 10
- Incorrect error formatting HOT 5
- Is there a way to do something like "everything up until SomeParser" HOT 4
- Use all the groups from regex HOT 3
- Slow parsing performance 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 parsimmon.