packcr 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/exe/packcr +3 -0
  3. data/lib/packcr/broadcast.rb +17 -0
  4. data/lib/packcr/cli.rb +27 -0
  5. data/lib/packcr/code_block.rb +2 -4
  6. data/lib/packcr/context.rb +66 -61
  7. data/lib/packcr/generated/context.rb +440 -0
  8. data/lib/packcr/generated/node/action_node.rb +60 -0
  9. data/lib/packcr/generated/node/alternate_node.rb +98 -0
  10. data/lib/packcr/generated/node/capture_node.rb +39 -0
  11. data/lib/packcr/generated/node/charclass_node.rb +372 -0
  12. data/lib/packcr/generated/node/eof_node.rb +20 -0
  13. data/lib/packcr/generated/node/error_node.rb +67 -0
  14. data/lib/packcr/generated/node/expand_node.rb +30 -0
  15. data/lib/packcr/generated/node/predicate_node.rb +140 -0
  16. data/lib/packcr/generated/node/quantity_node.rb +167 -0
  17. data/lib/packcr/generated/node/reference_node.rb +70 -0
  18. data/lib/packcr/generated/node/rule_node.rb +63 -0
  19. data/lib/packcr/generated/node/sequence_node.rb +42 -0
  20. data/lib/packcr/generated/node/string_node.rb +60 -0
  21. data/lib/packcr/generator.rb +14 -11
  22. data/lib/packcr/node/action_node.rb +18 -7
  23. data/lib/packcr/node/alternate_node.rb +22 -12
  24. data/lib/packcr/node/capture_node.rb +14 -12
  25. data/lib/packcr/node/charclass_node.rb +44 -24
  26. data/lib/packcr/node/eof_node.rb +25 -0
  27. data/lib/packcr/node/error_node.rb +20 -13
  28. data/lib/packcr/node/expand_node.rb +18 -9
  29. data/lib/packcr/node/predicate_node.rb +19 -26
  30. data/lib/packcr/node/quantity_node.rb +31 -24
  31. data/lib/packcr/node/reference_node.rb +31 -9
  32. data/lib/packcr/node/root_node.rb +61 -0
  33. data/lib/packcr/node/rule_node.rb +29 -3
  34. data/lib/packcr/node/sequence_node.rb +48 -31
  35. data/lib/packcr/node/string_node.rb +19 -16
  36. data/lib/packcr/node.rb +34 -5
  37. data/lib/packcr/parser.rb +4493 -3896
  38. data/lib/packcr/stream.rb +22 -22
  39. data/lib/packcr/templates/context/header.c.erb +29 -0
  40. data/lib/packcr/templates/context/source.c.erb +1069 -0
  41. data/lib/packcr/templates/context/source.rb.erb +341 -0
  42. data/lib/packcr/templates/node/action.c.erb +16 -0
  43. data/lib/packcr/templates/node/action.rb.erb +21 -0
  44. data/lib/packcr/templates/node/alternate.c.erb +34 -0
  45. data/lib/packcr/templates/node/alternate.rb.erb +39 -0
  46. data/lib/packcr/templates/node/capture.c.erb +17 -0
  47. data/lib/packcr/templates/node/capture.rb.erb +14 -0
  48. data/lib/packcr/templates/node/charclass.c.erb +47 -0
  49. data/lib/packcr/templates/node/charclass.rb.erb +49 -0
  50. data/lib/packcr/templates/node/charclass_any.c.erb +5 -0
  51. data/lib/packcr/templates/node/charclass_any.rb.erb +7 -0
  52. data/lib/packcr/templates/node/charclass_fail.c.erb +1 -0
  53. data/lib/packcr/templates/node/charclass_fail.rb.erb +1 -0
  54. data/lib/packcr/templates/node/charclass_one.c.erb +19 -0
  55. data/lib/packcr/templates/node/charclass_one.rb.erb +23 -0
  56. data/lib/packcr/templates/node/charclass_utf8.c.erb +49 -0
  57. data/lib/packcr/templates/node/charclass_utf8.rb.erb +50 -0
  58. data/lib/packcr/templates/node/charclass_utf8_reverse.rb.erb +51 -0
  59. data/lib/packcr/templates/node/eof.c.erb +1 -0
  60. data/lib/packcr/templates/node/eof.rb.erb +3 -0
  61. data/lib/packcr/templates/node/error.c.erb +28 -0
  62. data/lib/packcr/templates/node/error.rb.erb +34 -0
  63. data/lib/packcr/templates/node/expand.c.erb +16 -0
  64. data/lib/packcr/templates/node/expand.rb.erb +16 -0
  65. data/lib/packcr/templates/node/predicate.c.erb +30 -0
  66. data/lib/packcr/templates/node/predicate.rb.erb +28 -0
  67. data/lib/packcr/templates/node/predicate_neg.c.erb +23 -0
  68. data/lib/packcr/templates/node/predicate_neg.rb.erb +22 -0
  69. data/lib/packcr/templates/node/quantity_many.c.erb +45 -0
  70. data/lib/packcr/templates/node/quantity_many.rb.erb +47 -0
  71. data/lib/packcr/templates/node/quantity_one.c.erb +21 -0
  72. data/lib/packcr/templates/node/quantity_one.rb.erb +23 -0
  73. data/lib/packcr/templates/node/reference.c.erb +17 -0
  74. data/lib/packcr/templates/node/reference.rb.erb +21 -0
  75. data/lib/packcr/templates/node/reference_reverse.rb.erb +21 -0
  76. data/lib/packcr/templates/node/rule.c.erb +26 -0
  77. data/lib/packcr/templates/node/rule.rb.erb +30 -0
  78. data/lib/packcr/templates/node/sequence.c.erb +12 -0
  79. data/lib/packcr/templates/node/sequence.rb.erb +12 -0
  80. data/lib/packcr/templates/node/string_many.c.erb +11 -0
  81. data/lib/packcr/templates/node/string_many.rb.erb +10 -0
  82. data/lib/packcr/templates/node/string_one.c.erb +8 -0
  83. data/lib/packcr/templates/node/string_one.rb.erb +10 -0
  84. data/lib/packcr/util.rb +21 -24
  85. data/lib/packcr/version.rb +1 -1
  86. data/lib/packcr.rb +9 -13
  87. metadata +117 -10
  88. data/lib/packcr/buffer.rb +0 -47
