fix comments, add new keywords

This commit is contained in:
zongor 2025-10-17 12:55:26 -07:00
parent bfec1109a8
commit e5e235f95c
1 changed files with 101 additions and 95 deletions

196
undar-mode.el Normal file → Executable file
View File

@ -1,95 +1,101 @@
;;; undar-mode.el --- Major mode for editing Undar code -*- lexical-binding: t; -*- ;;; undar-mode.el --- Major mode for editing Undar code -*- lexical-binding: t; -*-
;; Copyright (C) 2025 zongor ;; Copyright (C) 2025 zongor
;; ;;
;; This program is free software: you can redistribute it and/or modify ;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by ;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or ;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version. ;; (at your option) any later version.
;; ;;
;; This program is distributed in the hope that it will be useful, ;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details. ;; GNU General Public License for more details.
;; ;;
;; You should have received a copy of the GNU General Public License ;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; ;;;
(defvar undar-keywords (defvar undar-keywords
'("function" "to" "in" "is" "as" "use" "if" "else" "default" '("function" "to" "in" "is" "as" "use" "if" "else" "default"
"for" "try" "catch" "while" "do" "exit" "switch" "for" "try" "catch" "while" "do" "exit" "switch" "continue"
"return" "const" "type" "this" "yield" "case" "return" "const" "type" "this" "yield" "case" "loop" "plex"
"assert" "break" "mod" "not" "and" "or" "print" "let" "assert" "break" "mod" "not" "and" "or" "print" "ref" "interface"
"band" "bor" "bxor" "srl" "sll") "implements" "impls" "band" "bor" "bxor" "srl" "sll")
"Keywords in Undar.") "Keywords in Undar.")
(defvar undar-types (defvar undar-types
'("byte" "str" "real" "bool" '("byte" "str" "real" "bool"
"int" "nat" "f32" "f64" "int" "nat" "f32" "void"
"u8" "u16" "u32" "u64" "u8" "u16" "u32"
"i8" "i16" "i32" "i64") "i8" "i16" "i32")
"Types in Undar.") "Types in Undar.")
(defvar undar-constants (defvar undar-constants
'("true" "false" "nil") '("true" "false" "nil")
"Constants in Undar.") "Constants in Undar.")
(defvar undar-font-lock-keywords (defvar undar-font-lock-keywords
`( `(
;; Keywords ;; Keywords
(,(regexp-opt undar-keywords 'words) . font-lock-keyword-face) (,(regexp-opt undar-keywords 'words) . font-lock-keyword-face)
;; Types ;; Types
(,(regexp-opt undar-types 'words) . font-lock-type-face) (,(regexp-opt undar-types 'words) . font-lock-type-face)
;; Constants ;; Constants
(,(regexp-opt undar-constants 'words) . font-lock-constant-face) (,(regexp-opt undar-constants 'words) . font-lock-constant-face)
;; Structs: CamelCase identifiers ;; Structs: CamelCase identifiers
("\\b[A-Z][a-zA-Z0-9_]*\\b" . font-lock-type-face) ("\\b[A-Z][a-zA-Z0-9_]*\\b" . font-lock-type-face)
;; Function definitions: "fn name(...)" ;; Function definitions: "function name(...)"
("\\_<function\\>\\s-+\\(\\w+\\)(" (1 font-lock-function-name-face)) ("\\_<function\\>\\s-+\\([\\w+\\)(" (1 font-lock-function-name-face))
;; Function calls: "name(...)" ;; Numbers (Integers and Floats)
("\\_<\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s-*(" (1 font-lock-function-name-face)) ("\\b-?[0-9]+\\(?:\\.[0-9]+\\)?\\(?:[eE][+-]?[0-9]+\\)?\\b" . font-lock-constant-face)
;; Floating point numbers ;; Strings with double quotes
("\\b-?[0-9]+\\.[0-9]+\\b" . font-lock-constant-face) ("\"[^\"\n]*\"" . font-lock-string-face)
;; Integers ;; Strings with backticks
("\\b-?[0-9]+\\b" . font-lock-constant-face) ("`[^`\n]*`" . font-lock-string-face))
"Font lock keywords for Undar mode.")
;; Strings with double quotes
("\"[^\"\n]*\"" . font-lock-string-face) (defvar undar-mode-syntax-table
(let ((st (make-syntax-table)))
;; Strings with backticks ;; C-style comments: // and /* */
("`[^`]*`" . font-lock-string-face)) ;; Clear potential existing syntax for / and * if needed (though default is usually fine initially)
;; Clearing standard C++/C comment syntax if it was somehow set beforehand is generally not needed
"Font lock keywords for Undar mode.") ;; unless explicitly overridden elsewhere, so we rely on the default being clean.
(defvar undar-mode-syntax-table ;; The crucial setup for both // and /* */ comments together:
(let ((st (make-syntax-table))) ;; / is punctuation and starts both // (1, 2) and /* (4) comments
;; '/' starts a comment (modify-syntax-entry ?\/ ". 124" st)
(modify-syntax-entry ?! "<" st) ;; * is punctuation and part of /* */ (2, 3) and also part of // (b) comments (this is key for //)
(modify-syntax-entry ?\n ">" st) (modify-syntax-entry ?* ". 23b" st)
;; Strings ;; Newline ends comments (>) and is part of // comments (b) - essential for // to end correctly
(modify-syntax-entry ?\" "\"" st) (modify-syntax-entry ?\n ">" st)
(modify-syntax-entry ?` "\"" st) ;; Strings
st) (modify-syntax-entry ?\" "\"" st)
"Syntax table for Undar mode.") (modify-syntax-entry ?` "\"" st)
st)
(define-derived-mode undar-mode prog-mode "Undâr" "Syntax table for Undar mode, using C-style comments (// and /* */).")
"Major mode for editing Undar code."
:syntax-table undar-mode-syntax-table (define-derived-mode undar-mode prog-mode "Undâr"
(setq-local font-lock-defaults '(undar-font-lock-keywords)) "Major mode for editing Undar code."
(setq-local comment-start "! ") :syntax-table undar-mode-syntax-table
(setq-local comment-end "") (setq-local font-lock-defaults '(undar-font-lock-keywords))
(setq-local parse-sexp-ignore-comments t) ;; Set comment syntax for commands like M-; (comment-dwim)
(setq-local indent-line-function 'c-indent-line)) (setq-local comment-start "// ")
(setq-local comment-end "")
;;;###autoload (setq-local comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
(add-to-list 'auto-mode-alist '("\\.ul\\'" . undar-mode)) ;; Ensure comments are ignored during parsing (e.g., for indentation)
(setq-local parse-sexp-ignore-comments t)
(provide 'undar-mode) ;; Use C-style indentation function
;;; undar-mode.el ends here (setq-local indent-line-function 'c-indent-line))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.ul\\'" . undar-mode))
(provide 'undar-mode)
;;; undar-mode.el ends here