ob-ebnf.el 3.0 KB

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