capi.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <ruby_parser/capi.hh>
  2. #include <cstdio>
  3. ruby_parser::typedruby25*
  4. rbdriver_typedruby25_new(const char* source_ptr, size_t source_length, const ruby_parser::builder* builder)
  5. {
  6. std::string source { source_ptr, source_length };
  7. return new ruby_parser::typedruby25(source, *builder);
  8. }
  9. void
  10. rbdriver_typedruby25_free(ruby_parser::typedruby25* driver)
  11. {
  12. delete driver;
  13. }
  14. const void*
  15. rbdriver_parse(ruby_parser::base_driver* driver, ruby_parser::SelfPtr self)
  16. {
  17. return driver->parse(self);
  18. }
  19. bool
  20. rbdriver_in_definition(const ruby_parser::base_driver *driver)
  21. {
  22. return driver->def_level > 0;
  23. }
  24. bool
  25. rbdriver_env_is_declared(const ruby_parser::base_driver *driver, const char* name, size_t length)
  26. {
  27. std::string id { name, length };
  28. return driver->lex.is_declared(id);
  29. }
  30. void
  31. rbdriver_env_declare(ruby_parser::base_driver *driver, const char* name, size_t length)
  32. {
  33. std::string id { name, length };
  34. driver->lex.declare(id);
  35. }
  36. size_t
  37. rbtoken_get_start(const ruby_parser::token* tok)
  38. {
  39. return tok->start();
  40. }
  41. size_t
  42. rbtoken_get_end(const ruby_parser::token* tok)
  43. {
  44. return tok->end();
  45. }
  46. size_t
  47. rbtoken_get_string(const ruby_parser::token* tok, const char** out_ptr)
  48. {
  49. *out_ptr = tok->string().data();
  50. return tok->string().size();
  51. }
  52. size_t
  53. rblist_get_length(const ruby_parser::node_list* list)
  54. {
  55. return list->size();
  56. }
  57. const void*
  58. rblist_index(ruby_parser::node_list* list, size_t index)
  59. {
  60. return list->at(index);
  61. }
  62. size_t
  63. rbdriver_diag_get_length(const ruby_parser::base_driver* driver)
  64. {
  65. return driver->diagnostics.size();
  66. }
  67. void
  68. rbdriver_diag_get(const ruby_parser::base_driver* driver, size_t index, struct cdiagnostic *diag)
  69. {
  70. auto &cppdiag = driver->diagnostics.at(index);
  71. diag->level = cppdiag.level();
  72. diag->type = cppdiag.error_class();
  73. diag->data = cppdiag.data().c_str();
  74. diag->beginPos = cppdiag.location().beginPos;
  75. diag->endPos = cppdiag.location().endPos;
  76. }
  77. void
  78. rbdriver_diag_report(ruby_parser::base_driver* driver, const struct cdiagnostic *diag)
  79. {
  80. driver->external_diagnostic(
  81. diag->level,
  82. diag->type,
  83. diag->beginPos,
  84. diag->endPos,
  85. diag->data ? std::string(diag->data) : ""
  86. );
  87. }