A semantic search engine for source code https://bitshift.benkurtovic.com/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

127 lignes
3.3 KiB

  1. require 'socket'
  2. require 'ruby_parser'
  3. require 'sexp_processor'
  4. module Bitshift
  5. class Parser
  6. def initialize(source)
  7. @source = source
  8. end
  9. def parse
  10. parser = RubyParser.new
  11. tree = parser.parse(@source)
  12. offset = tree.line - 1
  13. processor = CachedWalker.new offset, tree
  14. processor.process(tree)
  15. return processor.to_s
  16. end
  17. end
  18. class CachedWalker < SexpProcessor
  19. attr_accessor :symbols
  20. attr_accessor :offset
  21. def initialize(offset, tree)
  22. super()
  23. module_hash = Hash.new {|hash, key| hash[key] = { assignments: [], uses: [] }}
  24. class_hash = module_hash.clone
  25. function_hash = module_hash.clone
  26. var_hash = module_hash.clone
  27. @require_empty = false
  28. @offset = offset
  29. @symbols = {
  30. modules: module_hash,
  31. classes: class_hash,
  32. functions: function_hash,
  33. vars: var_hash
  34. }
  35. end
  36. def block_position(exp)
  37. end_ln = (start_ln = exp.line - offset)
  38. cur_exp = exp
  39. while cur_exp.is_a? Sexp
  40. end_ln = cur_exp.line - offset
  41. cur_exp = cur_exp.last
  42. break if cur_exp == nil
  43. end
  44. pos = [start_ln, -1, end_ln, -1]
  45. return pos
  46. end
  47. def statement_position(exp)
  48. pos = Hash.new
  49. end_ln = start_ln = exp.line - offset
  50. pos = [start_ln, -1, end_ln, -1]
  51. return pos
  52. end
  53. def process_module(exp)
  54. pos = block_position(exp)
  55. exp.shift
  56. name = exp.shift
  57. symbols[:modules][name][:assignments] << pos
  58. exp.each_sexp {|s| process(s)}
  59. return exp.clear
  60. end
  61. def process_class(exp)
  62. pos = block_position(exp)
  63. exp.shift
  64. name = exp.shift
  65. symbols[:classes][name][:assignments] << pos
  66. exp.each_sexp {|s| process(s)}
  67. return exp.clear
  68. end
  69. def process_defn(exp)
  70. pos = block_position(exp)
  71. exp.shift
  72. name = exp.shift
  73. symbols[:functions][name][:assignments] << pos
  74. exp.each_sexp {|s| process(s)}
  75. return exp.clear
  76. end
  77. def process_call(exp)
  78. pos = statement_position(exp)
  79. exp.shift
  80. exp.shift
  81. name = exp.shift
  82. symbols[:functions][name][:uses] << pos
  83. exp.each_sexp {|s| process(s)}
  84. return exp.clear
  85. end
  86. def process_iasgn(exp)
  87. pos = statement_position(exp)
  88. exp.shift
  89. name = exp.shift
  90. symbols[:vars][name][:assignments] << pos
  91. exp.each_sexp {|s| process(s)}
  92. return exp.clear
  93. end
  94. def process_lasgn(exp)
  95. pos = statement_position(exp)
  96. exp.shift
  97. name = exp.shift
  98. symbols[:vars][name][:assignments] << pos
  99. exp.each_sexp {|s| process(s)}
  100. return exp.clear
  101. end
  102. def to_s
  103. str = symbols.to_s
  104. str = str.gsub(/:(\w*)=>/, '"\1":')
  105. return str
  106. end
  107. end
  108. end