5

**** Stephan 解决问题 1 - 请参阅下面的答案****

\在语法表中标记为转义字符,然后覆盖 Mathematica 语法元素(如\[Infinity]. 这是我的syntax-propertize-function

(defconst math-syntax-propertize-function
  (syntax-propertize-rules
   ("\\\\\\[\\([A-Z][A-Za-z]*\\)]" (0 "_"))))

我从(defun math-node()函数中引用它,如下所示:

  (set (make-local-variable 'syntax-propertize-function)
       math-syntax-propertize-function)

在我的第一次尝试中,我没有使用该make-local-variable功能,当我的 elisp 缓冲区突出显示出错时,我感到很惊讶。

****结束问题1的解决方案****

我在 Emacs 中实现了一个从 cc-mode 派生的主要模式,用于编辑 Mathematica 文件。目标是语法高亮和缩进。我将把与 Mathematica 内核的接口留到以后。

我的基本功能正常工作,但有几个症结给我带来了麻烦。

****问题 1** - 该\字符用作转义字符并作为多字符、括号关键字的前缀。**

像许多语言一样,Mathematica 使用\字符进行转义",而其他\字符是字符串。

Mathematic 具有 Mathematica 中所说的语法字符,如\[Times]\[Element]\[Infinity]等,代表数学运算符和常量。

而且,Mathematica在函数定义和调用等方面大量使用[and]而不是(and 。)

因此,如果我在语法表中标记\为转义字符,那么我的括号在我使用语法字符的任何地方都会不匹配。例如,

    If[x < \[Pi], True, False]

当然,cc-mode 有意忽略[. \鉴于 Mathematica 的函数性质,如果不能匹配括号,该模式几乎是无用的。想想没有paren匹配的lisp。

如果我不将\语法表作为转义字符放入,那么如何处理注释和字符串中的转义序列?

如果我可以将 Times、Element、Infinity 等放入关键字列表并让一切正常工作,那就太好了。

****问题 2** - Mathematica 的语法与 C、C++、Java、ObjC 等有很大不同,以至于 cc-mode 的内置语法分析并不总能产生所需的结果。**

考虑以下代码块:

    FooBar[expression1,
           expression2,
           expression3];

这种格式很漂亮,因为表达式被识别为参数列表。

但是,如果将列表作为参数传递,

    FooBar[{expression1,
                expression2,
                expression3}];

{结果并不漂亮,因为表达式被认为是and中单个语句的延续}。不幸的是,简单的设置c-continuation-offset破坏0了实际的延续,比如,

    addMe[x_Real, y_Real] :=
        Plus[x, y];

你想要缩进的。

问题在于 Mathematica{}delineate 列表而不是代码块。

这是我正在使用的当前 elisp 文件:

(require 'cc-mode)

;; There are required at compile time to get the sources for the                                
;; language constants.                                                                          
(eval-when-compile
  (require 'cc-langs)
  (require 'cc-fonts))

;; Add math mode the the language constant system. This needs to be                             
;; done at compile time because that is when the language constants                             
;; are evaluated.                                                                               
(eval-and-compile
  (c-add-language 'math-mode 'c-mode))


;; Function names                                                                               
(c-lang-defconst c-cpp-matchers
  math (append
        (c-lang-const c-cpp-matchers c)
        ;; Abc[                                                                                 
        '(("\\<\\([A-Z][A-Za-z0-9]*\\)\\>\\[" 1 font-lock-type-face))
        ;; abc[                                                                                 
        '(("\\<\\([A-Za-z][A-Za-z0-9]*\\)\\>\\[" 1 font-lock-function-name-face))
        ;; Abc                                                                                  
        '(("\\<\\([A-Z][A-Za-z0-9]*\\)\\>" 1 font-lock-keyword-face))
        ;; abc_                                                                                 
        '(("\\<\\([a-z][A-Za-z0-9]*[_]\\)\\>" 1 font-lock-variable-name-face))
        ))

;; font-lock-comment-face                                                                       
;; font-lock-doc-face                                                                           
;; font-lock-string-face                                                                        
;; font-lock-keyword-fact                                                                       
;; font-lock-function-name-face                                                                 
;; font-lock-constant-face                                                                      
;; font-lock-type-face                                                                          
;; font-lock-builtin-face                                                                       
;; font-lock-reference-face                                                                     
;; font-lock-warning-face                                                                       


;; There is no line comment character.                                                          
(c-lang-defconst c-line-comment-starter
  math nil)

;; The block comment starter is (*.                                                             
(c-lang-defconst c-block-comment-starter
  math "(*")

;; The block comment ender is *).                                                               
(c-lang-defconst c-block-comment-ender
  math "*)")

;; The assignment operators.                                                                    
(c-lang-defconst c-assignment-operators
  math '("=" ":=" "+=" "-=" "*=" "/=" "->" ":>"))

;; The operators.                                                                               
(c-lang-defconst c-operators
  math `(
         ;; Unary.                                                                              
         (prefix "+" "-" "!")
         ;; Multiplicative.                                                                     
         (left-assoc "*" "/")
         ;; Additive.                                                                           
         (left-assoc "+" "-")
         ;; Relational.                                                                         
         (left-assoc "<" ">" "<=" ">=")
         ;; Equality.                                                                           
         (left-assoc "==" "=!=")  
         ;; Assignment.                                                                         
         (right-assoc ,@(c-lang-const c-assignment-operators))
         ;; Sequence.                                                                           
         (left-assoc ",")))


;; Syntax modifications necessary to recognize keywords with                                    
;; punctuation characters.                                                                      
;; (c-lang-defconst c-identifier-syntax-modifications                                           
;;   math (append '((?\\ . "w"))                                                                
;;             (c-lang-const c-identifier-syntax-modifications)))                               

;; Constants.                                                                                   
(c-lang-defconst c-constant-kwds
  math '( "False" "True" )) ;; "\\[Infinity]" "\\[Times]" "\\[Divide]" "\\[Sqrt]" "\\[Element]"\
))                                                                                              

(defcustom math-font-lock-extra-types nil
  "Extra types to recognize in math mode.")

(defconst math-font-lock-keywords-1 (c-lang-const c-matchers-1 math)
  "Minimal highlighting for math mode.")

(defconst math-font-lock-keywords-2 (c-lang-const c-matchers-2 math)
  "Fast normal highlighting for math mode.")

(defconst math-font-lock-keywords-3 (c-lang-const c-matchers-3 math)
  "Accurate normal highlighting for math mode.")

(defvar math-font-lock-keywords math-font-lock-keywords-3
  "Default expressions to highlight in math mode.")

(defvar math-mode-syntax-table nil
  "Syntax table used in math mode.")

(message "Setting math-mode-syntax-table to nil to force re-initialization")
(setq math-mode-syntax-table nil)

;; If a syntax table has not yet been set, allocate a new syntax table                          
;; and setup the entries.                                                                       
(unless math-mode-syntax-table
  (setq math-mode-syntax-table
        (funcall (c-lang-const c-make-mode-syntax-table math)))

  (message "Modifying the math-mode-syntax-table")

  ;; character (                                                                                
  ;; ( - open paren class                                                                       
  ;; ) - matching paren character                                                               
  ;; 1 - 1st character of comment delimitter (**)                                               
  ;; n - nested comments allowed                                                                
  (modify-syntax-entry ?\( "()1n" math-mode-syntax-table)

  ;; character )                                                                                
  ;; ) - close parent class                                                                     
  ;; ( - matching paren character                                                               
  ;; 4 - 4th character of comment delimitter (**)                                               
  ;; n - nested comments allowed                                                                
  (modify-syntax-entry ?\) ")(4n" math-mode-syntax-table)

  ;; character *                                                                                
  ;; . - punctuation class                                                                      
  ;; 2 - 2nd character of comment delimitter (**)    
  ;; 3 - 3rd character of comment delimitter (**)                                               
  (modify-syntax-entry ?\* ". 23n" math-mode-syntax-table)

  ;; character [                                                                                
  ;; ( - open paren class                                                                       
  ;; ] - matching paren character                                                               
  (modify-syntax-entry ?\[ "(]" math-mode-syntax-table)

  ;; character ]                                                                                
  ;; ) - close paren class                                                                      
  ;; [ - mathcing paren character                                                               
  (modify-syntax-entry ?\] ")[" math-mode-syntax-table)

  ;; character {                                                                                
  ;; ( - open paren class                                                                       
  ;; } - matching paren character                                                               
  (modify-syntax-entry ?\{ "(}" math-mode-syntax-table)

  ;; character }                                                                                
  ;; ) - close paren class                                                                      
  ;; { - matching paren character                                                               
  (modify-syntax-entry ?\} "){" math-mode-syntax-table)

  ;; The following characters are punctuation (i.e. they cannot appear                          
  ;; in identifiers).                                                                           
  ;;                                                                                            
  ;; / ' % & + - ^ < > = |                                                                      
  (modify-syntax-entry ?\/ "." math-mode-syntax-table)
  (modify-syntax-entry ?\' "." math-mode-syntax-table)
  (modify-syntax-entry ?% "." math-mode-syntax-table)
  (modify-syntax-entry ?& "." math-mode-syntax-table)
  (modify-syntax-entry ?+ "." math-mode-syntax-table)
  (modify-syntax-entry ?- "." math-mode-syntax-table)
  (modify-syntax-entry ?^ "." math-mode-syntax-table)
  (modify-syntax-entry ?< "." math-mode-syntax-table)
  (modify-syntax-entry ?= "." math-mode-syntax-table)
  (modify-syntax-entry ?> "." math-mode-syntax-table)
  (modify-syntax-entry ?| "." math-mode-syntax-table)

  ;; character $                                                                                
  ;; _ - word class (since $ is allowed in identifier names)                                    
  (modify-syntax-entry ?\$ "_" math-mode-syntax-table)

  ;; character \                                                                                
  ;; . - punctuation class (for now treat \ as punctuation                                      
  ;;     until we can fix the \[word] issue).                                                   
  (modify-syntax-entry ?\\ "." math-mode-syntax-table)

  ) ;; end of math-mode-syntax-table adjustments                                                

;;                                                                                              
;;                                                                                              
(defvar math-mode-abbrev-table nil
  "Abbrevation table used in math mode buffers.")

(defvar math-mode-map (let ((map (c-make-inherited-keymap)))
                        map)
  "Keymap used in math mode buffers.")

;; math-mode                                                                                    
;;                                                                                              
(defun math-mode ()
  "Major mode for editing Mathematica code."

  (interactive)
  (kill-all-local-variables)

  (c-initialize-cc-mode t)

  (set-syntax-table math-mode-syntax-table)

  (setq major-mode 'math-mode
        mode-name "Math"
        local-abbrev-table math-mode-abbrev-table
        abbrev-mode t)

  (use-local-map math-mode-map)

  (c-init-language-vars math-mode)
  (c-common-init 'math-mode)

  (run-hooks 'c-mode-common-hook)
  (run-hooks 'math-mode-hook)
  (c-update-modeline))

(provide 'math-mode)                   

以及一些截图格式化代码

4

1 回答 1

5

虽然 cc-mode 旨在适应各种语言,但我不确定它是否适合 Mathematica,因为语法与 cc-mode 所支持的语法相去甚远。我建议尝试 SMIE(一种出现在 Emacs-23.4 中的缩进引擎,最初是为 SML 构建的,但目前用于多种语言)。就像 cc-mode 一样,SMIE 也不是所有语言的理想选择,但如果在您的情况下它比 cc-mode 效果更好,我不会感到惊讶。

对于反斜杠问题,最好的办法是使用syntax-propertize-function更改特定反斜杠的转义性质(在语法表中将 \ 设置为转义,然后将 \[foo] 的 \ 标记为非转义,或者保留 \作为语法表中的非转义,然后将 \" 和 \\ 的那些 \ 标记为转义)。

于 2013-03-11T19:24:25.273 回答