zz-scratch.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, it, beforeAll } from 'vitest';
  2. import { extractFromSource } from '../src/extraction';
  3. import { initGrammars, loadAllGrammars, getParser } from '../src/extraction/grammars';
  4. beforeAll(async () => {
  5. await initGrammars();
  6. await loadAllGrammars();
  7. });
  8. const CODE = `struct V {
  9. int x;
  10. V operator+(const V& o) const { return V{x + o.x}; }
  11. V operator[](int i) const { return V{x + i}; }
  12. int get() const { return x; }
  13. };
  14. int plainCaller(const V& a) { return a.get(); }
  15. V explicitCaller(const V& a, const V& b) { return a.operator+(b); }
  16. `;
  17. describe('scratch', () => {
  18. it('dumps', async () => {
  19. const parser: any = getParser('cpp' as any);
  20. const tree = parser.parse(CODE);
  21. const dump = (n: any, d = 0) => {
  22. let out = `${' '.repeat(d)}${n.type} [${JSON.stringify(n.text.slice(0, 40))}]\n`;
  23. for (let i = 0; i < n.childCount; i++) {
  24. const c = n.child(i);
  25. const f = n.fieldNameForChild ? n.fieldNameForChild(i) : null;
  26. out += `${' '.repeat(d + 1)}${f ? f + ': ' : ''}`.trimEnd() ? '' : '';
  27. out += dump(c, d + 1);
  28. }
  29. return out;
  30. };
  31. // just dump the explicitCaller subtree
  32. console.log(dump(tree.rootNode));
  33. const result = extractFromSource('optest.cpp', CODE);
  34. console.log('NODES', result.nodes.map((n: any) => `${n.kind}:${n.name}`));
  35. console.log('KEYS', Object.keys(result));
  36. console.log('UREFS', JSON.stringify((result as any).unresolvedReferences, null, 1));
  37. });
  38. });