java.scm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 Julien Lepiller <julien@lepiller.eu>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; 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. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build maven java)
  19. #:use-module (ice-9 peg)
  20. #:use-module (ice-9 textual-ports)
  21. #:export (parse-java-file))
  22. (define-peg-pattern java-file body (and (* WS) (* (and top-level-statement
  23. (* WS)))))
  24. (define-peg-pattern WS none (or " " "\n" "\t" "\r"))
  25. (define-peg-pattern top-level-statement body (or package import-pat class-pat comment inline-comment))
  26. (define-peg-pattern package all (and (ignore "package") (* WS) package-name
  27. (* WS) (ignore ";")))
  28. (define-peg-pattern import-pat all (and (ignore "import") (* WS)
  29. (? (and (ignore "static") (* WS)))
  30. package-name
  31. (* WS) (ignore ";")))
  32. (define-peg-pattern comment all (and (? (and annotation-pat (* WS))) (ignore "/*")
  33. comment-part))
  34. (define-peg-pattern comment-part body (or (ignore (and (* "*") "/"))
  35. (and (* "*") (+ comment-chr) comment-part)))
  36. (define-peg-pattern comment-chr body (or "\t" "\n" (range #\ #\)) (range #\+ #\xffff)))
  37. (define-peg-pattern inline-comment none (and (ignore "//") (* inline-comment-chr)
  38. (ignore "\n")))
  39. (define-peg-pattern inline-comment-chr body (range #\ #\xffff))
  40. (define-peg-pattern package-name body (* (or (range #\a #\z) (range #\A #\Z)
  41. (range #\0 #\9) "_" ".")))
  42. (define-peg-pattern class-pat all (and (? (and annotation-pat (* WS)))
  43. (* (ignore (or inline-comment comment)))
  44. (? (and (ignore "private") (* WS)))
  45. (? (and (ignore "public") (* WS)))
  46. (? (and (ignore "static") (* WS)))
  47. (? (and (ignore "final") (* WS)))
  48. (? (and (ignore "abstract") (* WS)))
  49. (ignore "class")
  50. (* WS) package-name (* WS)
  51. (? extends)
  52. (? implements)
  53. (ignore "{") class-body (ignore "}")))
  54. (define-peg-pattern extends all (? (and (ignore "extends") (* WS)
  55. package-name (* WS))))
  56. (define-peg-pattern implements all (? (and (ignore "implements") (* WS)
  57. package-name (* WS))))
  58. (define-peg-pattern annotation-pat all (and (ignore "@") package-name
  59. (? (and
  60. (* WS)
  61. (ignore "(") (* WS)
  62. annotation-attr (* WS)
  63. (* (and (ignore ",") (* WS)
  64. annotation-attr (* WS)))
  65. (ignore ")")))))
  66. (define-peg-pattern annotation-attr all (or (and attr-name (* WS) (ignore "=")
  67. (* WS) attr-value (* WS))
  68. attr-value))
  69. (define-peg-pattern attr-name all (* (or (range #\a #\z) (range #\A #\Z) (range #\0 #\9)
  70. "_")))
  71. (define-peg-pattern attr-value all (or "true" "false"
  72. (+ (or (range #\0 #\9) (range #\a #\z)
  73. (range #\A #\Z) "." "_"))
  74. array-pat
  75. string-pat))
  76. (define-peg-pattern array-pat body
  77. (and (ignore "{") (* WS) value
  78. (* (and (* WS) "," (* WS) value))
  79. (* WS) (ignore "}")))
  80. (define-peg-pattern string-pat body (and (ignore "\"") (* string-chr) (ignore "\"")))
  81. (define-peg-pattern string-chr body (or " " "!" (and (ignore "\\") "\"")
  82. (and (ignore "\\") "\\") (range #\# #\xffff)))
  83. (define-peg-pattern class-body all (and (* WS) (* (and class-statement (* WS)))))
  84. (define-peg-pattern class-statement body (or inline-comment comment param-pat
  85. method-pat class-pat))
  86. (define-peg-pattern param-pat all (and (* (and annotation-pat (* WS)
  87. (? (ignore inline-comment))
  88. (* WS)))
  89. (? (and (ignore (or "private" "public"
  90. "protected"))
  91. (* WS)))
  92. (? (and (ignore "static") (* WS)))
  93. (? (and (ignore "volatile") (* WS)))
  94. (? (and (ignore "final") (* WS)))
  95. type-name (* WS) param-name
  96. (? (and (* WS) (ignore "=") (* WS) value))
  97. (ignore ";")))
  98. (define-peg-pattern value none (or string-pat (+ valuechr)))
  99. (define-peg-pattern valuechr none (or comment inline-comment "\n"
  100. "\t" "\r"
  101. (range #\ #\:) (range #\< #\xffff)))
  102. (define-peg-pattern param-name all (* (or (range #\a #\z) (range #\A #\Z) (range #\0 #\9)
  103. "_")))
  104. (define-peg-pattern type-name all type-pat)
  105. (define-peg-pattern type-pat body
  106. (or "?"
  107. (and (* (or (range #\a #\z) (range #\A #\Z) (range #\0 #\9) "_"))
  108. (? "...")
  109. (? "[]")
  110. (? type-param))))
  111. (define-peg-pattern type-param body (and "<" (? type-pat)
  112. (* (and (* WS) "," (* WS) type-pat))
  113. (* WS) ">"))
  114. (define-peg-pattern method-pat all (and (* (and annotation-pat (* WS)))
  115. (? (and (ignore (or "private" "public" "protected"))
  116. (* WS)))
  117. (? (and (ignore type-param) (* WS)))
  118. (? (and (ignore (or "abstract" "final"))
  119. (* WS)))
  120. (? (and (ignore "static") (* WS)))
  121. type-name (* WS) param-name (* WS)
  122. (ignore "(")
  123. param-list (ignore ")") (* WS)
  124. (? (and (ignore "throws") (* WS) package-name (* WS)
  125. (* (and (ignore ",") (* WS) package-name
  126. (* WS)))))
  127. (or (ignore ";")
  128. (and (ignore "{") (* WS)
  129. (? (and method-statements (* WS)))
  130. (ignore "}")))))
  131. (define-peg-pattern param-list all (and (* WS) (* (and (? annotation-pat) (* WS)
  132. type-name (* WS)
  133. param-name (* WS)
  134. (? (ignore ",")) (* WS)))))
  135. (define-peg-pattern method-statements none (and (or (+ method-chr)
  136. (and "{" method-statements "}")
  137. string-pat)
  138. (? method-statements)))
  139. (define-peg-pattern method-chr none (or "\t" "\n" "\r" " " "!" (range #\# #\z) "|"
  140. (range #\~ #\xffff)))
  141. (define (parse-java-file file)
  142. (peg:tree (match-pattern java-file (call-with-input-file file get-string-all))))