Skip to content
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

Should expression be a subtype of expression_statement instead of a child? #94

Closed
andycraig opened this issue Nov 29, 2020 · 4 comments
Closed

Comments

@andycraig
Copy link

andycraig commented Nov 29, 2020

In grammar.js, expression is a choice of expression_statement: https://github.com/tree-sitter/tree-sitter-python/blob/master/grammar.js#L183-L184

In node-types.json, expression is a child of expression_statement: https://github.com/tree-sitter/tree-sitter-python/blob/master/src/node-types.json#L1164-L1182

Is this correct? Since expression is a choice of expression_statement, I would have expected expression to be a subtype of expression_statement in node-types.json, not a child.

(I'm interested in this because I want to identify nodes that are valid (syntactically correct) Python by themselves so that I can send them to a REPL. My method for doing this is to check whether they are subtypes of _compound_statement or _simple_statement. But this breaks down for expression_statement, which has children instead of subtypes.)

Thank you for making tree-sitter-python!

EDIT: Fixed link.

@somebody1234
Copy link

somebody1234 commented Nov 29, 2020

:/ you linked the same thing twice

edit: also note that node-types.json is generated (from grammar.js)

edit 2: i'm guessing what you probably mean then is, should expression_statement be a supertype, which can certainly be done (one of the choices isn't a node type so that will need to be refactored but otherwise it looks fine) so we'll just have to wait for max's reply i guess.

@andycraig
Copy link
Author

Hi @somebody1234, thank you for your reply. I think I may have misunderstood how subtypes and children work. I thought that subtypes generally corresponded to choice, but reading the docs again I see that they correspond to supertypes.

It sounds like adding expression_statement as a supertype would resolve this particular case but I'd probably need the same thing to be done for lots of different node types (across multiple languages too, as I'm aiming for this to be language-independent). So I think what I actually need to do is modify my algorithm to identify child nodes corresponding to choice.

@maxbrunsfeld
Copy link
Contributor

Conceptually, I don't think that an expression is a subtype of an expression_statement. In my mind, statements and expressions are two distinct categories of nodes. An expression_statement is a simple type of statement, which contains an expression (or an assignment, which can't occur as a normal expression).

@andycraig
Copy link
Author

Hi @maxbrunsfeld, it sounds like my concept of subtypes and supertypes in the context of Tree-sitter was not correct. Thank you for clarifying. I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants