iregexp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ require_relative "../iregexp.rb"
2
+
3
+ class IREGEXP
4
+
5
+ # precedence:
6
+ # 0: | alt
7
+ # 1: seq
8
+ # 2: ... occ dot pos neg - p P
9
+
10
+ def prec_check(inner, targetprec, prec, options)
11
+ lp, rp = options[:paren]
12
+ if targetprec > prec
13
+ "#{lp||"("}#{inner}#{rp||")"}"
14
+ else
15
+ inner
16
+ end
17
+ end
18
+
19
+ TNR_MAP = {"\t" => "\\t", "\n" => "\\n", "\r" => "\\r"}
20
+
21
+ def write_escape(ch, esc, map = TNR_MAP)
22
+ if c1 = map[ch]
23
+ c1
24
+ elsif ch =~ esc
25
+ "\\#{ch}"
26
+ else
27
+ ch
28
+ end
29
+ end
30
+
31
+ CC_ESC_CHARS = /\A[-\[\\\]]\z/
32
+
33
+ def write_charclass(pn, chars)
34
+ "[#{"^" if pn == "neg"}#{chars.map {|ch|
35
+ case ch
36
+ in String; write_escape(ch, CC_ESC_CHARS)
37
+ in ["-", l, r]; "#{write_escape(l, CC_ESC_CHARS)}-#{write_escape(r, CC_ESC_CHARS)}"
38
+ in ["p"|"P", _cat]; write_rhs(ch)
39
+ end
40
+ }.join}]"
41
+ end
42
+
43
+ SEQ_ESC_CHARS = /\A[()*+.?\[\\\]{|}]\z/ #
44
+
45
+ def write_rhs(v, targetprec = 0, options = {})
46
+ # warn "** v = #{v.inspect}"
47
+ prec, ret =
48
+ case v
49
+ in ["pos" | "neg", *chars]
50
+ [2, write_charclass(v[0], chars)]
51
+ in ["alt", *atoms]
52
+ [0, atoms.map{write_rhs(_1, 1, options)}.join("|")]
53
+ in ["seq", *atoms]
54
+ [1, atoms.map{write_rhs(_1, 1, options)}.join]
55
+ in ["rep", s, e, atom]
56
+ occur = case [s, e]
57
+ in [0, false]; "*"
58
+ in [1, false]; "+"
59
+ in [Integer, false]; "{#{s},}"
60
+ in [0, 1]; "?"
61
+ in [1, 1]; ""
62
+ in [^s, ^s]; "{#{s}}"
63
+ else; "{#{s},#{e}}"
64
+ end
65
+ [1, "#{write_rhs(atom, 2, options)}#{occur}"]
66
+ in ["dot"]
67
+ [2, options[:dot] || "."]
68
+ in ["p" | "P", cat]
69
+ [2, "\\#{v[0]}{#{cat}}"]
70
+ in String
71
+ [2, write_escape(v, SEQ_ESC_CHARS)] # XXX esc!
72
+ end
73
+ prec_check(ret, targetprec, prec, options)
74
+ end
75
+
76
+ def to_s
77
+ write_rhs(self.tree)
78
+ end
79
+
80
+ def to_pcre
81
+ "\\A#{write_rhs(self.tree, 1, {paren: ["(?:", ")"]})}\\z"
82
+ end
83
+
84
+ def to_regexp
85
+ /#{to_pcre}/
86
+ end
87
+
88
+ def to_js
89
+ "^#{write_rhs(self.tree, 1, {paren: ["(?:", ")"], dot: "[^\\n\\r]"})}$"
90
+ end
91
+
92
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iregexp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carsten Bormann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: treetop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: neatjson
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: regexp-examples
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.1
69
+ description: iregexp implements converters and miscellaneous tools for I-Regexp
70
+ email: cabo@tzi.org
71
+ executables:
72
+ - iregexp
73
+ - iregexp~
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - bin/iregexp
78
+ - bin/iregexp~
79
+ - iregexp.gemspec
80
+ - lib/iregexp.rb
81
+ - lib/parser/iregexp-util.rb
82
+ - lib/parser/iregexpgrammar.rb
83
+ - lib/writer/iregexp-writer.rb
84
+ homepage: http://github.com/cabo/iregexp
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.4.6
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: I-Regexp Tools
107
+ test_files: []