This is a quick-and-dirty (for now) Emacs mode for the Pascal tree-sitter grammar. It just does syntax highlighting but piggy-backs on opascal-mode for everything else.
The ancillary utilities (flycheck, compile mode) only target Free Pascal. Thus, they are named *-free-pascal.el.
This is not on MELPA.
- Clone the repo
- Run
(treesit-install-language-grammar)and use the grammar from here: https://github.com/bolsen/tree-sitter-pascal. (This is my fork, which I started to fix small issues in, until I can fully recommend the original Isopod/tree-sitter-pascal version. Using the upstream version will break highlighting completely.) - Add to your init file:
(load-file "path-to/pascal-ts-mode.el")
- https://www.masteringemacs.org/article/lets-write-a-treesitter-major-mode
- I took a lot of hints from
ada-ts-mode: https://github.com/brownts/ada-ts-mode.
Besides using tree-sitter, what made me try this (and also easy to do and "modernizing" in the process):
- TS parser recognizes keywords in the form of
Procedureas well asprocedure. - Preprocessor directives are properly colored and not treated like comments
- Types and variables, in the right places, are highlighted better.
- Highlighting different types of procedure/function declarations are better.
Also included is a simple flycheck mode to run the FPC compiler to check for errors.
To setup:
(load-file "path-to/flycheck-free-pascal.el")
(setq flycheck-free-pascal-executable "path/to/fpc")
If you use lsp-mode, you need to chain this with LSP. There is a syntax check mode in LSP (genericptr fork), but at the moment, I don't have it working. In this case, you can just chain the checker. If the server supports syntax and semantic checks to the level of the compiler output, then this handler can be removed.
(flycheck-add-next-checker 'lsp 'free-pascal)
Free Pascal is not supported in Emacs by default, but all that's needed is to have a regexes to detect error/warning lines from the compiler output. To setup:
(load-file "path-to/flycheck-free-pascal.el")
(compilation-free-pascal)
These forms are not handled correctly by tree-sitter at the moment, so they will not color correctly:
function ToArray: TArray<T>; override; final;: Thefinalkeyword is not implemented in tree-sitter (with{$mode delphi}. Example:generics.collections.pas).experimentalkeyword is listed but it is not highlighting here.- The tree-sitter parser doesn't like this (line 795, generic.collections.pas in FP 3.2.2 ):
TAVLTreeMap<TKey, TValue> = class(TCustomAVLTreeMap<TKey, TValue, TEmptyRecord>) public property Items; default; end; TIndexedAVLTreeMap<TKey, TValue> = class(TCustomAVLTreeMap<TKey, TValue, SizeInt>) - tree-sitter is strict with the order of
exceptandfinally. There can be a case where you havefinallybefore anexceptbut each is in a preprocessor{$if...}.
For this project:
- Align more with the tree-sitter-pascal project's highlights.scm file.
- Implement hooks to handle indentation.
- Add other useful tools since tooling is otherwise sparse using Emacs (flycheck, better lsp-pascal)