Class: Sass::Script::Parser

Inherits:
Object
  • Object
show all
Defined in:
/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb

Overview

The parser for SassScript. It parses a string of code into a tree of Tree::Nodes.

Direct Known Subclasses

CssParser

Constant Summary

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Parser) initialize(str, line, offset, options = {})

Returns a new instance of Parser

Parameters:

  • str (String, StringScanner)

    The source text to parse

  • line (Fixnum)

    The line on which the SassScript appears. Used for error reporting and sourcemap building

  • offset (Fixnum)

    The character (not byte) offset where the script starts in the line. Used for error reporting and sourcemap building

  • options ({Symbol => Object}) (defaults to: {})

    An options hash; see the Sass options documentation. This supports an additional :allow_extra_text option that controls whether the parser throws an error when extra text is encountered after the parsed construct.



33
34
35
36
37
38
39
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 33

def initialize(str, line, offset, options = {})
  @options = options
  @allow_extra_text = options.delete(:allow_extra_text)
  @lexer = lexer_class.new(str, line, offset, options)
  @stop_at = nil
  @css_variable_warning = nil
end

Class Method Details

+ (Script::Tree::Node) parse(value, line, offset, options = {})

Parses a SassScript expression.

Returns:

See Also:



238
239
240
241
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 238

def self.parse(value, line, offset, options = {})
  css_variable = options.delete :css_variable
  new(value, line, offset, options).parse(css_variable)
end

Instance Method Details

- (Fixnum) line

The line number of the parser’s current position.

Returns:

  • (Fixnum)


12
13
14
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 12

def line
  @lexer.line
end

- (Fixnum) offset

The column number of the parser’s current position.

Returns:

  • (Fixnum)


19
20
21
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 19

def offset
  @lexer.offset
end

- (Script::Tree::Node) parse(css_variable = false)

Parses a SassScript expression.

Parameters:

  • css_variable (Boolean) (defaults to: false)

    Whether this is the value of a CSS variable.

Returns:

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 70

def parse(css_variable = false)
  if css_variable
    @css_variable_warning = CssVariableWarning.new
  end

  expr = assert_expr :expr
  assert_done
  expr.options = @options
  check_for_interpolation expr

  if css_variable
    @css_variable_warning.value = expr
  end

  expr
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

- ((Array<Script::Tree::Node>, Script::Tree::Node)) parse_function_definition_arglist

Parses the argument list for a function definition.

Returns:

Raises:



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 186

def parse_function_definition_arglist
  args, splat = defn_arglist!(true)
  assert_done

  args.each do |k, v|
    check_for_interpolation k
    k.options = @options

    if v
      check_for_interpolation v
      v.options = @options
    end
  end

  if splat
    check_for_interpolation splat
    splat.options = @options
  end

  return args, splat
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

- (Script::Tree::Node) parse_interpolated(warn_for_color = false)

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.

Parameters:

  • warn_for_color (Boolean) (defaults to: false)

    Whether raw color values passed to interoplation should cause a warning.

Returns:

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 50

def parse_interpolated(warn_for_color = false)
  # Start two characters back to compensate for #{
  start_pos = Sass::Source::Position.new(line, offset - 2)
  expr = assert_expr :expr
  assert_tok :end_interpolation
  expr = Sass::Script::Tree::Interpolation.new(
    nil, expr, nil, false, false, :warn_for_color => warn_for_color)
  check_for_interpolation expr
  expr.options = @options
  node(expr, start_pos)
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

- ((Array<Script::Tree::Node>, Script::Tree::Node)) parse_mixin_definition_arglist

Parses the argument list for a mixin definition.

Returns:

Raises:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 156

def parse_mixin_definition_arglist
  args, splat = defn_arglist!(false)
  assert_done

  args.each do |k, v|
    check_for_interpolation k
    k.options = @options

    if v
      check_for_interpolation v
      v.options = @options
    end
  end

  if splat
    check_for_interpolation splat
    splat.options = @options
  end

  return args, splat
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

- ((Array<Script::Tree::Node>, {String => Script::Tree::Node}, Script::Tree::Node, Script::Tree::Node)) parse_mixin_include_arglist

Parses the argument list for a mixin include.

The root nodes of the positional arguments, keyword arguments, and splat argument(s). Keyword arguments are in a hash from names to values.

Returns:

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 117

def parse_mixin_include_arglist
  args, keywords = [], {}
  if try_tok(:lparen)
    args, keywords, splat, kwarg_splat = mixin_arglist
    assert_tok(:rparen)
  end
  assert_done

  args.each do |a|
    check_for_interpolation a
    a.options = @options
  end

  keywords.each do |_k, v|
    check_for_interpolation v
    v.options = @options
  end

  if splat
    check_for_interpolation splat
    splat.options = @options
  end

  if kwarg_splat
    check_for_interpolation kwarg_splat
    kwarg_splat.options = @options
  end

  return args, keywords, splat, kwarg_splat
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

- (Script::Tree::Node) parse_string

Parse a single string value, possibly containing interpolation. Doesn’t assert that the scanner is finished after parsing.

Returns:

Raises:



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 216

def parse_string
  unless (peek = @lexer.peek) &&
      (peek.type == :string ||
      (peek.type == :funcall && peek.value.downcase == 'url'))
    lexer.expected!("string")
  end

  expr = assert_expr :funcall
  check_for_interpolation expr
  expr.options = @options
  @lexer.unpeek!
  expr
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

- (Script::Tree::Node) parse_until(tokens)

Parses a SassScript expression, ending it when it encounters one of the given identifier tokens.

Parameters:

  • tokens (#include?(String))

    A set of strings that delimit the expression.

Returns:

Raises:



96
97
98
99
100
101
102
103
104
105
106
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/parser.rb', line 96

def parse_until(tokens)
  @stop_at = tokens
  expr = assert_expr :expr
  assert_done
  expr.options = @options
  check_for_interpolation expr
  expr
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end