@@ -17,31 +17,34 @@ class Packcr
17
17
  $stdout.print "')\n"
18
18
  end
19
19
 
20
- def generate_code(gen, onfail, indent, bare)
20
+ def generate_code(gen, onfail, indent, bare, oncut: nil)
21
21
  n = value&.length || 0
22
- if n > 0
23
- if n > 1
24
- gen.write Packcr.template("node/string_many.#{gen.lang}.erb", binding, indent: indent)
25
- return Packcr::CODE_REACH__BOTH
26
- else
27
- gen.write Packcr.template("node/string_one.#{gen.lang}.erb", binding, indent: indent)
28
- return Packcr::CODE_REACH__BOTH
29
- end
22
+ return unless n > 0
23
+
24
+ if n > 1
25
+ gen.write Packcr.format_code(get_many_code(gen, onfail, indent, bare, oncut, n), indent: indent)
30
26
  else
31
- # no code to generate
32
- return Packcr::CODE_REACH__ALWAYS_SUCCEED
27
+ gen.write Packcr.format_code(get_one_code(gen, onfail, indent, bare, oncut, n), indent: indent)
33
28
  end
34
- Packcr::CODE_REACH__BOTH
35
29
  end
36
30
 
37
- def verify_variables(vars)
38
- end
31
+ def reachability
32
+ n = value&.length || 0
33
+ if n <= 0
34
+ return Packcr::CODE_REACH__ALWAYS_SUCCEED
35
+ end
39
36
 
40
- def verify_captures(ctx, capts)
37
+ Packcr::CODE_REACH__BOTH
41
38
  end
42
39
 
43
- def link_references(ctx)
40
+ def to_h
41
+ {
42
+ type: :string,
43
+ value: value,
44
+ }
44
45
  end
45
46
  end
46
47
  end
47
48
  end
49
+
50
+ require "packcr/generated/node/string_node"
data/lib/packcr/node.rb CHANGED
@@ -1,25 +1,53 @@
1
1
  class Packcr
2
2
  class Node
3
- def seq(expr)
4
- SequenceNode.new(self, expr)
3
+ def seq(expr, cut: false)
4
+ SequenceNode.new(self).seq(expr, cut: cut)
5
5
  end
6
6
 
7
7
  def alt(expr)
8
- AlternateNode.new(self, expr)
8
+ AlternateNode.new(self).alt(expr)
9
9
  end
10
10
 
11
- def setup_rule(rule)
11
+ def verify_variables(vars)
12
12
  nodes.each do |node|
13
- node.setup_rule(rule)
13
+ node.verify_variables(vars)
14
+ end
15
+ end
16
+
17
+ def verify_captures(ctx, capts)
18
+ nodes.each do |node|
19
+ node.verify_captures(ctx, capts)
20
+ end
21
+ end
22
+
23
+ def link_references(ctx); end
24
+
25
+ def setup_rule(rule); end
26
+
27
+ def setup(ctx, rule)
28
+ setup_rule(rule)
29
+ link_references(ctx)
30
+
31
+ nodes.each do |node|
32
+ node.setup(ctx, rule)
14
33
  end
15
34
  end
16
35
 
17
36
  def nodes
18
37
  []
19
38
  end
39
+
40
+ def reversible?(_gen)
41
+ false
42
+ end
43
+
44
+ def sequence?
45
+ false
46
+ end
20
47
  end
21
48
  end
22
49
 
50
+ require "packcr/node/root_node"
23
51
  require "packcr/node/rule_node"
24
52
  require "packcr/node/reference_node"
25
53
  require "packcr/node/string_node"
@@ -32,3 +60,4 @@ require "packcr/node/capture_node"
32
60
  require "packcr/node/expand_node"
33
61
  require "packcr/node/action_node"
34
62
  require "packcr/node/error_node"
63
+ require "packcr/node/eof_node"