sbyc 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,25 +1,26 @@
1
- module CodeTree
2
- module Name2X
3
- class ModuleDelegate < Delegate
1
+ module SByC
2
+ module CodeTree
3
+ module Name2X
4
+ class ModuleDelegate < Delegate
4
5
 
5
- # Overrides the default behavior to use const_get instead.
6
- def fetch(name)
7
- name = name2name(name)
8
- delegate.const_defined?(name) ? delegate.const_get(name) : nil
9
- rescue NameError
10
- nil
11
- end
6
+ # Overrides the default behavior to use const_get instead.
7
+ def fetch(name)
8
+ name = name2name(name)
9
+ delegate.const_defined?(name) ? delegate.const_get(name) : nil
10
+ rescue NameError
11
+ nil
12
+ end
12
13
 
13
- # Capitalize a name
14
- def name2name(name)
15
- src = name.to_s
16
- src.gsub!(/[^a-zA-Z\s]/," ")
17
- src = " " + src.split.join(" ")
18
- src.gsub!(/ (.)/) { $1.upcase }
19
- src.to_sym
20
- end
14
+ # Capitalize a name
15
+ def name2name(name)
16
+ src = name.to_s
17
+ src.gsub!(/[^a-zA-Z\s]/," ")
18
+ src = " " + src.split.join(" ")
19
+ src.gsub!(/ (.)/) { $1.upcase }
20
+ src.to_sym
21
+ end
21
22
 
22
- end # class ModuleDelegate
23
- end # module Name2X
24
- end # module CodeTree
25
-
23
+ end # class ModuleDelegate
24
+ end # module Name2X
25
+ end # module CodeTree
26
+ end # module SByC
@@ -1,124 +1,126 @@
1
- module CodeTree
2
- class ProcParser
3
- #
4
- # Collector when :multiline option is set to true.
5
- #
6
- class Collector
1
+ module SByC
2
+ module CodeTree
3
+ class ProcParser
4
+ #
5
+ # Collector when :multiline option is set to true.
6
+ #
7
+ class Collector
7
8
 
8
- # Which nodes are kept?
9
- attr_reader :kept
9
+ # Which nodes are kept?
10
+ attr_reader :kept
10
11
 
11
- # Creates a collector instance
12
- def initialize
13
- @kept = []
14
- end
12
+ # Creates a collector instance
13
+ def initialize
14
+ @kept = []
15
+ end
15
16
 
16
- # Callback when a new Expr is created
17
- def built(who, children)
18
- @kept << who
19
- children.each{|c| @kept.delete_if{|k| k.__id__ == c.__id__}}
20
- end
17
+ # Callback when a new Expr is created
18
+ def built(who, children)
19
+ @kept << who
20
+ children.each{|c| @kept.delete_if{|k| k.__id__ == c.__id__}}
21
+ end
21
22
 
22
- # Returns expressions as an array of AstNodes
23
- def to_a
24
- kept.collect{|c| c.__to_functional_code}
25
- end
23
+ # Returns expressions as an array of AstNodes
24
+ def to_a
25
+ kept.collect{|c| c.__to_functional_code}
26
+ end
26
27
 
27
- end # class Collector
28
+ end # class Collector
28
29
 
29
- # An expression
30
- class Expr
30
+ # An expression
31
+ class Expr
31
32
 
32
- # Methods that we keep
33
- KEPT_METHODS = [ "__send__", "__id__", "instance_eval", "initialize", "object_id",
34
- "singleton_method_added", "singleton_method_undefined", "method_missing",
35
- "__evaluate__", "coerce", "kind_of?"]
33
+ # Methods that we keep
34
+ KEPT_METHODS = [ "__send__", "__id__", "instance_eval", "initialize", "object_id",
35
+ "singleton_method_added", "singleton_method_undefined", "method_missing",
36
+ "__evaluate__", "coerce", "kind_of?"]
36
37
 
37
- class << self
38
- def __clean_scope__
39
- # Removes all methods that are not needed to the class
40
- (instance_methods + private_instance_methods).each do |m|
41
- m_to_s = m.to_s
42
- undef_method(m_to_s.to_sym) unless ('__' == m_to_s[0..1]) or KEPT_METHODS.include?(m_to_s)
38
+ class << self
39
+ def __clean_scope__
40
+ # Removes all methods that are not needed to the class
41
+ (instance_methods + private_instance_methods).each do |m|
42
+ m_to_s = m.to_s
43
+ undef_method(m_to_s.to_sym) unless ('__' == m_to_s[0..1]) or KEPT_METHODS.include?(m_to_s)
44
+ end
43
45
  end
44
46
  end
45
- end
46
47
 
