ob-ebnf.el 3.0 KB

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