class SpdxParser

Constants

SKIP_PARENS

Public Class Methods

parse(data) click to toggle source
# File lib/spdx_parser.rb, line 13
def self.parse(data)
  data ||= ""
  parse_tree(data)
end
parse_licenses(data) click to toggle source
# File lib/spdx_parser.rb, line 18
def self.parse_licenses(data)
  tree = parse_tree(data)
  tree.get_licenses
end

Private Class Methods

clean(root_node) click to toggle source
# File lib/spdx_parser.rb, line 32
                     def self.clean(root_node)
  root_node.elements&.delete_if { |node| node.instance_of?(Treetop::Runtime::SyntaxNode) }
end
parse_tree(data) click to toggle source
# File lib/spdx_parser.rb, line 23
                     def self.parse_tree(data)
  parser = SpdxGrammarParser.new # The generated grammar parser is not thread safe

  tree = parser.parse(data)
  raise SpdxGrammar::SpdxParseError, "Unable to parse expression '#{data}'. Parse error at offset: #{parser.index}" if tree.nil?

  prune(tree)
end
prune(root_node) click to toggle source
# File lib/spdx_parser.rb, line 36
                     def self.prune(root_node)
  clean(root_node)

  root_node.elements&.each_with_index do |node, i|
    case node
    when SpdxGrammar::GroupedExpression, SpdxGrammar::Operand
      clean(node)
      child = node.elements[0]
      child.parent = root_node
      root_node.elements[i] = child

      case child
      when SpdxGrammar::GroupedExpression, SpdxGrammar::Operand
        # re-prune if child's child is a GroupedExpression or Operand
        prune(root_node)
      else
        prune(child)
      end
    else
      prune(node)
    end
  end

  case root_node
  when SpdxGrammar::GroupedExpression
    child = root_node.elements[0]
    child.parent = root_node.parent

    return child
  end

  root_node
end