Class: Sass::Script::Parser
- Inherits:
-
Object
- Object
- Sass::Script::Parser
- 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
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
-
.parse(str, line, offset, filename = nil) ⇒ Script::Tree::Node
Parses a SassScript expression.
Instance Method Summary collapse
-
#initialize(str, line, offset, options = {}) ⇒ Parser
constructor
A new instance of Parser.
-
#line ⇒ Integer
The line number of the parser's current position.
-
#offset ⇒ Integer
The column number of the parser's current position.
-
#parse ⇒ Script::Tree::Node
Parses a SassScript expression.
-
#parse_function_definition_arglist ⇒ (Array<Script::Tree::Node>, Script::Tree::Node)
Parses the argument list for a function definition.
-
#parse_interpolated(warn_for_color = false) ⇒ Script::Tree::Node
Parses a SassScript expression within an interpolated segment (
#{}
). -
#parse_mixin_definition_arglist ⇒ (Array<Script::Tree::Node>, Script::Tree::Node)
Parses the argument list for a mixin definition.
-
#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.
-
#parse_string ⇒ Script::Tree::Node
Parse a single string value, possibly containing interpolation.
-
#parse_until(tokens) ⇒ Script::Tree::Node
Parses a SassScript expression, ending it when it encounters one of the given identifier tokens.
Constructor Details
#initialize(str, line, offset, options = {}) ⇒ Parser
Returns a new instance of Parser.
32 33 34 35 36 37 |
# File 'lib/sass/script/parser.rb', line 32
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
end
|
Class Method Details
.parse(str, line, offset, filename = nil) ⇒ Script::Tree::Node
Parses a SassScript expression.
227 228 229 |
# File 'lib/sass/script/parser.rb', line 227
def self.parse(*args)
new(*args).parse
end
|
Instance Method Details
#line ⇒ Integer
The line number of the parser's current position.
11 12 13 |
# File 'lib/sass/script/parser.rb', line 11
def line
@lexer.line
end
|
#offset ⇒ Integer
The column number of the parser's current position.
18 19 20 |
# File 'lib/sass/script/parser.rb', line 18
def offset
@lexer.offset
end
|
#parse ⇒ Script::Tree::Node
Parses a SassScript expression.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sass/script/parser.rb', line 67
def parse
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
|
#parse_function_definition_arglist ⇒ (Array<Script::Tree::Node>, Script::Tree::Node)
Parses the argument list for a function definition.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/sass/script/parser.rb', line 174
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
|
#parse_interpolated(warn_for_color = false) ⇒ Script::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.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sass/script/parser.rb', line 48
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
|
#parse_mixin_definition_arglist ⇒ (Array<Script::Tree::Node>, Script::Tree::Node)
Parses the argument list for a mixin definition.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sass/script/parser.rb', line 144
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
|
#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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/sass/script/parser.rb', line 105
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 |_, 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
|
#parse_string ⇒ Script::Tree::Node
Parse a single string value, possibly containing interpolation. Doesn't assert that the scanner is finished after parsing.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/sass/script/parser.rb', line 204
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
|
#parse_until(tokens) ⇒ Script::Tree::Node
Parses a SassScript expression, ending it when it encounters one of the given identifier tokens.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sass/script/parser.rb', line 84
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
|