summaryrefslogtreecommitdiff
path: root/src/thue/rule.cr
blob: cd45adc7c7c2d9bf50e3ee945b02e3fa199695f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require "./thue.cr"

abstract struct Thue::Rule
  @regex : Regex

  getter pattern : String

  def initialize(@pattern : String)
    @regex = Regex.new(Regex.escape(@pattern))
  end

  def matches?(str : String) : Bool
    str.matches?(@regex)
  end

  abstract def replacement : String
end

struct Thue::NormalRule < Thue::Rule
  def initialize(pattern : String, @replacement : String)
    super(pattern)
  end

  def replacement : String
    @replacement
  end
end

struct Thue::InputRule < Thue::Rule
  def replacement : String
    gets || ""
  end
end

struct Thue::OutputRule < Thue::Rule
  def initialize(pattern : String, @output : String)
    super(pattern)
  end

  def replacement : String
    print @output
    return ""
  end
end