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



52
53
54
# File 'lib/redparse/parse_tree_server.rb', line 52

def self.path_to_server_command
  File.expand_path __FILE__
end

Instance Method Details

#ensure_parse_tree_and_1_8Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/redparse/parse_tree_server.rb', line 56

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)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/redparse/parse_tree_server.rb', line 125

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/redparse/parse_tree_server.rb', line 76

def main
  Process.setsid
  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("\n") #warnings
      }
    end
    rescue Exception=>e; put e; raise
    ensure exit!
    end
end