aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-02-26 17:20:53 +0100
committerMattias Andrée <m@maandree.se>2026-02-26 17:20:53 +0100
commitdaf124f492c3bdc59c075c145db4f41186f63083 (patch)
tree30024cb96501dc80cce0f75d7ba34de2751c3cb8
parentAdd \t and improve printing of text, and print the input text when PRINT_ACTIONS used (diff)
downloadlibparser-daf124f492c3bdc59c075c145db4f41186f63083.tar.gz
libparser-daf124f492c3bdc59c075c145db4f41186f63083.tar.bz2
libparser-daf124f492c3bdc59c075c145db4f41186f63083.tar.xz
Add extras/libparser-mode.el
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r--extras/libparser-mode.el79
1 files changed, 79 insertions, 0 deletions
diff --git a/extras/libparser-mode.el b/extras/libparser-mode.el
new file mode 100644
index 0000000..a97eb1f
--- /dev/null
+++ b/extras/libparser-mode.el
@@ -0,0 +1,79 @@
+; See LICENSE file for copyright and license details.
+
+(defvar libparser-mode-syntax-table
+ (let ((st (make-syntax-table)))
+ (modify-syntax-entry ?\" "\"" st)
+ (modify-syntax-entry ?\\ "\\" st)
+
+ (modify-syntax-entry ?\( "(" st)
+ (modify-syntax-entry ?\) ")" st)
+ (modify-syntax-entry ?\[ "(" st)
+ (modify-syntax-entry ?\] ")" st)
+ (modify-syntax-entry ?\{ "(" st)
+ (modify-syntax-entry ?\} ")" st)
+ (modify-syntax-entry ?\< "(" st)
+ (modify-syntax-entry ?\> ")" st)
+
+ (modify-syntax-entry ?\, "." st)
+ (modify-syntax-entry ?\; "." st)
+ (modify-syntax-entry ?! "." st)
+ (modify-syntax-entry ?? "." st)
+
+ (modify-syntax-entry ?\| "." st)
+ (modify-syntax-entry ?= "." st)
+ (modify-syntax-entry ?+ "." st)
+ (modify-syntax-entry ?- "." st)
+
+ st
+ )
+)
+
+; TODO "*)" inside quotes in comments should not be recognised
+(defun libparser--syntax-propertize (start end)
+ (goto-char start)
+ (while (search-forward "(*" end t)
+ (unless (nth 3 (syntax-ppss))
+ (put-text-property (match-beginning 0)
+ (1+ (match-beginning 0))
+ 'syntax-table (string-to-syntax "<"))
+ (put-text-property (1+ (match-beginning 0)) (match-end 0)
+ 'syntax-table (string-to-syntax "<"))))
+ (goto-char start)
+ (while (search-forward "*)" end t)
+ (unless (nth 3 (syntax-ppss))
+ (put-text-property (1+ (match-beginning 0)) (match-end 0)
+ 'syntax-table (string-to-syntax ">"))))
+)
+
+(defconst libparser-font-lock-keywords
+ `(
+ ; TODO only inside < > (identifier otherwise); or even better, colour whole < >
+ ("\\_<0[xX][0-9A-Fa-f]+\\_>" . font-lock-constant-face)
+ ("\\_<[0-9]+\\_>" . font-lock-constant-face)
+
+ ("\\_<[A-Za-z0-9][A-Za-z0-9_-]*\\_>-*" . font-lock-variable-name-face)
+ ; _-prefixed identifiers are not coloured
+
+ ("[=;]" . font-lock-keyword-face)
+ ("[][!?+{}-]" . font-lock-keyword-face)
+ ; concatenations (,) and alternations (|) are not coloured
+
+ ("[()]" . font-lock-bracket-face)
+ )
+)
+
+(define-derived-mode libparser-mode prog-mode "libparser"
+ "Major mode for libparser grammar files."
+ :syntax-table libparser-mode-syntax-table
+
+ (setq-local syntax-propertize-function #'libparser--syntax-propertize)
+
+ (setq-local font-lock-defaults
+ '(libparser-font-lock-keywords)
+ )
+
+ (setq-local comment-start "(*")
+ (setq-local comment-end "*)")
+)
+
+(provide 'libparser-mode)