packcr 0.0.7 → 0.1.0

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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +73 -0
  4. data/lib/packcr/context.rb +15 -6
  5. data/lib/packcr/generated/context.rb +290 -143
  6. data/lib/packcr/generated/node/action_node.rb +19 -2
  7. data/lib/packcr/generated/node/alternate_node.rb +54 -8
  8. data/lib/packcr/generated/node/capture_node.rb +16 -1
  9. data/lib/packcr/generated/node/charclass_node.rb +76 -9
  10. data/lib/packcr/generated/node/eof_node.rb +8 -1
  11. data/lib/packcr/generated/node/error_node.rb +9 -2
  12. data/lib/packcr/generated/node/expand_node.rb +9 -2
  13. data/lib/packcr/generated/node/predicate_node.rb +35 -2
  14. data/lib/packcr/generated/node/quantity_node.rb +84 -31
  15. data/lib/packcr/generated/node/reference_node.rb +16 -2
  16. data/lib/packcr/generated/node/rule_node.rb +38 -7
  17. data/lib/packcr/generated/node/sequence_node.rb +18 -0
  18. data/lib/packcr/generated/node/string_node.rb +28 -4
  19. data/lib/packcr/node/reference_node.rb +1 -0
  20. data/lib/packcr/node/rule_node.rb +2 -1
  21. data/lib/packcr/parser.rb +993 -838
  22. data/lib/packcr/stream.rb +1 -1
  23. data/lib/packcr/templates/context/header.c.erb +3 -3
  24. data/lib/packcr/templates/context/source.c.erb +438 -420
  25. data/lib/packcr/templates/context/source.rb.erb +185 -167
  26. data/lib/packcr/templates/context/source.rs.erb +625 -0
  27. data/lib/packcr/templates/node/action.c.erb +2 -2
  28. data/lib/packcr/templates/node/action.rs.erb +6 -0
  29. data/lib/packcr/templates/node/alternate.c.erb +2 -2
  30. data/lib/packcr/templates/node/alternate.rs.erb +39 -0
  31. data/lib/packcr/templates/node/capture.c.erb +2 -2
  32. data/lib/packcr/templates/node/capture.rs.erb +13 -0
  33. data/lib/packcr/templates/node/charclass.c.erb +4 -4
  34. data/lib/packcr/templates/node/charclass_any.c.erb +2 -2
  35. data/lib/packcr/templates/node/charclass_one.c.erb +4 -4
  36. data/lib/packcr/templates/node/charclass_utf8.c.erb +2 -2
  37. data/lib/packcr/templates/node/charclass_utf8.rs.erb +41 -0
  38. data/lib/packcr/templates/node/eof.c.erb +1 -1
  39. data/lib/packcr/templates/node/error.c.erb +4 -4
  40. data/lib/packcr/templates/node/expand.c.erb +2 -2
  41. data/lib/packcr/templates/node/predicate.c.erb +1 -1
  42. data/lib/packcr/templates/node/predicate_neg.c.erb +1 -1
  43. data/lib/packcr/templates/node/predicate_neg.rs.erb +24 -0
  44. data/lib/packcr/templates/node/quantity_many.c.erb +9 -7
  45. data/lib/packcr/templates/node/quantity_many.rs.erb +50 -0
  46. data/lib/packcr/templates/node/quantity_one.c.erb +9 -7
  47. data/lib/packcr/templates/node/quantity_one.rb.erb +4 -4
  48. data/lib/packcr/templates/node/reference.c.erb +6 -6
  49. data/lib/packcr/templates/node/reference.rs.erb +3 -0
  50. data/lib/packcr/templates/node/rule.c.erb +9 -9
  51. data/lib/packcr/templates/node/rule.rs.erb +34 -0
  52. data/lib/packcr/templates/node/sequence.rs.erb +12 -0
  53. data/lib/packcr/templates/node/string_many.c.erb +2 -2
  54. data/lib/packcr/templates/node/string_many.rs.erb +10 -0
  55. data/lib/packcr/templates/node/string_one.c.erb +2 -2
  56. data/lib/packcr/templates/node/string_one.rs.erb +9 -0
  57. data/lib/packcr/util.rb +10 -1
  58. data/lib/packcr/version.rb +1 -1
  59. metadata +18 -65
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7f6f8510a8922ec8a3f379de4470af6889a01a0f98aebf46b69f16e2c398c64
4
- data.tar.gz: aa728bb984a9a343c71099fe6652b7a37c5a3a20b535ad6d5cbc29feaaa776d0
3
+ metadata.gz: a1797f847b90f8c53e2745592691b7f7ae12a5e18a1861f25b7e84825278529d
4
+ data.tar.gz: f593a9234386b345f4af1de58467c31640ad2233918f3149f00d53c8df70c379
5
5
  SHA512:
