ob-java.el: Allow for more whitespace in java code

* lisp/ob-java.el (org-babel-java--package-re)
(org-babel-java--imports-re, org-babel-java--class-re)
(org-babel-java--main-re, org-babel-java--any-method-re):
Updated regexps to allow for more whitespace in the content of java
code blocks.  Convert regexps to `rx' to improve clarity.
* testing/lisp/test-ob-java.el (ob-java/simple-with-main-whitespace):
Added test case with excessive whitespace.

Reported-by: Jarmo Hurri <jarmo.hurri@iki.fi>
Ref: https://orgmode.org/list/87o8k68w05.fsf@iki.fi
This commit is contained in:
Ian Martins 2020-11-12 05:18:48 -05:00
parent c38fda993d
commit dc2238144f
2 changed files with 48 additions and 5 deletions

View File

@ -77,15 +77,40 @@ like javac -verbose."
:package-version '(Org . "9.5")
:type 'symbol)
(defconst org-babel-java--package-re "^[[:space:]]*package[[:space:]]+\\\([[:alnum:]_\.]+\\\);$"
(defconst org-babel-java--package-re (rx line-start (0+ space) "package"
(1+ space) (group (1+ (in alnum ?_ ?.))) ; capture the package name
(0+ space) ?\; line-end)
"Regexp for the package statement.")
(defconst org-babel-java--imports-re "^[[:space:]]*import[[:space:]]+\\\([[:alnum:]_\.]+\\\);$"
(defconst org-babel-java--imports-re (rx line-start (0+ space) "import"
(1+ space) (group (1+ (in alnum ?_ ?.))) ; capture the fully qualified class name
(0+ space) ?\; line-end)
"Regexp for import statements.")
(defconst org-babel-java--class-re "^[[:space:]]*\\\(?:public[[:space:]]+\\\)?class[[:space:]]+\\\([[:alnum:]_]+\\\)[[:space:]]*\n?[[:space:]]*{"
(defconst org-babel-java--class-re (rx line-start (0+ space) (opt (seq "public" (1+ space)))
"class" (1+ space)
(group (1+ (in alnum ?_))) ; capture the class name
(0+ space) ?{)
"Regexp for the class declaration.")
(defconst org-babel-java--main-re "public static void main(String\\\(?:\\[]\\\)?[[:space:]]+[^ ]+\\\(?:\\[]\\\)?).*\n?[[:space:]]*{"
(defconst org-babel-java--main-re (rx line-start (0+ space) "public"
(1+ space) "static"
(1+ space) "void"
(1+ space) "main"
(0+ space) ?\(
(0+ space) "String"
(0+ space) (1+ (in alnum ?_ ?\[ ?\] space)) ; "[] args" or "args[]"
(0+ space) ?\)
(0+ space) (opt "throws" (1+ (in alnum ?_ ?, ?. space)))
?{)
"Regexp for the main method declaration.")
(defconst org-babel-java--any-method-re "public .*(.*).*\n?[[:space:]]*{"
(defconst org-babel-java--any-method-re (rx line-start
(0+ space) (opt (seq (1+ alnum) (1+ space))) ; visibility
(opt (seq "static" (1+ space))) ; binding
(1+ (in alnum ?_ ?\[ ?\])) ; return type
(1+ space) (1+ (in alnum ?_)) ; method name
(0+ space) ?\(
(0+ space) (0+ (in alnum ?_ ?\[ ?\] ?, space)) ; params
(0+ space) ?\)
(0+ space) (opt "throws" (1+ (in alnum ?_ ?, ?. space)))
?{)
"Regexp for any method.")
(defconst org-babel-java--result-wrapper "\n public static String __toString(Object val) {
if (val instanceof String) {

View File

@ -128,6 +128,24 @@ public static void main(String args[]) {
#+end_src"
(should (string= "42" (org-babel-execute-src-block)))))
(ert-deftest ob-java/simple-with-main-whitespace ()
"Hello world program that defines a main function with the square brackets after `args'."
(org-test-with-temp-text
"#+begin_src java :results output silent
public
static
void
main
(
String
args []
)
{
System.out.print(42);
}
#+end_src"
(should (string= "42" (org-babel-execute-src-block)))))
(ert-deftest ob-java/simple-with-class ()
"Hello world program that defines a class."
(org-test-with-temp-text