1
0

pascal.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type { Node as SyntaxNode } from 'web-tree-sitter';
  2. import { getNodeText, getChildByField } from '../tree-sitter-helpers';
  3. import type { LanguageExtractor } from '../tree-sitter-types';
  4. export const pascalExtractor: LanguageExtractor = {
  5. functionTypes: ['declProc'],
  6. classTypes: ['declClass'],
  7. methodTypes: ['declProc'],
  8. interfaceTypes: ['declIntf'],
  9. structTypes: [],
  10. enumTypes: ['declEnum'],
  11. typeAliasTypes: ['declType'],
  12. importTypes: ['declUses'],
  13. callTypes: ['exprCall'],
  14. variableTypes: ['declField', 'declConst'],
  15. nameField: 'name',
  16. bodyField: 'body',
  17. paramsField: 'args',
  18. returnField: 'type',
  19. getSignature: (node, source) => {
  20. const args = getChildByField(node, 'args');
  21. const returnType = node.namedChildren.find(
  22. (c: SyntaxNode) => c.type === 'typeref'
  23. );
  24. if (!args && !returnType) return undefined;
  25. let sig = '';
  26. if (args) sig = getNodeText(args, source);
  27. if (returnType) {
  28. sig += ': ' + getNodeText(returnType, source);
  29. }
  30. return sig || undefined;
  31. },
  32. getVisibility: (node) => {
  33. let current = node.parent;
  34. while (current) {
  35. if (current.type === 'declSection') {
  36. for (let i = 0; i < current.childCount; i++) {
  37. const child = current.child(i);
  38. if (child?.type === 'kPublic' || child?.type === 'kPublished')
  39. return 'public';
  40. if (child?.type === 'kPrivate') return 'private';
  41. if (child?.type === 'kProtected') return 'protected';
  42. }
  43. }
  44. current = current.parent;
  45. }
  46. return undefined;
  47. },
  48. isExported: (_node, _source) => {
  49. // In Pascal, symbols declared in the interface section are exported
  50. return false;
  51. },
  52. isStatic: (node) => {
  53. for (let i = 0; i < node.childCount; i++) {
  54. if (node.child(i)?.type === 'kClass') return true;
  55. }
  56. return false;
  57. },
  58. isConst: (node) => {
  59. return node.type === 'declConst';
  60. },
  61. };