47
- # Creates an expression instance
48
- def initialize(name, children, collector)
49
- class << self; __clean_scope__; end
50
- @name, @children, @collector = name, children, collector
51
- if @collector and @name
52
- @collector.built(self, children)
48
+ # Creates an expression instance
49
+ def initialize(name, children, collector)
50
+ class << self; __clean_scope__; end
51
+ @name, @children, @collector = name, children, collector
52
+ if @collector and @name
53
+ @collector.built(self, children)
54
+ end
53
55
  end
54
- end
55
56
 
56
- # Returns the associated functional code
57
- def __to_functional_code
58
- children = (@children || []).collect{|c|
59
- c.kind_of?(Expr) ? c.__to_functional_code : AstNode.coerce(c)
60
- }
61
- CodeTree::AstNode.coerce([@name, children])
62
- end
63
- alias :inspect :__to_functional_code
57
+ # Returns the associated functional code
58
+ def __to_functional_code
59
+ children = (@children || []).collect{|c|
60
+ c.kind_of?(Expr) ? c.__to_functional_code : AstNode.coerce(c)
61
+ }
62
+ CodeTree::AstNode.coerce([@name, children])
63
+ end
64
+ alias :inspect :__to_functional_code
64
65
 
65
- # Called when a method is missing
66
- def method_missing(name, *args)
67
- if @name.nil?
68
- if name == :[]
69
- Expr.new(:'?', args, @collector)
70
- elsif args.empty?
71
- Expr.new(:'?', [name], @collector)
66
+ # Called when a method is missing
67
+ def method_missing(name, *args)
68
+ if @name.nil?
69
+ if name == :[]
70
+ Expr.new(:'?', args, @collector)
71
+ elsif args.empty?
72
+ Expr.new(:'?', [name], @collector)
73
+ else
74
+ Expr.new(name, args, @collector)
75
+ end
72
76
  else
77
+ args.unshift(self)
73
78
  Expr.new(name, args, @collector)
74
79
  end
75
- else
76
- args.unshift(self)
77
- Expr.new(name, args, @collector)
78
80
  end
79
- end
80
81
 
81
- def coerce(other)
82
- [self, other]
83
- end
82
+ def coerce(other)
83
+ [self, other]
84
+ end
84
85
 
85
- end # class Expr
86
+ end # class Expr
86
87
 
87
- # Parses a Proc object
88
- def self.parse_proc(block, options = {})
89
- collector = options[:multiline] ? Collector.new : nil
90
- e = case block.arity
91
- when -1, 0
92
- expr = Expr.new(nil, nil, collector)
93
- expr.instance_eval(&block)
94
- when 1
95
- block.call(Expr.new(nil, nil, collector))
96
- else
97
- raise ArgumentError, "Unexpected block arity #{block.arity}"
98
- end
99
- return collector.to_a if collector
100
- case e
101
- when Expr
102
- e.__to_functional_code
103
- else
104
- CodeTree::AstNode.coerce(e)
88
+ # Parses a Proc object
89
+ def self.parse_proc(block, options = {})
90
+ collector = options[:multiline] ? Collector.new : nil
91
+ e = case block.arity
92
+ when -1, 0
93
+ expr = Expr.new(nil, nil, collector)
94
+ expr.instance_eval(&block)
95
+ when 1
96
+ block.call(Expr.new(nil, nil, collector))
97
+ else
98
+ raise ArgumentError, "Unexpected block arity #{block.arity}"
99
+ end
100
+ return collector.to_a if collector
101
+ case e
102
+ when Expr
103
+ e.__to_functional_code
104
+ else
105
+ CodeTree::AstNode.coerce(e)
106
+ end
105
107
  end
106
- end
107
108
 
108
- # Parses a block
109
- def self.parse(code = nil, options = {}, &block)
110
- block = code || block
111
- case block
112
- when Proc
113
- parse_proc(block, options)
114
- when AstNode
115
- block
116
- when String
117
- parse(Kernel.eval("proc{ #{block} }"), options)
118
- else
119
- raise ArgumentError, "Unable to parse #{block}"
109
+ # Parses a block
110
+ def self.parse(code = nil, options = {}, &block)
111
+ block = code || block
112
+ case block
113
+ when Proc
114
+ parse_proc(block, options)
115
+ when AstNode
116
+ block
117
+ when String
118
+ parse(Kernel.eval("proc{ #{block} }"), options)
119
+ else
120
+ raise ArgumentError, "Unable to parse #{block}"
121
+ end
120
122
  end
121
- end
122
123
 
123
- end # class ProcParser
124
- end # module CodeTree
124
+ end # class ProcParser
125
+ end # module CodeTree
126
+ end # module SByC
@@ -1,80 +1,82 @@
1
- module CodeTree
2
- module Producing
3
- class Producer
1
+ module SByC
2
+ module CodeTree
3
+ module Producing
4
+ class Producer
4
5
 
