2008-10-12 22:03:06 -04:00
|
|
|
require 'sass/script/lexer'
|
2008-10-11 07:00:55 -04:00
|
|
|
|
|
|
|
module Sass
|
2008-10-12 22:03:06 -04:00
|
|
|
module Script
|
2008-10-12 23:26:56 -04:00
|
|
|
class Parser
|
2008-12-09 15:09:34 -05:00
|
|
|
def initialize(str, line, offset)
|
|
|
|
@lexer = Lexer.new(str, line, offset)
|
2008-10-11 07:00:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
assert_expr :expr
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse(*args)
|
|
|
|
new(*args).parse
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Defines a simple left-associative production.
|
|
|
|
# name is the name of the production,
|
|
|
|
# sub is the name of the production beneath it,
|
|
|
|
# and ops is a list of operators for this precedence level
|
|
|
|
def self.production(name, sub, *ops)
|
|
|
|
class_eval <<RUBY
|
|
|
|
def #{name}
|
|
|
|
return unless e = #{sub}
|
|
|
|
while tok = try_tok(#{ops.map {|o| o.inspect}.join(', ')})
|
|
|
|
e = Operation.new(e, assert_expr(#{sub.inspect}), tok.first)
|
|
|
|
end
|
|
|
|
e
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.unary(op, sub)
|
|
|
|
class_eval <<RUBY
|
|
|
|
def unary_#{op}
|
|
|
|
return #{sub} unless try_tok(:#{op})
|
|
|
|
UnaryOperation.new(assert_expr(:unary_#{op}), :#{op})
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
production :expr, :concat, :comma
|
|
|
|
|
|
|
|
def concat
|
2008-10-11 17:13:39 -04:00
|
|
|
return unless e = or_expr
|
|
|
|
while sub = or_expr
|
2008-10-11 07:00:55 -04:00
|
|
|
e = Operation.new(e, sub, :concat)
|
|
|
|
end
|
|
|
|
e
|
|
|
|
end
|
|
|
|
|
2008-10-11 17:13:39 -04:00
|
|
|
production :or_expr, :and_expr, :or
|
|
|
|
production :and_expr, :eq_or_neq, :and
|
2008-10-11 17:39:28 -04:00
|
|
|
production :eq_or_neq, :relational, :eq, :neq
|
|
|
|
production :relational, :plus_or_minus, :gt, :gte, :lt, :lte
|
2008-10-11 07:00:55 -04:00
|
|
|
production :plus_or_minus, :times_div_or_mod, :plus, :minus
|
2008-10-11 17:13:39 -04:00
|
|
|
production :times_div_or_mod, :unary_minus, :times, :div, :mod
|
2008-10-11 07:00:55 -04:00
|
|
|
|
|
|
|
unary :minus, :unary_div
|
|
|
|
unary :div, :unary_not # For strings, so /foo/bar works
|
|
|
|
unary :not, :funcall
|
|
|
|
|
|
|
|
def funcall
|
|
|
|
return paren unless name = try_tok(:ident)
|
|
|
|
# An identifier without arguments is just a string
|
2008-12-09 11:58:58 -05:00
|
|
|
unless try_tok(:lparen)
|
|
|
|
warn %Q{WARNING: Implicit strings are deprecated. '#{name.last}' was not quoted. Please add double quotes. E.g. "#{name.last}".}
|
|
|
|
Script::String.new(name.last)
|
|
|
|
else
|
|
|
|
args = arglist || []
|
|
|
|
assert_tok(:rparen)
|
|
|
|
Script::Funcall.new(name.last, args)
|
|
|
|
end
|
2008-10-11 07:00:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def arglist
|
|
|
|
return unless e = concat
|
|
|
|
return [e] unless try_tok(:comma)
|
|
|
|
[e, *arglist]
|
|
|
|
end
|
|
|
|
|
|
|
|
def paren
|
2008-10-12 22:03:06 -04:00
|
|
|
return variable unless try_tok(:lparen)
|
2008-10-11 07:00:55 -04:00
|
|
|
e = assert_expr(:expr)
|
|
|
|
assert_tok(:rparen)
|
|
|
|
return e
|
|
|
|
end
|
|
|
|
|
2008-10-12 22:03:06 -04:00
|
|
|
def variable
|
2008-10-11 07:00:55 -04:00
|
|
|
return literal unless c = try_tok(:const)
|
2008-10-12 23:26:56 -04:00
|
|
|
Variable.new(c.last)
|
2008-10-11 07:00:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def literal
|
|
|
|
(t = try_tok(:string, :number, :color, :bool)) && (return t.last)
|
|
|
|
end
|
|
|
|
|
|
|
|
# It would be possible to have unified #assert and #try methods,
|
|
|
|
# but detecting the method/token difference turns out to be quite expensive.
|
|
|
|
|
|
|
|
def assert_expr(name)
|
|
|
|
(e = send(name)) && (return e)
|
|
|
|
raise Sass::SyntaxError.new("Expected expression, was #{@lexer.done? ? 'end of text' : "#{@lexer.peek.first} token"}.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_tok(*names)
|
|
|
|
(t = try_tok(*names)) && (return t)
|
|
|
|
raise Sass::SyntaxError.new("Expected #{names.join(' or ')} token, was #{@lexer.done? ? 'end of text' : "#{@lexer.peek.first} token"}.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def try_tok(*names)
|
2008-10-14 12:34:43 -04:00
|
|
|
peeked = @lexer.peek
|
|
|
|
peeked && names.include?(peeked.first) && @lexer.token
|
2008-10-11 07:00:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|