Class: Sass::Script::Parser

Inherits:
Object
  • Object
show all
Defined in:
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 collapse

PRECEDENCE =
[
  :comma, :single_eq, :space, :or, :and,
  [:eq, :neq],
  [:gt, :gte, :lt, :lte],
  [:plus, :minus],
  [:times, :div, :mod],
]
ASSOCIATIVE =
[:plus, :times]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



29
30
31
32
# File 'lib/sass/script/parser.rb', line 29

def initialize(str, line, offset, options = {})
  @options = options
  @lexer = lexer_class.new(str, line, offset, options)
end

Class Method Details

.parse(str, line, offset, filename = nil) ⇒ Script::Tree::Node

Parses a SassScript expression.

Returns:

See Also:



176
177
178
# File 'lib/sass/script/parser.rb', line 176

def self.parse(*args)
  new(*args).parse
end

Instance Method Details

#lineFixnum

The line number of the parser's current position.

Returns:

  • (Fixnum)


11
12
13
# File 'lib/sass/script/parser.rb', line 11

def line
  @lexer.line
end

#offsetFixnum

The column number of the parser's current position.

Returns:

  • (Fixnum)


18
19
20
# File 'lib/sass/script/parser.rb', line 18

def offset
  @lexer.offset
end

#parseScript::Tree::Node

Parses a SassScript expression.

Returns:

Raises:



55
56
57
58
59
60
61
62
63
# File 'lib/sass/script/parser.rb', line 55

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

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

Parses the argument list for a function definition.

Returns:

Raises:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sass/script/parser.rb', line 134

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

  args.each do |k, v|
    k.options = @options
    v.options = @options if v
  end
  splat.options = @options if splat
  return args, splat
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

#parse_interpolatedScript::Tree::Node

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.

Returns:

Raises:



41
42
43
44
45
46
47
48
49
# File 'lib/sass/script/parser.rb', line 41

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

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

Parses the argument list for a mixin definition.

Returns:

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sass/script/parser.rb', line 114

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

  args.each do |k, v|
    k.options = @options
    v.options = @options if v
  end
  splat.options = @options if splat
  return args, splat
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

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

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:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sass/script/parser.rb', line 91

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 {|a| a.options = @options}
  keywords.each {|k, v| v.options = @options}
  splat.options = @options if splat
  kwarg_splat.options = @options if kwarg_splat
  return args, keywords, splat, kwarg_splat
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

#parse_stringScript::Tree::Node

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

Returns:

Raises:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sass/script/parser.rb', line 154

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
  expr.options = @options
  @lexer.unpeek!
  expr
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

#parse_until(tokens) ⇒ Script::Tree::Node

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:



71
72
73
74
75
76
77
78
79
80
# File 'lib/sass/script/parser.rb', line 71

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