catala/syntax_highlighting/emacs/catala-mode.el
Louis Gesbert 4ae392c900 AST refactoring
Many changes got bundled in here and would be too tedious to separate.

Closes #330

See changes in `shared_ast/definitions.ml` to check the main point.

- the biggest change is a modification of the struct and enum types in
  expressions: they are now stored as `Map`s throughout passes, and no longer
  converted to indexed lists after scopelang. Their accessors are also changed,
  and tuples only exist in Lcalc (they're used for closure conversion).

  This implied adding some more information in the contexts, to keep the mapping
  between struct fields and scope output variables. It should also be much more
  robust (no longer relying on assumptions upon different orderings).

- another very pervasive change is more cosmetic: the rewrite of the main AST to
  use inline records, labelling individual subfields.

- moved the checks for correct definitions and accesses of structures from
  `Scope_to_dcalc` to `Typing`

- defining some new shallow iterators in module `Shared_ast.Expr`, and
  factorising a few same-pass rewriting functions accordingly (closure
  conversion, optimisations, etc.)

- some smaller style improvements (ensuring we use the proper compare/equal
  functions instead of `=` in a few `when` closes, for example)
2022-11-17 18:16:09 +01:00

89 lines
5.0 KiB
EmacsLisp

(defvar catala-code-mode-hook nil)
(defun catala-set-syntax-table()
(let ((st (syntax-table)))
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?% "_" st)
(modify-syntax-entry ?- "." st)
(modify-syntax-entry ?\; "." st)
(modify-syntax-entry ?. "." st)
(modify-syntax-entry ?, "." st)
(modify-syntax-entry ?: "." st)
(modify-syntax-entry ?$ "_" st)
(modify-syntax-entry ?@ "_" st)
(modify-syntax-entry ?€ "_" st)
(modify-syntax-entry ?^ "_" st)
(modify-syntax-entry ?| "$" st)
))
(define-generic-mode 'catala-mode-fr
'("#")
'("contexte" "entrée" "sortie" "interne"
"champ d'application" "si et seulement si" "dépend de" "déclaration" "inclus" "collection" "contenu" "optionnel" "structure" "énumération" "contexte" "entrée" "sortie" "interne" "règle" "sous condition" "condition" "donnée" "conséquence" "rempli" "égal à" "assertion" "définition" "état" "étiquette" "exception" "soit")
'(("\\<\\(selon\\|sous\s+forme\\|fixé\\|par\\|décroissante\\|croissante\\|varie\\|avec\\|on\s+a\\|soit\\|dans\\|tel\s+que\\|existe\\|pour\\|tout\\|de\\|si\\|alors\\|sinon\\|initial\\)\\>" . font-lock-builtin-face)
("\\<\\(vrai\\|faux\\)\\>" . font-lock-constant-face)
("\\<\\([0-9][0-9 ]*\\(,[0-9]*\\|\\)\\)\\>" . font-lock-constant-face)
("\\(->\\|+.\\|+@\\|+^\\|+€\\|+\\|-.\\|-@\\|-^\\|-€\\|-\\|*.\\|*@\\|*^\\|*€\\|*\\|/.\\|/@\\|/€\\|/\\|!\\|>.\\|>=.\\|<=.\\|<.\\|>@\\|>=@\\|<=@\\|<@\\|>€\\|>=€\\|<=€\\|<€\\|>^\\|>=^\\|<=^\\|<^\\|>\\|>=\\|<=\\|<\\|=\\)" . font-lock-keyword-face)
("\\<\\(\\|non\\|ou\s+bien\\|ou\\|et\\|€\\|%\\|an\\|mois\\|jour\\)\\>" . font-lock-keyword-face)
("\\<\\(entier\\|booléen\\|date\\|durée\\|argent\\|texte\\|décimal\\|décret\\|loi\\|nombre\\|somme\\)\\>" . font-lock-type-face)
("\\<[a-zéèàâùîôêœç][a-zéèàâùîôêœçA-ZÉÈÀÂÙÎÔÊŒÇ0-9_']*\\>" . font-lock-variable-name-face)
("\\<[A-ZÉÈÀÂÙÎÔÊŒÇ][a-zéèàâùîôêœçA-ZÉÈÀÂÙÎÔÊŒÇ0-9_']*\\>" . font-lock-function-name-face)
("\\<\\(|[0-9]\\+-[0-9]\\+-[0-9]\\+|\\)\\>" . font-lock-constant-face)
)
'("\\.catala_fr_raw$")
'(catala-set-syntax-table)
"A basic generic major mode for catala files (fr version)"
)
(define-generic-mode 'catala-mode-en
'("#")
'("context" "input" "output" "internal"
"scope" "depends on" "declaration" "includes" "collection" "content" "optional" "structure" "enumeration" "context" "input" "output" "internal" "rule" "under condition" "condition" "data" "consequence" "fulfilled" "equals" "assertion" "definition" "state" "label" "exception" "let")
'(("\\<\\(match\\|with\s+pattern\\|fixed\\|by\\|decreasing\\|increasing\\|varies\\|with\\|we\s+have\\|let\\|in\\|such\s+that\\|exists\\|for\\|all\\|of\\|if\\|then\\|else\\|initial\\)\\>" . font-lock-builtin-face)
("|[0-9]\\+-[0-9]\\+-[0-9]\\+|" . font-lock-constant-face)
("\\<\\(true\\|false\\)\\>" . font-lock-constant-face)
("\\<\\([0-9][0-9,]*\\(\\.[0-9]*\\|\\)\\)\\>" . font-lock-constant-face)
("\\(->\\|+.\\|+@\\|+^\\|+$\\|+\\|-.\\|-@\\|-^\\|-$\\|-\\|*.\\|*@\\|*^\\|*$\\|*\\|/.\\|/@\\|/$\\|/\\|!\\|>.\\|>=.\\|<=.\\|<.\\|>@\\|>=@\\|<=@\\|<@\\|>$\\|>=$\\|<=$\\|<$\\|>^\\|>=^\\|<=^\\|<^\\|>\\|>=\\|<=\\|<\\|=\\)" . font-lock-keyword-face)
("\\<\\(not\\|or\\|xor\\|and\\|\\$\\|%\\|year\\|month\\|day\\)\\>" . font-lock-keyword-face)
("\\<\\(integer\\|boolean\\|date\\|duration\\|money\\|text\\|decimal\\|number\\|sum\\)\\>" . font-lock-type-face)
("\\<[a-zéèàâùîôêœç][a-zéèàâùîôêœçA-ZÉÈÀÂÙÎÔÊŒÇ0-9_']*\\>" . font-lock-variable-name-face)
("\\<[A-ZÉÈÀÂÙÎÔÊŒÇ][a-zéèàâùîôêœçA-ZÉÈÀÂÙÎÔÊŒÇ0-9_']*\\>" . font-lock-function-name-face))
'("\\.catala_en_raw$")
'(catala-set-syntax-table)
"A basic generic major mode for catala files (en version)"
)
(defun catala-code-mode()
"Major mode for catala code (inside of ```catala blocks)"
(interactive)
(let* ((buf (window-buffer (minibuffer-selected-window)))
(name (buffer-file-name buf)))
(if (and name (string-match-p "\\.catala_fr" name))
(catala-mode-fr)
(catala-mode-en)))
(run-mode-hooks 'catala-code-mode-hook))
(provide 'catala-code-mode)
;;;###autoload
(define-derived-mode catala-mode markdown-mode "Catala"
"Catala major mode based on markdown-mode"
(setq-local markdown-fontify-code-blocks-natively t)
(setq-local markdown-code-lang-modes
'(("catala-metadata" . catala-code-mode)
("catala" . catala-code-mode))))
(provide 'catala-mode)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.catala_\\(fr\\|en\\|pl\\)" . catala-mode))
;; To use, add the following lines to ~/.emacs (without the leading ';') :
; (autoload 'catala-mode "catala-mode" "Catala major mode based on markdown-mode." t)
; (add-to-list 'auto-mode-alist '("\\.catala_\\(fr\\|en\\|pl\\)" . catala-mode))
;; You may additionally need the following if you don't already have any emacs setup in opam:
; (add-to-list 'load-path (concat (string-trim (shell-command-to-string "opam var prefix")) "/share/emacs/site-lisp"))