Class | Sass::Script::Parser |
In: |
lib/sass/script/parser.rb
|
Parent: | Object |
The parser for SassScript. It parses a string of code into a tree of {Script::Node}s.
PRECEDENCE | = | [ :comma, :single_eq, :concat, :or, :and, [:eq, :neq], [:gt, :gte, :lt, :lte], [:plus, :minus], [:times, :div, :mod], ] | ||
EXPR_NAMES | = | { :string => "string", :default => "expression (e.g. 1px, bold)", :arglist => "mixin argument", :fn_arglist => "function argument", } | It would be possible to have unified assert and try methods, but detecting the method/token difference turns out to be quite expensive. |
@param str [String, StringScanner] The source text to parse @param line [Fixnum] The line on which the SassScript appears.
Used for error reporting
@param offset [Fixnum] The number of characters in on which the SassScript appears.
Used for error reporting
@param options [{Symbol => Object}] An options hash;
see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
# File lib/sass/script/parser.rb, line 22 22: def initialize(str, line, offset, options = {}) 23: @options = options 24: @lexer = lexer_class.new(str, line, offset, options) 25: end
Parses a SassScript expression.
@overload parse(str, line, offset, filename = nil) @return [Script::Node] The root node of the parse tree @see Parser#initialize @see Parser#parse
# File lib/sass/script/parser.rb, line 119 119: def self.parse(*args) 120: new(*args).parse 121: end
Returns an integer representing the precedence of the given operator. A lower integer indicates a looser binding.
@private
# File lib/sass/script/parser.rb, line 137 137: def precedence_of(op) 138: PRECEDENCE.each_with_index do |e, i| 139: return i if Array(e).include?(op) 140: end 141: raise "[BUG] Unknown operator #{op}" 142: end
Parses a SassScript expression.
@return [Script::Node] The root node of the parse tree @raise [Sass::SyntaxError] if the expression isn‘t valid SassScript
# File lib/sass/script/parser.rb, line 48 48: def parse 49: expr = assert_expr :expr 50: assert_done 51: expr.options = @options 52: expr 53: rescue Sass::SyntaxError => e 54: e.modify_backtrace :line => @lexer.line, :filename => @options[:filename] 55: raise e 56: end
Parses a SassScript expression within an interpolated segment (`#{}`). This means that it stops when it comes across an unmatched `}`, which signals the end of an interpolated segment, it returns rather than throwing an error.
@return [Script::Node] The root node of the parse tree @raise [Sass::SyntaxError] if the expression isn‘t valid SassScript
# File lib/sass/script/parser.rb, line 34 34: def parse_interpolated 35: expr = assert_expr :expr 36: assert_tok :end_interpolation 37: expr.options = @options 38: expr 39: rescue Sass::SyntaxError => e 40: e.modify_backtrace :line => @lexer.line, :filename => @options[:filename] 41: raise e 42: end
Parses the argument list for a mixin definition.
@return [Array<Script::Node>] The root nodes of the arguments. @raise [Sass::SyntaxError] if the argument list isn‘t valid SassScript
# File lib/sass/script/parser.rb, line 99 99: def parse_mixin_definition_arglist 100: args = defn_arglist!(false) 101: assert_done 102: 103: args.each do |k, v| 104: k.options = @options 105: v.options = @options if v 106: end 107: args 108: rescue Sass::SyntaxError => e 109: e.modify_backtrace :line => @lexer.line, :filename => @options[:filename] 110: raise e 111: end
Parses the argument list for a mixin include.
@return [Array<Script::Node>] The root nodes of the arguments. @raise [Sass::SyntaxError] if the argument list isn‘t valid SassScript
# File lib/sass/script/parser.rb, line 79 79: def parse_mixin_include_arglist 80: args = [] 81: 82: if try_tok(:lparen) 83: args = arglist || args 84: assert_tok(:rparen) 85: end 86: assert_done 87: 88: args.each {|a| a.options = @options} 89: args 90: rescue Sass::SyntaxError => e 91: e.modify_backtrace :line => @lexer.line, :filename => @options[:filename] 92: raise e 93: end
Parses a SassScript expression, ending it when it encounters one of the given identifier tokens.
@param [include?(String)] A set of strings that delimit the expression. @return [Script::Node] The root node of the parse tree @raise [Sass::SyntaxError] if the expression isn‘t valid SassScript
# File lib/sass/script/parser.rb, line 64 64: def parse_until(tokens) 65: @stop_at = tokens 66: expr = assert_expr :expr 67: assert_done 68: expr.options = @options 69: expr 70: rescue Sass::SyntaxError => e 71: e.modify_backtrace :line => @lexer.line, :filename => @options[:filename] 72: raise e 73: end