packcr 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ require "stringio"
1
2
 
2
3
  class Packcr
3
4
  class Generator
@@ -3,8 +3,9 @@ class Packcr
3
3
  class ActionNode < Packcr::Node
4
4
  attr_accessor :code, :index, :vars, :capts
5
5
 
6
- def initialize
7
- super
6
+ def initialize(code = nil)
7
+ super()
8
+ @code = code
8
9
  self.vars = []
9
10
  self.capts = []
10
11
  end
@@ -47,6 +48,11 @@ class Packcr
47
48
 
48
49
  def link_references(ctx)
49
50
  end
51
+
52
+ def setup_rule(rule)
53
+ @index = rule.codes.length
54
+ rule.codes.push(self)
55
+ end
50
56
  end
51
57
  end
52
58
  end
@@ -3,9 +3,14 @@ class Packcr
3
3
  class AlternateNode < Packcr::Node
4
4
  attr_accessor :nodes
5
5
 
6
- def initialize
7
- super
8
- self.nodes = []
6
+ def initialize(*nodes)
7
+ super()
8
+ self.nodes = nodes
9
+ end
10
+
11
+ def alt(node)
12
+ @nodes << node
13
+ self
9
14
  end
10
15
 
11
16
  def debug_dump(indent = 0)
@@ -3,6 +3,10 @@ class Packcr
3
3
  class CaptureNode < Packcr::Node
4
4
  attr_accessor :expr, :index
5
5
 
6
+ def initialize(expr = nil)
7
+ @expr = expr
8
+ end
9
+
6
10
  def debug_dump(indent = 0)
7
11
  $stdout.print "#{" " * indent}Capture(index:"
8
12
  Packcr.dump_integer_value(index)
@@ -29,6 +33,16 @@ class Packcr
29
33
  def link_references(ctx)
30
34
  expr.link_references(ctx)
31
35
  end
36
+
37
+ def nodes
38
+ [expr]
39
+ end
40
+
41
+ def setup_rule(rule)
42
+ @index = rule.capts.length
43
+ rule.capts << self
44
+ super
45
+ end
32
46
  end
33
47
  end
34
48
  end
@@ -3,6 +3,10 @@ class Packcr
3
3
  class CharclassNode < Packcr::Node
4
4
  attr_accessor :value
5
5
 
6
+ def initialize(value = nil)
7
+ @value = value
8
+ end
9
+
6
10
  def debug_dump(indent = 0)
7
11
  $stdout.print "#{" " * indent}Charclass(value:'"
8
12
  Packcr.dump_escaped_string(value)
@@ -3,8 +3,11 @@ class Packcr
3
3
  class ErrorNode < Packcr::Node
4
4
  attr_accessor :expr, :code, :index, :vars, :capts
5
5
 
6
- def initialize
7
- super
6
+ def initialize(expr = nil, code = nil, index = nil)
7
+ super()
8
+ @expr = expr
9
+ @code = code
10
+ @index = index
8
11
  self.vars = []
9
12
  self.capts = []
10
13
  end
@@ -47,6 +50,16 @@ class Packcr
47
50
  def link_references(ctx)
48
51
  expr.link_references(ctx)
49
52
  end
53
+
54
+ def nodes
55
+ [expr]
56
+ end
57
+
58
+ def setup_rule(rule)
59
+ @index = rule.codes.length
60
+ rule.codes.push(self)
61
+ super
62
+ end
50
63
  end
51
64
  end
52
65
  end
@@ -3,6 +3,10 @@ class Packcr
3
3
  class ExpandNode < Packcr::Node
4
4
  attr_accessor :index, :line, :col
5
5
 
6
+ def initialize(index = nil)
7
+ @index = index
8
+ end
9
+
6
10
  def debug_dump(indent = 0)
7
11
  $stdout.print "#{" " * indent}Expand(index:"
8
12
  Packcr.dump_integer_value(index)
@@ -3,9 +3,10 @@ class Packcr
3
3
  class PredicateNode < Packcr::Node
4
4
  attr_accessor :neg, :expr
5
5
 
6
- def initialize
7
- super
8
- self.neg = false
6
+ def initialize(expr = nil, neg = false)
7
+ super()
8
+ @expr = expr
9
+ @neg = neg
9
10
  end
10
11
 
11
12
  def debug_dump(indent = 0)
@@ -46,6 +47,10 @@ class Packcr
46
47
  def link_references(ctx)
47
48
  expr.link_references(ctx)
48
49
  end
50
+
51
+ def nodes
52
+ [expr]
53
+ end
49
54
  end
