ob-ebnf.el 2.9 KB

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