1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] Add an abstract superclass for SassScript parse-tree nodes.

This commit is contained in:
Nathan Weizenbaum 2009-04-25 02:00:36 -07:00
parent ab789485c8
commit 329f87c9a8
9 changed files with 40 additions and 31 deletions

View file

@ -1,4 +1,5 @@
require 'strscan'
require 'sass/script/node'
require 'sass/script/variable'
require 'sass/script/funcall'
require 'sass/script/operation'
@ -42,8 +43,7 @@ module Sass
# Used for error reporting
# @param filename [String] The path to the file in which the SassScript appeared.
# Used for error reporting
# @return [#perform(Sass::Environment)] The root node of the parse tree,
# for example {Operation}
# @return [Script::Node] The root node of the parse tree
def self.parse(value, line, offset, filename = nil)
Parser.parse(value, line, offset, filename)
rescue Sass::SyntaxError => e

View file

@ -6,7 +6,7 @@ module Sass
# A function call either calls one of the functions in {Script::Functions},
# or if no function with the given name exists
# it returns a string representation of the function call.
class Funcall
class Funcall < Node
# The name of the function.
#
# @return [String]
@ -14,11 +14,11 @@ module Sass
# The arguments to the function.
#
# @return [Array<#perform(Sass::Environment)>]
# @return [Array<Script::Node>]
attr_reader :args
# @param name [String] See \{#name}
# @param name [Array<#perform(Sass::Environment)>] See \{#args}
# @param name [Array<Script::Node>] See \{#args}
def initialize(name, args)
@name = name
@args = args

View file

@ -4,7 +4,7 @@ module Sass::Script
# Many of these methods, especially the ones that correspond to SassScript operations,
# are designed to be overridden by subclasses which may change the semantics somewhat.
# The operations listed here are just the defaults.
class Literal
class Literal < Node
require 'sass/script/string'
require 'sass/script/number'
require 'sass/script/color'

14
lib/sass/script/node.rb Normal file
View file

@ -0,0 +1,14 @@
module Sass::Script
# The abstract superclass for SassScript parse tree nodes.
#
# Use \{#perform} to evaluate a parse tree.
class Node
# Evaluates the node.
#
# @param environment [Sass::Environment] The environment in which to evaluate the SassScript
# @return [Literal] The SassScript object that is the value of the SassScript
def perform(environment)
raise NotImplementedError.new("All subclasses of Sass::Script::Node must override #perform.")
end
end
end

View file

@ -8,10 +8,10 @@ require 'sass/script/unary_operation'
module Sass::Script
# A SassScript parse node representing a binary operation,
# such as `!a + !b` or `"foo" + 1`.
class Operation
# @param operand1 [#perform(Sass::Environment)] The parse-tree node
class Operation < Node
# @param operand1 [Script::Node] The parse-tree node
# for the right-hand side of the operator
# @param operand2 [#perform(Sass::Environment)] The parse-tree node
# @param operand2 [Script::Node] The parse-tree node
# for the left-hand side of the operator
# @param operator [Symbol] The operator to perform.
# This should be one of the binary operator names in {Lexer::OPERATORS}

View file

@ -3,9 +3,7 @@ require 'sass/script/lexer'
module Sass
module Script
# The parser for SassScript.
# It parses a string of code into a tree of nodes
# such as {Operation} that can then have #perform
# called to create the resulting {Literal}.
# It parses a string of code into a tree of {Script::Node}s.
class Parser
# @param str [String, StringScanner] The source text to parse
# @param line [Fixnum] The line on which the SassScript appears.
@ -24,8 +22,7 @@ module Sass
# which signals the end of an interpolated segment,
# it returns rather than throwing an error.
#
# @return [#perform(Sass::Environment)] The root node of the parse tree,
# for example {Operation}
# @return [Script::Node] The root node of the parse tree
# @raise [Sass::SyntaxError] if the expression isn't valid SassScript
def parse_interpolated
expr = assert_expr :expr
@ -35,8 +32,7 @@ module Sass
# Parses a SassScript expression.
#
# @return [#perform(Sass::Environment)] The root node of the parse tree,
# for example {Operation}
# @return [Script::Node] The root node of the parse tree
# @raise [Sass::SyntaxError] if the expression isn't valid SassScript
def parse
expr = assert_expr :expr
@ -47,8 +43,7 @@ module Sass
# Parses a SassScript expression.
#
# @call-seq parse(str, line, offset, filename = nil)
# @return [#perform(Sass::Environment)] The root node of the parse tree,
# for example {Operation}
# @return [Script::Node] The root node of the parse tree
# @see Parser#initialize
# @see Parser#parse
def self.parse(*args)

