ob-ebnf.el 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; ob-ebnf.el --- org-babel functions for ebnf evaluation
  2. ;; Copyright (C) 2013 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 (assoc :file params)))
  48. (dest-dir (file-name-directory dest-file))
  49. (dest-root (file-name-sans-extension
  50. (file-name-nondirectory dest-file)))
  51. (dest-ext (file-name-extension dest-file))
  52. (style (cdr (assoc :style params)))
  53. (current-dir default-directory)
  54. (result nil))
  55. (with-temp-buffer
  56. (when style (ebnf-push-style style))
  57. (let ((comment-format
  58. (cond ((string= ebnf-syntax 'yacc) "/*%s*/")
  59. ((string= ebnf-syntax 'ebnf) ";%s")
  60. ((string= ebnf-syntax 'iso-ebnf) "(*%s*)")
  61. (t (setq result
  62. (format "EBNF error: format %s not supported."
  63. ebnf-syntax))))))
  64. (setq ebnf-eps-prefix dest-dir)
  65. (insert (format comment-format (format "[%s" dest-root)))
  66. (newline)
  67. (insert body)
  68. (newline)
  69. (insert (format comment-format (format "]%s" dest-root)))
  70. (ebnf-eps-buffer)
  71. (when style (ebnf-pop-style))))
  72. result)))
  73. (provide 'ob-ebnf)
  74. ;;; ob-ebnf.el ends here