emacs:如何编写类似于Algol的语言的语法色彩模式,该语言使用关键字stropping

发布于 2025-02-09 05:37:41 字数 2237 浏览 3 评论 0原文

我正在尝试为Imp设置语法着色,IMP是一种使用关键字划线的类似Algol的语言,即关键字是' ''cartem oftherphabetics to任何非α,例如 %开始 。我发现 modify-syntax-entry 足以为IMP注释着色,但是我不得不使用Hi-Lock来颜色关键字。我喜欢的帮助是:如何从我的〜/.emacs 文件中加载hi-lock的正则是。每个单独的.IMP文件中的规则?到目前为止,这是我所拥有的:

(defconst my-imp-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?' "\"" table)
    (modify-syntax-entry ?\" "\"" table)
    (modify-syntax-entry ?! "<" table)
    (modify-syntax-entry ?\n ">" table)
    (modify-syntax-entry ?{ "<" table)
    (modify-syntax-entry ?} ">" table)
    (modify-syntax-entry ?\( "()" table)
    (modify-syntax-entry ?\) ")(" table)
    table))
(define-derived-mode my-imp-mode prog-mode "Simple Imp Mode"
  :syntax-table my-imp-mode-syntax-table
  (font-lock-fontify-buffer))
(add-to-list 'auto-mode-alist '("\\.i\\'" . my-imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp\\'" . my-imp-mode))
(global-hi-lock-mode 1)
(setq hi-lock-file-patterns-policy (lambda (pattern) t))
(defface imp-keyword
  '((t (:weight bold :foreground "cyan")))
  "Face for IMP keywords"
  :group 'hi-lock-faces)
(defface imp-constant
  '((t (:foreground "green")))
  "Face for IMP numeric constants"
  :group 'hi-lock-faces)
;; I would like to load these patterns on opening a .imp file:
;; (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
;; (("\\<[\\-+]*[0-9]*\\.?[0-9]+\\(\\|@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))

这是一个IMP文件的示例:

! Hi-lock: (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
! Hi-lock: (("\\<[\\-]?[0-9]*\\.?[0-9]+\\(\\@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))

%begin
! ackerman function - this is a whole-line comment
%integer x,y,j,k

%integerfn acker {short for ackerman - this is a bracketed comment by the way} (%integer m,n)
  %if m = 0 %then %result = n+1
  %if n = 0 %then %result = acker(m-1,1)
  %result = acker(m-1, acker(m, n-1))
%end

prompt("Ackerman, First param (1..4)?"); read(x)
prompt("         Second param (1..7)?"); read(y)
write(acker(x,y), 4); newline

%endofprogram

通过从上方应用ELISP,您可以看到预期的突出显示的样子。 (着色的细节只是草稿,直到我弄清楚如何做。我可以调整颜色等)

I'm trying to set up syntax colouring for Imp, an Algol-like language that uses keyword stropping, i.e. a keyword is the '%' character followed by alphabetics up to any non-alpha, such as %begin. I found that modify-syntax-entry was sufficient to colour Imp comments, but I had to use Hi-lock to colour keywords. What I'ld like help with is: how can I load the regexps for hi-lock from my ~/.emacs file when opening any file with a .imp extension, rather than having to explicitly save the rules in every individual .imp file? Here's what I have so far:

(defconst my-imp-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?' "\"" table)
    (modify-syntax-entry ?\" "\"" table)
    (modify-syntax-entry ?! "<" table)
    (modify-syntax-entry ?\n ">" table)
    (modify-syntax-entry ?{ "<" table)
    (modify-syntax-entry ?} ">" table)
    (modify-syntax-entry ?\( "()" table)
    (modify-syntax-entry ?\) ")(" table)
    table))
(define-derived-mode my-imp-mode prog-mode "Simple Imp Mode"
  :syntax-table my-imp-mode-syntax-table
  (font-lock-fontify-buffer))
(add-to-list 'auto-mode-alist '("\\.i\\'" . my-imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp\\'" . my-imp-mode))
(global-hi-lock-mode 1)
(setq hi-lock-file-patterns-policy (lambda (pattern) t))
(defface imp-keyword
  '((t (:weight bold :foreground "cyan")))
  "Face for IMP keywords"
  :group 'hi-lock-faces)
(defface imp-constant
  '((t (:foreground "green")))
  "Face for IMP numeric constants"
  :group 'hi-lock-faces)
;; I would like to load these patterns on opening a .imp file:
;; (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
;; (("\\<[\\-+]*[0-9]*\\.?[0-9]+\\(\\|@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))

Here is an example of an Imp file:

! Hi-lock: (("\\%[A-Za-z]*" (0 (quote imp-keyword) prepend)))
! Hi-lock: (("\\<[\\-]?[0-9]*\\.?[0-9]+\\(\\@[\\-+]?[0-9]+\\)?\\>" (0 (quote imp-constant) prepend)))

%begin
! ackerman function - this is a whole-line comment
%integer x,y,j,k

%integerfn acker {short for ackerman - this is a bracketed comment by the way} (%integer m,n)
  %if m = 0 %then %result = n+1
  %if n = 0 %then %result = acker(m-1,1)
  %result = acker(m-1, acker(m, n-1))
%end

prompt("Ackerman, First param (1..4)?"); read(x)
prompt("         Second param (1..7)?"); read(y)
write(acker(x,y), 4); newline

%endofprogram

You can see what the expected highlighting looks like by applying the elisp from above. (The details of the colouring are just a draft until I work out how to do it. I can tweak the colours etc later)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2025-02-16 05:37:41

不需要HI -Lock模块 - 可以使用标准语法着色机制为新语言编写语法着色,如下所示:

(defconst imp-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; turn off ' and " from being string delimiters
    (modify-syntax-entry ?'  "-" table)
    (modify-syntax-entry ?\" "-" table)
    table))

;; Highlight comments, %stropped keywords, and numbers:
(setq imp-highlights
      '(
        ;; many forms of comments including after labels
        (";[ ]*!" ".*"      nil nil (0 font-lock-comment-face))
        ("^[ ]*!" ".*;"     nil nil (0 font-lock-comment-face))
        ("^[ ]*!" ".*"      nil nil (0 font-lock-comment-face))
        ("%c\\([ ]+[%]+\\|[%]*\\)*o\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*e\\([ ]+[%]+\\|[%]*\\)*n\\([ ]+[%]+\\|[%]*\\)*t" ".*;"   nil nil
(0 font-lock-comment-face))
        ("%c\\([ ]+[%]+\\|[%]*\\)*o\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*e\\([ ]+[%]+\\|[%]*\\)*n\\([ ]+[%]+\\|[%]*\\)*t" "[^$]*" nil nil
(0 font-lock-comment-face))
        (":[ ]*!" "[^$]*"   nil nil (0 font-lock-comment-face))
        ("{[^}]*}"                  . 'font-lock-comment-face )
        ("{.*$"                     . 'font-lock-comment-face )
        ;; char constants, old-style strings, and old-style based constants
        ("[MBOXER]?'[^']*'"          . 'font-lock-constant-face)
        ("\"[^\"]*\""               . 'font-lock-string-face  )
        ;; Based IMP constants
        ("\\<[0-9][ ]*[0-9 ]*[ ]*_[ ]*[0-9A-Fa-f][0-9A-Fa-f ]*\\>" . 'font-lock-constant-face)
        ;; Decimal IMP constants
        ("\\<[0-9][0-9 ]*\\.[ ]*[0-9][0-9 ]*@[ ]*[-+]?[ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ("\\<[0-9][0-9 ]*\\.[ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ;; Integer IMP constants
        ("\\<[0-9][ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ("\\<[0-9][ ]*\\>" . 'font-lock-constant-face)
        ;; stropped keywords
        ("\\%[A-Za-z][A-Za-z]*"       . 'font-lock-keyword-face )
        ))

(define-derived-mode imp-mode prog-mode "Simple Imp Mode"
   :syntax-table imp-mode-syntax-table
  (setq font-lock-defaults '(imp-highlights))
  (font-lock-fontify-buffer)
  )

;; Apply automatically to Imp source files:
(add-to-list 'auto-mode-alist '("\\.i\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp77\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp80\\'" . imp-mode))

;; enter <esc>xfun<cr> to disable imp-mode if unwanted during an edit.

基于 http://xahlee.info/emacs/emacs/emacs/elisp_syntax_coloring.html

The Hi-lock module was not required - it is possible to write syntax colouring for a new language using the standard syntax colouring mechanism, as follows:

(defconst imp-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; turn off ' and " from being string delimiters
    (modify-syntax-entry ?'  "-" table)
    (modify-syntax-entry ?\" "-" table)
    table))

;; Highlight comments, %stropped keywords, and numbers:
(setq imp-highlights
      '(
        ;; many forms of comments including after labels
        (";[ ]*!" ".*"      nil nil (0 font-lock-comment-face))
        ("^[ ]*!" ".*;"     nil nil (0 font-lock-comment-face))
        ("^[ ]*!" ".*"      nil nil (0 font-lock-comment-face))
        ("%c\\([ ]+[%]+\\|[%]*\\)*o\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*e\\([ ]+[%]+\\|[%]*\\)*n\\([ ]+[%]+\\|[%]*\\)*t" ".*;"   nil nil
(0 font-lock-comment-face))
        ("%c\\([ ]+[%]+\\|[%]*\\)*o\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*m\\([ ]+[%]+\\|[%]*\\)*e\\([ ]+[%]+\\|[%]*\\)*n\\([ ]+[%]+\\|[%]*\\)*t" "[^$]*" nil nil
(0 font-lock-comment-face))
        (":[ ]*!" "[^$]*"   nil nil (0 font-lock-comment-face))
        ("{[^}]*}"                  . 'font-lock-comment-face )
        ("{.*
quot;                     . 'font-lock-comment-face )
        ;; char constants, old-style strings, and old-style based constants
        ("[MBOXER]?'[^']*'"          . 'font-lock-constant-face)
        ("\"[^\"]*\""               . 'font-lock-string-face  )
        ;; Based IMP constants
        ("\\<[0-9][ ]*[0-9 ]*[ ]*_[ ]*[0-9A-Fa-f][0-9A-Fa-f ]*\\>" . 'font-lock-constant-face)
        ;; Decimal IMP constants
        ("\\<[0-9][0-9 ]*\\.[ ]*[0-9][0-9 ]*@[ ]*[-+]?[ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ("\\<[0-9][0-9 ]*\\.[ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ;; Integer IMP constants
        ("\\<[0-9][ ]*[0-9][0-9 ]*\\>" . 'font-lock-constant-face)
        ("\\<[0-9][ ]*\\>" . 'font-lock-constant-face)
        ;; stropped keywords
        ("\\%[A-Za-z][A-Za-z]*"       . 'font-lock-keyword-face )
        ))

(define-derived-mode imp-mode prog-mode "Simple Imp Mode"
   :syntax-table imp-mode-syntax-table
  (setq font-lock-defaults '(imp-highlights))
  (font-lock-fontify-buffer)
  )

;; Apply automatically to Imp source files:
(add-to-list 'auto-mode-alist '("\\.i\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp77\\'" . imp-mode))
(add-to-list 'auto-mode-alist '("\\.imp80\\'" . imp-mode))

;; enter <esc>xfun<cr> to disable imp-mode if unwanted during an edit.

Based on the info from http://xahlee.info/emacs/emacs/elisp_syntax_coloring.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文