PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/haml-3.1.4/vendor/sass/lib/sass/engine.rb

https://bitbucket.org/mulligan/extractext
Ruby | 878 lines | 640 code | 93 blank | 145 comment | 98 complexity | 89838529674449afdcc4a20404628d77 MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-3.0, GPL-2.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, BSD-2-Clause, JSON
  1. require 'strscan'
  2. require 'set'
  3. require 'digest/sha1'
  4. require 'sass/cache_stores'
  5. require 'sass/tree/node'
  6. require 'sass/tree/root_node'
  7. require 'sass/tree/rule_node'
  8. require 'sass/tree/comment_node'
  9. require 'sass/tree/prop_node'
  10. require 'sass/tree/directive_node'
  11. require 'sass/tree/media_node'
  12. require 'sass/tree/variable_node'
  13. require 'sass/tree/mixin_def_node'
  14. require 'sass/tree/mixin_node'
  15. require 'sass/tree/function_node'
  16. require 'sass/tree/return_node'
  17. require 'sass/tree/extend_node'
  18. require 'sass/tree/if_node'
  19. require 'sass/tree/while_node'
  20. require 'sass/tree/for_node'
  21. require 'sass/tree/each_node'
  22. require 'sass/tree/debug_node'
  23. require 'sass/tree/warn_node'
  24. require 'sass/tree/import_node'
  25. require 'sass/tree/charset_node'
  26. require 'sass/tree/visitors/base'
  27. require 'sass/tree/visitors/perform'
  28. require 'sass/tree/visitors/cssize'
  29. require 'sass/tree/visitors/convert'
  30. require 'sass/tree/visitors/to_css'
  31. require 'sass/tree/visitors/deep_copy'
  32. require 'sass/tree/visitors/set_options'
  33. require 'sass/tree/visitors/check_nesting'
  34. require 'sass/selector'
  35. require 'sass/environment'
  36. require 'sass/script'
  37. require 'sass/scss'
  38. require 'sass/error'
  39. require 'sass/importers'
  40. require 'sass/shared'
  41. module Sass
  42. # A Sass mixin or function.
  43. #
  44. # `name`: `String`
  45. # : The name of the mixin/function.
  46. #
  47. # `args`: `Array<(String, Script::Node)>`
  48. # : The arguments for the mixin/function.
  49. # Each element is a tuple containing the name of the argument
  50. # and the parse tree for the default value of the argument.
  51. #
  52. # `environment`: {Sass::Environment}
  53. # : The environment in which the mixin/function was defined.
  54. # This is captured so that the mixin/function can have access
  55. # to local variables defined in its scope.
  56. #
  57. # `tree`: `Array<Tree::Node>`
  58. # : The parse tree for the mixin/function.
  59. Callable = Struct.new(:name, :args, :environment, :tree)
  60. # This class handles the parsing and compilation of the Sass template.
  61. # Example usage:
  62. #
  63. # template = File.load('stylesheets/sassy.sass')
  64. # sass_engine = Sass::Engine.new(template)
  65. # output = sass_engine.render
  66. # puts output
  67. class Engine
  68. include Sass::Util
  69. # A line of Sass code.
  70. #
  71. # `text`: `String`
  72. # : The text in the line, without any whitespace at the beginning or end.
  73. #
  74. # `tabs`: `Fixnum`
  75. # : The level of indentation of the line.
  76. #
  77. # `index`: `Fixnum`
  78. # : The line number in the original document.
  79. #
  80. # `offset`: `Fixnum`
  81. # : The number of bytes in on the line that the text begins.
  82. # This ends up being the number of bytes of leading whitespace.
  83. #
  84. # `filename`: `String`
  85. # : The name of the file in which this line appeared.
  86. #
  87. # `children`: `Array<Line>`
  88. # : The lines nested below this one.
  89. #
  90. # `comment_tab_str`: `String?`
  91. # : The prefix indentation for this comment, if it is a comment.
  92. class Line < Struct.new(:text, :tabs, :index, :offset, :filename, :children, :comment_tab_str)
  93. def comment?
  94. text[0] == COMMENT_CHAR && (text[1] == SASS_COMMENT_CHAR || text[1] == CSS_COMMENT_CHAR)
  95. end
  96. end
  97. # The character that begins a CSS property.
  98. PROPERTY_CHAR = ?:
  99. # The character that designates the beginning of a comment,
  100. # either Sass or CSS.
  101. COMMENT_CHAR = ?/
  102. # The character that follows the general COMMENT_CHAR and designates a Sass comment,
  103. # which is not output as a CSS comment.
  104. SASS_COMMENT_CHAR = ?/
  105. # The character that indicates that a comment allows interpolation
  106. # and should be preserved even in `:compressed` mode.
  107. SASS_LOUD_COMMENT_CHAR = ?!
  108. # The character that follows the general COMMENT_CHAR and designates a CSS comment,
  109. # which is embedded in the CSS document.
  110. CSS_COMMENT_CHAR = ?*
  111. # The character used to denote a compiler directive.
  112. DIRECTIVE_CHAR = ?@
  113. # Designates a non-parsed rule.
  114. ESCAPE_CHAR = ?\\
  115. # Designates block as mixin definition rather than CSS rules to output
  116. MIXIN_DEFINITION_CHAR = ?=
  117. # Includes named mixin declared using MIXIN_DEFINITION_CHAR
  118. MIXIN_INCLUDE_CHAR = ?+
  119. # The regex that matches and extracts data from
  120. # properties of the form `:name prop`.
  121. PROPERTY_OLD = /^:([^\s=:"]+)\s*(?:\s+|$)(.*)/
  122. # The default options for Sass::Engine.
  123. # @api public
  124. DEFAULT_OPTIONS = {
  125. :style => :nested,
  126. :load_paths => ['.'],
  127. :cache => true,
  128. :cache_location => './.sass-cache',
  129. :syntax => :sass,
  130. :filesystem_importer => Sass::Importers::Filesystem
  131. }.freeze
  132. # Converts a Sass options hash into a standard form, filling in
  133. # default values and resolving aliases.
  134. #
  135. # @param options [{Symbol => Object}] The options hash;
  136. # see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
  137. # @return [{Symbol => Object}] The normalized options hash.
  138. # @private
  139. def self.normalize_options(options)
  140. options = DEFAULT_OPTIONS.merge(options.reject {|k, v| v.nil?})
  141. # If the `:filename` option is passed in without an importer,
  142. # assume it's using the default filesystem importer.
  143. options[:importer] ||= options[:filesystem_importer].new(".") if options[:filename]
  144. # Tracks the original filename of the top-level Sass file
  145. options[:original_filename] ||= options[:filename]
  146. options[:cache_store] ||= Sass::CacheStores::Chain.new(
  147. Sass::CacheStores::Memory.new, Sass::CacheStores::Filesystem.new(options[:cache_location]))
  148. # Support both, because the docs said one and the other actually worked
  149. # for quite a long time.
  150. options[:line_comments] ||= options[:line_numbers]
  151. options[:load_paths] = options[:load_paths].map do |p|
  152. next p unless p.is_a?(String) || (defined?(Pathname) && p.is_a?(Pathname))
  153. options[:filesystem_importer].new(p.to_s)
  154. end
  155. # Backwards compatibility
  156. options[:property_syntax] ||= options[:attribute_syntax]
  157. case options[:property_syntax]
  158. when :alternate; options[:property_syntax] = :new
  159. when :normal; options[:property_syntax] = :old
  160. end
  161. options
  162. end
  163. # Returns the {Sass::Engine} for the given file.
  164. # This is preferable to Sass::Engine.new when reading from a file
  165. # because it properly sets up the Engine's metadata,
  166. # enables parse-tree caching,
  167. # and infers the syntax from the filename.
  168. #
  169. # @param filename [String] The path to the Sass or SCSS file
  170. # @param options [{Symbol => Object}] The options hash;
  171. # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  172. # @return [Sass::Engine] The Engine for the given Sass or SCSS file.
  173. # @raise [Sass::SyntaxError] if there's an error in the document.
  174. def self.for_file(filename, options)
  175. had_syntax = options[:syntax]
  176. if had_syntax
  177. # Use what was explicitly specificed
  178. elsif filename =~ /\.scss$/
  179. options.merge!(:syntax => :scss)
  180. elsif filename =~ /\.sass$/
  181. options.merge!(:syntax => :sass)
  182. end
  183. Sass::Engine.new(File.read(filename), options.merge(:filename => filename))
  184. end
  185. # The options for the Sass engine.
  186. # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  187. #
  188. # @return [{Symbol => Object}]
  189. attr_reader :options
  190. # Creates a new Engine. Note that Engine should only be used directly
  191. # when compiling in-memory Sass code.
  192. # If you're compiling a single Sass file from the filesystem,
  193. # use \{Sass::Engine.for\_file}.
  194. # If you're compiling multiple files from the filesystem,
  195. # use {Sass::Plugin.
  196. #
  197. # @param template [String] The Sass template.
  198. # This template can be encoded using any encoding
  199. # that can be converted to Unicode.
  200. # If the template contains an `@charset` declaration,
  201. # that overrides the Ruby encoding
  202. # (see {file:SASS_REFERENCE.md#encodings the encoding documentation})
  203. # @param options [{Symbol => Object}] An options hash.
  204. # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  205. # @see {Sass::Engine.for_file}
  206. # @see {Sass::Plugin}
  207. def initialize(template, options={})
  208. @options = self.class.normalize_options(options)
  209. @template = template
  210. end
  211. # Render the template to CSS.
  212. #
  213. # @return [String] The CSS
  214. # @raise [Sass::SyntaxError] if there's an error in the document
  215. # @raise [Encoding::UndefinedConversionError] if the source encoding
  216. # cannot be converted to UTF-8
  217. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  218. def render
  219. return _render unless @options[:quiet]
  220. Sass::Util.silence_sass_warnings {_render}
  221. end
  222. alias_method :to_css, :render
  223. # Parses the document into its parse tree. Memoized.
  224. #
  225. # @return [Sass::Tree::Node] The root of the parse tree.
  226. # @raise [Sass::SyntaxError] if there's an error in the document
  227. def to_tree
  228. @tree ||= @options[:quiet] ?
  229. Sass::Util.silence_sass_warnings {_to_tree} :
  230. _to_tree
  231. end
  232. # Returns the original encoding of the document,
  233. # or `nil` under Ruby 1.8.
  234. #
  235. # @return [Encoding, nil]
  236. # @raise [Encoding::UndefinedConversionError] if the source encoding
  237. # cannot be converted to UTF-8
  238. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  239. def source_encoding
  240. check_encoding!
  241. @original_encoding
  242. end
  243. # Gets a set of all the documents
  244. # that are (transitive) dependencies of this document,
  245. # not including the document itself.
  246. #
  247. # @return [[Sass::Engine]] The dependency documents.
  248. def dependencies
  249. _dependencies(Set.new, engines = Set.new)
  250. engines - [self]
  251. end
  252. # Helper for \{#dependencies}.
  253. #
  254. # @private
  255. def _dependencies(seen, engines)
  256. return if seen.include?(key = [@options[:filename], @options[:importer]])
  257. seen << key
  258. engines << self
  259. to_tree.grep(Tree::ImportNode) do |n|
  260. next if n.css_import?
  261. n.imported_file._dependencies(seen, engines)
  262. end
  263. end
  264. private
  265. def _render
  266. rendered = _to_tree.render
  267. return rendered if ruby1_8?
  268. begin
  269. # Try to convert the result to the original encoding,
  270. # but if that doesn't work fall back on UTF-8
  271. rendered = rendered.encode(source_encoding)
  272. rescue EncodingError
  273. end
  274. rendered.gsub(Regexp.new('\A@charset "(.*?)"'.encode(source_encoding)),
  275. "@charset \"#{source_encoding.name}\"".encode(source_encoding))
  276. end
  277. def _to_tree
  278. if (@options[:cache] || @options[:read_cache]) &&
  279. @options[:filename] && @options[:importer]
  280. key = sassc_key
  281. sha = Digest::SHA1.hexdigest(@template)
  282. if root = @options[:cache_store].retrieve(key, sha)
  283. root.options = @options
  284. return root
  285. end
  286. end
  287. check_encoding!
  288. if @options[:syntax] == :scss
  289. root = Sass::SCSS::Parser.new(@template, @options[:filename]).parse
  290. else
  291. root = Tree::RootNode.new(@template)
  292. append_children(root, tree(tabulate(@template)).first, true)
  293. end
  294. root.options = @options
  295. if @options[:cache] && key && sha
  296. begin
  297. old_options = root.options
  298. root.options = {}
  299. @options[:cache_store].store(key, sha, root)
  300. ensure
  301. root.options = old_options
  302. end
  303. end
  304. root
  305. rescue SyntaxError => e
  306. e.modify_backtrace(:filename => @options[:filename], :line => @line)
  307. e.sass_template = @template
  308. raise e
  309. end
  310. def sassc_key
  311. @options[:cache_store].key(*@options[:importer].key(@options[:filename], @options))
  312. end
  313. def check_encoding!
  314. return if @checked_encoding
  315. @checked_encoding = true
  316. @template, @original_encoding = check_sass_encoding(@template) do |msg, line|
  317. raise Sass::SyntaxError.new(msg, :line => line)
  318. end
  319. end
  320. def tabulate(string)
  321. tab_str = nil
  322. comment_tab_str = nil
  323. first = true
  324. lines = []
  325. string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^[^\n]*?$/).each_with_index do |line, index|
  326. index += (@options[:line] || 1)
  327. if line.strip.empty?
  328. lines.last.text << "\n" if lines.last && lines.last.comment?
  329. next
  330. end
  331. line_tab_str = line[/^\s*/]
  332. unless line_tab_str.empty?
  333. if tab_str.nil?
  334. comment_tab_str ||= line_tab_str
  335. next if try_comment(line, lines.last, "", comment_tab_str, index)
  336. comment_tab_str = nil
  337. end
  338. tab_str ||= line_tab_str
  339. raise SyntaxError.new("Indenting at the beginning of the document is illegal.",
  340. :line => index) if first
  341. raise SyntaxError.new("Indentation can't use both tabs and spaces.",
  342. :line => index) if tab_str.include?(?\s) && tab_str.include?(?\t)
  343. end
  344. first &&= !tab_str.nil?
  345. if tab_str.nil?
  346. lines << Line.new(line.strip, 0, index, 0, @options[:filename], [])
  347. next
  348. end
  349. comment_tab_str ||= line_tab_str
  350. if try_comment(line, lines.last, tab_str * lines.last.tabs, comment_tab_str, index)
  351. next
  352. else
  353. comment_tab_str = nil
  354. end
  355. line_tabs = line_tab_str.scan(tab_str).size
  356. if tab_str * line_tabs != line_tab_str
  357. message = <<END.strip.gsub("\n", ' ')
  358. Inconsistent indentation: #{Sass::Shared.human_indentation line_tab_str, true} used for indentation,
  359. but the rest of the document was indented using #{Sass::Shared.human_indentation tab_str}.
  360. END
  361. raise SyntaxError.new(message, :line => index)
  362. end
  363. lines << Line.new(line.strip, line_tabs, index, tab_str.size, @options[:filename], [])
  364. end
  365. lines
  366. end
  367. def try_comment(line, last, tab_str, comment_tab_str, index)
  368. return unless last && last.comment?
  369. # Nested comment stuff must be at least one whitespace char deeper
  370. # than the normal indentation
  371. return unless line =~ /^#{tab_str}\s/
  372. unless line =~ /^(?:#{comment_tab_str})(.*)$/
  373. raise SyntaxError.new(<<MSG.strip.gsub("\n", " "), :line => index)
  374. Inconsistent indentation:
  375. previous line was indented by #{Sass::Shared.human_indentation comment_tab_str},
  376. but this line was indented by #{Sass::Shared.human_indentation line[/^\s*/]}.
  377. MSG
  378. end
  379. last.comment_tab_str ||= comment_tab_str
  380. last.text << "\n" << line
  381. true
  382. end
  383. def tree(arr, i = 0)
  384. return [], i if arr[i].nil?
  385. base = arr[i].tabs
  386. nodes = []
  387. while (line = arr[i]) && line.tabs >= base
  388. if line.tabs > base
  389. raise SyntaxError.new("The line was indented #{line.tabs - base} levels deeper than the previous line.",
  390. :line => line.index) if line.tabs > base + 1
  391. nodes.last.children, i = tree(arr, i)
  392. else
  393. nodes << line
  394. i += 1
  395. end
  396. end
  397. return nodes, i
  398. end
  399. def build_tree(parent, line, root = false)
  400. @line = line.index
  401. node_or_nodes = parse_line(parent, line, root)
  402. Array(node_or_nodes).each do |node|
  403. # Node is a symbol if it's non-outputting, like a variable assignment
  404. next unless node.is_a? Tree::Node
  405. node.line = line.index
  406. node.filename = line.filename
  407. append_children(node, line.children, false)
  408. end
  409. node_or_nodes
  410. end
  411. def append_children(parent, children, root)
  412. continued_rule = nil
  413. continued_comment = nil
  414. children.each do |line|
  415. child = build_tree(parent, line, root)
  416. if child.is_a?(Tree::RuleNode)
  417. if child.continued? && child.children.empty?
  418. if continued_rule
  419. continued_rule.add_rules child
  420. else
  421. continued_rule = child
  422. end
  423. next
  424. elsif continued_rule
  425. continued_rule.add_rules child
  426. continued_rule.children = child.children
  427. continued_rule, child = nil, continued_rule
  428. end
  429. elsif continued_rule
  430. continued_rule = nil
  431. end
  432. if child.is_a?(Tree::CommentNode) && child.silent
  433. if continued_comment &&
  434. child.line == continued_comment.line +
  435. continued_comment.lines + 1
  436. continued_comment.value += ["\n"] + child.value
  437. next
  438. end
  439. continued_comment = child
  440. end
  441. check_for_no_children(child)
  442. validate_and_append_child(parent, child, line, root)
  443. end
  444. parent
  445. end
  446. def validate_and_append_child(parent, child, line, root)
  447. case child
  448. when Array
  449. child.each {|c| validate_and_append_child(parent, c, line, root)}
  450. when Tree::Node
  451. parent << child
  452. end
  453. end
  454. def check_for_no_children(node)
  455. return unless node.is_a?(Tree::RuleNode) && node.children.empty?
  456. Sass::Util.sass_warn(<<WARNING.strip)
  457. WARNING on line #{node.line}#{" of #{node.filename}" if node.filename}:
  458. This selector doesn't have any properties and will not be rendered.
  459. WARNING
  460. end
  461. def parse_line(parent, line, root)
  462. case line.text[0]
  463. when PROPERTY_CHAR
  464. if line.text[1] == PROPERTY_CHAR ||
  465. (@options[:property_syntax] == :new &&
  466. line.text =~ PROPERTY_OLD && $2.empty?)
  467. # Support CSS3-style pseudo-elements,
  468. # which begin with ::,
  469. # as well as pseudo-classes
  470. # if we're using the new property syntax
  471. Tree::RuleNode.new(parse_interp(line.text))
  472. else
  473. name, value = line.text.scan(PROPERTY_OLD)[0]
  474. raise SyntaxError.new("Invalid property: \"#{line.text}\".",
  475. :line => @line) if name.nil? || value.nil?
  476. parse_property(name, parse_interp(name), value, :old, line)
  477. end
  478. when ?$
  479. parse_variable(line)
  480. when COMMENT_CHAR
  481. parse_comment(line)
  482. when DIRECTIVE_CHAR
  483. parse_directive(parent, line, root)
  484. when ESCAPE_CHAR
  485. Tree::RuleNode.new(parse_interp(line.text[1..-1]))
  486. when MIXIN_DEFINITION_CHAR
  487. parse_mixin_definition(line)
  488. when MIXIN_INCLUDE_CHAR
  489. if line.text[1].nil? || line.text[1] == ?\s
  490. Tree::RuleNode.new(parse_interp(line.text))
  491. else
  492. parse_mixin_include(line, root)
  493. end
  494. else
  495. parse_property_or_rule(line)
  496. end
  497. end
  498. def parse_property_or_rule(line)
  499. scanner = StringScanner.new(line.text)
  500. hack_char = scanner.scan(/[:\*\.]|\#(?!\{)/)
  501. parser = Sass::SCSS::SassParser.new(scanner, @options[:filename], @line)
  502. unless res = parser.parse_interp_ident
  503. return Tree::RuleNode.new(parse_interp(line.text))
  504. end
  505. res.unshift(hack_char) if hack_char
  506. if comment = scanner.scan(Sass::SCSS::RX::COMMENT)
  507. res << comment
  508. end
  509. name = line.text[0...scanner.pos]
  510. if scanner.scan(/\s*:(?:\s|$)/)
  511. parse_property(name, res, scanner.rest, :new, line)
  512. else
  513. res.pop if comment
  514. Tree::RuleNode.new(res + parse_interp(scanner.rest))
  515. end
  516. end
  517. def parse_property(name, parsed_name, value, prop, line)
  518. if value.strip.empty?
  519. expr = Sass::Script::String.new("")
  520. else
  521. expr = parse_script(value, :offset => line.offset + line.text.index(value))
  522. end
  523. Tree::PropNode.new(parse_interp(name), expr, prop)
  524. end
  525. def parse_variable(line)
  526. name, value, default = line.text.scan(Script::MATCH)[0]
  527. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath variable declarations.",
  528. :line => @line + 1) unless line.children.empty?
  529. raise SyntaxError.new("Invalid variable: \"#{line.text}\".",
  530. :line => @line) unless name && value
  531. expr = parse_script(value, :offset => line.offset + line.text.index(value))
  532. Tree::VariableNode.new(name, expr, default)
  533. end
  534. def parse_comment(line)
  535. if line.text[1] == CSS_COMMENT_CHAR || line.text[1] == SASS_COMMENT_CHAR
  536. silent = line.text[1] == SASS_COMMENT_CHAR
  537. if loud = line.text[2] == SASS_LOUD_COMMENT_CHAR
  538. value = self.class.parse_interp(line.text, line.index, line.offset, :filename => @filename)
  539. value[0].slice!(2) # get rid of the "!"
  540. else
  541. value = [line.text]
  542. end
  543. value = with_extracted_values(value) do |str|
  544. str = str.gsub(/^#{line.comment_tab_str}/m, '')[2..-1] # get rid of // or /*
  545. format_comment_text(str, silent)
  546. end
  547. Tree::CommentNode.new(value, silent, loud)
  548. else
  549. Tree::RuleNode.new(parse_interp(line))
  550. end
  551. end
  552. def parse_directive(parent, line, root)
  553. directive, whitespace, value = line.text[1..-1].split(/(\s+)/, 2)
  554. offset = directive.size + whitespace.size + 1 if whitespace
  555. # If value begins with url( or ",
  556. # it's a CSS @import rule and we don't want to touch it.
  557. if directive == "import"
  558. parse_import(line, value)
  559. elsif directive == "mixin"
  560. parse_mixin_definition(line)
  561. elsif directive == "include"
  562. parse_mixin_include(line, root)
  563. elsif directive == "function"
  564. parse_function(line, root)
  565. elsif directive == "for"
  566. parse_for(line, root, value)
  567. elsif directive == "each"
  568. parse_each(line, root, value)
  569. elsif directive == "else"
  570. parse_else(parent, line, value)
  571. elsif directive == "while"
  572. raise SyntaxError.new("Invalid while directive '@while': expected expression.") unless value
  573. Tree::WhileNode.new(parse_script(value, :offset => offset))
  574. elsif directive == "if"
  575. raise SyntaxError.new("Invalid if directive '@if': expected expression.") unless value
  576. Tree::IfNode.new(parse_script(value, :offset => offset))
  577. elsif directive == "debug"
  578. raise SyntaxError.new("Invalid debug directive '@debug': expected expression.") unless value
  579. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath debug directives.",
  580. :line => @line + 1) unless line.children.empty?
  581. offset = line.offset + line.text.index(value).to_i
  582. Tree::DebugNode.new(parse_script(value, :offset => offset))
  583. elsif directive == "extend"
  584. raise SyntaxError.new("Invalid extend directive '@extend': expected expression.") unless value
  585. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath extend directives.",
  586. :line => @line + 1) unless line.children.empty?
  587. offset = line.offset + line.text.index(value).to_i
  588. Tree::ExtendNode.new(parse_interp(value, offset))
  589. elsif directive == "warn"
  590. raise SyntaxError.new("Invalid warn directive '@warn': expected expression.") unless value
  591. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath warn directives.",
  592. :line => @line + 1) unless line.children.empty?
  593. offset = line.offset + line.text.index(value).to_i
  594. Tree::WarnNode.new(parse_script(value, :offset => offset))
  595. elsif directive == "return"
  596. raise SyntaxError.new("Invalid @return: expected expression.") unless value
  597. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath return directives.",
  598. :line => @line + 1) unless line.children.empty?
  599. offset = line.offset + line.text.index(value).to_i
  600. Tree::ReturnNode.new(parse_script(value, :offset => offset))
  601. elsif directive == "charset"
  602. name = value && value[/\A(["'])(.*)\1\Z/, 2] #"
  603. raise SyntaxError.new("Invalid charset directive '@charset': expected string.") unless name
  604. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath charset directives.",
  605. :line => @line + 1) unless line.children.empty?
  606. Tree::CharsetNode.new(name)
  607. elsif directive == "media"
  608. Tree::MediaNode.new(value)
  609. else
  610. Tree::DirectiveNode.new(line.text)
  611. end
  612. end
  613. def parse_for(line, root, text)
  614. var, from_expr, to_name, to_expr = text.scan(/^([^\s]+)\s+from\s+(.+)\s+(to|through)\s+(.+)$/).first
  615. if var.nil? # scan failed, try to figure out why for error message
  616. if text !~ /^[^\s]+/
  617. expected = "variable name"
  618. elsif text !~ /^[^\s]+\s+from\s+.+/
  619. expected = "'from <expr>'"
  620. else
  621. expected = "'to <expr>' or 'through <expr>'"
  622. end
  623. raise SyntaxError.new("Invalid for directive '@for #{text}': expected #{expected}.")
  624. end
  625. raise SyntaxError.new("Invalid variable \"#{var}\".") unless var =~ Script::VALIDATE
  626. var = var[1..-1]
  627. parsed_from = parse_script(from_expr, :offset => line.offset + line.text.index(from_expr))
  628. parsed_to = parse_script(to_expr, :offset => line.offset + line.text.index(to_expr))
  629. Tree::ForNode.new(var, parsed_from, parsed_to, to_name == 'to')
  630. end
  631. def parse_each(line, root, text)
  632. var, list_expr = text.scan(/^([^\s]+)\s+in\s+(.+)$/).first
  633. if var.nil? # scan failed, try to figure out why for error message
  634. if text !~ /^[^\s]+/
  635. expected = "variable name"
  636. elsif text !~ /^[^\s]+\s+from\s+.+/
  637. expected = "'in <expr>'"
  638. end
  639. raise SyntaxError.new("Invalid for directive '@each #{text}': expected #{expected}.")
  640. end
  641. raise SyntaxError.new("Invalid variable \"#{var}\".") unless var =~ Script::VALIDATE
  642. var = var[1..-1]
  643. parsed_list = parse_script(list_expr, :offset => line.offset + line.text.index(list_expr))
  644. Tree::EachNode.new(var, parsed_list)
  645. end
  646. def parse_else(parent, line, text)
  647. previous = parent.children.last
  648. raise SyntaxError.new("@else must come after @if.") unless previous.is_a?(Tree::IfNode)
  649. if text
  650. if text !~ /^if\s+(.+)/
  651. raise SyntaxError.new("Invalid else directive '@else #{text}': expected 'if <expr>'.")
  652. end
  653. expr = parse_script($1, :offset => line.offset + line.text.index($1))
  654. end
  655. node = Tree::IfNode.new(expr)
  656. append_children(node, line.children, false)
  657. previous.add_else node
  658. nil
  659. end
  660. def parse_import(line, value)
  661. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.",
  662. :line => @line + 1) unless line.children.empty?
  663. scanner = StringScanner.new(value)
  664. values = []
  665. loop do
  666. unless node = parse_import_arg(scanner)
  667. raise SyntaxError.new("Invalid @import: expected file to import, was #{scanner.rest.inspect}",
  668. :line => @line)
  669. end
  670. values << node
  671. break unless scanner.scan(/,\s*/)
  672. end
  673. if scanner.scan(/;/)
  674. raise SyntaxError.new("Invalid @import: expected end of line, was \";\".",
  675. :line => @line)
  676. end
  677. return values
  678. end
  679. def parse_import_arg(scanner)
  680. return if scanner.eos?
  681. unless (str = scanner.scan(Sass::SCSS::RX::STRING)) ||
  682. (uri = scanner.scan(Sass::SCSS::RX::URI))
  683. return Tree::ImportNode.new(scanner.scan(/[^,;]+/))
  684. end
  685. val = scanner[1] || scanner[2]
  686. scanner.scan(/\s*/)
  687. if media = scanner.scan(/[^,;].*/)
  688. Tree::DirectiveNode.new("@import #{str || uri} #{media}")
  689. elsif uri
  690. Tree::DirectiveNode.new("@import #{uri}")
  691. elsif val =~ /^http:\/\//
  692. Tree::DirectiveNode.new("@import url(#{val})")
  693. else
  694. Tree::ImportNode.new(val)
  695. end
  696. end
  697. MIXIN_DEF_RE = /^(?:=|@mixin)\s*(#{Sass::SCSS::RX::IDENT})(.*)$/
  698. def parse_mixin_definition(line)
  699. name, arg_string = line.text.scan(MIXIN_DEF_RE).first
  700. raise SyntaxError.new("Invalid mixin \"#{line.text[1..-1]}\".") if name.nil?
  701. offset = line.offset + line.text.size - arg_string.size
  702. args = Script::Parser.new(arg_string.strip, @line, offset, @options).
  703. parse_mixin_definition_arglist
  704. Tree::MixinDefNode.new(name, args)
  705. end
  706. MIXIN_INCLUDE_RE = /^(?:\+|@include)\s*(#{Sass::SCSS::RX::IDENT})(.*)$/
  707. def parse_mixin_include(line, root)
  708. name, arg_string = line.text.scan(MIXIN_INCLUDE_RE).first
  709. raise SyntaxError.new("Invalid mixin include \"#{line.text}\".") if name.nil?
  710. offset = line.offset + line.text.size - arg_string.size
  711. args, keywords = Script::Parser.new(arg_string.strip, @line, offset, @options).
  712. parse_mixin_include_arglist
  713. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath mixin directives.",
  714. :line => @line + 1) unless line.children.empty?
  715. Tree::MixinNode.new(name, args, keywords)
  716. end
  717. FUNCTION_RE = /^@function\s*(#{Sass::SCSS::RX::IDENT})(.*)$/
  718. def parse_function(line, root)
  719. name, arg_string = line.text.scan(FUNCTION_RE).first
  720. raise SyntaxError.new("Invalid function definition \"#{line.text}\".") if name.nil?
  721. offset = line.offset + line.text.size - arg_string.size
  722. args = Script::Parser.new(arg_string.strip, @line, offset, @options).
  723. parse_function_definition_arglist
  724. Tree::FunctionNode.new(name, args)
  725. end
  726. def parse_script(script, options = {})
  727. line = options[:line] || @line
  728. offset = options[:offset] || 0
  729. Script.parse(script, line, offset, @options)
  730. end
  731. def format_comment_text(text, silent)
  732. content = text.split("\n")
  733. if content.first && content.first.strip.empty?
  734. removed_first = true
  735. content.shift
  736. end
  737. return silent ? "//" : "/* */" if content.empty?
  738. content.last.gsub!(%r{ ?\*/ *$}, '')
  739. content.map! {|l| l.gsub!(/^\*( ?)/, '\1') || (l.empty? ? "" : " ") + l}
  740. content.first.gsub!(/^ /, '') unless removed_first
  741. if silent
  742. "//" + content.join("\n//")
  743. else
  744. # The #gsub fixes the case of a trailing */
  745. "/*" + content.join("\n *").gsub(/ \*\Z/, '') + " */"
  746. end
  747. end
  748. def parse_interp(text, offset = 0)
  749. self.class.parse_interp(text, @line, offset, :filename => @filename)
  750. end
  751. # It's important that this have strings (at least)
  752. # at the beginning, the end, and between each Script::Node.
  753. #
  754. # @private
  755. def self.parse_interp(text, line, offset, options)
  756. res = []
  757. rest = Sass::Shared.handle_interpolation text do |scan|
  758. escapes = scan[2].size
  759. res << scan.matched[0...-2 - escapes]
  760. if escapes % 2 == 1
  761. res << "\\" * (escapes - 1) << '#{'
  762. else
  763. res << "\\" * [0, escapes - 1].max
  764. res << Script::Parser.new(
  765. scan, line, offset + scan.pos - scan.matched_size, options).
  766. parse_interpolated
  767. end
  768. end
  769. res << rest
  770. end
  771. end
  772. end