datalust / superpower

A C# parser construction toolkit with high-quality error reporting
Apache License 2.0
1.05k stars 98 forks source link

Accessing previous token from within parser!? Parsing function documentation/metadata #108

Closed xDGameStudios closed 4 years ago

xDGameStudios commented 4 years ago

Hi everyone! I'm making a parser for a custom language. This language uses JSDoc as way of adding function documentation and metadata. My idea was to tokenize the input with the following types of comments

// -> single line comment
/* */  -> multiline comment
/// -> documentation

then I would strip them from the TokenList...

var comments = tokens.where(x=> x.Kind == Kind.SingleLComment || x.Kind == Kind.MultiLComment)
var documentation = tokens.where(x=> x.Kind == Kind.Documentation)
var code = tokens.where(x=> !comments.Contains(x) && !documentation.Contains(x));

now the parser can parse the code tokens freely!! Then when needed ie.: when I parse a function declaration I would like to query for any amount of previous tokens while condition is met

from functionKeyword in MatchToken(...)
from ... in ...
from documentation in Token.PreviousWhile(functionKeyword, myFunction)
select new FunctionSyntax(functionKeyword, ..., documentation);

bool myFunction(Token<MyKind> previousToken)
{
        return previousToken.Kind == Kind.Documentation
}

but as the documentation tokens were stripped from the code ones one would need to query from within the documentation list... or even have each token hold a reference to the previous one... am I overthinking this? What would be the best/clean approach to do something like this?!

Or maybe instead of using the previous token I could look for the nearest-previous span instead.. for example... look for the nearest-previous locations compared to the current span token inside comments, documentation and code token list... then if the span is from a token in the documentation retrieve it... is this do-able?!