periplus-map 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. periplus/__init__.py +70 -0
  2. periplus/checkmap.py +80 -0
  3. periplus/cli.py +658 -0
  4. periplus/contract/schema/map.schema.json +138 -0
  5. periplus/contract/schema/navigation.schema.json +200 -0
  6. periplus/contract/schema/pack-file.schema.json +1405 -0
  7. periplus/contract/schema/pack-manifest.schema.json +245 -0
  8. periplus/contract/schema/project-settings.schema.json +85 -0
  9. periplus/contract/spec/engine.md +151 -0
  10. periplus/contract/spec/rules.md +352 -0
  11. periplus/detect.py +228 -0
  12. periplus/engine/__init__.py +5 -0
  13. periplus/engine/check.py +481 -0
  14. periplus/engine/emit.py +368 -0
  15. periplus/engine/packload.py +2321 -0
  16. periplus/engine/pattern.py +27 -0
  17. periplus/engine/rules.py +1946 -0
  18. periplus/engine/select.py +491 -0
  19. periplus/engine/source.py +172 -0
  20. periplus/engine/tree.py +424 -0
  21. periplus/errors.py +332 -0
  22. periplus/init.py +358 -0
  23. periplus/layout.py +28 -0
  24. periplus/manifest.py +498 -0
  25. periplus/map.py +364 -0
  26. periplus/navigate/__init__.py +1 -0
  27. periplus/navigate/find.py +143 -0
  28. periplus/navigate/ids.py +30 -0
  29. periplus/navigate/listing.py +42 -0
  30. periplus/navigate/mapfile.py +189 -0
  31. periplus/navigate/path.py +175 -0
  32. periplus/navigate/show.py +175 -0
  33. periplus/navigate/types.py +83 -0
  34. periplus/navigate/walk.py +138 -0
  35. periplus/packs/advancedqueue_basic@0.0.1/pack.yaml +14 -0
  36. periplus/packs/advancedqueue_basic@0.0.1/queue_job_type/queue_job_type.yaml +9 -0
  37. periplus/packs/advancedqueue_basic@0.0.1/queue_job_type/queue_job_type_annotation.yaml +43 -0
  38. periplus/packs/advancedqueue_basic@0.0.1/queue_job_type/queue_job_type_attribute.yaml +43 -0
  39. periplus/packs/ai_basic@0.0.1/ai_agent/ai_agent.yaml +8 -0
  40. periplus/packs/ai_basic@0.0.1/ai_agent/ai_agent_attribute.yaml +43 -0
  41. periplus/packs/ai_basic@0.0.1/ai_function_call/ai_function_call.yaml +8 -0
  42. periplus/packs/ai_basic@0.0.1/ai_function_call/ai_function_call_attribute.yaml +43 -0
  43. periplus/packs/ai_basic@0.0.1/pack.yaml +15 -0
  44. periplus/packs/config_pages_basic@0.0.1/bundle/config_pages_bundle_config.yaml +54 -0
  45. periplus/packs/config_pages_basic@0.0.1/pack.yaml +11 -0
  46. periplus/packs/crop_basic@0.0.1/bundle/crop_bundle_config.yaml +54 -0
  47. periplus/packs/crop_basic@0.0.1/pack.yaml +11 -0
  48. periplus/packs/drupal@0.1.0/block/block_placement.yaml +25 -0
  49. periplus/packs/drupal@0.1.0/block/block_placement_code.yaml +58 -0
  50. periplus/packs/drupal@0.1.0/block/block_placement_config.yaml +53 -0
  51. periplus/packs/drupal@0.1.0/block/block_plugin.yaml +52 -0
  52. periplus/packs/drupal@0.1.0/block/block_plugin_altered.yaml +61 -0
  53. periplus/packs/drupal@0.1.0/block/block_plugin_annotation.yaml +69 -0
  54. periplus/packs/drupal@0.1.0/block/block_plugin_attribute.yaml +56 -0
  55. periplus/packs/drupal@0.1.0/block/block_plugin_content.yaml +46 -0
  56. periplus/packs/drupal@0.1.0/block/block_plugin_derived.yaml +110 -0
  57. periplus/packs/drupal@0.1.0/block/block_plugin_deriver.yaml +30 -0
  58. periplus/packs/drupal@0.1.0/block/block_type.yaml +14 -0
  59. periplus/packs/drupal@0.1.0/block/block_type_code.yaml +43 -0
  60. periplus/packs/drupal@0.1.0/block/block_type_config.yaml +45 -0
  61. periplus/packs/drupal@0.1.0/cache/cache.yaml +18 -0
  62. periplus/packs/drupal@0.1.0/cache/cache_backend.yaml +31 -0
  63. periplus/packs/drupal@0.1.0/component/sdc.yaml +19 -0
  64. periplus/packs/drupal@0.1.0/component/sdc_definition.yaml +25 -0
  65. periplus/packs/drupal@0.1.0/component/sdc_use.yaml +25 -0
  66. periplus/packs/drupal@0.1.0/config/config_object.yaml +65 -0
  67. periplus/packs/drupal@0.1.0/display/display.yaml +32 -0
  68. periplus/packs/drupal@0.1.0/display/display_code.yaml +53 -0
  69. periplus/packs/drupal@0.1.0/display/display_config.yaml +95 -0
  70. periplus/packs/drupal@0.1.0/entity/entity.yaml +33 -0
  71. periplus/packs/drupal@0.1.0/entity/entity_class.yaml +60 -0
  72. periplus/packs/drupal@0.1.0/entity/entity_save.yaml +27 -0
  73. periplus/packs/drupal@0.1.0/entity/entity_type_annotation.yaml +85 -0
  74. periplus/packs/drupal@0.1.0/entity/entity_type_attribute.yaml +76 -0
  75. periplus/packs/drupal@0.1.0/field/field_instance_code.yaml +40 -0
  76. periplus/packs/drupal@0.1.0/field/field_instance_config.yaml +58 -0
  77. periplus/packs/drupal@0.1.0/field/field_storage.yaml +37 -0
  78. periplus/packs/drupal@0.1.0/field/field_storage_code.yaml +39 -0
  79. periplus/packs/drupal@0.1.0/field/field_storage_config.yaml +24 -0
  80. periplus/packs/drupal@0.1.0/form/form.yaml +48 -0
  81. periplus/packs/drupal@0.1.0/form/form_class.yaml +50 -0
  82. periplus/packs/drupal@0.1.0/form/form_id.yaml +48 -0
  83. periplus/packs/drupal@0.1.0/form/form_methods.yaml +39 -0
  84. periplus/packs/drupal@0.1.0/hook/hook.yaml +8 -0
  85. periplus/packs/drupal@0.1.0/hook/hook_attribute.yaml +28 -0
  86. periplus/packs/drupal@0.1.0/module/module.yaml +8 -0
  87. periplus/packs/drupal@0.1.0/module/module_info.yaml +50 -0
  88. periplus/packs/drupal@0.1.0/pack.yaml +184 -0
  89. periplus/packs/drupal@0.1.0/plugin/plugin.yaml +17 -0
  90. periplus/packs/drupal@0.1.0/plugin/plugin_annotation.yaml +120 -0
  91. periplus/packs/drupal@0.1.0/recommendations/recommendations.yaml +14 -0
  92. periplus/packs/drupal@0.1.0/service/service.yaml +19 -0
  93. periplus/packs/drupal@0.1.0/service/service_autowire.yaml +28 -0
  94. periplus/packs/drupal@0.1.0/service/service_definition.yaml +24 -0
  95. periplus/packs/drupal@0.1.0/service/service_use.yaml +43 -0
  96. periplus/packs/drupal_basic@0.3.0/action/action.yaml +9 -0
  97. periplus/packs/drupal_basic@0.3.0/action/action_annotation.yaml +43 -0
  98. periplus/packs/drupal_basic@0.3.0/action/action_attribute.yaml +43 -0
  99. periplus/packs/drupal_basic@0.3.0/block/block_placement.yaml +16 -0
  100. periplus/packs/drupal_basic@0.3.0/block/block_placement_config.yaml +51 -0
  101. periplus/packs/drupal_basic@0.3.0/block_plugin/block_plugin.yaml +14 -0
  102. periplus/packs/drupal_basic@0.3.0/block_plugin/block_plugin_annotation.yaml +47 -0
  103. periplus/packs/drupal_basic@0.3.0/block_plugin/block_plugin_attribute.yaml +43 -0
  104. periplus/packs/drupal_basic@0.3.0/block_type/block_type.yaml +8 -0
  105. periplus/packs/drupal_basic@0.3.0/block_type/block_type_config.yaml +54 -0
  106. periplus/packs/drupal_basic@0.3.0/boundary/boundary_types.yaml +19 -0
  107. periplus/packs/drupal_basic@0.3.0/bundle/bundle.yaml +20 -0
  108. periplus/packs/drupal_basic@0.3.0/bundle/bundle_config.yaml +393 -0
  109. periplus/packs/drupal_basic@0.3.0/call/call.yaml +349 -0
  110. periplus/packs/drupal_basic@0.3.0/component/component.yaml +30 -0
  111. periplus/packs/drupal_basic@0.3.0/config/config_object.yaml +119 -0
  112. periplus/packs/drupal_basic@0.3.0/constraint/constraint.yaml +8 -0
  113. periplus/packs/drupal_basic@0.3.0/constraint/constraint_annotation.yaml +43 -0
  114. periplus/packs/drupal_basic@0.3.0/constraint/constraint_attribute.yaml +43 -0
  115. periplus/packs/drupal_basic@0.3.0/display/display.yaml +30 -0
  116. periplus/packs/drupal_basic@0.3.0/display/display_config.yaml +203 -0
  117. periplus/packs/drupal_basic@0.3.0/display/display_mode_config.yaml +60 -0
  118. periplus/packs/drupal_basic@0.3.0/entity_type/config_entity_type_annotation.yaml +46 -0
  119. periplus/packs/drupal_basic@0.3.0/entity_type/config_entity_type_attribute.yaml +43 -0
  120. periplus/packs/drupal_basic@0.3.0/entity_type/content_entity_type_annotation.yaml +46 -0
  121. periplus/packs/drupal_basic@0.3.0/entity_type/content_entity_type_attribute.yaml +43 -0
  122. periplus/packs/drupal_basic@0.3.0/entity_type/entity_type.yaml +16 -0
  123. periplus/packs/drupal_basic@0.3.0/field/field_instance_config.yaml +116 -0
  124. periplus/packs/drupal_basic@0.3.0/field/field_storage.yaml +21 -0
  125. periplus/packs/drupal_basic@0.3.0/field/field_storage_config.yaml +45 -0
  126. periplus/packs/drupal_basic@0.3.0/field_formatter/field_formatter.yaml +8 -0
  127. periplus/packs/drupal_basic@0.3.0/field_formatter/field_formatter_annotation.yaml +46 -0
  128. periplus/packs/drupal_basic@0.3.0/field_formatter/field_formatter_attribute.yaml +43 -0
  129. periplus/packs/drupal_basic@0.3.0/field_type/field_type_annotation.yaml +46 -0
  130. periplus/packs/drupal_basic@0.3.0/field_type/field_type_attribute.yaml +50 -0
  131. periplus/packs/drupal_basic@0.3.0/field_widget/field_widget_annotation.yaml +46 -0
  132. periplus/packs/drupal_basic@0.3.0/field_widget/field_widget_attribute.yaml +50 -0
  133. periplus/packs/drupal_basic@0.3.0/form/form_id.yaml +80 -0
  134. periplus/packs/drupal_basic@0.3.0/hook/hook.yaml +100 -0
  135. periplus/packs/drupal_basic@0.3.0/hook/hook_attribute.yaml +64 -0
  136. periplus/packs/drupal_basic@0.3.0/hook/hook_theme_include.yaml +34 -0
  137. periplus/packs/drupal_basic@0.3.0/library/library.yaml +12 -0
  138. periplus/packs/drupal_basic@0.3.0/library/library_definition.yaml +58 -0
  139. periplus/packs/drupal_basic@0.3.0/local_action/local_action.yaml +12 -0
  140. periplus/packs/drupal_basic@0.3.0/local_action/local_action_definition.yaml +49 -0
  141. periplus/packs/drupal_basic@0.3.0/mail/mail.yaml +8 -0
  142. periplus/packs/drupal_basic@0.3.0/mail/mail_attribute.yaml +43 -0
  143. periplus/packs/drupal_basic@0.3.0/media_source/media_source.yaml +8 -0
  144. periplus/packs/drupal_basic@0.3.0/media_source/media_source_annotation.yaml +43 -0
  145. periplus/packs/drupal_basic@0.3.0/media_source/media_source_attribute.yaml +79 -0
  146. periplus/packs/drupal_basic@0.3.0/menu_link/menu_link.yaml +18 -0
  147. periplus/packs/drupal_basic@0.3.0/menu_link/menu_link_definition.yaml +48 -0
  148. periplus/packs/drupal_basic@0.3.0/module/module.yaml +7 -0
  149. periplus/packs/drupal_basic@0.3.0/module/module_info.yaml +58 -0
  150. periplus/packs/drupal_basic@0.3.0/pack.yaml +528 -0
  151. periplus/packs/drupal_basic@0.3.0/permission/permission.yaml +7 -0
  152. periplus/packs/drupal_basic@0.3.0/permission/permission_definition.yaml +27 -0
  153. periplus/packs/drupal_basic@0.3.0/queue_worker/queue_worker.yaml +9 -0
  154. periplus/packs/drupal_basic@0.3.0/queue_worker/queue_worker_annotation.yaml +43 -0
  155. periplus/packs/drupal_basic@0.3.0/queue_worker/queue_worker_attribute.yaml +43 -0
  156. periplus/packs/drupal_basic@0.3.0/rest_resource/rest_resource.yaml +8 -0
  157. periplus/packs/drupal_basic@0.3.0/rest_resource/rest_resource_annotation.yaml +43 -0
  158. periplus/packs/drupal_basic@0.3.0/rest_resource/rest_resource_attribute.yaml +43 -0
  159. periplus/packs/drupal_basic@0.3.0/role/role.yaml +14 -0
  160. periplus/packs/drupal_basic@0.3.0/role/role_config.yaml +44 -0
  161. periplus/packs/drupal_basic@0.3.0/route/route.yaml +42 -0
  162. periplus/packs/drupal_basic@0.3.0/route/route_definition.yaml +113 -0
  163. periplus/packs/drupal_basic@0.3.0/route/route_options.yaml +55 -0
  164. periplus/packs/drupal_basic@0.3.0/route/route_requirements.yaml +108 -0
  165. periplus/packs/drupal_basic@0.3.0/service/service.yaml +56 -0
  166. periplus/packs/drupal_basic@0.3.0/service/service_definition.yaml +181 -0
  167. periplus/packs/drupal_basic@0.3.0/template/template.yaml +86 -0
  168. periplus/packs/drupal_basic@0.3.0/template/template_route_link.yaml +30 -0
  169. periplus/packs/drupal_basic@0.3.0/theme/theme.yaml +15 -0
  170. periplus/packs/drupal_basic@0.3.0/theme/theme_info.yaml +61 -0
  171. periplus/packs/drupal_basic@0.3.0/theme_hook/template_suggestion.yaml +29 -0
  172. periplus/packs/drupal_basic@0.3.0/theme_hook/theme_hook.yaml +87 -0
  173. periplus/packs/drupal_basic@0.3.0/theme_hook/theme_hook_render.yaml +20 -0
  174. periplus/packs/drupal_basic@0.3.0/url/url.yaml +88 -0
  175. periplus/packs/drupal_basic@0.3.0/views_plugin/views_field_attribute.yaml +80 -0
  176. periplus/packs/drupal_basic@0.3.0/views_plugin/views_filter_attribute.yaml +80 -0
  177. periplus/packs/drupal_basic@0.3.0/views_plugin/views_plugin.yaml +15 -0
  178. periplus/packs/drupal_basic@0.3.0/views_plugin/views_style_attribute.yaml +80 -0
  179. periplus/packs/drupal_js_basic@0.0.1/attachment/attachment.yaml +12 -0
  180. periplus/packs/drupal_js_basic@0.0.1/attachment/attachment_in_php.yaml +15 -0
  181. periplus/packs/drupal_js_basic@0.0.1/attachment/attachment_in_php_array.yaml +52 -0
  182. periplus/packs/drupal_js_basic@0.0.1/attachment/attachment_in_twig.yaml +26 -0
  183. periplus/packs/drupal_js_basic@0.0.1/attachment/php_function_holder.yaml +13 -0
  184. periplus/packs/drupal_js_basic@0.0.1/behavior/behavior.yaml +198 -0
  185. periplus/packs/drupal_js_basic@0.0.1/pack.yaml +153 -0
  186. periplus/packs/drupal_js_basic@0.0.1/setting/setting.yaml +13 -0
  187. periplus/packs/drupal_js_basic@0.0.1/setting/setting_in_js.yaml +15 -0
  188. periplus/packs/drupal_js_basic@0.0.1/setting/setting_in_php.yaml +92 -0
  189. periplus/packs/drush_basic@0.0.1/command/command.yaml +15 -0
  190. periplus/packs/drush_basic@0.0.1/command/command_annotation.yaml +60 -0
  191. periplus/packs/drush_basic@0.0.1/command/command_attribute.yaml +88 -0
  192. periplus/packs/drush_basic@0.0.1/pack.yaml +29 -0
  193. periplus/packs/eck_basic@0.0.1/bundle/eck_bundle_config.yaml +56 -0
  194. periplus/packs/eck_basic@0.0.1/entity_type/eck_entity_type_config.yaml +26 -0
  195. periplus/packs/eck_basic@0.0.1/pack.yaml +28 -0
  196. periplus/packs/go@0.0.2/interfaces/satisfies.yaml +47 -0
  197. periplus/packs/go@0.0.2/pack.yaml +51 -0
  198. periplus/packs/go@0.0.2/types/find_declarations.yaml +137 -0
  199. periplus/packs/go@0.0.2/types/node_types.yaml +36 -0
  200. periplus/packs/go_basic@0.0.3/pack.yaml +104 -0
  201. periplus/packs/go_basic@0.0.3/rules/calls.yaml +25 -0
  202. periplus/packs/go_basic@0.0.3/rules/declarations.yaml +40 -0
  203. periplus/packs/go_basic@0.0.3/rules/imports.yaml +16 -0
  204. periplus/packs/go_basic@0.0.3/types/node_types.yaml +15 -0
  205. periplus/packs/js_basic@0.0.1/function/function.yaml +68 -0
  206. periplus/packs/js_basic@0.0.1/pack.yaml +50 -0
  207. periplus/packs/laravel_basic@0.0.5/pack.yaml +313 -0
  208. periplus/packs/laravel_basic@0.0.5/rules/classes.yaml +42 -0
  209. periplus/packs/laravel_basic@0.0.5/rules/config.yaml +229 -0
  210. periplus/packs/laravel_basic@0.0.5/rules/routes.yaml +210 -0
  211. periplus/packs/laravel_basic@0.0.5/rules/types.yaml +50 -0
  212. periplus/packs/laravel_basic@0.0.5/rules/views.yaml +100 -0
  213. periplus/packs/paragraphs_basic@0.0.1/bundle/paragraph_bundle_config.yaml +54 -0
  214. periplus/packs/paragraphs_basic@0.0.1/pack.yaml +12 -0
  215. periplus/packs/php@0.1.0/edges/edge_kinds.yaml +64 -0
  216. periplus/packs/php@0.1.0/edges/find_calls.yaml +34 -0
  217. periplus/packs/php@0.1.0/edges/find_imports.yaml +18 -0
  218. periplus/packs/php@0.1.0/edges/find_member_access.yaml +22 -0
  219. periplus/packs/php@0.1.0/edges/find_structure.yaml +38 -0
  220. periplus/packs/php@0.1.0/embedded/embedded_regions.yaml +19 -0
  221. periplus/packs/php@0.1.0/imports/imports.yaml +20 -0
  222. periplus/packs/php@0.1.0/pack.yaml +117 -0
  223. periplus/packs/php@0.1.0/resolution/excluded_tokens.yaml +23 -0
  224. periplus/packs/php@0.1.0/resolution/resolution.yaml +35 -0
  225. periplus/packs/php@0.1.0/types/find_declarations.yaml +102 -0
  226. periplus/packs/php@0.1.0/types/node_types.yaml +55 -0
  227. periplus/packs/php_basic@0.2.0/edges/edge_kinds.yaml +48 -0
  228. periplus/packs/php_basic@0.2.0/edges/find_structure.yaml +750 -0
  229. periplus/packs/php_basic@0.2.0/imports/imports.yaml +19 -0
  230. periplus/packs/php_basic@0.2.0/pack.yaml +94 -0
  231. periplus/packs/php_basic@0.2.0/types/find_declarations.yaml +267 -0
  232. periplus/packs/php_basic@0.2.0/types/node_types.yaml +55 -0
  233. periplus/packs/profile_basic@0.0.1/bundle/profile_bundle_config.yaml +54 -0
  234. periplus/packs/profile_basic@0.0.1/pack.yaml +12 -0
  235. periplus/packs/salesforce_basic@0.0.1/event/salesforce_event.yaml +36 -0
  236. periplus/packs/salesforce_basic@0.0.1/pack.yaml +31 -0
  237. periplus/packs/salesforce_basic@0.0.1/salesforce_mapping/salesforce_mapping.yaml +19 -0
  238. periplus/packs/salesforce_basic@0.0.1/salesforce_mapping/salesforce_mapping_config.yaml +83 -0
  239. periplus/packs/salesforce_basic@0.0.1/salesforce_mapping_field/salesforce_mapping_field.yaml +8 -0
  240. periplus/packs/salesforce_basic@0.0.1/salesforce_mapping_field/salesforce_mapping_field_annotation.yaml +43 -0
  241. periplus/packs/twig@0.0.1/pack.yaml +26 -0
  242. periplus/packs/twig@0.0.1/types/template.yaml +57 -0
  243. periplus/packs/twig_basic@0.1.0/pack.yaml +11 -0
  244. periplus/packs/twig_tweak_basic@0.0.1/pack.yaml +27 -0
  245. periplus/packs/twig_tweak_basic@0.0.1/rules/block.yaml +20 -0
  246. periplus/packs/twig_tweak_basic@0.0.1/rules/entity.yaml +35 -0
  247. periplus/packs/twig_tweak_basic@0.0.1/rules/field.yaml +21 -0
  248. periplus/packs/twig_tweak_basic@0.0.1/rules/menu.yaml +21 -0
  249. periplus/packs/webform_basic@0.0.1/bundle/webform_submission_bundle_config.yaml +54 -0
  250. periplus/packs/webform_basic@0.0.1/pack.yaml +20 -0
  251. periplus/packs/webform_basic@0.0.1/webform_handler/webform_handler.yaml +8 -0
  252. periplus/packs/webform_basic@0.0.1/webform_handler/webform_handler_annotation.yaml +43 -0
  253. periplus/packs/yaml@0.0.1/pack.yaml +72 -0
  254. periplus/packs/yaml@0.0.1/types/find_mapping_entry.yaml +21 -0
  255. periplus/packs/yaml@0.0.1/types/yaml_types.yaml +44 -0
  256. periplus/packs/yaml_basic@0.1.0/pack.yaml +10 -0
  257. periplus/packs.py +447 -0
  258. periplus/preflight.py +102 -0
  259. periplus/py.typed +0 -0
  260. periplus/report.py +596 -0
  261. periplus/resolution.py +355 -0
  262. periplus/settings.py +617 -0
  263. periplus/settings.stub.yml +99 -0
  264. periplus/spec.py +774 -0
  265. periplus/update.py +197 -0
  266. periplus/validate.py +655 -0
  267. periplus_map-0.2.1.dist-info/METADATA +566 -0
  268. periplus_map-0.2.1.dist-info/RECORD +272 -0
  269. periplus_map-0.2.1.dist-info/WHEEL +4 -0
  270. periplus_map-0.2.1.dist-info/entry_points.txt +2 -0
  271. periplus_map-0.2.1.dist-info/licenses/LICENSE +202 -0
  272. periplus_map-0.2.1.dist-info/licenses/NOTICE +2 -0
