Class: RegularExpression::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/regular_expression/pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Pattern

Returns a new instance of Pattern.



7
8
9
10
# File 'lib/regular_expression/pattern.rb', line 7

def initialize(source)
  ast = Parser.new.parse(source)
  @bytecode = Bytecode.compile(ast.to_nfa)
end

Instance Attribute Details

#bytecodeObject (readonly)

Returns the value of attribute bytecode.



5
6
7
# File 'lib/regular_expression/pattern.rb', line 5

def bytecode
  @bytecode
end

Instance Method Details

#compile(compiler: Generator::X86) ⇒ Object



12
13
14
15
16
17
# File 'lib/regular_expression/pattern.rb', line 12

def compile(compiler: Generator::X86)
  cfg = CFG.build(bytecode)

  singleton_class.undef_method(:match?)
  define_singleton_method(:match?, &compiler.compile(cfg))
end

#match?(string) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/regular_expression/pattern.rb', line 19

def match?(string)
  Interpreter.new(bytecode).match?(string)
end