RagelMode

I’ve been working with Ragel (http://www.complang.org/ragel/) the state machine compiler, and put together a simple mmmMode based ragel highlighter. It just barely works, which is good enough for me at the moment. Feel free to patch fixes.

                (define-generic-mode 'ragel-mode
                   '(?#) ;; comments
                   '( 
                     ;; keywords
                      "machine" "action" "access" "context" "include" "import" "export" "prepush" "postpop"
                      "when" "inwhen" "outwhen" "err" "lerr" "eof" "from" "to" 
                      "alphtype" "getkey" "write" 
                      ;; rules
                      "any" "ascii" "extend" "alpha" "digit" "alnum" "lower" "upper" 
                      "xdigit" "cntrl" "graph" "print" "punct" "space" "zlen" "empty" 
                      ;; inline code matching
                      "fpc" "fc" "fcurs" "fbuf" "fblen" "ftargs" "fstack"
                      "fhold" "fgoto" "fcall" "fret" "fentry" "fnext" "fexec" "fbreak"
                     )
                   '(
                     ;; literals
                     ;("\\([^\\)]*\\)" . font-lock-constant-face)
                     ;("\\[[[^\\]]*\\]" . font-lock-constant-face)
                     ("\(\"\\?'\"\'|\\?\"'\|'[^']*'\|\"[^\"]*\"\)" . font-lock-constant-face)

                     ;; Numbers
                     ("[0-9][0-9]*" . font-lock-constant-face)
                     ("0x[0-9a-fA-F][0-9a-fA-F]*" . font-lock-constant-face)
                     
                     ;; Operators
                     ("[>$%@]" . font-lock-constant-face)
                    ("<>\|<" . font-lock-constant-face)
;                    ("[>\<$%@][!\^/*~]" . font-lock-constant-face)
;                    ("[>$%]?" . font-lock-constant-face)
;                    ("<>[!\^/*~]" . font-lock-constant-face)
                     ("=>" . font-lock-constant-face)
                     ("->" . font-lock-constant-face)

                     (":>" . font-lock-constant-face)
                     (":>>" . font-lock-constant-face)
                     ("<:" . font-lock-constant-face)
                     )
                   nil ;'(".rl\\'") 
                   nil
                   "Generic mode for mmm-mode editing .rl files.")

                 (mmm-add-classes
                  '((embedded-ragel
                     :submode ragel-mode
                     :save-matches 1
                     :case-fold-search 1
                     :face mmm-declaration-submode-face
                     :front "%%{"
                     :include-front t
                     ;; :front-offset (end-of-line 1)
                     :back "}%%"
                     :include-back t
                     ;; :back-offset (end-of-line 1)
                     ;; line match? "%%[^{]" "%%$"
                     )))
                 (mmm-add-mode-ext-class 'c++-mode nil 'embedded-ragel)
                 (mmm-add-mode-ext-class 'cperl-mode nil 'embedded-ragel)
                 (mmm-add-mode-ext-class 'c-mode nil 'embedded-ragel)

                 (add-to-list 'mmm-c-derived-modes 'embedded-ragel)

Just found this implementation for working with ruby. https://github.com/bartuer/dot-emacs/blob/master/bartuer-ragel.el

(defun ragel-indent-line ()
  "proxy indent line to js2"
  (interactive)
  (js2-indent-line)
  )

(defun ragel-new-line ()
  "handle new line"
  (interactive)
  (newline-and-indent)
  (ragel-indent-line)
  )

(defun bartuer-ragel-load ()
  "mode hooks for ragel"

  (set (make-local-variable 'ruby-block-beg-keywords) (cons "%%{" ruby-block-beg-keywords))
  (set (make-local-variable 'ruby-block-beg-re) (regexp-opt ruby-block-beg-keywords))
  (set (make-local-variable 'ruby-block-end-re) "\\<end\\|%%}\\>")

  (font-lock-add-keywords
   nil
   '(
     ("^ *\\(#.*\\)$" . font-lock-variable-name-face)
     (">=" . font-lock-negation-char-face)
     ("\\(fsm\\)\\(->\\)"
      (1 font-lock-variable-name-face)
      (2 font-lock-negation-char-face)
      )
     ("\\<\\(\\|fhold\\|fgoto\\|fcall\\|fret\\|fentry\\|fnext\\|fexec\\|fbreak\\)\\>" . font-lock-builtin-face)
     ("\\<\\(any\\|ascii\\|extend\\|alpha\\|digit\\|alnum\\|lower\\|uper\\|xdigit\\|cntrl\\|graph\\|print\\|punct\\|space\\|null\\|zlen\\|empty\\)\\>" . font-lock-variable-name-face)
     ("\\<\\(machine\\|action\\|context\\|include\\|range\\|import\\|export\\|prepush\\|postpop\\)\\>" . font-lock-function-name-face)
     ("\\<\\(write\\) +\\(init\\|data\\|exec\\|exports\\|start\\|error\\|first_final\\|contained\\)\\>"
      (1 font-lock-function-name-face nil t)
      (2 font-lock-variable-name-face nil t)
      )
     ("\\<\\(when\\|inwhen\\|outwhen\\|err\\|lerr\\|eof\\|from\\|to\\)\\>" . font-lock-keyword-face)
     ("\\<\\(fpc\\|fc\\|fcurs\\|fbuf\\|fblen\\|ftargs\\|fstack\\)\\>" . font-lock-constant-face)
     ("\\<\\(noerror\\|nofinal\\|noprefix\\|noend\\|nocs\\|contained\\)\\>" . font-lock-constant-face)
     ("\\([a-zA-Z\\)]\\|]\\)\\([*+]+\\)" 
      (2 font-lock-keyword-face))
     ("\\([a-zA-Z]+\\)\\(:\\)"
      (2 font-lock-keyword-face))
     ("\\([*|][*|]\\)" . font-lock-keyword-face)
     ("\\([>$@*%<]>?\\)\\([~^~*/]\\|!\\)"
      (1 font-lock-keyword-face)
      (2 font-lock-keyword-face))
     ("[^ ]\\([+?*]\\)"
      (1 font-lock-keyword-face))
      ("\\( \\. \\| \\.\\. \\|\\*\\*\\|[>^$%@&|\\!-]\\| ->\\|:>\\|:>>\\|<:\\|=>\\|:=\\|%%\\)" . font-lock-keyword-face)
     ) 
   (font-lock-fontify-buffer))

  (set (make-local-variable 'indent-line-function) 'ragel-indent-line)

  (define-key ruby-mode-map "\C-j" 'compile-ruby)
  (define-key c-mode-map "\M-g\C-j" 'show-graph)
  (define-key c-mode-map "\M-gj" 'show-graph)
  (define-key c-mode-map "\M-gx" 'compile-xml)
  (define-key c-mode-map "\C-m" 'newline-and-indent)
)