module Yadriggy

Yadriggy is a platform for building embedded DSLs.

It means mistletoe in Japanese.

Copyright (C) 2017- Shigeru Chiba. All rights reserved.

Copyright (C) 2017- Shigeru Chiba. All rights reserved.

Copyright (C) 2017- Shigeru Chiba. All rights reserved.

Copyright (C) 2017- Shigeru Chiba. All rights reserved.

Copyright (C) 2017- Shigeru Chiba. All rights reserved.

Copyright (C) 2017- Shigeru Chiba. All rights reserved.

Constants

DynType

Dynamic type.

Undef

Undefined value.

VERSION
Void

Void type.

Public Class Methods

debug() click to toggle source

Current debug level (0, 1, or 2). @return [Integer] the current level.

# File lib/yadriggy.rb, line 25
def self.debug() @@debug end
debug=(level) click to toggle source

Sets the current debug level. @param [Integer] level the debug level.

# File lib/yadriggy.rb, line 29
def self.debug=(level)
  @@debug = level
end
define_syntax(&block) click to toggle source

Defines syntax and returns a {Syntax} object. {Yadriggy#define_syntax} is not available in a method body. @param [Proc] block the syntax definition. @return [Syntax] the defined syntax.

# File lib/yadriggy/syntax.rb, line 11
def self.define_syntax(&block)
  ast = reify(block)
  if Syntax.check_syntax(ast.tree)
    Syntax.new(ast)
  else
    raise Syntax.last_error
  end
end
reify(proc = nil, &block) click to toggle source

Gets the abstract syntax tree (AST) of the given procedure `proc`. If `proc` is nil, the block argument is converted into an abstract syntax tree. The returned {ASTree} object is a container holding not only an AST but also other data. To get an AST, call {ASTree#tree} on the {ASTree} object.

{ASTree#reify} on the {ASTree} object obtains the ASTs of other procs and methods. It returns the same AST for the same proc or method since it records ASTs obtained before. The table of the recorded ASTs are shared among {ASTree} objects. Every call to {Yadriggy#reify} on {Yadriggy} makes a new table. Hence,

“` ast1 = Yadriggy.reify(proc1) ast2 = ast.reify(proc2) a1 = Yadriggy.reify(proc1) # a1 != ast1 a2 = a1.reify(proc2) # a2 != ast2 b2 = a1.reify(proc2) # b2 == a2 “`

Although `ast1` and `a1`, and `ast2` and `a2` are different copies of the AST of the same proc, `a2` and `b2` refer to the same AST.

@param [Method|UnboundMethod|Proc] proc the procedure. @yield the block is used as the procedure if `proc` is `nil`. @return [ASTree|nil] the abstract syntax tree.

@see ASTree#reify

# File lib/yadriggy/ast.rb, line 35
def self.reify(proc = nil, &block)
  code = proc || block
  return nil if code.nil?
  # a is used only for bootstrapping.
  a = ASTree.new(ASTreeTable.new, code, '?', [:zsuper])
  a.astrees.delete(code)
  a.reify(code)
end
reset_pry() click to toggle source

Discards all the code given to Pry before. This should be called when the code given before includes a syntax error and hence {reify} cannot obtain an abstract syntax tree.

# File lib/yadriggy/source_code.rb, line 32
def self.reset_pry
  SourceCode.reset_pry
end

Private Class Methods

simpler_pretty_print(pp, obj, key) click to toggle source
# File lib/yadriggy/ast.rb, line 1892
def self.simpler_pretty_print(pp, obj, key)
  pp.object_address_group(obj) do
    obj.instance_variables.each do |v|
      pp.breakable
      v = v.to_s if Symbol === v
      pp.text(v)
      pp.text('=')
      pp.group(1) do
        if v == key
          pp.text('...')
        else
          pp.breakable
          pp.pp(obj.instance_eval(v))
        end
      end
    end
  end
end