haml--haml/lib/sass/repl.rb

52 lines
1.1 KiB
Ruby
Raw Normal View History

2008-10-16 23:23:45 +00:00
require 'readline'
module Sass
2009-02-26 06:11:49 +00:00
class Repl
def initialize(options = {})
@options = options
end
2008-10-16 23:23:45 +00:00
2009-02-26 06:11:49 +00:00
def run
environment = Environment.new
environment.set_var('important', Script::String.new('!important'))
@line = 0
loop do
@line += 1
unless text = Readline.readline('>> ')
puts
return
2008-10-16 23:23:45 +00:00
end
2009-02-26 06:11:49 +00:00
Readline::HISTORY << text
parse_input(environment, text)
2008-10-16 23:23:45 +00:00
end
2009-02-26 06:11:49 +00:00
end
2008-10-16 23:23:45 +00:00
2009-02-26 06:11:49 +00:00
private
2008-10-16 23:23:45 +00:00
2009-02-26 06:11:49 +00:00
def parse_input(environment, text)
case text
when Script::MATCH
name = $1
guarded = $2 == '||='
val = Script::Parser.parse($3, @line, text.size - $3.size)
2008-10-16 23:23:45 +00:00
2009-02-26 06:11:49 +00:00
unless guarded && environment.var(name)
environment.set_var(name, val.perform(environment))
end
2008-10-16 23:23:45 +00:00
2009-02-26 06:11:49 +00:00
p environment.var(name)
else
p Script::Parser.parse(text, @line, 0).perform(environment)
end
rescue Sass::SyntaxError => e
puts "SyntaxError: #{e.message}"
if @options[:trace]
e.backtrace.each do |e|
puts "\tfrom #{e}"
2008-10-16 23:23:45 +00:00
end
end
end
end
end