5
- # Rules kepts by node function
6
- attr_reader :rules
6
+ # Rules kepts by node function
7
+ attr_reader :rules
7
8
 
8
- # Extension options.
9
- attr_reader :extension_options
9
+ # Extension options.
10
+ attr_reader :extension_options
10
11
 
11
- # Creates a producer instance
12
- def initialize(default_rules = true)
13
- @rules = {}
14
- @extension_options = {}
15
- if default_rules
16
- rule(:_) {|r,node| node.literal}
17
- rule("*"){|r,node| r.apply(node.children)}
12
+ # Creates a producer instance
13
+ def initialize(default_rules = true)
14
+ @rules = {}
15
+ @extension_options = {}
16
+ if default_rules
17
+ rule(:_) {|r,node| node.literal}
18
+ rule("*"){|r,node| r.apply(node.children)}
19
+ end
20
+ yield(self) if block_given?
18
21
  end
19
- yield(self) if block_given?
20
- end
21
22
 
22
- # Adds an extension module
23
- def add_extension(mod, options = {})
24
- self.extend(mod)
25
- options = mod.send(:prepare_options, options || {}) if mod.respond_to?(:prepare_options)
26
- extension_options[mod] = options
27
- self
28
- end
23
+ # Adds an extension module
24
+ def add_extension(mod, options = {})
25
+ self.extend(mod)
26
+ options = mod.send(:prepare_options, options || {}) if mod.respond_to?(:prepare_options)
27
+ extension_options[mod] = options
28
+ self
29
+ end
29
30
 
30
- # Adds a rule
31
- def rule(function_name, &block)
32
- rules[function_name] = block
33
- end
31
+ # Adds a rule
32
+ def rule(function_name, &block)
33
+ rules[function_name] = block
34
+ end
34
35
 
35
- # Applies on some arguments
36
- def apply(*args)
37
- case args = apply_args_conventions(*args)
38
- when CodeTree::AstNode
39
- apply_on_node(args)
40
- when Array
41
- args.collect{|c| apply(c)}
42
- else
43
- args
36
+ # Applies on some arguments
37
+ def apply(*args)
38
+ case args = apply_args_conventions(*args)
39
+ when CodeTree::AstNode
40
+ apply_on_node(args)
41
+ when Array
42
+ args.collect{|c| apply(c)}
43
+ else
44
+ args
45
+ end
44
46
  end
45
- end
46
47
 
47
- # Applies on a given node
48
- def apply_on_node(node)
49
- func = node.function
50
- if rules.key?(func)
51
- @rules[func].call(self, node)
52
- elsif rules.key?("*")
53
- @rules["*"].call(self, node)
54
- else
55
- nil
48
+ # Applies on a given node
49
+ def apply_on_node(node)
50
+ func = node.function
51
+ if rules.key?(func)
52
+ @rules[func].call(self, node)
53
+ elsif rules.key?("*")
54
+ @rules["*"].call(self, node)
55
+ else
56
+ nil
57
+ end
56
58
  end
57
- end
58
59
 
59
- # Applies arument conventions allowed by _apply_
60
- def apply_args_conventions(*args)
61
- if args.size == 1 and args[0].kind_of?(CodeTree::AstNode)
62
- args[0]
63
- elsif args.size > 1 and args[0].kind_of?(Symbol)
64
- function = args.shift
65
- children = args.collect{|c| apply_args_conventions(c)}.flatten
66
- CodeTree::AstNode.coerce([function, children])
67
- elsif args.all?{|a| a.kind_of?(CodeTree::AstNode)}
68
- args
69
- elsif args.size == 1
70
- args[0]
71
- else
72
- raise ArgumentError, "Unable to apply on #{args.inspect} (#{args.size})", caller
60
+ # Applies arument conventions allowed by _apply_
61
+ def apply_args_conventions(*args)
62
+ if args.size == 1 and args[0].kind_of?(CodeTree::AstNode)
63
+ args[0]
64
+ elsif args.size > 1 and args[0].kind_of?(Symbol)
65
+ function = args.shift
66
+ children = args.collect{|c| apply_args_conventions(c)}.flatten
67
+ CodeTree::AstNode.coerce([function, children])
68
+ elsif args.all?{|a| a.kind_of?(CodeTree::AstNode)}
69
+ args
70
+ elsif args.size == 1
71
+ args[0]
72
+ else
73
+ raise ArgumentError, "Unable to apply on #{args.inspect} (#{args.size})", caller
74
+ end
73
75
  end
74
- end
75
76
 
76
- private :apply_on_node
77
- private :apply_args_conventions
78
- end # class Producer
79
- end # module Producing
80
- end # module CodeTree
77
+ private :apply_on_node
78
+ private :apply_args_conventions
79
+ end # class Producer
80
+ end # module Producing
81
+ end # module CodeTree
82
+ end # module SByC
@@ -1,77 +1,79 @@
1
- module CodeTree
2
- module Producing
3
- module TracingMethods
1
+ module SByC
2
+ module CodeTree
3
+ module Producing
4
+ module TracingMethods
4
5
 
