Class: ParseTreeServer

Inherits:
Object
  • Object
show all
Includes:
ParseTreeComm
Defined in:
lib/redparse/parse_tree_server.rb

Constant Summary

Constants included from ParseTreeComm

ParseTreeComm::SERIALIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseTreeComm

#get, #put

Class Method Details

.path_to_server_commandObject



35
36
37
# File 'lib/redparse/parse_tree_server.rb', line 35

def self.path_to_server_command
  File.expand_path __FILE__
end

Instance Method Details

#ensure_parse_tree_and_1_8Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redparse/parse_tree_server.rb', line 39

def ensure_parse_tree_and_1_8
  if ::RUBY_VERSION[/^\d+\.\d+/].to_f>1.8
    ruby18=ENV['RUBY1_8']||fail("you must use ruby <= 1.8 (with parse_tree) or set RUBY1_8 env to a 1.8 interpreter")
    exec ruby18, $0
  else
    begin require 'rubygems'; rescue LoadError; end

    if File.exist? find_home+"/.redparse/parse_tree_server.rc"
      $:.concat File.readlines(find_home+"/.redparse/parse_tree_server.rc").map{|l| l.chop }
    end

    require 'parse_tree'
  end
rescue Exception=>e
  put e
  put nil
  put nil
  raise
end

#find_homeObject

Finds the user’s home directory. – Some comments from the ruby-talk list regarding finding the home directory:

I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
to be depending on HOME in those code samples. I propose that
it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
least on Win32).

(originally stolen from rubygems)



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/redparse/parse_tree_server.rb', line 107

def find_home
  ['HOME', 'USERPROFILE'].each do |homekey|
    return ENV[homekey] if ENV[homekey]
  end

  if ENV['HOMEDRIVE'] && ENV['HOMEPATH'] then
    return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}"
  end

  begin
    File.expand_path("~")
  rescue
    if File::ALT_SEPARATOR then
        "C:/"
    else
        "/"
    end
  end
end

#mainObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/redparse/parse_tree_server.rb', line 59

def main
  si=STDIN
  so=STDOUT
  @out=so; @in=si
  ensure_parse_tree_and_1_8
    begin
    warnstash=Tempfile.new "warnstash"
    STDERR.reopen warnstash
    instance=ParseTree.new
    while true
      str=get
      exit! if str==:exit!
      if str==:version
        put ::RUBY_VERSION
        next
      end

      pos=STDERR.pos

      tree=
      begin
        instance.parse_tree_for_string(str) #tree
      rescue Exception=>e; 
        tree=e
      end
      put tree

      open(STDERR.path){|f| 
        f.pos=pos
        put warnings=f.read.split #warnings
      }
    end
    rescue Exception=>e; put e; raise
    ensure exit!
    end
end