PageRenderTime 403ms CodeModel.GetById 381ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/xsd/codegen/gensupport.rb

http://github.com/agross/netopenspace
Ruby | 166 lines | 139 code | 21 blank | 6 comment | 6 complexity | 14c5b257a62b7b8681be9e1e1d692456 MD5 | raw file
  1# XSD4R - Code generation support
  2# Copyright (C) 2004, 2005  NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
  3
  4# This program is copyrighted free software by NAKAMURA, Hiroshi.  You can
  5# redistribute it and/or modify it under the same terms of Ruby's license;
  6# either the dual license version in 2003, or any later version.
  7
  8
  9module XSD
 10module CodeGen
 11
 12# from the file 'keywords' in 1.9.
 13KEYWORD = {}
 14%w(
 15__LINE__
 16__FILE__
 17BEGIN
 18END
 19alias
 20and
 21begin
 22break
 23case
 24class
 25def
 26defined?
 27do
 28else
 29elsif
 30end
 31ensure
 32false
 33for
 34if
 35in
 36module
 37next
 38nil
 39not
 40or
 41redo
 42rescue
 43retry
 44return
 45self
 46super
 47then
 48true
 49undef
 50unless
 51until
 52when
 53while
 54yield
 55).each { |k| KEYWORD[k] = nil }
 56
 57module GenSupport
 58  def capitalize(target)
 59    target.sub(/^([a-z])/) { $1.tr!('[a-z]', '[A-Z]') }
 60  end
 61  module_function :capitalize
 62
 63  def uncapitalize(target)
 64    target.sub(/^([A-Z])/) { $1.tr!('[A-Z]', '[a-z]') }
 65  end
 66  module_function :uncapitalize
 67
 68  def safeconstname(name)
 69    safename = name.scan(/[a-zA-Z0-9_]+/).collect { |ele|
 70      GenSupport.capitalize(ele)
 71    }.join
 72    if /^[A-Z]/ !~ safename or keyword?(safename)
 73      safename = "C_#{safename}"
 74    end
 75    safename
 76  end
 77  module_function :safeconstname
 78
 79  def safeconstname?(name)
 80    /\A[A-Z][a-zA-Z0-9_]*\z/ =~ name and !keyword?(name)
 81  end
 82  module_function :safeconstname?
 83
 84  def safemethodname(name)
 85    safename = name.scan(/[a-zA-Z0-9_]+/).join('_')
 86    safename = uncapitalize(safename)
 87    if /^[a-z]/ !~ safename
 88      safename = "m_#{safename}"
 89    end
 90    safename
 91  end
 92  module_function :safemethodname
 93
 94  def safemethodname?(name)
 95    /\A[a-zA-Z_][a-zA-Z0-9_]*[=!?]?\z/ =~ name
 96  end
 97  module_function :safemethodname?
 98
 99  def safevarname(name)
100    safename = uncapitalize(name.scan(/[a-zA-Z0-9_]+/).join('_'))
101    if /^[a-z]/ !~ safename or keyword?(safename)
102      "v_#{safename}"
103    else
104      safename
105    end
106  end
107  module_function :safevarname
108
109  def safevarname?(name)
110    /\A[a-z_][a-zA-Z0-9_]*\z/ =~ name and !keyword?(name)
111  end
112  module_function :safevarname?
113
114  def keyword?(word)
115    KEYWORD.key?(word)
116  end
117  module_function :keyword?
118
119  def format(str, indent = nil)
120    str = trim_eol(str)
121    str = trim_indent(str)
122    if indent
123      str.gsub(/^/, " " * indent)
124    else
125      str
126    end
127  end
128
129private
130
131  def trim_eol(str)
132    str.collect { |line|
133      line.sub(/\r?\n\z/, "") + "\n"
134    }.join
135  end
136
137  def trim_indent(str)
138    indent = nil
139    str = str.collect { |line| untab(line) }.join
140    str.each do |line|
141      head = line.index(/\S/)
142      if !head.nil? and (indent.nil? or head < indent)
143        indent = head
144      end
145    end
146    return str unless indent
147    str.collect { |line|
148      line.sub(/^ {0,#{indent}}/, "")
149    }.join
150  end
151
152  def untab(line, ts = 8)
153    while pos = line.index(/\t/)
154      line = line.sub(/\t/, " " * (ts - (pos % ts)))
155    end
156    line
157  end
158
159  def dump_emptyline
160    "\n"
161  end
162end
163
164
165end
166end