@@ -0,0 +1,424 @@
1
+ """Parse a source file with the grammar its pack pins, and read full names from the tree.
2
+
3
+ The engine knows no language. The grammar's distribution, module and function names are built
4
+ from the ``language`` value of a pack's ``grammar`` block, and every tree node type read here comes
5
+ from a pack.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import importlib
11
+ import re
12
+ from collections.abc import Mapping, Sequence
13
+ from dataclasses import dataclass, field, replace
14
+ from functools import cache
15
+ from importlib.metadata import PackageNotFoundError, version
16
+ from pathlib import Path
17
+
18
+ from tree_sitter import Language, Node, Parser
19
+
20
+ from periplus.engine.pattern import reshape
21
+ from periplus.errors import ExitCode, Problem
22
+ from periplus.manifest import MANIFEST_FILENAME, LoadedPack
23
+
24
+ __all__ = [
25
+ "Grammar",
26
+ "ImportRule",
27
+ "STEPS",
28
+ "ParsedFile",
29
+ "Step",
30
+ "argument_of",
31
+ "grammar_problems",
32
+ "line_of",
33
+ "literal_of",
34
+ "parse_file",
35
+ "resolve_name",
36
+ "text_of",
37
+ ]
38
+
39
+ #: A resolution step as a rutter declares it: its kind, and its argument or ``""``.
40
+ Step = tuple[str, str]
41
+
42
+ #: Each step kind, and whether it takes an argument.
43
+ STEPS = {
44
+ "full_if_prefixed": True,
45
+ "bound_first_segment": False,
46
+ "prefix_with": True,
47
+ "prefix_bare_with": True,
48
+ "lookup_last_segment_in": True,
49
+ "as_written": False,
50
+ }
51
+
52
+
53
+ def resolve_name(
54
+ written: str,
55
+ steps: Sequence[Step],
56
+ separator: str,
57
+ bindings: Mapping[str, str],
58
+ values: Mapping[str, str],
59
+ *,
60
+ tables: Mapping[str, Mapping[str, str]],
61
+ ) -> str | None:
62
+ """The name the first step that holds gives; ``None`` when none holds.
63
+
64
+ ``full_if_prefixed`` holds when the name starts with its text and gives the rest.
65
+ ``bound_first_segment`` holds when ``bindings`` holds the name's first segment and gives the
66
+ bound target joined with the remaining segments. ``prefix_with`` holds when ``values`` holds
67
+ the value it names and gives that value joined with the name, an empty part left out.
68
+ ``prefix_bare_with`` does the same for a name that does not hold the separator.
69
+ ``lookup_last_segment_in`` holds when the name's last segment is a key of the table of
70
+ ``tables`` it names and gives that key's value alone. ``as_written`` always holds.
71
+ """
72
+ for kind, argument in steps:
73
+ if kind == "full_if_prefixed" and written.startswith(argument):
74
+ return written.removeprefix(argument)
75
+ if kind == "bound_first_segment":
76
+ first, _, rest = written.partition(separator) if separator else (written, "", "")
77
+ if first in bindings:
78
+ return separator.join(part for part in (bindings[first], rest) if part)
79
+ if kind == "lookup_last_segment_in":
80
+ segment = written.split(separator)[-1] if separator else written
81
+ if segment in tables.get(argument, {}):
82
+ return tables[argument][segment]
83
+ bare = kind == "prefix_bare_with" and not (separator and separator in written)
84
+ if (kind == "prefix_with" or bare) and argument in values:
85
+ return separator.join(part for part in (values[argument], written) if part)
86
+ if kind == "as_written":
87
+ return written
88
+ return None
89
+
90
+
91
+ @dataclass(frozen=True, slots=True)
92
+ class ImportRule:
93
+ """One ``imports`` entry of a pack file.
94
+
95
+ ``declaration`` and ``group`` are the tree node types of an import and of its grouped form,
96
+ and ``clause`` the node that binds one name. ``facts`` pairs a fact's name with the tree field
97
+ it names, read on the clause or else on the declaration. The rest is the entry's ``bind``:
98
+ ``names`` are the sources of the bound name, tried in order, each a fact, ``last_segment`` or
99
+ ``path``; ``normalize`` reshapes the name; a fact whose text is in ``unbound_aliases`` binds
100
+ nothing; ``prefix_from_group`` joins a clause of the grouped form to the declaration's written
101
+ prefix; ``strip_prefix`` is stripped from the path's front; and a declaration or clause where
102
+ the field of the fact ``skip_when`` is set binds nothing.
103
+ """
104
+
105
+ declaration: str
106
+ group: str | None
107
+ clause: str
108
+ facts: tuple[tuple[str, str], ...]
109
+ names: tuple[str, ...] = ()
110
+ prefix_from_group: bool = False
111
+ strip_prefix: str = ""
112
+ skip_when: str = ""
113
+ normalize: tuple[tuple[re.Pattern[str], str], ...] = ()
114
+ unbound_aliases: frozenset[str] = frozenset()
115
+
116
+
117
+ @dataclass(frozen=True, slots=True)
118
+ class Grammar:
119
+ """What the pack that owns a file type says about its tree and its names.
120
+
121
+ ``parts`` and ``separator`` build a full name, and ``resolve`` holds the steps that turn a
122
+ written name into a full one. ``namespace`` is the node type that declares a
123
+ namespace and ``namespace_name`` the field holding its name; ``namespace_value`` names instead
124
+ the value of the file's table that is its one namespace. ``written`` holds the node types
125
+ of a name written in source.
126
+
127
+ ``attribute`` is the node type of an attribute and ``attribute_list`` the node that holds it on
128
+ a declaration; ``arguments`` is the field of its arguments, ``argument`` the node type of one,
129
+ and ``argument_name`` the field of a named argument's name. ``literals`` are the node types of
130
+ a text literal and ``literal_content`` the node type of its text. Each is empty when the pack
131
+ declares no attribute. ``comment`` is the node type of a comment, empty when none is declared.
132
+ ``call_arguments`` is the field of a call's arguments. ``call_argument`` is the node type of
133
+ one, and each named child that is not an extra is one when it is empty; ``call_argument_name``
134
+ is the field of a named argument's name. ``call_literals`` are the node types of a call's text
135
+ literal, ``call_literal_content`` those of its text, and ``call_literal_delimiters`` those of
136
+ its children that are skipped.
137
+
138
+ ``constant_access`` is the node type of a class constant access, ``constant_scope`` the types
139
+ its first named child, the scope, may have, and ``constant_name`` those of its last, the name.
140
+ ``constant_enclosing`` holds the scope texts that stand for the enclosing class; any other
141
+ scope that is not a written name stands for no class, and the argument for no text.
142
+ ``constant_declaration`` is the node type of one class constant declaration, and
143
+ ``constant_declared_name`` and ``constant_value`` the types of its first and last named child.
144
+ """
145
+
146
+ language: str
147
+ parts: tuple[str, ...]
148
+ separator: str
149
+ namespace: str
150
+ namespace_name: str
151
+ written: frozenset[str]
152
+ imports: tuple[ImportRule, ...]
153
+ attribute: str = ""
154
+ attribute_list: str = ""
155
+ arguments: str = ""
156
+ argument: str = ""
157
+ argument_name: str = ""
158
+ literals: frozenset[str] = frozenset()
159
+ literal_content: str = ""
160
+ comment: str = ""
161
+ resolve: tuple[Step, ...] = ()
162
+ namespace_value: str = ""
163
+ call_arguments: str = ""
164
+ call_argument: str = ""
165
+ call_argument_name: str = ""
166
+ call_literals: frozenset[str] = frozenset()
167
+ call_literal_content: frozenset[str] = frozenset()
168
+ call_literal_delimiters: frozenset[str] = frozenset()
169
+ constant_access: str = ""
170
+ constant_scope: frozenset[str] = frozenset()
171
+ constant_name: frozenset[str] = frozenset()
172
+ constant_enclosing: frozenset[str] = frozenset()
173
+ constant_declaration: str = ""
174
+ constant_declared_name: frozenset[str] = frozenset()
175
+ constant_value: frozenset[str] = frozenset()
176
+
177
+ def join(self, values: Mapping[str, str]) -> str | None:
178
+ """The full name: each part's value that is not empty, joined by the separator; ``None``
179
+ when a part is absent."""
180
+ if any(part not in values for part in self.parts):
181
+ return None
182
+ return self.separator.join(values[part] for part in self.parts if values[part])
183
+
184
+
185
+ @dataclass(frozen=True, slots=True)
186
+ class ParsedFile:
187
+ """One parsed file: whether its tree holds an error, its nodes by type, and its names."""
188
+
189
+ broken: bool
190
+ by_type: Mapping[str, tuple[Node, ...]]
191
+ namespaces: tuple[tuple[int, str], ...]
192
+ #: The import table of each namespace block, keyed by the block's index in ``namespaces``;
193
+ #: ``-1`` is the code before the first namespace declaration.
194
+ imports: Mapping[int, Mapping[str, str]]
195
+ #: The file's value table: its path values and the project's settings values.
196
+ values: Mapping[str, str] = field(default_factory=dict)
197
+ #: The run's class constants with a text value, by the full name of their class and their
198
+ #: name; one mapping shared by every file the run parses.
199
+ constants: Mapping[tuple[str, str], str] = field(default_factory=dict)
200
+
201
+ def namespace_at(self, node: Node) -> str:
202
+ """The name of the last namespace declaration that starts before the node, or ``""``."""
203
+ block = self.block_at(node.start_byte)
204
+ return self.namespaces[block][1] if block >= 0 else ""
205
+
206
+ def block_at(self, start: int) -> int:
207
+ """The index of the last namespace declaration that starts before the byte, or ``-1``."""
208
+ found = -1
209
+ for index, (begins, _) in enumerate(self.namespaces):
210
+ if begins >= start:
211
+ break
212
+ found = index
213
+ return found
214
+
215
+ def resolve(self, written: Node, grammar: Grammar) -> str | None:
216
+ """The full name a written name stands for, by the grammar's resolution steps; ``None``
217
+ when no step holds, and the name as written when the grammar declares none.
218
+
219
+ The bindings are the imports of the name's namespace block, and the value ``namespace``
220
+ is the namespace in force.
221
+ """
222
+ return resolve_name(
223
+ text_of(written),
224
+ grammar.resolve or (("as_written", ""),),
225
+ grammar.separator,
226
+ self.imports.get(self.block_at(written.start_byte), {}),
227
+ {"namespace": self.namespace_at(written)},
228
+ tables={},
229
+ )
230
+
231
+
232
+ def text_of(node: Node) -> str:
233
+ """The node's source text."""
234
+ return (node.text or b"").decode("utf-8", "replace")
235
+
236
+
237
+ def argument_of(attribute: Node, key: str, grammar: Grammar, call: bool = False) -> Node | None:
238
+ """The value of the attribute's argument named ``key``, or at position ``key`` when it is a
239
+ whole number; ``None`` when the attribute has no such argument. ``call`` reads a call's
240
+ arguments by the grammar's call keys. A wrapped or named argument's value is its last named
241
+ child."""
242
+ field, wrapper, named = (
243
+ (grammar.call_arguments, grammar.call_argument, grammar.call_argument_name)
244
+ if call
245
+ else (grammar.arguments, grammar.argument, grammar.argument_name)
246
+ )
247
+ holder = attribute.child_by_field_name(field)
248
+ found = [
249
+ c
250
+ for c in (holder.named_children if holder else ())
251
+ if (c.type == wrapper if wrapper else not c.is_extra)
252
+ ]
253
+ for position, argument in enumerate(found):
254
+ name = argument.child_by_field_name(named)
255
+ if text_of(name) == key if name is not None else key == str(position):
256
+ return argument.named_children[-1] if wrapper or name is not None else argument
257
+ return None
258
+
259
+
260
+ def literal_of(node: Node, grammar: Grammar, call: bool = False) -> str | None:
261
+ """The text of a text literal; ``None`` for any other node, or a literal holding more.
262
+ ``call`` reads it by the grammar's call keys."""
263
+ literals, content, delimiters = (
264
+ (grammar.call_literals, grammar.call_literal_content, grammar.call_literal_delimiters)
265
+ if call
266
+ else (grammar.literals, frozenset({grammar.literal_content}), frozenset())
267
+ )
268
+ parts = [p for p in node.named_children if p.type not in delimiters]
269
+ if node.type not in literals or any(p.type not in content for p in parts):
270
+ return None
271
+ return "".join(text_of(part) for part in parts)
272
+
273
+
274
+ def line_of(node: Node) -> int:
275
+ """The node's 1-based start line."""
276
+ # Indexed, not ``.row``: tree-sitter 0.26.0's ``Point.row`` drops a reference to the row on
277
+ # each read, which frees a row above 256 while it is still in use and crashes the process.
278
+ return node.start_point[0] + 1
279
+
280
+
281
+ def parse_file(
282
+ path: Path,
283
+ grammar: Grammar,
284
+ values: Mapping[str, str],
285
+ constants: Mapping[tuple[str, str], str],
286
+ ) -> ParsedFile:
287
+ """The file parsed with the grammar, indexed by node type, with its namespaces, imports and
288
+ value table, and the run's constants. With ``namespace_value``, the value of that name is one
289
+ namespace over the whole file, and the file has none when the value is absent."""
290
+ tree = _parser(grammar.language).parse(path.read_bytes())
291
+ by_type: dict[str, list[Node]] = {}
292
+ pending = [tree.root_node]
293
+ while pending:
294
+ node = pending.pop()
295
+ by_type.setdefault(node.type, []).append(node)
296
+ pending.extend(reversed(node.children))
297
+ namespaces = tuple(
298
+ (node.start_byte, text_of(name) if name is not None else "")
299
+ for node in by_type.get(grammar.namespace, [])
300
+ for name in [node.child_by_field_name(grammar.namespace_name)]
301
+ )
302
+ if grammar.namespace_value:
303
+ found = values.get(grammar.namespace_value)
304
+ namespaces = ((-1, found),) if found is not None else ()
305
+ parsed = ParsedFile(
306
+ broken=tree.root_node.has_error,
307
+ by_type={name: tuple(nodes) for name, nodes in by_type.items()},
308
+ namespaces=namespaces,
309
+ imports={},
310
+ values=values,
311
+ constants=constants,
312
+ )
313
+ imports: dict[int, dict[str, str]] = {}
314
+ for rule in grammar.imports:
315
+ for declaration in by_type.get(rule.declaration, []):
316
+ block = parsed.block_at(declaration.start_byte)
317
+ imports.setdefault(block, {}).update(_bound(declaration, rule, grammar))
318
+ return replace(parsed, imports=imports)
319
+
320
+
321
+ def _bound(declaration: Node, rule: ImportRule, grammar: Grammar) -> dict[str, str]:
322
+ """Each name one import declaration binds, to the path it is written with, as the rule's
323
+ ``bind`` says; a clause no name source gives a name binds nothing."""
324
+ facts = dict(rule.facts)
325
+ skip = facts.get(rule.skip_when, "")
326
+ if skip and declaration.child_by_field_name(skip) is not None:
327
+ return {}
328
+ groups = [child for child in declaration.children if child.type == rule.group]
329
+ prefix = next((text_of(c) for c in declaration.children if c.type in grammar.written), "")
330
+ clauses = [
331
+ clause
332
+ for holder in groups or [declaration]
333
+ for clause in holder.children
334
+ if clause.type == rule.clause
335
+ ]
336
+ bound: dict[str, str] = {}
337
+ for clause in clauses:
338
+ if skip and clause.child_by_field_name(skip) is not None:
339
+ continue
340
+ named = {
341
+ s: clause.child_by_field_name(facts[s]) or declaration.child_by_field_name(facts[s])
342
+ for s in rule.names
343
+ if s in facts
344
+ }
345
+ path = next(
346
+ (
347
+ text_of(c)
348
+ for c in clause.children
349
+ if c.type in grammar.written and c not in named.values()
350
+ ),
351
+ None,
352
+ )
353
+ if path is None:
354
+ continue
355
+ if groups and rule.prefix_from_group:
356
+ path = grammar.separator.join((prefix, path))
357
+ path = path.removeprefix(rule.strip_prefix)
358
+ name = None
359
+ for source in rule.names:
360
+ node = named.get(source)
361
+ if node is not None:
362
+ name = None if text_of(node) in rule.unbound_aliases else text_of(node)
363
+ break
364
+ if source in ("last_segment", "path"):
365
+ name = path if source == "path" else path.rsplit(grammar.separator, 1)[-1]
366
+ break
367
+ if name is not None:
368
+ bound[reshape(name, rule.normalize)] = path
369
+ return bound
370
+
371
+
372
+ @cache
373
+ def _parser(language: str) -> Parser:
374
+ """A parser for the language: module ``tree_sitter_<language>``, its ``language_<language>``
375
+ function, or ``language`` when it has no such function."""
376
+ module = importlib.import_module(f"tree_sitter_{language}")
377
+ function = getattr(module, f"language_{language}", None) or module.language
378
+ return Parser(Language(function()))
379
+
380
+
381
+ def grammar_problems(packs: Sequence[LoadedPack]) -> list[Problem]:
382
+ """Each pin of a loaded pack's grammar that the installed distributions do not meet, and each
383
+ pinned grammar that is installed at its version and cannot be imported."""
384
+ problems: list[Problem] = []
385
+ for pack in packs:
386
+ grammar = pack.manifest.document.get("grammar")
387
+ if not isinstance(grammar, Mapping):
388
+ continue
389
+ runtime = str(grammar["provider"])
390
+ for distribution, pinned in (
391
+ (runtime, str(grammar["provider_version"])),
392
+ (f"{runtime}-{grammar['language']}", str(grammar["grammar_version"])),
393
+ ):
394
+ try:
395
+ found = version(distribution)
396
+ except PackageNotFoundError:
397
+ found = "none"
398
+ if found == pinned and distribution != runtime:
399
+ try:
400
+ _parser(str(grammar["language"]))
401
+ except Exception as error: # noqa: BLE001 - any failure of a grammar's import
402
+ problems.append(
403
+ Problem(
404
+ ExitCode.GRAMMAR_MISMATCH,
405
+ f"{pack.name.pack}/{MANIFEST_FILENAME} pins the grammar "
406
+ f"{distribution} {pinned}, and it cannot be imported: {error}",
407
+ {"pack": pack.name.pack, "distribution": distribution},
408
+ )
409
+ )
410
+ elif found != pinned:
411
+ problems.append(
412
+ Problem(
413
+ ExitCode.GRAMMAR_MISMATCH,
414
+ f"{pack.name.pack}/{MANIFEST_FILENAME} pins {distribution} {pinned}, "
415
+ f"and the version found is {found}",
416
+ {
417
+ "pack": pack.name.pack,
418
+ "distribution": distribution,
419
+ "pinned": pinned,
420
+ "found": found,
421
+ },
422
+ )
423
+ )
424
+ return problems