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

How to use tree-sitter-objdump with Node.js? #5

Open
lancejpollard opened this issue Jan 12, 2024 · 4 comments
Open

How to use tree-sitter-objdump with Node.js? #5

lancejpollard opened this issue Jan 12, 2024 · 4 comments

Comments

@lancejpollard
Copy link

Is it possible to use this objdump parser with node.js? If so, would you mind showing real quick a hello world? Basically to generate an AST.

Thanks so much!

@ColinKennedy
Copy link
Owner

Have you tried looking at https://github.com/tree-sitter/node-tree-sitter? It has an example hello world setup for a Javascript parser. I'm guessing the steps there are the same for tree-sitter-objdump as long as you replace const JavaScript = require('tree-sitter-javascript'); with const Objdump = require('tree-sitter-objdump'); or some equivalent. I don't know node.js but I can try it if this doesn't work out for you.

@lancejpollard
Copy link
Author

lancejpollard commented Jan 13, 2024

@ColinKennedy first error, I will try just cloning the repo and requiring, it doesn't look like it's published to npm.

 ERR_PNPM_FETCH_404  GET https://registry.npmjs.org/tree-sitter-objdump: Not Found - 404

This error happened while installing a direct dependency of /Users/lancepollard/start/termsurf/deck/task

tree-sitter-objdump is not in the npm registry, or you have no permission to fetch it.

Then I get:

Error: Cannot find module '../../build/Release/tree_sitter_objdump_binding'
Require stack:
- /Users/lancepollard/Desktop/tree-sit/tree-sitter-objdump/bindings/node/index.js
- /Users/lancepollard/Desktop/tree-sit/test.js

With test script:

const Parser = require('tree-sitter');
const Objdump = require('./tree-sitter-objdump');

const parser = new Parser();
parser.setLanguage(Objdump);

@lancejpollard
Copy link
Author

lancejpollard commented Jan 13, 2024

Hey! I copied all the code from objdump into a local repo, then followed these instructions! I didn't think it would be so easy :) Thanks for the push!

npm install tree-sitter-cli nan
export PATH=$PATH:./node_modules/.bin
tree-sitter generate
objdump -S test.out > test.dump
tree-sitter parse test.dump
...
    (memory_offset [129, 0] - [130, 30]
      (address [129, 0] - [129, 9])
      (ERROR [129, 9] - [130, 9]
        (machine_code_bytes [129, 11] - [129, 13]
          (byte [129, 11] - [129, 13]))
        (instruction [129, 13] - [129, 39])
        (ERROR [129, 40] - [129, 43]
          (address [129, 40] - [129, 42])
          (ERROR [129, 42] - [129, 43]))
        (address [130, 0] - [130, 9]))
      (machine_code_bytes [130, 11] - [130, 13]
        (byte [130, 11] - [130, 13]))
      (instruction [130, 13] - [130, 30]))))

Now to get it in JSON...

Do you know how to export JSON from the CLI, or do I have to resort to some C API stuff?

That output is from the CLI, not from JS, didn't get that working.

@ColinKennedy
Copy link
Owner

I wouldn't know, sorry. tree-sitter-objdump is just a parser for objdump, I don't have expert knowledge on what exists in the tree-sitter ecosystem. To talk to someone about tree-sitter APIs / features, I recommend messaging on their Discord or GitHub discussions. I've messaged those folks on Discord before and they're quick + knowledgeable.

Discord: https://discord.com/invite/w7nTvsVJhm
GitHub Discussions: https://github.com/tree-sitter/tree-sitter/discussions

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

2 participants