Class: Sass::Script::Parser

Inherits:
Object
  • Object
show all
Defined in:
/var/www/sass-pages/.sass/lib/sass/script/parser.rb

Overview

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

Direct Known Subclasses

CssParser

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

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

A new instance of Parser

Parameters:

  • (String, StringScanner) str — The source text to parse
  • (Fixnum) line — The line on which the SassScript appears. Used for error reporting
  • (Fixnum) offset — The number of characters in on which the SassScript appears. Used for error reporting
  • ({Symbol => Object}) options (defaults to: {}) — An options hash; see the Sass options documentation


22
23
24
25
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 22

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)

Parses a SassScript expression.

Returns:

See Also:



139
140
141
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 139

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

Instance Method Details

- (Fixnum) line

The line number of the parser’s current position.

Returns:

  • (Fixnum)


11
12
13
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 11

def line
  @lexer.line
end

- (Script::Node) parse

Parses a SassScript expression.

Returns:

Raises:



48
49
50
51
52
53
54
55
56
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 48

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

- (Array<Script::Node>) parse_function_definition_arglist

Parses the argument list for a function definition.

Returns:

  • (Array<Script::Node>) — The root nodes of the arguments.

Raises:



119
120
121
122
123
124
125
126
127
128
129
130
131
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 119

def parse_function_definition_arglist
  args = defn_arglist!(true)
  assert_done

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

- (Script::Node) 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.

Returns:

Raises:



34
35
36
37
38
39
40
41
42
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 34

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

- (Array<Script::Node>) parse_mixin_definition_arglist

Parses the argument list for a mixin definition.

Returns:

  • (Array<Script::Node>) — The root nodes of the arguments.

Raises:



101
102
103
104
105
106
107
108
109
110
111
112
113
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 101

def parse_mixin_definition_arglist
  args = defn_arglist!(false)
  assert_done

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

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

Parses the argument list for a mixin include.

Returns:

  • ((Array<Script::Node>, {String => Script::Note})) — The root nodes of the arguments. Keyword arguments are in a hash from names to values.

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 81

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

  args.each {|a| a.options = @options}
  keywords.each {|k, v| v.options = @options}
  return args, keywords
rescue Sass::SyntaxError => e
  e.modify_backtrace :line => @lexer.line, :filename => @options[:filename]
  raise e
end

- (Script::Node) parse_until(tokens)

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

Parameters:

  • (#include?(String)) A — set of strings that delimit the expression.

Returns:

Raises:



64
65
66
67
68
69
70
71
72
73
# File '/var/www/sass-pages/.sass/lib/sass/script/parser.rb', line 64

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