50
55
  end
51
56
  end
@@ -3,10 +3,11 @@ class Packcr
3
3
  class QuantityNode < Packcr::Node
4
4
  attr_accessor :min, :max, :expr
5
5
 
6
- def initialize
7
- super
8
- self.min = 0
9
- self.max = 0
6
+ def initialize(expr = nil, min = 0, max = 0)
7
+ super()
8
+ @expr = expr
9
+ @min = min
10
+ @max = max
10
11
  end
11
12
 
12
13
  def debug_dump(indent = 0)
@@ -50,6 +51,10 @@ class Packcr
50
51
  def link_references(ctx)
51
52
  expr.link_references(ctx)
52
53
  end
54
+
55
+ def nodes
56
+ [expr]
57
+ end
53
58
  end
54
59
  end
55
60
  end
@@ -3,6 +3,13 @@ class Packcr
3
3
  class ReferenceNode < Packcr::Node
4
4
  attr_accessor :var, :index, :name, :rule, :line, :col
5
5
 
6
+ def initialize(name = nil, var = nil, line = nil, col = nil)
7
+ @name = name
8
+ @var = var
9
+ @line = line
10
+ @col = col
11
+ end
12
+
6
13
  def debug_dump(indent = 0)
7
14
  $stdout.print "#{" " * indent}Reference(var:'#{var || "(null)"}', index:"
8
15
  Packcr.dump_integer_value(index)
@@ -14,6 +21,21 @@ class Packcr
14
21
  Packcr::CODE_REACH__BOTH
15
22
  end
16
23
 
24
+ def setup_rule(rule)
25
+ return unless var
26
+ i = rule.vars.index do |ref|
27
+ unless ref.is_a?(Packcr::Node::ReferenceNode)
28
+ raise "Unexpected node type: #{ref.class}"
29
+ end
30
+ var == ref.var
31
+ end
32
+ if !i
33
+ i = rule.vars.length
34
+ rule.vars << self
35
+ end
36
+ @index = i
37
+ end
38
+
17
39
  def verify_variables(vars)
18
40
  return if index.nil?
19
41
 
@@ -3,12 +3,16 @@ class Packcr
3
3
  class RuleNode < Packcr::Node
4
4
  attr_accessor :codes, :name, :expr, :ref, :vars, :capts, :line, :col
5
5
 
6
- def initialize
7
- super
6
+ def initialize(expr = nil, name = nil, line = nil, col = nil)
7
+ super()
8
8
  self.ref = 0
9
9
  self.vars = []
10
10
  self.capts = []
11
11
  self.codes = []
12
+ @expr = expr
13
+ @name = name
14
+ @line = line
15
+ @col = col
12
16
  end
13
17
 
14
18
  def debug_dump(indent = 0)
@@ -25,6 +29,14 @@ class Packcr
25
29
  def add_ref
26
30
  @ref += 1
27
31
  end
32
+
33
+ def setup
34
+ setup_rule(self)
35
+ end
36
+
37
+ def nodes
38
+ [expr]
39
+ end
28
40
  end
29
41
  end
30
42
  end
@@ -3,9 +3,14 @@ class Packcr
3
3
  class SequenceNode < Packcr::Node
4
4
  attr_accessor :nodes
5
5
 
6
- def initialize
7
- super
8
- self.nodes = []
6
+ def initialize(*nodes)
7
+ super()
8
+ self.nodes = nodes
9
+ end
10
+
11
+ def seq(node)
12
+ @nodes << node
13
+ self
9
14
  end
10
15
 
11
16
  def debug_dump(indent = 0)
@@ -3,6 +3,10 @@ class Packcr
3
3
  class StringNode < Packcr::Node
4
4
  attr_accessor :value
5
5
 
6
+ def initialize(value = nil)
7
+ self.value = value
8
+ end
9
+
6
10
  def value=(value)
7
11
  @value = value&.b
8
12
  end
data/lib/packcr/node.rb CHANGED
@@ -1,5 +1,22 @@
1
1
  class Packcr
2
2
  class Node
3
+ def seq(expr)
4
+ SequenceNode.new(self, expr)
5
+ end
6
+
7
+ def alt(expr)
8
+ AlternateNode.new(self, expr)
9
+ end
10
+
11
+ def setup_rule(rule)
12
+ nodes.each do |node|
13
+ node.setup_rule(rule)
14
+ end
15
+ end
16
+
17
+ def nodes
18
+ []
19
+ end
3
20
  end
4
21
  end
5
22