Replies: 1 comment 2 replies
-
Hey Daniel, thanks for dropping by! It's a bit of a tough question, I'm not sure just how well this fits into Superpower's model, but it seems like it's worth a shot. I haven't attempted it myself, but this is how I'd first try tackling it. Whipping this up in a hurry - apologies if it's a bit half-baked, would really love to hear how it goes. 1. Custom token kinds Superpower parsers are almost always written around enums, but (unless we've broken anything in the last few versions 😅 ) it's possible to use any arbitrary token type. I'd try classes: [Token(Description = "number")]
class Number: YourToken { }
[Token(Description = "indent")]
class Indent: YourToken
{
public int Depth { get; }
public Indent(int depth) { Depth = depth }
} 2. Custom tokenizer There's an example of one of these in the repo (test cases perhaps)? The idea is to discard insignificant whitespace, while keeping track of what whitespace is significant (the good old imperative way :-)) and yielding up Imagining this is a fragment of your source text: loop:
print 'hi!' The tokenized representation will be something like 3. Context-sensitive parser Here's where things get a bit awkward; expression-y things are fine and you'd write them like any other Superpower parser. Imagine you've created those rules as static members in an
var parsers = new StatementParsers(0);
// StatementParsers ctor:
public StatementParser(int requiredIndent)
{
_requiredIndent = new Indent(requiredIndent);
} In public TokenListParser<YourToken, Statement> Statement { get; } = Token.EqualTo(_requiredIndent).IgnoreThen(Loop.Or(...));
public TokenListParser<YourToken, Statement[]> Block { get; } = Statement.AtLeastOnce();
public TokenListParser<YourToken, Loop> =
from loop in Token.EqualTo(new LoopKeyword())
from colon in Token.EqualTo(new ColonToken())
from body in new StatementParsers(_requiredIndent.Indent + 2).Block
return new Loop(body); Lots of mechanics here to figure out - probably much nicer making stateless tokens like Hope this helps, good luck with it! |
Beta Was this translation helpful? Give feedback.
-
I'm working on parsing a language that uses a two space indent to denote blocks, rather like Python or YAML. I'm wondering if someone can point me to an example of parsing this kind of thing with Superpower.
Thanks,
Daniel
Beta Was this translation helpful? Give feedback.
All reactions