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.

### Methods

line new parse parse parse_interpolated parse_mixin_definition_arglist parse_mixin_include_arglist parse_until precedence_of

### Constants

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.

### Public Class methods

new(str, line, offset, options = {})

@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}

[Source]

        # 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

parse(*args)

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

[Source]

         # File lib/sass/script/parser.rb, line 119
    119:       def self.parse(*args)
    120:         new(*args).parse
    121:       end

precedence_of(op)

Returns an integer representing the precedence of the given operator. A lower integer indicates a looser binding.

@private

[Source]

         # 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

### Public Instance methods

line()

The line number of the parser‘s current position.

@return [Fixnum]

[Source]

        # File lib/sass/script/parser.rb, line 11
    11:       def line
    12:         @lexer.line
    13:       end

parse()

Parses a SassScript expression.

@return [Script::Node] The root node of the parse tree @raise [Sass::SyntaxError] if the expression isn‘t valid SassScript

[Source]

        # 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

parse_interpolated()

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

[Source]

        # 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

parse_mixin_definition_arglist()

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

[Source]

         # 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

parse_mixin_include_arglist()

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

[Source]

        # 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

parse_until(tokens)

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

[Source]

        # 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

[Validate]