This commit is contained in:
zongor 2025-05-16 16:52:32 -04:00
parent a1cb38b82b
commit 2d7e18e9c1
1 changed files with 91 additions and 40 deletions

View File

@ -1,19 +1,16 @@
;;; ztl-mode.el --- Major mode for editing Ztl code -*- lexical-binding: t; -*- ;;; ztl-mode.el --- Major mode for editing Ztl code -*- lexical-binding: t; -*-
;; ;;
;; Licensed under the GPLv3 License. ;; Licensed under the GPLv3 License.
(require 'font-lock)
(defvar ztl-keywords (defvar ztl-keywords
'("fn" "to" "in" "is" "as" "use" "set" "if" "else" "for" "loop" "concurrent" '("fn" "to" "in" "is" "as" "use" "set" "if" "else"
"while" "push" "pop" "return" "const" "type" "this" "eq" "ne" "do" "panic" "for" "loop" "concurrent" "while" "do" "exits" "exit"
"mod" "not" "and" "or" "xor" "band" "bor" "bxor" "srl" "sll" "let") "return" "const" "type" "this" "panic"
"break" "mod" "not" "and" "or" "print" "let")
"Keywords in Ztl.") "Keywords in Ztl.")
(defvar ztl-types (defvar ztl-types
'("byte" "str" "int" "real" '("byte" "str" "real" "bool" "err" "any")
"logical" "bool" "err"
"i8" "i16" "i32" "i64" "i128"
"f8" "f16" "f32" "f64" "f128")
"Types in Ztl.") "Types in Ztl.")
(defvar ztl-constants (defvar ztl-constants
@ -21,41 +18,96 @@
"Constants in Ztl.") "Constants in Ztl.")
(defvar ztl-font-lock-keywords (defvar ztl-font-lock-keywords
(list `(
;; Constants
(cons (regexp-opt ztl-constants 'words) font-lock-constant-face)
;; Keywords ;; Keywords
(cons (regexp-opt ztl-keywords 'words) font-lock-keyword-face) (,(regexp-opt ztl-keywords 'words) . font-lock-keyword-face)
;; Types ;; Types
(cons (regexp-opt ztl-types 'words) font-lock-type-face) (,(regexp-opt ztl-types 'words) . font-lock-type-face)
;; Structs (PascalCase)
'("\\b[A-Z][A-Za-z0-9_]*\\b" . font-lock-type-face) ;; Constants
;; Numbers (,(regexp-opt ztl-constants 'words) . font-lock-constant-face)
'("\\b\\([0-9]+\\(?:\\.[0-9]*\\)?\\)\\b" . font-lock-number-face)
;; Structs: CamelCase identifiers
("\\b[A-Z][a-zA-Z0-9_]*\\b" . font-lock-type-face)
;; Floating point numbers
("-?[0-9]+\\.[0-9]+" . font-lock-constant-face)
;; Ints
("-?[0-9]+" . font-lock-constant-face)
;; Strings with double quotes
("\"[^\"\n]*\"" . font-lock-string-face)
;; Strings with backticks
("`[^`]*`" . font-lock-string-face)
;; Function definitions
("\\_<fn\\>\\s-+\\(\\w+\\)(" (1 font-lock-function-name-face))
;; Function calls ;; Function calls
'("\\b\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s-*(" (1 font-lock-function-name-face)) ("\\_<\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s-*(" (1 font-lock-function-name-face))
) )
"Font lock definitions for Ztl.") "Font lock keywords for Ztl mode.")
(defvar ztl-syntax-table (defvar ztl-mode-syntax-table
(let ((table (make-syntax-table))) (let ((st (make-syntax-table)))
;; Comments: C and C++ style ;; C++-style comments
(modify-syntax-entry ?/ ". 14b" table) (modify-syntax-entry ?/ ". 14b" st)
(modify-syntax-entry ?* ". 23" table) ;; C-style comments
(modify-syntax-entry ?\n ">" table) (modify-syntax-entry ?* ". 23" st)
;; Strings ;; Strings
(modify-syntax-entry ?\" "\"" table) (modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\\ "\\" table) (modify-syntax-entry ?` "\"" st)
table) st)
"Syntax table for `ztl-mode'.") "Syntax table for Ztl mode.")
(define-derived-mode ztl-mode prog-mode "ZTL" (defun ztl-indent-line ()
"Major mode for editing Ztl source files." "Indent current line as Ztl code."
:syntax-table ztl-syntax-table (interactive)
:group 'ztl (let ((indent (calculate-ztl-indent)))
(if (null indent)
(setq indent 0))
(indent-line-to indent)))
(defun calculate-ztl-indent ()
"Calculate the proper indentation for the current line."
(save-excursion
(beginning-of-line)
(let ((pos (point-marker))
(indent 0))
(skip-chars-forward " \t")
(if (looking-at "\\s)\\|\\s(")
(setq indent 0)
(condition-case nil
(progn
(backward-up-list 1)
(while (and (> (point) (point-min))
(progn
(forward-line -1)
(skip-chars-forward " \t")
(or (looking-at "\\s<") ; comment
(looking-at "\\s\"") ; string
(looking-at "\\s$")))) ; skip to previous line
nil)
(setq indent (current-indentation))
(forward-line 0)
(skip-chars-forward " \t")
(if (looking-at "\\s(") ; opening brace
(setq indent (+ indent tab-width))))
(error nil)))
indent)))
(define-derived-mode ztl-mode prog-mode "Ztl"
"Major mode for editing Ztl code."
:syntax-table ztl-mode-syntax-table
(setq-local font-lock-defaults '(ztl-font-lock-keywords)) (setq-local font-lock-defaults '(ztl-font-lock-keywords))
(setq-local comment-start "// ") (setq-local comment-start "// ")
(setq-local comment-end "") (setq-local comment-end "")
(setq-local parse-sexp-ignore-comments t)
(setq-local indent-line-function 'ztl-indent-line)
(setq-local syntax-propertize-function (setq-local syntax-propertize-function
(syntax-propertize-rules (syntax-propertize-rules
;; Line comments starting with // ;; Line comments starting with //
@ -65,5 +117,4 @@
(add-to-list 'auto-mode-alist '("\\.ztl\\'" . ztl-mode)) (add-to-list 'auto-mode-alist '("\\.ztl\\'" . ztl-mode))
(provide 'ztl-mode) (provide 'ztl-mode)
;;; ztl-mode.el ends here
;; End of ztl-mode.el