tree sitter
play

Tree-sitter @maxbrunsfeld What is Tree-sitter? Why I wrote - PowerPoint PPT Presentation

Tree-sitter @maxbrunsfeld What is Tree-sitter? Why I wrote Tree-sitter What were building with Tree-sitter at GitHub Algorithms behind Tree-sitter What is Tree-si tu er? Uniform parsing of di ff erent languages if (a) c();


  1. Tree-sitter @maxbrunsfeld

  2. • What is Tree-sitter? • Why I wrote Tree-sitter • What we’re building with Tree-sitter at GitHub • Algorithms behind Tree-sitter

  3. What is Tree-si tu er?

  4. Uniform parsing of di ff erent languages

  5. if (a) c(); Incremental parsing

  6. if (a.b) c(); ✨ ✨ Incremental parsing

  7. Why I wroteTree-si tu er

  8. Single-language code analysis tools

  9. Syntax Highlighting Today

  10. Syntax Highlighting Today Why three di ff erent colors for types?

  11. Syntax Highlighting Today Why three di ff erent colors for types?

  12. Syntax Highlighting Today Why three di ff erent colors for types?

  13. Why not use standard parsers? • Most are too slow for text editors • Many require extra information that’s not available • Each has its own dependencies

  14. Why use Tree-si tu er • It’s fast • It requires no extra information • Parsers have no dependencies

  15. What we’re doing with Tree-si tu er

  16. Syntax Highlighting - Go

  17. Syntax Highlighting - C

  18. Syntax Highlighting - C++

  19. Syntax Highlighting - TypeScript

  20. Syntax Highlighting - Python

  21. Syntax Highlighting - Ruby

  22. Syntax Highlighting - Rust

  23. Syntax Highlighting - Long Lines

  24. Syntax Highlighting - Long Lines

  25. Syntax Highlighting - Performance $ wc -l react.dev.js 20599 react.dev.js $ tree-sitter parse react.dev.js --time react.dev.js 69ms

  26. Code Folding

  27. Code Folding Indentation doesn’t always match syntax

  28. Code Folding

  29. Extend Selection

  30. Pull Request Table of Contents

  31. How Tree-si tu er Works

  32. Writing a Grammar grammar.js

  33. Writing a Grammar parser.c

  34. Writing a Grammar parser.c

  35. Using the parser

  36. Algorithms

  37. Existing Research Practical Algorithms for Incremental Software Development Environments Tim A. Wagner 1998

  38. LR Parsing

  39. LR Parsing Text: x * y + z

  40. LR Parsing Text: x * y + z Stack:

  41. LR Parsing Text: x * y + z Stack: variable

  42. LR Parsing Text: x * y + z Stack: variable *

  43. LR Parsing Text: x * y + z Stack: variable * variable

  44. LR Parsing Text: x * y + z Stack: product variable * variable

  45. LR Parsing Text: x * y + z Stack: product variable * variable +

  46. LR Parsing Text: x * y + z Stack: product variable * variable + variable

  47. LR Parsing Text: x * y + z sum Stack: product + variable variable * variable

  48. LR Parsing • Does this work for all languages?

  49. LR Parsing • Does this work for all languages? Nope. x = (y); // parenthesized expression x = (y) => z; // arrow function

  50. x = (y); x = (y) => z;

  51. GLR Parsing x = (y) => z;

  52. GLR Parsing x = (y) => z;

  53. GLR Parsing x = (y) => z;

  54. Error Recovery

  55. Error Recovery

  56. Error Recovery

  57. Error Recovery

  58. Incremental Parsing Text: var a = new B(); a.c(); return a;

  59. Incremental Parsing Text: var a = new B(); a.c(d); return a;

  60. Incremental Parsing Text: var a = new B(); a.c(d); return a; Stack:

  61. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable Stack: declaration

  62. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable member Stack: declaration expression

  63. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable member Stack: ‘(‘ declaration expression

  64. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable member Stack: identifier ‘(‘ declaration expression

  65. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable member Stack: expression ‘(‘ declaration expression

  66. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable member Stack: expression ‘(‘ ‘)’ declaration expression

  67. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable member Stack: arguments declaration expression

  68. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable call Stack: declaration expression

  69. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable call Stack: ‘;’ declaration expression

  70. Incremental Parsing Text: var a = new B(); a.c(d); return a; variable expression Stack: declaration statement

  71. Incremental Parsing Text: var a = new B(); a.c(d); return a; program Stack: repetition

  72. Incremental Parsing Text: var a = new B(); a.c(d); return a; program return Stack: repetition statement

  73. Incremental Parsing Text: var a = new B(); a.c(d); return a; Stack: program

  74. Next Steps • Add support for more languages • New Atom features using the syntax trees • New GitHub.com features using syntax trees

  75. Thank Y ou! @maxbrunsfeld

Recommend


More recommend