Files
emacs-gdscript-mode/gdscript-syntax.el
2020-02-15 14:23:49 -06:00

191 lines
8.4 KiB
EmacsLisp

;;; gdscript-syntax.el --- Syntax highlighting and table for GDScript. -*- lexical-binding: t; -*-
;; Copyright (C) 2020 GDQuest, Free Software Foundation, Inc.
;; Author: Nathan Lovato <nathan@gdquest.com>, Fabián E. Gallina <fgallina@gnu.org>
;; URL: https://github.com/GDQuest/emacs-gdscript-mode/
;; Version: 1.0.0
;; Package-Requires: ((emacs "26.0"))
;; Maintainer: nathan@gdquest.com
;; Created: Feb 2020
;; Keywords: languages
;; 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
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
(defun gdscript--get-package-file-content-as-string (file-path)
"Returns the content of a file in this package as a list of
strings. Used to retrieve lists of keywords for syntax
highlighting."
(with-temp-buffer
(insert-file-contents (concat (file-name-directory (or load-file-name buffer-file-name)) file-path))
(split-string (buffer-string) "\n" t)))
(defvar gdscript-keywords (gdscript--get-package-file-content-as-string "data/keywords"))
(defvar gdscript-built-in-constants (gdscript--get-package-file-content-as-string "data/built-in-constants"))
;; Only contains types that are not classes and that the Godot editor highlights
;; like built-in keywords
(defvar gdscript-built-in-types (gdscript--get-package-file-content-as-string "data/built-in-types"))
(defvar gdscript-built-in-functions (gdscript--get-package-file-content-as-string "data/built-in-functions"))
;; Contains all engine classes and node types, including vectors, transforms, etc.
(defvar gdscript-built-in-classes (gdscript--get-package-file-content-as-string "data/built-in-classes"))
(defun regex-maker (words)
(regexp-opt words 'symbols))
;;; Font-lock and syntax
(eval-and-compile (defun gdscript-syntax--context-compiler-macro (form type &optional syntax-ppss)
(pcase type
(''comment
`(let ((ppss (or ,syntax-ppss
(syntax-ppss))))
(and (nth 4 ppss)
(nth 8 ppss))))
(''string
`(let ((ppss (or ,syntax-ppss
(syntax-ppss))))
(and (nth 3 ppss)
(nth 8 ppss))))
(''paren
`(nth 1
(or ,syntax-ppss
(syntax-ppss))))
(_ form))))
;; Controls font-face mappings or colors to highlight groups of keywords
(defvar gdscript-font-lock `((,(regex-maker gdscript-keywords)
1
font-lock-keyword-face)
(,(regex-maker (concatenate 'list gdscript-built-in-constants
gdscript-built-in-types gdscript-built-in-functions))
1
font-lock-builtin-face)
(,(regex-maker gdscript-built-in-classes)
1
font-lock-type-face)
(,(rx symbol-start
"func"
(1+ space)
(group (1+ (or word ?_))))
(1 font-lock-function-name-face))
(,(rx symbol-start
(or "var" "const")
(1+ space)
(group (1+ (or word ?_))))
(1 font-lock-variable-name-face))))
(defvar gdscript-syntax-table (make-syntax-table))
(defun gdscript-syntax-context (type &optional syntax-ppss)
"Return non-nil if point is on TYPE using SYNTAX-PPSS.
TYPE can be `comment', `string' or `paren'. It returns the start
character address of the specified TYPE."
(declare (compiler-macro gdscript-syntax--context-compiler-macro))
(let ((ppss (or syntax-ppss (syntax-ppss))))
(pcase type
('comment (and (nth 4 ppss) (nth 8 ppss)))
('string (and (nth 3 ppss) (nth 8 ppss)))
('paren (nth 1 ppss))
(_ nil))))
(defun gdscript-syntax-context-type (&optional syntax-ppss)
"Return the context type using SYNTAX-PPSS.
The type returned can be `comment', `string' or `paren'."
(let ((ppss (or syntax-ppss (syntax-ppss))))
(cond
((nth 8 ppss) (if (nth 4 ppss) 'comment 'string))
((nth 1 ppss) 'paren))))
(define-inline gdscript-syntax-comment-or-string-p (&optional ppss)
"Return non-nil if PPSS is inside comment or string."
(nth 8 (or ppss (syntax-ppss))))
(define-inline gdscript-syntax-closing-paren-p ()
"Return non-nil if char after point is a closing paren."
(eq (syntax-class (syntax-after (point)))
(syntax-class (string-to-syntax ")"))))
(defconst gdscript-syntax-propertize-function
(syntax-propertize-rules
((rx (or "\"\"\"" "'''"))
(0 (ignore (gdscript-syntax-stringify))))))
(define-inline gdscript-syntax-count-quotes (quote-char &optional point limit)
"Count number of quotes around point (max is 3).
QUOTE-CHAR is the quote char to count. Optional argument POINT is
the point where scan starts (defaults to current point), and LIMIT
is used to limit the scan."
(let ((i 0))
(while (and (< i 3)
(or (not limit) (< (+ point i) limit))
(eq (char-after (+ point i)) quote-char))
(setq i (1+ i)))
i))
(defun gdscript-syntax-stringify ()
"Put `syntax-table' property correctly on single/triple quotes."
(let* ((ppss (save-excursion (backward-char 3) (syntax-ppss)))
(string-start (and (eq t (nth 3 ppss)) (nth 8 ppss)))
(quote-starting-pos (- (point) 3))
(quote-ending-pos (point)))
(cond ((or (nth 4 ppss) ;Inside a comment
(and string-start
;; Inside of a string quoted with different triple quotes.
(not (eq (char-after string-start)
(char-after quote-starting-pos)))))
;; Do nothing.
nil)
((nth 5 ppss)
;; The first quote is escaped, so it's not part of a triple quote!
(goto-char (1+ quote-starting-pos)))
((null string-start)
;; This set of quotes delimit the start of a string.
(put-text-property quote-starting-pos (1+ quote-starting-pos)
'syntax-table (string-to-syntax "|")))
(t
;; This set of quotes delimit the end of a string.
(put-text-property (1- quote-ending-pos) quote-ending-pos
'syntax-table (string-to-syntax "|"))))))
(defvar gdscript-mode-syntax-table
(let ((table (make-syntax-table)))
;; Give punctuation syntax to ASCII that normally has symbol
;; syntax or has word syntax and isn't a letter.
(let ((symbol (string-to-syntax "_"))
(sst (standard-syntax-table)))
(dotimes (i 128)
(unless (= i ?_)
(if (equal symbol (aref sst i))
(modify-syntax-entry i "." table)))))
(modify-syntax-entry ?$ "." table)
(modify-syntax-entry ?% "." table)
;; exceptions
(modify-syntax-entry ?# "<" table)
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?' "\"" table)
(modify-syntax-entry ?` "$" table)
table)
"Syntax table for Gdscript files.")
(defvar gdscript-dotty-syntax-table
(let ((table (make-syntax-table gdscript-mode-syntax-table)))
(modify-syntax-entry ?. "w" table)
(modify-syntax-entry ?_ "w" table)
table)
"Dotty syntax table for Gdscript files.
It makes underscores and dots word constituent chars.")
(provide 'gdscript-syntax)