-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can this library support customized Token parsing? #10
Comments
Hello! It is certainly possible, the library is completely input type agnostic. To be able to parse non-character elements, you'd pass in the sequence of tokens to the reader. The passed in source is expected to be read with an indexer operator, so a Once you got your reader, you'd define your basic helper combinator to match on a certain token type. The combinator template <TokenKind Kind>
bool has_token_kind(Token const& t) { return t.kind == Kind; }
template <TokenKind Kind>
inline constexpr auto match = pc::one[pc::filter(has_token_kind<Kind>)]; This will give you a parser that can match a certain kind of token (for ex. |
Thanks for the reply. For some reasons, I need to do some type check of parsing the I think it is still a bit complex for me to tweak the CppCmb compiler. If you have time, can you help to add such sample code for a Token class? My actual Token class could be:
Which does not use the Another question is: is it possible to add some operator precedence climbing feature(maybe it is called pratt parser, see:Operator-precedence parser - Wikipedia )? I mean it is better for handling the expressions like I have a discussion/request here in another peg based parser here: |
To be honest, I don't think I'd use any generator or library for parsing C++, simply because it's context sensitive (well in the case of C++ it's even more complex than context sensitive). Most parsing tools primarily target context-free languages, ones that can be well-structured without any semantic analysis, CppCmb is no exception. The parser has no direct ability to feed back or forward information for other elements of a front-end (well, technically one could do it with some transformation combinators, but never played around with the idea in practice). If you just need a "good-enough" parser, or a parser that can produce ambiguous trees that you can disambiguate with post-processing steps, you might want to take a look at TreeSitter, which was made especially for editors for incremental parsing, and parsing ambiguous constructs that can be disambiguated on the fly.
I'll try to find some time during the weekend.
When I wrote this library, I simply didn't feel the need, as one can encode precedence directly in the grammar, as shown in the last example of the wiki. It should be totally possible, just needs more template-metaprogramming that unrolls the precedence list into the appropriately nested grammars. |
Hi, thanks for the detailed reply. I think parsing every component of the C++ code is too complex. But my idea is that a simple C++ parser can extract the I have read your sample code in your example A full expression parser, it is complex, I'm not sure, but I can't find some document that let a client user to add the
It looks like the Thanks. |
Hi, this library is nice! Good work! Though it has many advanced C++ template code which is a bit hard for me.
Back to my question, I mean in my application, I have my own lexer(mainly the C-preprocessor) implemented myself, so my question is: can the library support parsing rules for custom lexer.
For example, the lexer has some kinds of
std::vector<Token>
supplied, theToken
class may have definition like:So, I would like this library to parse the
Token
stream not thechar
stream.Any suggestions?
Thanks.
The text was updated successfully, but these errors were encountered: