/nrepl-client/nrepl-client/nrepl-client.factor

https://bitbucket.org/kotarak/vimclojure/ · Factor · 82 lines · 50 code · 12 blank · 20 comment · 3 complexity · 10ac03f3e590afbb2d813b1fbf97c795 MD5 · raw file

  1. ! Copyright 2011 (c) Meikel Brandmeyer.
  2. ! All rights reserved.
  3. !
  4. ! Permission is hereby granted, free of charge, to any person obtaining a copy
  5. ! of this software and associated documentation files (the "Software"), to deal
  6. ! in the Software without restriction, including without limitation the rights
  7. ! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. ! copies of the Software, and to permit persons to whom the Software is
  9. ! furnished to do so, subject to the following conditions:
  10. !
  11. ! The above copyright notice and this permission notice shall be included in
  12. ! all copies or substantial portions of the Software.
  13. !
  14. ! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. ! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. ! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. ! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. ! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. ! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. ! THE SOFTWARE.
  21. USING: accessors combinators command-line io io.encodings.utf8 io.sockets
  22. kernel math math.parser namespaces prettyprint sequences strings uuid ;
  23. IN: nrepl-client
  24. TUPLE: message id code stdin ;
  25. : <message> ( code stdin -- message )
  26. uuid1 2over message boa swap drop swap drop ;
  27. TUPLE: response id stdout stderr value nspace status ;
  28. : <response> ( id -- response )
  29. "" "" "" "" "more" response boa ;
  30. : read-response-chunk-count ( -- count )
  31. readln string>number ;
  32. : read-trimmed-line ( -- line )
  33. readln rest but-last ;
  34. : read-response-chunk ( response -- response )
  35. read-trimmed-line
  36. { { "id" [ readln drop ] }
  37. { "out" [ [ read-trimmed-line append ] change-stdout ] }
  38. { "err" [ [ read-trimmed-line append ] change-stderr ] }
  39. { "value" [ read-trimmed-line >>value ] }
  40. { "ns" [ read-trimmed-line >>nspace ] }
  41. { "status" [ read-trimmed-line >>status ] }
  42. } case ;
  43. : read-response-chunks ( response -- response )
  44. read-response-chunk-count [ read-response-chunk ] times ;
  45. : read-response ( response -- response )
  46. [ dup status>> "done" = not ] [ read-response-chunks ] while ;
  47. : print-response ( response -- )
  48. "{" write
  49. """ "stdout" : """" write dup stdout>> write """",""" write
  50. """ "stderr" : """" write dup stderr>> write """",""" write
  51. """ "value" : """" write dup value>> write """",""" write
  52. """ "namespace" : """" write dup nspace>> write """" """ write
  53. "}" print
  54. flush
  55. drop ;
  56. : send-message ( message -- response )
  57. "3" print
  58. """"id"""" print dup id>> .
  59. """"code"""" print dup code>> .
  60. """"in"""" print dup stdin>> .
  61. flush
  62. id>> <response> ;
  63. : nrepl-session ( -- )
  64. readln contents <message>
  65. command-line get first2 string>number <inet> utf8 [
  66. send-message
  67. read-response
  68. ] with-client
  69. print-response ;
  70. MAIN: nrepl-session