Sha256: 5c98dc28fad5b643c9c3e58ed0940407abff59af5eea0e6d07f4f76e5fea5dff

Contents?: true

Size: 1.64 KB

Versions: 37

Compression:

Stored size: 1.64 KB

Contents

#!/usr/bin/env ruby
$LOAD_PATH.unshift(
  File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib'))

require 'optparse'
require 'bel_parser'

options = {}
OptionParser.new do |opts|
  opts.banner = <<-USAGE.gsub(/^ {4}/, '')
    Debug the AST of BEL Script.

    Read from a BEL Script file.
    usage: #$PROGRAM_NAME --file [FILE]

    Read BEL Script from standard input.
    usage: #$PROGRAM_NAME
  USAGE

  opts.on('-f', '--file FILE', 'BEL script file to read.') do |bel|
    options[:file] = bel
  end

  opts.on('-c', '--[no-]complete', 'Output only complete AST.') do |c|
    options[:complete] = c
  end
end.parse!

file = options[:file]
io   =
  if file
    File.open(file, external_encoding: 'UTF-8')
  else
    $stdin
  end

class ::AST::Node

  def _metadata
    ivars = instance_variables - [:@type, :@children, :@hash]
    ivars.map { |iv| [iv, instance_variable_get(iv)] }.to_s
  end
  private :_metadata

  def to_sexp(indent=0)
    indented = "  " * indent
    sexp = "#{indented}(#{fancy_type} #{_metadata}"

    first_node_child = children.index do |child|
      child.is_a?(::AST::Node) || child.is_a?(Array)
    end || children.count

    children.each_with_index do |child, idx|
      if child.is_a?(::AST::Node) && idx >= first_node_child
        sexp << "\n#{child.to_sexp(indent + 1)}"
      else
        sexp << " #{child.inspect}"
      end
    end

    sexp << ")"

    sexp
  end
end

BELParser::ASTGenerator.new(io).each do |result|
  line_number, line, asts = result
  puts line
  asts.each do |ast|
    if ast
      next if options[:complete] && ast.incomplete?
      puts ast.to_sexp(1)
    else
      puts '  (null)'
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
bel_parser-1.1.6-java bin/bel2_debug_ast
bel_parser-1.1.6 bin/bel2_debug_ast
bel_parser-1.1.5 bin/bel2_debug_ast
bel_parser-1.1.4-java bin/bel2_debug_ast
bel_parser-1.1.4 bin/bel2_debug_ast
bel_parser-1.1.3-java bin/bel2_debug_ast
bel_parser-1.1.3 bin/bel2_debug_ast
bel_parser-1.1.2-java bin/bel2_debug_ast
bel_parser-1.1.2 bin/bel2_debug_ast
bel_parser-1.1.1-java bin/bel2_debug_ast
bel_parser-1.1.1 bin/bel2_debug_ast
bel_parser-1.0.8-java bin/bel2_debug_ast
bel_parser-1.0.8 bin/bel2_debug_ast
bel_parser-1.0.7-java bin/bel2_debug_ast
bel_parser-1.0.7 bin/bel2_debug_ast
bel_parser-1.0.6-java bin/bel2_debug_ast
bel_parser-1.0.6 bin/bel2_debug_ast
bel_parser-1.0.5-java bin/bel2_debug_ast
bel_parser-1.0.5 bin/bel2_debug_ast
bel_parser-1.0.4-java bin/bel2_debug_ast