PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/Ruby/lib/antlr3/test/core-extensions.rb

https://bitbucket.org/jwalton/antlr3
Ruby | 269 lines | 145 code | 39 blank | 85 comment | 21 complexity | 9728a1f2639b8c6219eec02f1983e70c MD5 | raw file
  1. #!/usr/bin/ruby
  2. # encoding: utf-8
  3. class String
  4. def /( subpath )
  5. File.join( self, subpath.to_s )
  6. end
  7. def here_indent( chr = '| ' )
  8. dup.here_indent!( chr )
  9. end
  10. def here_indent!( chr = '| ' )
  11. chr = Regexp.escape( chr )
  12. exp = Regexp.new( "^ *#{ chr }" )
  13. self.gsub!( exp,'' )
  14. return self
  15. end
  16. def here_flow( chr = '| ' )
  17. dup.here_flow!( chr )
  18. end
  19. def here_flow!( chr = '| ' )
  20. here_indent!( chr ).gsub!( /\n\s+/,' ' )
  21. return( self )
  22. end
  23. # Indent left or right by n spaces.
  24. # (This used to be called #tab and aliased as #indent.)
  25. #
  26. # CREDIT: Gavin Sinclair
  27. # CREDIT: Trans
  28. def indent( n )
  29. if n >= 0
  30. gsub( /^/, ' ' * n )
  31. else
  32. gsub( /^ {0,#{ -n }}/, "" )
  33. end
  34. end
  35. # Outdent just indents a negative number of spaces.
  36. #
  37. # CREDIT: Noah Gibbs
  38. def outdent( n )
  39. indent( -n )
  40. end
  41. # Returns the shortest length of leading whitespace for all non-blank lines
  42. #
  43. # n = %Q(
  44. # a = 3
  45. # b = 4
  46. # ).level_of_indent #=> 2
  47. #
  48. # CREDIT: Kyle Yetter
  49. def level_of_indent
  50. self.scan( /^ *(?=\S)/ ).map { |space| space.length }.min || 0
  51. end
  52. def fixed_indent( n )
  53. self.outdent( self.level_of_indent ).indent( n )
  54. end
  55. # Provides a margin controlled string.
  56. #
  57. # x = %Q{
  58. # | This
  59. # | is
  60. # | margin controlled!
  61. # }.margin
  62. #
  63. #
  64. # NOTE: This may still need a bit of tweaking.
  65. #
  66. # CREDIT: Trans
  67. def margin( n=0 )
  68. #d = /\A.*\n\s*(.)/.match( self )[1]
  69. #d = /\A\s*(.)/.match( self)[1] unless d
  70. d = ( ( /\A.*\n\s*(.)/.match( self ) ) ||
  71. ( /\A\s*(.)/.match( self ) ) )[ 1 ]
  72. return '' unless d
  73. if n == 0
  74. gsub( /\n\s*\Z/,'' ).gsub( /^\s*[#{ d }]/, '' )
  75. else
  76. gsub( /\n\s*\Z/,'' ).gsub( /^\s*[#{ d }]/, ' ' * n )
  77. end
  78. end
  79. # Expands tabs to +n+ spaces. Non-destructive. If +n+ is 0, then tabs are
  80. # simply removed. Raises an exception if +n+ is negative.
  81. #
  82. # Thanks to GGaramuno for a more efficient algorithm. Very nice.
  83. #
  84. # CREDIT: Gavin Sinclair
  85. # CREDIT: Noah Gibbs
  86. # CREDIT: GGaramuno
  87. def expand_tabs( n=8 )
  88. n = n.to_int
  89. raise ArgumentError, "n must be >= 0" if n < 0
  90. return gsub( /\t/, "" ) if n == 0
  91. return gsub( /\t/, " " ) if n == 1
  92. str = self.dup
  93. while
  94. str.gsub!( /^([^\t\n]*)(\t+)/ ) { |f|
  95. val = ( n * $2.size - ( $1.size % n ) )
  96. $1 << ( ' ' * val )
  97. }
  98. end
  99. str
  100. end
  101. # The reverse of +camelcase+. Makes an underscored of a camelcase string.
  102. #
  103. # Changes '::' to '/' to convert namespaces to paths.
  104. #
  105. # Examples
  106. # "SnakeCase".snakecase #=> "snake_case"
  107. # "Snake-Case".snakecase #=> "snake_case"
  108. # "SnakeCase::Errors".underscore #=> "snake_case/errors"
  109. def snakecase
  110. gsub( /::/, '/' ). # NOT SO SURE ABOUT THIS -T
  111. gsub( /([A-Z]+)([A-Z][a-z])/,'\1_\2' ).
  112. gsub( /([a-z\d])([A-Z])/,'\1_\2' ).
  113. tr( "-", "_" ).
  114. downcase
  115. end
  116. end
  117. class Module
  118. # Returns the module's container module.
  119. #
  120. # module Example
  121. # class Demo
  122. # end
  123. # end
  124. #
  125. # Example::Demo.modspace #=> Example
  126. #
  127. # See also Module#basename.
  128. #
  129. # CREDIT: Trans
  130. def modspace
  131. space = name[ 0...( name.rindex( '::' ) || 0 ) ]
  132. space.empty? ? Object : eval( space )
  133. end
  134. end
  135. module Kernel
  136. autoload :Tempfile, 'tempfile'
  137. def screen_width( out=STDERR )
  138. default_width = ENV[ 'COLUMNS' ] || 80
  139. tiocgwinsz = 0x5413
  140. data = [ 0, 0, 0, 0 ].pack( "SSSS" )
  141. if out.ioctl( tiocgwinsz, data ) >= 0 then
  142. rows, cols, xpixels, ypixels = data.unpack( "SSSS" )
  143. if cols >= 0 then cols else default_width end
  144. else
  145. default_width
  146. end
  147. rescue Exception => e
  148. default_width rescue ( raise e )
  149. end
  150. end
  151. class File
  152. # given some target path string, and an optional reference path
  153. # (Dir.pwd by default), this method returns a string containing
  154. # the relative path of the target path from the reference path
  155. #
  156. # Examples:
  157. # File.relative_path('rel/path') # => './rel/path'
  158. # File.relative_path('/some/abs/path', '/some') # => './abs/path'
  159. # File.relative_path('/some/file.txt', '/some/abs/path') # => '../../file.txt'
  160. def self.relative_path( target, reference = Dir.pwd )
  161. pair = [ target, reference ].map! do |path|
  162. File.expand_path( path.to_s ).split( File::Separator ).tap do |list|
  163. if list.empty? then list << String.new( File::Separator )
  164. elsif list.first.empty? then list.first.replace( File::Separator )
  165. end
  166. end
  167. end
  168. target_list, reference_list = pair
  169. while target_list.first == reference_list.first
  170. target_list.shift
  171. reference_list.shift or break
  172. end
  173. relative_list = Array.new( reference_list.length, '..' )
  174. relative_list.empty? and relative_list << '.'
  175. relative_list.concat( target_list ).compact!
  176. return relative_list.join( File::Separator )
  177. end
  178. end
  179. class Dir
  180. defined?( DOTS ) or DOTS = %w(. ..).freeze
  181. def self.children( directory )
  182. entries = Dir.entries( directory ) - DOTS
  183. entries.map! do |entry|
  184. File.join( directory, entry )
  185. end
  186. end
  187. def self.mkpath( path )
  188. $VERBOSE and $stderr.puts( "INFO: Dir.mkpath(%p)" % path )
  189. test( ?d, path ) and return( path )
  190. parent = File.dirname( path )
  191. test( ?d, parent ) or mkpath( parent )
  192. Dir.mkdir( path )
  193. return( path )
  194. end
  195. end
  196. class Array
  197. # Pad an array with a given <tt>value</tt> upto a given <tt>length</tt>.
  198. #
  199. # [0,1,2].pad(6,"a") #=> [0,1,2,"a","a","a"]
  200. #
  201. # If <tt>length</tt> is a negative number padding will be added
  202. # to the beginning of the array.
  203. #
  204. # [0,1,2].pad(-6,"a") #=> ["a","a","a",0,1,2]
  205. #
  206. # CREDIT: Richard Laugesen
  207. def pad( len, val=nil )
  208. return dup if self.size >= len.abs
  209. if len < 0
  210. Array.new( ( len+size ).abs,val ) + self
  211. else
  212. self + Array.new( len-size,val )
  213. end
  214. end
  215. # Like #pad but changes the array in place.
  216. #
  217. # a = [0,1,2]
  218. # a.pad!(6,"x")
  219. # a #=> [0,1,2,"x","x","x"]
  220. #
  221. # CREDIT: Richard Laugesen
  222. def pad!( len, val=nil )
  223. return self if self.size >= len.abs
  224. if len < 0
  225. replace Array.new( ( len+size ).abs,val ) + self
  226. else
  227. concat Array.new( len-size,val )
  228. end
  229. end
  230. end