2008-08-08 23:00:48 -04:00
|
|
|
require 'strscan'
|
2009-04-21 23:51:07 -07:00
|
|
|
require 'digest/sha1'
|
2006-12-04 08:40:23 +00:00
|
|
|
require 'sass/tree/node'
|
2009-09-14 01:34:16 -07:00
|
|
|
require 'sass/tree/root_node'
|
2006-12-04 08:40:23 +00:00
|
|
|
require 'sass/tree/rule_node'
|
2007-03-17 23:34:26 +00:00
|
|
|
require 'sass/tree/comment_node'
|
2009-06-19 03:49:40 -07:00
|
|
|
require 'sass/tree/prop_node'
|
2007-08-12 10:54:35 +00:00
|
|
|
require 'sass/tree/directive_node'
|
2008-10-15 01:21:12 -07:00
|
|
|
require 'sass/tree/variable_node'
|
2008-10-15 20:00:28 -07:00
|
|
|
require 'sass/tree/mixin_def_node'
|
2008-10-13 09:49:35 -07:00
|
|
|
require 'sass/tree/mixin_node'
|
|
|
|
require 'sass/tree/if_node'
|
|
|
|
require 'sass/tree/while_node'
|
|
|
|
require 'sass/tree/for_node'
|
2008-12-24 19:33:47 -08:00
|
|
|
require 'sass/tree/debug_node'
|
2009-07-12 10:21:21 -04:00
|
|
|
require 'sass/tree/import_node'
|
2008-10-15 20:00:28 -07:00
|
|
|
require 'sass/environment'
|
2008-10-12 19:03:06 -07:00
|
|
|
require 'sass/script'
|
2007-01-28 10:14:15 +00:00
|
|
|
require 'sass/error'
|
2009-04-22 00:02:07 -07:00
|
|
|
require 'sass/files'
|
2008-05-31 21:17:43 -07:00
|
|
|
require 'haml/shared'
|
2006-11-28 19:43:58 +00:00
|
|
|
|
|
|
|
module Sass
|
2009-04-28 15:06:56 -07:00
|
|
|
# A Sass mixin.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `name`: `String`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The name of the mixin.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `args`: `Array<(String, Script::Node)>`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The arguments for the mixin.
|
|
|
|
# Each element is a tuple containing the name of the argument
|
|
|
|
# and the parse tree for the default value of the argument.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `environment`: {Sass::Environment}
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The environment in which the mixin was defined.
|
|
|
|
# This is captured so that the mixin can have access
|
|
|
|
# to local variables defined in its scope.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `tree`: {Sass::Tree::Node}
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The parse tree for the mixin.
|
2008-10-15 20:00:28 -07:00
|
|
|
Mixin = Struct.new(:name, :args, :environment, :tree)
|
|
|
|
|
2009-04-28 15:06:56 -07:00
|
|
|
# This class handles the parsing and compilation of the Sass template.
|
|
|
|
# Example usage:
|
2006-12-17 16:45:07 +00:00
|
|
|
#
|
2009-04-28 15:06:56 -07:00
|
|
|
# template = File.load('stylesheets/sassy.sass')
|
|
|
|
# sass_engine = Sass::Engine.new(template)
|
|
|
|
# output = sass_engine.render
|
|
|
|
# puts output
|
2006-11-28 19:43:58 +00:00
|
|
|
class Engine
|
2009-01-22 14:53:59 -08:00
|
|
|
include Haml::Util
|
2009-04-28 15:06:56 -07:00
|
|
|
|
|
|
|
# A line of Sass code.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `text`: `String`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The text in the line, without any whitespace at the beginning or end.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `tabs`: `Fixnum`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The level of indentation of the line.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `index`: `Fixnum`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The line number in the original document.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `offset`: `Fixnum`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The number of bytes in on the line that the text begins.
|
|
|
|
# This ends up being the number of bytes of leading whitespace.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `filename`: `String`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The name of the file in which this line appeared.
|
|
|
|
#
|
2009-06-18 13:08:40 -07:00
|
|
|
# `children`: `Array<Line>`
|
2009-04-28 15:06:56 -07:00
|
|
|
# : The lines nested below this one.
|
2009-06-14 11:14:44 -07:00
|
|
|
class Line < Struct.new(:text, :tabs, :index, :offset, :filename, :children)
|
|
|
|
def comment?
|
|
|
|
text[0] == COMMENT_CHAR && (text[1] == SASS_COMMENT_CHAR || text[1] == CSS_COMMENT_CHAR)
|
|
|
|
end
|
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
|
2009-06-19 03:33:03 -07:00
|
|
|
# The character that begins a CSS property.
|
|
|
|
PROPERTY_CHAR = ?:
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2006-12-22 06:22:15 +00:00
|
|
|
# The character that designates that
|
2009-06-19 03:33:03 -07:00
|
|
|
# a property should be assigned to a SassScript expression.
|
2007-01-28 10:14:15 +00:00
|
|
|
SCRIPT_CHAR = ?=
|
2007-03-17 20:48:29 +00:00
|
|
|
|
|
|
|
# The character that designates the beginning of a comment,
|
|
|
|
# either Sass or CSS.
|
|
|
|
COMMENT_CHAR = ?/
|
|
|
|
|
2007-03-17 23:34:26 +00:00
|
|
|
# The character that follows the general COMMENT_CHAR and designates a Sass comment,
|
2007-03-17 20:48:29 +00:00
|
|
|
# which is not output as a CSS comment.
|
|
|
|
SASS_COMMENT_CHAR = ?/
|
2007-02-28 16:35:37 +00:00
|
|
|
|
2007-03-17 23:34:26 +00:00
|
|
|
# The character that follows the general COMMENT_CHAR and designates a CSS comment,
|
|
|
|
# which is embedded in the CSS document.
|
|
|
|
CSS_COMMENT_CHAR = ?*
|
2007-03-18 21:11:32 +00:00
|
|
|
|
|
|
|
# The character used to denote a compiler directive.
|
|
|
|
DIRECTIVE_CHAR = ?@
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2007-12-11 10:36:40 +00:00
|
|
|
# Designates a non-parsed rule.
|
|
|
|
ESCAPE_CHAR = ?\\
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2008-04-09 10:21:49 +01:00
|
|
|
# Designates block as mixin definition rather than CSS rules to output
|
2008-04-16 15:13:55 +01:00
|
|
|
MIXIN_DEFINITION_CHAR = ?=
|
2008-04-09 10:21:49 +01:00
|
|
|
|
|
|
|
# Includes named mixin declared using MIXIN_DEFINITION_CHAR
|
|
|
|
MIXIN_INCLUDE_CHAR = ?+
|
|
|
|
|
2009-06-19 03:33:03 -07:00
|
|
|
# The regex that matches properties of the form <tt>name: prop</tt>.
|
|
|
|
PROPERTY_NEW_MATCHER = /^[^\s:"]+\s*[=:](\s|$)/
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2007-06-28 08:43:09 +00:00
|
|
|
# The regex that matches and extracts data from
|
2009-06-19 03:33:03 -07:00
|
|
|
# properties of the form <tt>name: prop</tt>.
|
|
|
|
PROPERTY_NEW = /^([^\s=:"]+)(\s*=|:)(?:\s+|$)(.*)/
|
2009-06-19 03:01:20 -07:00
|
|
|
|
|
|
|
# The regex that matches and extracts data from
|
2009-06-19 03:33:03 -07:00
|
|
|
# properties of the form <tt>:name prop</tt>.
|
|
|
|
PROPERTY_OLD = /^:([^\s=:"]+)\s*(=?)(?:\s+|$)(.*)/
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2009-04-21 23:27:55 -07:00
|
|
|
# The default options for Sass::Engine.
|
|
|
|
DEFAULT_OPTIONS = {
|
|
|
|
:style => :nested,
|
|
|
|
:load_paths => ['.'],
|
2009-04-22 16:41:55 -07:00
|
|
|
:cache => true,
|
2009-04-22 13:12:32 -07:00
|
|
|
:cache_location => './.sass-cache',
|
2009-04-21 23:27:55 -07:00
|
|
|
}.freeze
|
|
|
|
|
2009-04-28 15:06:56 -07:00
|
|
|
# @param template [String] The Sass template.
|
|
|
|
# @param options [Hash<Symbol, Object>] An options hash;
|
2009-06-18 13:40:57 -07:00
|
|
|
# see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
|
2006-12-04 02:47:37 +00:00
|
|
|
def initialize(template, options={})
|
2009-04-21 23:27:55 -07:00
|
|
|
@options = DEFAULT_OPTIONS.merge(options)
|
2008-08-04 01:00:32 -04:00
|
|
|
@template = template
|
2009-06-19 03:01:20 -07:00
|
|
|
|
|
|
|
# Backwards compatibility
|
2009-06-19 03:33:03 -07:00
|
|
|
@options[:property_syntax] ||= @options[:attribute_syntax]
|
|
|
|
case @options[:property_syntax]
|
|
|
|
when :alternate; @options[:property_syntax] = :new
|
|
|
|
when :normal; @options[:property_syntax] = :old
|
2009-06-19 03:01:20 -07:00
|
|
|
end
|
2006-11-28 20:33:22 +00:00
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2009-04-28 15:06:56 -07:00
|
|
|
# Render the template to CSS.
|
|
|
|
#
|
|
|
|
# @return [String] The CSS
|
|
|
|
# @raise [Sass::SyntaxError] if there's an error in the document
|
2006-12-04 02:47:37 +00:00
|
|
|
def render
|
2009-04-23 16:42:37 -07:00
|
|
|
to_tree.render
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2007-02-01 06:59:08 +00:00
|
|
|
|
|
|
|
alias_method :to_css, :render
|
2007-03-18 21:11:32 +00:00
|
|
|
|
2009-04-28 15:06:56 -07:00
|
|
|
# Parses the document into its parse tree.
|
|
|
|
#
|
|
|
|
# @return [Sass::Tree::Node] The root of the parse tree.
|
|
|
|
# @raise [Sass::SyntaxError] if there's an error in the document
|
2009-04-21 23:51:37 -07:00
|
|
|
def to_tree
|
2009-09-23 15:32:08 -07:00
|
|
|
check_encoding(@template) {|msg, line| raise Sass::SyntaxError.new(msg, :line => line)}
|
|
|
|
|
2009-09-14 02:11:02 -07:00
|
|
|
root = Tree::RootNode.new(@template)
|
2008-08-04 01:00:32 -04:00
|
|
|
append_children(root, tree(tabulate(@template)).first, true)
|
2009-04-21 19:13:49 -07:00
|
|
|
root.options = @options
|
2008-10-15 01:21:12 -07:00
|
|
|
root
|
2009-09-13 15:52:59 -07:00
|
|
|
rescue SyntaxError => e
|
|
|
|
e.modify_backtrace(:filename => @options[:filename], :line => @line)
|
2009-09-14 02:11:02 -07:00
|
|
|
e.sass_template = @template
|
2009-09-13 15:52:59 -07:00
|
|
|
raise e
|
2007-03-18 21:11:32 +00:00
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2006-12-04 08:40:23 +00:00
|
|
|
private
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def tabulate(string)
|
|
|
|
tab_str = nil
|
|
|
|
first = true
|
2009-06-14 11:14:44 -07:00
|
|
|
lines = []
|
|
|
|
string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^.*?$/).each_with_index do |line, index|
|
2009-04-06 16:24:47 -07:00
|
|
|
index += (@options[:line] || 1)
|
2009-06-21 16:12:04 -07:00
|
|
|
if line.strip.empty?
|
|
|
|
lines.last.text << "\n" if lines.last && lines.last.comment?
|
|
|
|
next
|
|
|
|
end
|
2007-01-31 04:30:30 +00:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
line_tab_str = line[/^\s*/]
|
|
|
|
unless line_tab_str.empty?
|
|
|
|
tab_str ||= line_tab_str
|
2008-05-31 21:37:44 -07:00
|
|
|
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Indenting at the beginning of the document is illegal.",
|
|
|
|
:line => index) if first
|
|
|
|
|
|
|
|
raise SyntaxError.new("Indentation can't use both tabs and spaces.",
|
|
|
|
:line => index) if tab_str.include?(?\s) && tab_str.include?(?\t)
|
2006-11-28 19:43:58 +00:00
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
first &&= !tab_str.nil?
|
2009-06-14 11:14:44 -07:00
|
|
|
if tab_str.nil?
|
|
|
|
lines << Line.new(line.strip, 0, index, 0, @options[:filename], [])
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if lines.last && lines.last.comment? && line =~ /^(?:#{tab_str}){#{lines.last.tabs + 1}}(.*)$/
|
|
|
|
lines.last.text << "\n" << $1
|
|
|
|
next
|
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
|
|
|
|
line_tabs = line_tab_str.scan(tab_str).size
|
2009-09-13 15:52:59 -07:00
|
|
|
if tab_str * line_tabs != line_tab_str
|
|
|
|
message = <<END.strip.gsub("\n", ' ')
|
2008-08-04 01:00:32 -04:00
|
|
|
Inconsistent indentation: #{Haml::Shared.human_indentation line_tab_str, true} used for indentation,
|
|
|
|
but the rest of the document was indented using #{Haml::Shared.human_indentation tab_str}.
|
|
|
|
END
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new(message, :line => index)
|
|
|
|
end
|
|
|
|
|
2009-06-14 11:14:44 -07:00
|
|
|
lines << Line.new(line.strip, line_tabs, index, tab_str.size, @options[:filename], [])
|
|
|
|
end
|
|
|
|
lines
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def tree(arr, i = 0)
|
2008-11-22 16:54:02 -08:00
|
|
|
return [], i if arr[i].nil?
|
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
base = arr[i].tabs
|
|
|
|
nodes = []
|
|
|
|
while (line = arr[i]) && line.tabs >= base
|
|
|
|
if line.tabs > base
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("The line was indented #{line.tabs - base} levels deeper than the previous line.",
|
|
|
|
:line => line.index) if line.tabs > base + 1
|
2008-04-19 09:58:45 -07:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
nodes.last.children, i = tree(arr, i)
|
|
|
|
else
|
|
|
|
nodes << line
|
|
|
|
i += 1
|
2008-05-31 22:03:28 -07:00
|
|
|
end
|
2007-01-28 10:14:15 +00:00
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
return nodes, i
|
2006-12-19 05:22:19 +00:00
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2008-10-14 23:10:41 -07:00
|
|
|
def build_tree(parent, line, root = false)
|
2008-08-04 01:00:32 -04:00
|
|
|
@line = line.index
|
2009-07-12 10:21:21 -04:00
|
|
|
node_or_nodes = parse_line(parent, line, root)
|
2007-01-28 10:14:15 +00:00
|
|
|
|
2009-07-12 10:21:21 -04:00
|
|
|
Array(node_or_nodes).each do |node|
|
|
|
|
# Node is a symbol if it's non-outputting, like a variable assignment
|
|
|
|
next unless node.is_a? Tree::Node
|
2007-03-18 04:52:16 +00:00
|
|
|
|
2009-07-12 10:21:21 -04:00
|
|
|
node.line = line.index
|
|
|
|
node.filename = line.filename
|
2008-04-24 13:28:15 -07:00
|
|
|
|
2009-07-12 10:21:21 -04:00
|
|
|
if node.is_a?(Tree::CommentNode)
|
|
|
|
node.lines = line.children
|
|
|
|
else
|
|
|
|
append_children(node, line.children, false)
|
|
|
|
end
|
2008-03-30 11:15:37 -07:00
|
|
|
end
|
2009-07-12 10:21:21 -04:00
|
|
|
|
|
|
|
node_or_nodes
|
2008-08-04 01:00:32 -04:00
|
|
|
end
|
2008-03-30 11:15:37 -07:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def append_children(parent, children, root)
|
|
|
|
continued_rule = nil
|
|
|
|
children.each do |line|
|
2008-10-14 23:10:41 -07:00
|
|
|
child = build_tree(parent, line, root)
|
2008-08-04 01:00:32 -04:00
|
|
|
|
|
|
|
if child.is_a?(Tree::RuleNode) && child.continued?
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Rules can't end in commas.",
|
|
|
|
:line => child.line) unless child.children.empty?
|
2008-08-04 01:00:32 -04:00
|
|
|
if continued_rule
|
|
|
|
continued_rule.add_rules child
|
|
|
|
else
|
|
|
|
continued_rule = child
|
2007-03-18 04:52:16 +00:00
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
next
|
2007-03-18 04:52:16 +00:00
|
|
|
end
|
2008-03-30 11:15:37 -07:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
if continued_rule
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Rules can't end in commas.",
|
|
|
|
:line => continued_rule.line) unless child.is_a?(Tree::RuleNode)
|
2008-08-04 01:00:32 -04:00
|
|
|
continued_rule.add_rules child
|
|
|
|
continued_rule.children = child.children
|
|
|
|
continued_rule, child = nil, continued_rule
|
|
|
|
end
|
2008-03-30 11:15:37 -07:00
|
|
|
|
2009-07-19 14:56:32 -07:00
|
|
|
check_for_no_children(child)
|
2008-08-04 01:00:32 -04:00
|
|
|
validate_and_append_child(parent, child, line, root)
|
2006-12-19 05:22:19 +00:00
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Rules can't end in commas.",
|
|
|
|
:line => continued_rule.line) if continued_rule
|
2008-08-09 11:00:11 -04:00
|
|
|
|
|
|
|
parent
|
2006-12-19 05:22:19 +00:00
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def validate_and_append_child(parent, child, line, root)
|
|
|
|
unless root
|
|
|
|
case child
|
2008-10-15 20:00:28 -07:00
|
|
|
when Tree::MixinDefNode
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Mixins may only be defined at the root of a document.",
|
|
|
|
:line => line.index)
|
2009-07-12 10:21:21 -04:00
|
|
|
when Tree::ImportNode
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Import directives may only be used at the root of a document.",
|
|
|
|
:line => line.index)
|
2008-08-04 01:00:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-09 10:21:49 +01:00
|
|
|
case child
|
|
|
|
when Array
|
2008-08-04 01:00:32 -04:00
|
|
|
child.each {|c| validate_and_append_child(parent, c, line, root)}
|
2008-04-09 10:21:49 +01:00
|
|
|
when Tree::Node
|
|
|
|
parent << child
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-19 14:56:32 -07:00
|
|
|
def check_for_no_children(node)
|
|
|
|
return unless node.is_a?(Tree::RuleNode) && node.children.empty?
|
|
|
|
warning = (node.rules.size == 1) ? <<SHORT : <<LONG
|
2009-08-25 00:23:06 -07:00
|
|
|
WARNING on line #{node.line}:
|
2009-07-19 14:56:32 -07:00
|
|
|
Selector #{node.rules.first.inspect} doesn't have any properties and will not be rendered.
|
|
|
|
SHORT
|
|
|
|
|
2009-08-25 00:23:06 -07:00
|
|
|
WARNING on line #{node.line}:
|
2009-07-19 14:56:32 -07:00
|
|
|
Selector
|
|
|
|
#{node.rules.join("\n ")}
|
|
|
|
doesn't have any properties and will not be rendered.
|
|
|
|
LONG
|
|
|
|
|
|
|
|
warn(warning.strip)
|
|
|
|
end
|
|
|
|
|
2008-10-14 23:10:41 -07:00
|
|
|
def parse_line(parent, line, root)
|
2008-08-04 01:00:32 -04:00
|
|
|
case line.text[0]
|
2009-06-19 03:33:03 -07:00
|
|
|
when PROPERTY_CHAR
|
|
|
|
if line.text[1] != PROPERTY_CHAR
|
|
|
|
parse_property(line, PROPERTY_OLD)
|
2008-10-29 23:39:36 -07:00
|
|
|
else
|
|
|
|
# Support CSS3-style pseudo-elements,
|
|
|
|
# which begin with ::
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::RuleNode.new(line.text)
|
2008-10-29 23:39:36 -07:00
|
|
|
end
|
2008-10-12 19:03:06 -07:00
|
|
|
when Script::VARIABLE_CHAR
|
|
|
|
parse_variable(line)
|
2007-06-28 08:43:09 +00:00
|
|
|
when COMMENT_CHAR
|
2008-08-04 01:00:32 -04:00
|
|
|
parse_comment(line.text)
|
2007-06-28 08:43:09 +00:00
|
|
|
when DIRECTIVE_CHAR
|
2008-10-14 23:10:41 -07:00
|
|
|
parse_directive(parent, line, root)
|
2007-12-11 10:36:40 +00:00
|
|
|
when ESCAPE_CHAR
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::RuleNode.new(line.text[1..-1])
|
2008-04-09 10:21:49 +01:00
|
|
|
when MIXIN_DEFINITION_CHAR
|
|
|
|
parse_mixin_definition(line)
|
|
|
|
when MIXIN_INCLUDE_CHAR
|
2009-06-21 15:38:42 -07:00
|
|
|
if line.text[1].nil? || line.text[1] == ?\s
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::RuleNode.new(line.text)
|
2008-05-29 12:05:04 -07:00
|
|
|
else
|
2008-08-09 11:00:11 -04:00
|
|
|
parse_mixin_include(line, root)
|
2008-05-29 12:05:04 -07:00
|
|
|
end
|
2007-03-17 20:48:29 +00:00
|
|
|
else
|
2009-06-19 03:33:03 -07:00
|
|
|
if line.text =~ PROPERTY_NEW_MATCHER
|
|
|
|
parse_property(line, PROPERTY_NEW)
|
2007-05-03 08:46:02 +00:00
|
|
|
else
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::RuleNode.new(line.text)
|
2007-05-03 08:46:02 +00:00
|
|
|
end
|
2006-12-22 01:52:45 +00:00
|
|
|
end
|
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2009-06-19 03:33:03 -07:00
|
|
|
def parse_property(line, property_regx)
|
|
|
|
name, eq, value = line.text.scan(property_regx)[0]
|
2007-01-28 10:14:15 +00:00
|
|
|
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Invalid property: \"#{line.text}\".",
|
|
|
|
:line => @line) if name.nil? || value.nil?
|
|
|
|
|
2008-12-09 13:27:29 -08:00
|
|
|
expr = if (eq.strip[0] == SCRIPT_CHAR)
|
|
|
|
parse_script(value, :offset => line.offset + line.text.index(value))
|
|
|
|
else
|
|
|
|
value
|
2007-01-28 10:14:15 +00:00
|
|
|
end
|
2009-06-19 03:49:40 -07:00
|
|
|
Tree::PropNode.new(name, expr, property_regx == PROPERTY_OLD ? :old : :new)
|
2006-12-22 01:52:45 +00:00
|
|
|
end
|
2007-05-03 08:46:02 +00:00
|
|
|
|
2008-10-12 19:03:06 -07:00
|
|
|
def parse_variable(line)
|
|
|
|
name, op, value = line.text.scan(Script::MATCH)[0]
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath variable declarations.",
|
|
|
|
:line => @line + 1) unless line.children.empty?
|
|
|
|
raise SyntaxError.new("Invalid variable: \"#{line.text}\".",
|
|
|
|
:line => @line) unless name && value
|
2008-02-29 13:18:52 -08:00
|
|
|
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::VariableNode.new(name, parse_script(value, :offset => line.offset + line.text.index(value)), op == '||=')
|
2007-03-17 20:48:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_comment(line)
|
2009-02-21 18:09:14 -08:00
|
|
|
if line[1] == CSS_COMMENT_CHAR || line[1] == SASS_COMMENT_CHAR
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::CommentNode.new(line, line[1] == SASS_COMMENT_CHAR)
|
2007-03-17 20:48:29 +00:00
|
|
|
else
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::RuleNode.new(line)
|
2007-03-17 20:48:29 +00:00
|
|
|
end
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2007-03-18 21:11:32 +00:00
|
|
|
|
2008-10-14 23:10:41 -07:00
|
|
|
def parse_directive(parent, line, root)
|
2008-12-25 14:19:19 -08:00
|
|
|
directive, whitespace, value = line.text[1..-1].split(/(\s+)/, 2)
|
2008-12-28 13:32:04 -08:00
|
|
|
offset = directive.size + whitespace.size + 1 if whitespace
|
2007-03-18 21:11:32 +00:00
|
|
|
|
2008-02-29 22:38:01 -08:00
|
|
|
# If value begins with url( or ",
|
|
|
|
# it's a CSS @import rule and we don't want to touch it.
|
|
|
|
if directive == "import" && value !~ /^(url\(|")/
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.",
|
|
|
|
:line => @line + 1) unless line.children.empty?
|
2009-07-12 10:21:21 -04:00
|
|
|
value.split(/,\s*/).map {|f| Tree::ImportNode.new(f)}
|
2008-08-10 14:10:01 -04:00
|
|
|
elsif directive == "for"
|
|
|
|
parse_for(line, root, value)
|
2008-10-14 23:10:41 -07:00
|
|
|
elsif directive == "else"
|
|
|
|
parse_else(parent, line, value)
|
2008-08-10 14:21:25 -04:00
|
|
|
elsif directive == "while"
|
2008-12-28 13:36:10 -08:00
|
|
|
raise SyntaxError.new("Invalid while directive '@while': expected expression.") unless value
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::WhileNode.new(parse_script(value, :offset => offset))
|
2008-10-13 09:49:35 -07:00
|
|
|
elsif directive == "if"
|
2008-12-28 13:36:10 -08:00
|
|
|
raise SyntaxError.new("Invalid if directive '@if': expected expression.") unless value
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::IfNode.new(parse_script(value, :offset => offset))
|
2008-12-24 19:33:47 -08:00
|
|
|
elsif directive == "debug"
|
2008-12-28 13:36:10 -08:00
|
|
|
raise SyntaxError.new("Invalid debug directive '@debug': expected expression.") unless value
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath debug directives.",
|
|
|
|
:line => @line + 1) unless line.children.empty?
|
2008-12-24 19:33:47 -08:00
|
|
|
offset = line.offset + line.text.index(value).to_i
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::DebugNode.new(parse_script(value, :offset => offset))
|
2007-03-18 21:11:32 +00:00
|
|
|
else
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::DirectiveNode.new(line.text)
|
2008-08-09 11:00:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-10 14:10:01 -04:00
|
|
|
def parse_for(line, root, text)
|
|
|
|
var, from_expr, to_name, to_expr = text.scan(/^([^\s]+)\s+from\s+(.+)\s+(to|through)\s+(.+)$/).first
|
|
|
|
|
|
|
|
if var.nil? # scan failed, try to figure out why for error message
|
|
|
|
if text !~ /^[^\s]+/
|
2008-10-12 19:03:06 -07:00
|
|
|
expected = "variable name"
|
2008-08-10 14:10:01 -04:00
|
|
|
elsif text !~ /^[^\s]+\s+from\s+.+/
|
|
|
|
expected = "'from <expr>'"
|
|
|
|
else
|
|
|
|
expected = "'to <expr>' or 'through <expr>'"
|
|
|
|
end
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Invalid for directive '@for #{text}': expected #{expected}.")
|
2008-08-10 14:10:01 -04:00
|
|
|
end
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Invalid variable \"#{var}\".") unless var =~ Script::VALIDATE
|
2008-08-10 14:10:01 -04:00
|
|
|
|
2008-12-09 13:27:29 -08:00
|
|
|
parsed_from = parse_script(from_expr, :offset => line.offset + line.text.index(from_expr))
|
|
|
|
parsed_to = parse_script(to_expr, :offset => line.offset + line.text.index(to_expr))
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::ForNode.new(var[1..-1], parsed_from, parsed_to, to_name == 'to')
|
2008-08-10 14:21:25 -04:00
|
|
|
end
|
|
|
|
|
2008-10-14 23:10:41 -07:00
|
|
|
def parse_else(parent, line, text)
|
|
|
|
previous = parent.last
|
|
|
|
raise SyntaxError.new("@else must come after @if.") unless previous.is_a?(Tree::IfNode)
|
|
|
|
|
|
|
|
if text
|
|
|
|
if text !~ /^if\s+(.+)/
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Invalid else directive '@else #{text}': expected 'if <expr>'.")
|
2008-10-14 23:10:41 -07:00
|
|
|
end
|
2008-12-09 13:27:29 -08:00
|
|
|
expr = parse_script($1, :offset => line.offset + line.text.index($1))
|
2008-10-14 23:10:41 -07:00
|
|
|
end
|
|
|
|
|
2009-04-21 19:25:18 -07:00
|
|
|
node = Tree::IfNode.new(expr)
|
2008-10-14 23:10:41 -07:00
|
|
|
append_children(node, line.children, false)
|
|
|
|
previous.add_else node
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2008-04-09 10:21:49 +01:00
|
|
|
def parse_mixin_definition(line)
|
2008-09-29 22:43:05 -07:00
|
|
|
name, arg_string = line.text.scan(/^=\s*([^(]+)(.*)$/).first
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Invalid mixin \"#{line.text[1..-1]}\".") if name.nil?
|
2009-07-19 14:20:55 -07:00
|
|
|
|
|
|
|
offset = line.offset + line.text.size - arg_string.size
|
|
|
|
args = Script::Parser.new(arg_string.strip, @line, offset).parse_mixin_definition_arglist
|
2008-08-10 13:14:10 -07:00
|
|
|
default_arg_found = false
|
2009-04-21 19:25:18 -07:00
|
|
|
Tree::MixinDefNode.new(name, args)
|
2008-04-09 10:21:49 +01:00
|
|
|
end
|
|
|
|
|
2008-08-08 21:20:14 -04:00
|
|
|
def parse_mixin_include(line, root)
|
2008-09-29 22:43:05 -07:00
|
|
|
name, arg_string = line.text.scan(/^\+\s*([^(]+)(.*)$/).first
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Invalid mixin include \"#{line.text}\".") if name.nil?
|
2008-08-08 21:20:14 -04:00
|
|
|
|
2009-07-19 14:20:55 -07:00
|
|
|
offset = line.offset + line.text.size - arg_string.size
|
|
|
|
args = Script::Parser.new(arg_string.strip, @line, offset).parse_mixin_include_arglist
|
2009-09-13 15:52:59 -07:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath mixin directives.",
|
|
|
|
:line => @line + 1) unless line.children.empty?
|
2009-07-19 14:20:55 -07:00
|
|
|
Tree::MixinNode.new(name, args)
|
2008-08-08 23:00:48 -04:00
|
|
|
end
|
|
|
|
|
2008-12-09 13:27:29 -08:00
|
|
|
def parse_script(script, options = {})
|
|
|
|
line = options[:line] || @line
|
|
|
|
offset = options[:offset] || 0
|
2008-12-10 01:05:30 -08:00
|
|
|
Script.parse(script, line, offset, @options[:filename])
|
2008-12-09 09:28:48 -08:00
|
|
|
end
|
2006-11-28 19:43:58 +00:00
|
|
|
end
|
|
|
|
end
|