Class: Less2Sass::Less::Parser
- Inherits:
-
Object
- Object
- Less2Sass::Less::Parser
- Defined in:
- lib/less2sass/less/parser.rb
Overview
The parser for Less. It parses a Less project into a tree of Tree::Nodes. The project is meant as one or more files.
Instance Method Summary collapse
-
#build_tree(json, parent = nil) ⇒ Less2Sass::Less::Tree::Node
Converts the parsed Less project from JSON object to a Tree::Node representation.
-
#check_syntax ⇒ void
Checks, whether the Less project can be compiled.
-
#initialize(input) ⇒ Parser
constructor
A new instance of Parser.
-
#parse ⇒ Less2Sass::Less::Tree::Node
Parses a Less project.
Constructor Details
#initialize(input) ⇒ Parser
11 12 13 14 |
# File 'lib/less2sass/less/parser.rb', line 11 def initialize(input) @input = input = input == $stdin ? "-stdin \"#{Util.read_stdin_as_multiline_arg}\"" : File.(input) end |
Instance Method Details
#build_tree(json, parent = nil) ⇒ Less2Sass::Less::Tree::Node
Converts the parsed Less project from JSON object to a Tree::Node representation.
Note, that this method recursively calls itself.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/less2sass/less/parser.rb', line 50 def build_tree(json, parent = nil) node = create_node(json['class'], parent) json.each do |k, v| key = instance_var_sym(k) if v.is_a?(Hash) # Is the hash a node, or a simple value? if node?(v) # The node should be referenced under the original key, as well as # in the child nodes, so it's easier to handle conversion # and still be able to traverse the full tree. Since these would be # the same objects, the changes made during the transformation # process would be reflected at both places. subnode = build_tree(v, node) node.instance_variable_set(key, subnode) node << subnode else node.instance_variable_set(key, v) end elsif v.is_a?(Array) if node?(v[0]) # It's an array of nodes subnodes = [] v.each do |item| # Something is very wrong if the item is not another node raise LessASTParserError, item.class.to_s unless node?(item) subnode = build_tree(item, node) subnodes << subnode node << subnode end value = subnodes.length == 1 ? subnodes[0] : subnodes node.instance_variable_set(key, value) else # It's a simple array containing other values than nodes node.instance_variable_set(key, v) end else # Simple key-value pair next if k == 'class' node.instance_variable_set(key, v) end end node end |
#check_syntax ⇒ void
This method returns an undefined value.
Checks, whether the Less project can be compiled.
20 21 22 23 |
# File 'lib/less2sass/less/parser.rb', line 20 def check_syntax response = `lessc --lint #{File.expand_path(@input)} 2>&1 >/dev/null` raise LessCompilationError, response unless response.empty? end |
#parse ⇒ Less2Sass::Less::Tree::Node
Parses a Less project.
30 31 32 33 34 35 36 37 38 |
# File 'lib/less2sass/less/parser.rb', line 30 def parse string_ast = `node #{Util.scope(PARSER)} #{@options}` json_ast = JSON.parse(string_ast) if json_ast['class'] == 'error' raise LessImportNotFoundError, json_ast if json_ast['type'] == 'File' raise LessSyntaxError, json_ast if json_ast['type'] == 'Parse' end build_tree(json_ast) end |