6
- metadata.gz: ceee0d72c7fa96a8a71fd76d8015591f70c4ba321242966ed295522d55e37e2ce71cf8ab51bda644971f5b0ad675c4c0dbdd225e8f2eded2d5eb8f5a3957922f
7
- data.tar.gz: fd819a73b1b16827316ddef020068383952cf6d188ac728f5d06157330b49b0010561552676fce3652cc90b0f838f7750242622b83d0677335e6bc6235199d4f
6
+ metadata.gz: ba48fc2f71db11977fd9c1f12f520d639d9618383f08fcbe5b7e71930084f29f32e3fed2baac800ccedd8f3e50f026daf530636ebabd31e9bc682a4798e17b8e
7
+ data.tar.gz: 1cad26bb87e543e4404df9f5e491398655e3fdc36dbe50452a6805229c06db196ba7c2c2878e461e595321376f00c65110ccba78dad3474f8039991cdff0fd9b
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ PackCR: a packrat parser generator for C as ruby library.
2
+
3
+ Copyright (c) 2022 wanabe All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # PackCR
2
+
3
+ ## Overview
4
+
5
+ PackCR is a parser generator for C or Ruby.
6
+ This library is a port of PackCC rewritten in ruby.
7
+ The Original PackCC is https://github.com/arithy/packcc.
8
+
9
+ ## Usage
10
+
11
+ You can get Packcr object with `Packcr.new(peg_path)` and it can generate C source and header with `Packcr#run`.
12
+
13
+ Example(1):
14
+
15
+ ```
16
+ $ cat ./tmp/example1.sh
17
+ #!/bin/sh
18
+ set -e
19
+ cd $0/..
20
+ cp ../packcc/examples/calc.peg calc.c.peg
21
+ bundle exec ruby -e 'require "packcr"; Packcr.new("calc.c.peg").run'
22
+ gcc calc.c
23
+ echo "1 + 2 * 3 - 4 / 2" | ./a.out
24
+
25
+ $ ./tmp/example1.sh
26
+ answer=5
27
+ ```
28
+
29
+ Example(2):
30
+
31
+ ```
32
+ $ cat ./tmp/example2.sh
33
+ #!/bin/sh
34
+ set -e
35
+ cd $0/..
36
+ cp ../examples/calc.rb.peg .
37
+ bundle exec ruby -e 'require "packcr"; Packcr.new("calc.rb.peg").run'
38
+ ruby calc.rb "1 + 2 * 3 - 4 / 2"
39
+
40
+ $ ./tmp/example2.sh
41
+ answer=5
42
+ ```
43
+
44
+ ## Syntax
45
+
46
+ The syntax is almost the same as PackCC.
47
+ Additional syntax is shown below.
48
+
49
+ **`%location` `{` _source code_ `}`**
50
+
51
+ It defines your own location structure.
52
+ It should have some definitions for C:
53
+
54
+ - struct
55
+ - `packcr_location_t`
56
+ - function
57
+ - `void packcr_location_init(packcr_location_t *lp)`
58
+ - `void packcr_location_forward(packcr_location_t *lp, char *buf, size_t n)`
59
+ - `packcr_location_t packcr_location_add(packcr_location_t l1, packcr_location_t l2)`
60
+ - `packcr_location_t packcr_location_sub(packcr_location_t l1, packcr_location_t l2)`
61
+ - (The functions can be `static` and/or `inline`)
62
+
63
+ Or it should have some definitions for ruby:
64
+ - class
65
+ - `Location`
66
+ - method
67
+ - `Location#initialize(...)`
68
+ - `Location#+(loc)`
69
+ - `Location#-(loc)`
70
+ - `Location#forward(str, index, n)`
71
+
72
+ Locations are captured and they can be refer with **`$`**_n_**`sl`** and **`$`**_n_**`el`**.
73
+ You can see the examples [examples/calc_loc.peg](examples/calc_loc.peg) and [examples/calc_loc.rb.peg](examples/calc_loc.rb.peg).
@@ -40,9 +40,9 @@ class Packcr
40
40
  get_header_code: @hname,
41
41
  }
42
42
  @hid = File.basename(@hname).upcase.gsub(/[^A-Z0-9]/, "_")
43
- when :rb
43
+ when :rb, :rs
44
44
  @patterns = {
45
- get_source_code: "#{path}.rb",
45
+ get_source_code: "#{path}.#{@lang}",
46
46
  }
47
47
  else
48
48
  raise "unexpected lang: #{@lang}"
@@ -84,7 +84,7 @@ class Packcr
84
84
  end
85
85
 
86
86
  def prefix
87
- @prefix || "pcc"
87
+ @prefix || "packcr"
88
88
  end
89
89
 
90
90
  def pass_value_code(var)
@@ -96,6 +96,17 @@ class Packcr
96
96
  end
97
97
  end
98
98
 
99
+ def line_comment_code(line)
100
+ line = line.chomp
101
+ case @lang
102
+ when :c
103
+ line.gsub("*/", "* /")
104
+ "/* #{line} */"
105
+ when :rb
106
+ "# #{line}"
107
+ end
108
+ end
109
+
99
110
  def class_name
100
111
  prefix.gsub(/(?:_|^|(\W))([a-z])/) { "#{::Regexp.last_match(1)}#{::Regexp.last_match(2)}".upcase }
101
112
  end
@@ -150,9 +161,7 @@ class Packcr
150
161
 
151
162
  next if @errnum.zero?
152
163
 
153
- results.each do |_, r|
154
- r.close
155
- end
164
+ results.each_value(&:close)
156
165
  return false
157
166
  end
158
167