Next: , Previous: , Up: Emacs Lisp   [Contents][Index]


37 Parsing Program Source

Emacs provides various ways to parse program source text and produce a syntax tree. In a syntax tree, text is no longer a one-dimensional stream but a structured tree of nodes, where each node representing a piece of text. Thus a syntax tree can enable interesting features like precise fontification, indentation, navigation, structured editing, etc.

Emacs has a simple facility for parsing balanced expressions (see Parsing Expressions). There is also SMIE library for generic navigation and indentation (see Simple Minded Indentation Engine).

Emacs also provides integration with tree-sitter library (https://tree-sitter.github.io/tree-sitter) if compiled with it. The tree-sitter library implements an incremental parser and has support from a wide range of programming languages.

Function: tree-sitter-available-p

This function returns non-nil if tree-sitter features are available for this Emacs instance.

To access the syntax tree of the text in a buffer, we need to first load a language definition and create a parser with it. Next, we can query the parser for specific nodes in the syntax tree. Then, we can access various information about the node, and we can pattern-match a node with a powerful syntax. Finally, we explain how to work with source files that mixes multiple languages. The following sections explain how to do each of the tasks in detail.


Next: Abbrevs and Abbrev Expansion, Previous: Syntax Tables, Up: Emacs Lisp   [Contents][Index]