View file

@ -3,8 +3,8 @@ module Sass::Script
# such as `-!b` or `not true`.
#
# Currently only `-`, `/`, and `not` are unary operators.
class UnaryOperation
# @param operand [#perform(Sass::Environment)] The parse-tree node
class UnaryOperation < Node
# @param operand [Script::Node] The parse-tree node
# for the object of the operator
# @param operator [Symbol] The operator to perform
def initialize(operand, operator)

View file

@ -1,7 +1,7 @@
module Sass
module Script
# A SassScript parse node representing a variable.
class Variable
class Variable < Node
# The name of the variable.
#
# @return [String]

View file

@ -6,7 +6,7 @@ module Sass
# and for loops and so forth,
# in addition to nodes for CSS rules and properties.
#
# However, {Node#perform} returns a different sort of tree.
# However, {Tree::Node#perform} returns a different sort of tree.
# This tree maps more closely to the resulting CSS document
# than it does to the original Sass document.
# It still has nodes for CSS rules and properties,
@ -20,7 +20,7 @@ module Sass
class Node
# The child nodes of this node.
#
# @return [Array<Node>]
# @return [Array<Tree::Node>]
attr_accessor :children
# The line of the document on which this node appeared.
@ -43,7 +43,7 @@ module Sass
# Appends a child to the node.
#
# @param child [Node] The child node
# @param child [Tree::Node] The child node
# @raise [Sass::SyntaxError] if `child` is invalid
# @see #invalid_child?
def <<(child)
@ -55,14 +55,14 @@ module Sass
# Return the last child node.
#
# We need this because {Node} duck types as an Array for {Sass::Engine}.
# We need this because {Tree::Node} duck types as an Array for {Sass::Engine}.
#
# @return [Node] The last child node
# @return [Tree::Node] The last child node
def last
children.last
end
# Compares this node and another object (only other {Node}s will be equal).
# Compares this node and another object (only other {Tree::Node}s will be equal).
# This does a structural comparison;
# if the contents of the nodes and all the child nodes are equivalent,
# then the nodes are as well.
@ -107,7 +107,7 @@ module Sass
#
# @param environment [Sass::Environment] The lexical environment containing
# variable and mixin values
# @return [Node] The resulting tree of static nodes
# @return [Tree::Node] The resulting tree of static nodes
# @raise [Sass::SyntaxError] if some element of the tree is invalid
# @see Sass::Tree
def perform(environment)
@ -124,7 +124,7 @@ module Sass
#
# @param environment [Sass::Environment] The lexical environment containing
# variable and mixin values
# @return [Node, Array<Node>] The resulting static nodes
# @return [Tree::Node, Array<Tree::Node>] The resulting static nodes
# @see #perform
# @see Sass::Tree
def _perform(environment)
@ -148,7 +148,7 @@ module Sass
#
# @param environment [Sass::Environment] The lexical environment containing
# variable and mixin values
# @return [Array<Node>] The resulting static nodes
# @return [Array<Tree::Node>] The resulting static nodes
def perform_children(environment)
children.map {|c| c.perform(environment)}.flatten
end
@ -191,7 +191,7 @@ module Sass
# This is expected to be overriden by subclasses
# for which some children are invalid.
#
# @param child [Node] A potential child node
# @param child [Tree::Node] A potential child node
# @return [Boolean, String] Whether or not the child node is valid,
# as well as the error message to display if it is invalid
def invalid_child?(child)