Replies: 2 comments
-
Not sure how this could be implemented exactly. The "actual value" of something is a semantic property, while tree-sitter operates at the syntactic level. As you said, not all languages have types that actually map onto javascript's, so this would be difficult to generalize enough to justify building the functionality into tree-sitter. This functionality seems best suited to being put into a |
Beta Was this translation helpful? Give feedback.
-
As written, I think this functionality is out of scope for Tree-sitter itself. But maybe someone will have a creative idea about how this kind of functionality can be abstracted and shared somewhere. |
Beta Was this translation helpful? Give feedback.
-
I would like a way to get the actual value of AST nodes of literals. For example if I'm using tree-sitter's JS bindings to parse Python and there's an
integer
node I want to be able to domynode.value
and get a JavaScriptNumber
(or aint
if I'm using the Rust or Python bindings) instead of having to do it myself withparseInt(mynode.text)
, which will break in a number of cases, such as when the Python code uses underscores100_000
or a binary literal0b1001
etc.Same thing with strings, if you try to access them using
.text
you get the text as it appears in the source code of the program, including things like the surrounding quotes, escaped newlines and escaped quotes, instead of their actual value. Using tree-sitter-bash I had to write this code to extract the value of string literals, but it would be good for this logic to be part of tree-sitter instead of having to do it myself, because then it would be tested by more people in more varied use cases.Python's builtin
ast
module does this (obviously) https://docs.python.org/3/library/ast.html#ast.Constant . This would never be perfect because if I'm using tree-sitter's JS bindings to parse Python code, JavaScript's data types don't correspond perfectly to Python's so if an integer literal in the Python source is too big for JS's float64 you'll get an overflow, but who cares.(I originally posted this request in tree-sitter/tree-sitter-bash#101)
Beta Was this translation helpful? Give feedback.
All reactions