5
- # Returns the default options
6
- def self.default_options
7
- {:indent_support => true,
8
- :indent_string => " ",
9
- :newline_support => true,
10
- :io => STDOUT}
11
- end
6
+ # Returns the default options
7
+ def self.default_options
8
+ {:indent_support => true,
9
+ :indent_string => " ",
10
+ :newline_support => true,
11
+ :io => STDOUT}
12
+ end
12
13
 
13
- # Merge the default options and the options
14
- def self.prepare_options(options)
15
- default_options.merge(options)
16
- end
14
+ # Merge the default options and the options
15
+ def self.prepare_options(options)
16
+ default_options.merge(options)
17
+ end
17
18
 
18
- # Returns tracing extension options
19
- def tracing_options
20
- extension_options[TracingMethods] ||= {}
21
- end
19
+ # Returns tracing extension options
20
+ def tracing_options
21
+ extension_options[TracingMethods] ||= {}
22
+ end
22
23
 
23
- # Is indentation support installed?
24
- def tracing_indent_support?
25
- !!tracing_options[:indent_support]
26
- end
24
+ # Is indentation support installed?
25
+ def tracing_indent_support?
26
+ !!tracing_options[:indent_support]
27
+ end
27
28
 
28
- # Is new line support installed?
29
- def tracing_newline_support?
30
- !!tracing_options[:newline_support]
31
- end
29
+ # Is new line support installed?
30
+ def tracing_newline_support?
31
+ !!tracing_options[:newline_support]
32
+ end
32
33
 
33
- # Returns the string to use for indentation
34
- def tracing_indent_string
35
- tracing_options[:indent_string] ||= " "
36
- end
34
+ # Returns the string to use for indentation
35
+ def tracing_indent_string
36
+ tracing_options[:indent_string] ||= " "
37
+ end
37
38
 
38
- # Returns the current indentation level
39
- def tracing_indent_level
40
- tracing_options[:indent_level] ||= 0
41
- end
39
+ # Returns the current indentation level
40
+ def tracing_indent_level
41
+ tracing_options[:indent_level] ||= 0
42
+ end
42
43
 
43
- # Returns the IO object (actually, any object supporting the << operator)
44
- # that must be used for tracing the Producer.
45
- def tracing_io
46
- tracing_options[:io] ||= STDOUT
47
- end
44
+ # Returns the IO object (actually, any object supporting the << operator)
45
+ # that must be used for tracing the Producer.
46
+ def tracing_io
47
+ tracing_options[:io] ||= STDOUT
48
+ end
48
49
 
49
- # Logs a message on the IO object, without any other side effect (no
50
- # depth increment, for instance).
51
- def trace(message)
52
- message = (tracing_indent_string*tracing_indent_level + message.to_s) if tracing_indent_support? and tracing_indent_level>0
53
- message = "#{message}\n" if tracing_newline_support?
54
- if tracing_io.kind_of?(IO)
55
- tracing_io << message
56
- else
57
- tracing_io << message
50
+ # Logs a message on the IO object, without any other side effect (no
51
+ # depth increment, for instance).
52
+ def trace(message)
53
+ message = (tracing_indent_string*tracing_indent_level + message.to_s) if tracing_indent_support? and tracing_indent_level>0
54
+ message = "#{message}\n" if tracing_newline_support?
55
+ if tracing_io.kind_of?(IO)
56
+ tracing_io << message
57
+ else
58
+ tracing_io << message
59
+ end
58
60
  end
59
- end
60
61
 
61
- # Logs that a rule has been entered and increment the depth
62
- # level
63
- def trace_rule_entered(message)
64
- trace(message)
65
- tracing_options[:indent_level] = tracing_indent_level+1
66
- end
62
+ # Logs that a rule has been entered and increment the depth
63
+ # level
64
+ def trace_rule_entered(message)
65
+ trace(message)
66
+ tracing_options[:indent_level] = tracing_indent_level+1
67
+ end
67
68
 
68
- # Logs that a rule has been exited and decrements the depth
69
- # level
70
- def trace_rule_exited(message)
71
- tracing_options[:indent_level] = tracing_indent_level-1
72
- trace(message)
73
- end
69
+ # Logs that a rule has been exited and decrements the depth
70
+ # level
71
+ def trace_rule_exited(message)
72
+ tracing_options[:indent_level] = tracing_indent_level-1
73
+ trace(message)
74
+ end
74
75
 
75
- end # module TracingMethods
76
- end # module Producing
77
- end # module CodeTree
76
+ end # module TracingMethods
77
+ end # module Producing
78
+ end # module CodeTree
79
+ end # module SByC