all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* PATCH: Add JISON to ORG-Babel Supported Languages
@ 2014-11-19  2:29 Peter Moresi
  2014-11-21 23:14 ` Nicolas Goaziou
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Moresi @ 2014-11-19  2:29 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 552 bytes --]

Hi,

I'm spending a lot of time in org-mode doing literate programming and it
does most everything that I need.

However, it did not support the jison
<http://zaach.github.io/jison/try/> compiler
generator.  So I added it by copying ob-dot.el and making some minor
changes. I also wrote instructions that more or less follow the same
outline as the dot documentation
<http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-dot.html>.

If this patch is added to org-mode then org-babel will have a language for
making new languages.

Thanks,

Peter

[-- Attachment #1.2: Type: text/html, Size: 726 bytes --]

[-- Attachment #2: org-babel-jison.org --]
[-- Type: application/octet-stream, Size: 3951 bytes --]

#+TITLE: JISON Source Code Blocks in Org Mode
#+AUTHOR: Peter Moresi

* Introductions

  [[http://zaach.github.io/jison/][JISON]] is a port of GNU Bison to JavaScript. 

  This tool is a compiler generator that allows you to extend the browser and other JavaScript execution environments with new languages.

  [[http://zaach.github.io/jison/assets/images/jison_x1.png]]

* Requirement & Setup

  JISON is distributed as an open source project with a permissive MIT-like license.

  You can configure Org mode to execute jison source code blocks by adding a line to org-babel-load-languages:

  #+BEGIN_SRC emacs-lisp
    (org-babel-do-load-languages
     'org-babel-load-languages
     '((jison . t))) ; this line activates jison
  #+END_SRC

  If you desire syntax highlighting then you can use [[https://github.com/petermoresi/bison-mode][bison-mode]].

* Org Mode Features for jison Source Code Blocks

  JISON source code blocks produce a bottom-up parser in JavaScript. The default value for the :results header argument is "file" and for the :exports header argument it is "results".

  There are two jison specific header arguments that can be used to tailor the command line. They are:

  1. :cmd
     - this header argument can be used to change the program from the default "jison".
  2. :cmdline
     - the default value is "". A sensible values is "-m js". See jison --help for more options.

  The :file header argument is required for jison source code blocks.

* Sessions

  JISON does not support sessions.

* Result Types

  JISON source code blocks produce JavaScript files, so the default value "file" is the only sensible type of result.

* Examples of Use
** Create a Parser for your Language

  A typical use of jison is to create your own calculator language.

  #+NAME: calculator
  #+BEGIN_SRC jison :file calculator.js :exports both

    /* description: Parses and executes mathematical expressions. */

    /* lexical grammar */
    %lex
    %%

    \s+                   /* skip whitespace */
    [0-9]+("."[0-9]+)?\b  return 'NUMBER'
    "*"                   return '*'
    "/"                   return '/'
    "-"                   return '-'
    "+"                   return '+'
    "^"                   return '^'
    "!"                   return '!'
    "%"                   return '%'
    "("                   return '('
    ")"                   return ')'
    "PI"                  return 'PI'
    "E"                   return 'E'
    <<EOF>>               return 'EOF'
    .                     return 'INVALID'

    /lex

    /* operator associations and precedence */

    %left '+' '-'
    %left '*' '/'
    %left '^'
    %right '!'
    %right '%'
    %left UMINUS

    %start expressions

    %% /* language grammar */

    expressions
        : e EOF
            { return $1; }
        ;

    e
        : e '+' e
            {$$ = $1+$3;}
        | e '-' e
            {$$ = $1-$3;}
        | e '*' e
            {$$ = $1*$3;}
        | e '/' e
            {$$ = $1/$3;}
        | e '^' e
            {$$ = Math.pow($1, $3);}
        | e '!'
            {{
              $$ = (function fact (n) { return n==0 ? 1 : fact(n-1) * n })($1);
            }}
        | e '%'
            {$$ = $1/100;}
        | '-' e %prec UMINUS
            {$$ = -$2;}
        | '(' e ')'
            {$$ = $2;}
        | NUMBER
            {$$ = Number(yytext);}
        | E
            {$$ = Math.E;}
        | PI
            {$$ = Math.PI;}
        ;

  #+END_SRC

  Executing the source code block above will result in a JavaScript file.

  #+RESULTS: calculator
  [[file:calculator.js]]

** Run your new language

   This source code block loads the calculator and parses a mathmatically expression. 

  #+BEGIN_SRC js :var folder=(file-name-directory buffer-file-name)
    var calculator = require(folder + "calculator.js")

    return calculator.parse("5*PI^2");
  #+END_SRC

  #+RESULTS:
  : 49.34802200544679



   

[-- Attachment #3: 0001-Add-jison-support-in-org-babel.patch --]
[-- Type: application/octet-stream, Size: 3280 bytes --]

From a9831e968bb4b2dcd0776720fbfa86fa9e694d47 Mon Sep 17 00:00:00 2001
From: Peter Moresi <peter.moresi@gmail.com>
Date: Tue, 18 Nov 2014 18:11:01 -0800
Subject: [PATCH] Add jison support in org babel

---
 lisp/ob-jison.el | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 lisp/ob-jison.el

diff --git a/lisp/ob-jison.el b/lisp/ob-jison.el
new file mode 100644
index 0000000..be88129
--- /dev/null
+++ b/lisp/ob-jison.el
@@ -0,1 +1,77 @@
+;;; ob-jison.el --- org-babel functions for jison code block evaluation
+
+;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
+
+;; Author: Peter Moresi (based on ob-dot.el)
+;; Keywords: literate programming, reproducible research
+;; Homepage: http://orgmode.org
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This file converts a jison file into a JavaScript compiler
+;; to extend the language of the internet.
+
+;;; Code:
+(require 'ob)
+
+(defvar org-babel-default-header-args:jison
+  '((:results . "file") (:exports . "results"))
+  "Default arguments to use when evaluating a jison source block.")
+
+
+(defun org-babel-expand-body:jison (body params)
+  "Expand BODY according to PARAMS, return the expanded body."
+  (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
+    (mapc
+     (lambda (pair)
+       (let ((name (symbol-name (car pair)))
+	     (value (cdr pair)))
+	 (setq body
+	       (replace-regexp-in-string
+		(concat "\$" (regexp-quote name))
+		(if (stringp value) value (format "%S" value))
+		body))))
+     vars)
+    body))
+
+(defun org-babel-execute:jison (body params)
+  "Execute a block of JISON code with org-babel.
+This function is called by `org-babel-execute-src-block'."
+  (let* ((result-params (cdr (assoc :result-params params)))
+	 (out-file (cdr (or (assoc :file params)
+			    (error "You need to specify a :file parameter"))))
+	 (cmdline (or (cdr (assoc :cmdline params))
+		      ""))
+	 (cmd (or (cdr (assoc :cmd params)) "jison"))
+	 (in-file (org-babel-temp-file "jison-")))
+    (with-temp-file in-file
+      (insert (org-babel-expand-body:jison body params)))
+    (org-babel-eval
+     (concat cmd
+	     " " (org-babel-process-file-name in-file)
+	     " " cmdline
+	     " -o " (org-babel-process-file-name out-file)) "")
+    nil)) ;; signal that output has already been written to file
+
+
+(defun org-babel-prep-session:jison (session params)
+  "Return an error because org does not support sessions."
+  (error "JISON does not support sessions"))
+
+(provide 'ob-jison)
+;;; ob-jison.el ends here
-- 
1.9.3 (Apple Git-50)


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-11-21 23:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-19  2:29 PATCH: Add JISON to ORG-Babel Supported Languages Peter Moresi
2014-11-21 23:14 ` Nicolas Goaziou

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.