Class: Strling::Emitters::PCRE2
- Inherits:
-
Object
- Object
- Strling::Emitters::PCRE2
- Defined in:
- lib/strling/emitters/pcre2.rb
Overview
PCRE2 emitter class
Class Method Summary collapse
-
.build_flags(flags) ⇒ Object
Build flag string from Flags object.
-
.emit(ir_root, flags) ⇒ String
Emit IR as a PCRE2 regex pattern.
Instance Method Summary collapse
-
#emit_node(node) ⇒ String
Emit a single IR node.
-
#initialize(flags) ⇒ PCRE2
constructor
A new instance of PCRE2.
Constructor Details
#initialize(flags) ⇒ PCRE2
Returns a new instance of PCRE2.
35 36 37 |
# File 'lib/strling/emitters/pcre2.rb', line 35 def initialize(flags) @flags = flags end |
Class Method Details
.build_flags(flags) ⇒ Object
Build flag string from Flags object
81 82 83 84 85 86 87 88 89 |
# File 'lib/strling/emitters/pcre2.rb', line 81 def self.build_flags(flags) str = '' str += 'i' if flags.ignore_case str += 'm' if flags.multiline str += 's' if flags.dot_all str += 'u' if flags.unicode str += 'x' if flags.extended str end |
.emit(ir_root, flags) ⇒ String
Emit IR as a PCRE2 regex pattern
26 27 28 29 30 31 32 33 |
# File 'lib/strling/emitters/pcre2.rb', line 26 def self.emit(ir_root, flags) emitter = new(flags) pattern = emitter.emit_node(ir_root) # Add flag modifiers if any flag_str = build_flags(flags) flag_str.empty? ? pattern : "(?#{flag_str})#{pattern}" end |
Instance Method Details
#emit_node(node) ⇒ String
Emit a single IR node
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/strling/emitters/pcre2.rb', line 43 def emit_node(node) case node when Strling::Core::IRAlt, Strling::IR::Alt # (branch1|branch2|...) branches = node.branches.map { |b| emit_node(b) } branches.length == 1 ? branches[0] : "(#{branches.join('|')})" when Strling::Core::IRSeq, Strling::IR::Seq # Concatenate parts node.parts.map { |p| emit_node(p) }.join when Strling::Core::IRLit, Strling::IR::Lit # Escape metacharacters escape_literal(node.value) when Strling::Core::IRDot, Strling::IR::Dot '.' when Strling::Core::IRAnchor, Strling::IR::Anchor emit_anchor(node.at) when Strling::Core::IRCharClass, Strling::IR::CharClass emit_char_class(node) when Strling::Core::IRQuant, Strling::IR::Quant emit_quantifier(node) when Strling::Core::IRGroup, Strling::IR::Group emit_group(node) when Strling::Core::IRBackref emit_backref(node) when Strling::IR::BackRef emit_backref(node) when Strling::Core::IRLook, Strling::IR::Look emit_lookaround(node) when Strling::IR::Esc emit_escape(node) else raise "Unknown IR node type: #{node.class}" end end |