Giter Club home page Giter Club logo

Comments (3)

wavebeem avatar wavebeem commented on June 30, 2024

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.

farcaller avatar farcaller commented on June 30, 2024

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.

wavebeem avatar wavebeem commented on June 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.