dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ob-ebnf.el (3020B)


      1 ;;; ob-ebnf.el --- Babel Functions for EBNF          -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2013-2021 Free Software Foundation, Inc.
      4 
      5 ;; Author: Michael Gauland
      6 ;; Keywords: literate programming, reproducible research
      7 ;; Homepage: https://git.sr.ht/~bzg/org-contrib
      8 
      9 ;; This file is not part of GNU Emacs.
     10 
     11 ;; GNU Emacs is free software: you can redistribute it and/or modify
     12 ;; it under the terms of the GNU General Public License as published by
     13 ;; the Free Software Foundation, either version 3 of the License, or
     14 ;; (at your option) any later version.
     15 
     16 ;; GNU Emacs is distributed in the hope that it will be useful,
     17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 ;; GNU General Public License for more details.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     23 
     24 ;;; Commentary:
     25 
     26 ;; Org-Babel support for using ebnf2ps to generate encapsulated postscript
     27 ;; railroad diagrams.  It recognizes these arguments:
     28 ;;
     29 ;;     :file is required; it must include the extension '.eps.' All the rules
     30 ;;           in the block will be drawn in the same file.  This is done by
     31 ;;           inserting a '[<file>' comment at the start of the block (see the
     32 ;;           documentation for ebnf-eps-buffer for more information).
     33 ;;
     34 ;;     :style specifies a value in ebnf-style-database.  This provides the
     35 ;;            ability to customize the output.  The style can also specify the
     36 ;;            grammar syntax (by setting ebnf-syntax); note that only ebnf,
     37 ;;            iso-ebnf, and yacc are supported by this file.
     38 
     39 ;;; Requirements:
     40 
     41 ;;; Code:
     42 (require 'ob)
     43 (require 'ebnf2ps)
     44 
     45 ;; optionally declare default header arguments for this language
     46 (defvar org-babel-default-header-args:ebnf '((:style . nil)))
     47 
     48 ;; Use ebnf-eps-buffer to produce an encapsulated postscript file.
     49 ;;
     50 (defun org-babel-execute:ebnf (body params)
     51   "Execute a block of Ebnf code with org-babel.
     52 This function is called by `org-babel-execute-src-block'."
     53   (save-excursion
     54     (let* ((dest-file (cdr (assq :file params)))
     55 	   (dest-dir (file-name-directory dest-file))
     56 	   (dest-root (file-name-sans-extension
     57 		       (file-name-nondirectory dest-file)))
     58 	   (style (cdr (assq :style params)))
     59 	   (result nil))
     60       (with-temp-buffer
     61 	(when style (ebnf-push-style style))
     62 	(let ((comment-format
     63 	       (cond ((string= ebnf-syntax 'yacc) "/*%s*/")
     64 		     ((string= ebnf-syntax 'ebnf) ";%s")
     65 		     ((string= ebnf-syntax 'iso-ebnf) "(*%s*)")
     66 		     (t (setq result
     67 			      (format "EBNF error: format %s not supported."
     68 				      ebnf-syntax))))))
     69 	  (setq ebnf-eps-prefix dest-dir)
     70 	  (insert (format comment-format (format "[%s" dest-root)))
     71 	  (newline)
     72 	  (insert body)
     73 	  (newline)
     74 	  (insert (format comment-format (format "]%s" dest-root)))
     75 	  (ebnf-eps-buffer)
     76 	  (when style (ebnf-pop-style))))
     77       result)))
     78 
     79 (provide 'ob-ebnf)
     80 
     81 ;;; ob-ebnf.el ends here