Advertisement
Guest User

Untitled

a guest
Apr 20th, 2024
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.97 KB | None | 0 0
  1. require "json"
  2. require "markdown"
  3. require "optparse"
  4.  
  5. def parse_type(rs_type)
  6.   if rs_type.has_key?("primitive")
  7.     return parse_primitive_type(rs_type["primitive"])
  8.   elsif rs_type.has_key?("generic")
  9.     return parse_generic_type(rs_type["generic"])
  10.   elsif rs_type.has_key?("tuple")
  11.     return parse_tuple_type(rs_type["tuple"])
  12.   elsif rs_type.has_key?("array")
  13.     return parse_array_type(rs_type["array"])
  14.   elsif rs_type.has_key?("dyn_trait")
  15.     return parse_array_type(rs_type["dyn_trait"])
  16.   elsif rs_type.has_key?("raw_pointer")
  17.     return parse_array_type(rs_type["raw_pointer"])
  18.   elsif rs_type.has_key?("slice")
  19.     return parse_slice_type(rs_type["slice"])
  20.   elsif rs_type.has_key?("synthetic")
  21.     return parse_synthetic_type(rs_type["synthetic"])
  22.   elsif rs_type.has_key?("borrowed_ref")
  23.     return parse_borrowed_ref_type(rs_type["borrowed_ref"])
  24.   elsif rs_type.has_key?("bounds")
  25.     return parse_bounds_type(rs_type["bounds"])
  26.   elsif rs_type.has_key?("resolved_path")
  27.     return parse_resolved_path_type(rs_type["resolved_path"])
  28.   elsif rs_type.has_key?("qualified_path")
  29.     return parse_qualified_path_type(rs_type["qualified_path"])
  30.   else
  31.     warn "Error parsing type: Unknown type kind"
  32.     exit 1
  33.   end
  34. end
  35.  
  36. def parse_bounds(bounds)
  37.   outlives_parsed = parse_trail_outlives(bouds.fetch('outlives', nil))
  38.   trait_parsed = parse_bound_trait(bounds.fetch('trait_bound', nil))
  39.  
  40.   return { :outlives => outlives_parsed, :trait => trait_parsed }
  41. end
  42.  
  43. def parse_function_decl(fn_decl)
  44.   params_parsed = parse_function_params(fn_decl['inputs'])
  45.   output_parsed = parse_type(fn_decl['output'])
  46.  
  47.   return {
  48.     :params => params_parsed,
  49.     :output => output_parsed,
  50.     :is_variadic? => fn_decl['c_variadic']
  51.   }
  52. end
  53.  
  54. def parse_generic_param(param)
  55.   param_name = param['name']
  56.   param_kind = parse_generic_param_kind(param['kind'])
  57.  
  58.   return { :name => param_name, :kind => param_kind }
  59. end
  60.  
  61. def parse_generics(fn_generics)
  62.   params_parsed = parse_generic_params(fn_generics['params'])
  63.   preds_parsed = parse_generic_predicates(fn_generics['where_predicates'])
  64.  
  65.   return { :params => params_parsed, :preds => preds_parsed }
  66. end
  67.  
  68. def parse_function_headers(headers)
  69.   return {
  70.            :abi => headers['abi'],
  71.            :asyn? => headers['async'],
  72.            :const? => headers['const'],
  73.            :unsafe? => headers['unsafe']
  74.          }
  75. end
  76.  
  77. def parse_function(function)
  78.   decl = parse_function_decl(function['decl'])
  79.   generics = parse_generics(function['generics'])
  80.   headers = parse_function_headers(function['headers'])
  81.  
  82.   return { :decl => decl, :generics => generics, :headers => headers, :has_body? => function['has_body'] }
  83. end
  84.  
  85.  
  86. def parse_assoc_const(assoc_const)
  87.   const_default_value = assoc_const["default"]
  88.   const_default_type = parse_type(assoc_const["type"])
  89.  
  90.   return { :default_value => const_default_value, :default_type => const_default_type }
  91. end
  92.  
  93. def parse_assoc_type(assoc_type)
  94.   type_generics
  95.  
  96.  
  97. def parse_constant(constant)
  98.   const_type = parse_type(constant['type'])
  99.   const_expr = constant['expr']
  100.   const_value = constant['value']
  101.  
  102.   return {
  103.     :type => const_type,
  104.     :expr => const_expr,
  105.     :value = >const_value,
  106.     :is_literal? => constant['is_literal']
  107.   }
  108. end
  109.  
  110. def parse_enum(enum)
  111.   parsed_generics = parse_generics(enum['generics'])
  112.   parsed_impls = seek_items(enum['impls'])
  113.   parsed_variants = seek_items(enum['variants'])
  114.  
  115.   return {
  116.     :generics => parsed_generics,
  117.     :impls => parsed_impls,
  118.     :variants => parsed_variants,
  119.     :variants_are_sripped? => enum['variants_stripped']
  120.   }
  121. end
  122.  
  123. def parse_impl(impl)
  124.   parsed_generics = parse_generics(impl.fetch('generics', nil))
  125.   parsed_for = parse_impl_for(impl.fetch('for', nil))
  126.   parsed_items = parse_impl_items(impl.fetch('items', nil))
  127.   parsed_methods = parse_impl_methods(impl.fetch('provided_trait_methods', nil))
  128.   parsed_trait = parse_traits(impl.fetch('trait', nil))
  129.  
  130.   return {
  131.     :generics => parsed_generics,
  132.     :for => parsed_for,
  133.     :items => parsed_items,
  134.     :methods => parsed_items,
  135.     :trait => parsed_traits,
  136.     :is_unsafe? => impl['is_unsafe'],
  137.     :negative? => impl['negative'],
  138.     :synthetic => impl['synthetic'],
  139.   }
  140. end
  141.  
  142. def parse_macro(macro)
  143.   return { :macro_lit => macro }
  144. end
  145.  
  146. def parse_module(mod)
  147.   items = seek_items(mod['itmes'])
  148.  
  149.   return {
  150.     :items => items,
  151.     :is_crate? => mod['is_crate'],
  152.     :is_stripped? => mod['is_stripped'],
  153.   }
  154. end
  155.  
  156. def parse_struct(struct)
  157.   fields_parsed = seek_items(struct['fields'])
  158.   generics_parsed = parse_generics(struct.fetch('generics', nil))
  159.   impls_parsed = seek_items(struct.fetch('impls', nil))
  160.  
  161.   return {
  162.     :fields => fields_parsed,
  163.     :generics => generics_parsed,
  164.     :impls => impls_parsed,
  165.   }
  166. end
  167.  
  168. def parse_trait(trait)
  169.   args_parsed = parse_trait_args(trait['args'])
  170.   bounds_parsed = parse_trait_bounds(trait['bounds'])
  171.   generics_parsed = parse_trait_generics(trait['generics'])
  172.   items_parsed = parse_trait_items(traint['items'])
  173. end
  174.  
  175. def make_manpages(obj)
  176.   obj.each do |key, value|
  177.     if value['inner'].has_key?('function')
  178.       parse_function_decl(value['inner']['function']['decl'])
  179.     end
  180.   end
  181. end
  182.  
  183.  
  184.  
  185.  
  186. def read_json_file(path)
  187.   return JSON.parse(File.read(path))
  188. end
  189.  
  190. def main()
  191.   options = {}
  192.  
  193.   OptionParser.new do |parser|
  194.     parser.banner = "Usage: mk_rustman.rb [options] JSON_FILE"
  195.  
  196.       parser.on("-s", "--structure", "Dump the structure of the JSON file") do |name|
  197.       options[:structure] = name
  198.     end
  199.  
  200.     parser.on("-h", "--help", "Show this help message") do
  201.       warn parser
  202.       exit 1
  203.     end
  204.   end.parse!
  205.  
  206.   if ARGV.empty?
  207.     warn "You must pass a JSON file"
  208.     exit 1
  209.   end
  210.  
  211.   json_file = ARGV[0]
  212.   object = read_json_file(json_file)
  213.  
  214.   if options[:structure]
  215.     print_structure_and_types(object)
  216.     exit
  217.   else
  218.     if object.has_key?("index")
  219.       make_manpages(object["index"])
  220.     end
  221.   end
  222. end
  223.  
  224.  
  225.  
  226.  
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement