Class: ExpressionFactory
- Inherits:
-
Object
- Object
- ExpressionFactory
- Includes:
- Logue::Loggable
- Defined in:
- lib/glark/match/factory.rb
Instance Attribute Summary collapse
-
#expr ⇒ Object
readonly
Returns the value of attribute expr.
Instance Method Summary collapse
-
#initialize(matchspec) ⇒ ExpressionFactory
constructor
A new instance of ExpressionFactory.
- #make_and_distance(arg, args) ⇒ Object
- #make_and_expression(arg, args) ⇒ Object
- #make_expression(args, warn_option = false) ⇒ Object
-
#make_expressions(args) ⇒ Object
creates two expressions and returns them.
- #make_infix_expression(arg, args = []) ⇒ Object
- #make_not_expression(args) ⇒ Object
- #make_or_expression(args) ⇒ Object
- #make_regular_expression(pattern, negated = false) ⇒ Object
- #make_two_expressions(args, type) ⇒ Object
- #make_xor_expression(args) ⇒ Object
-
#read_file(fname) ⇒ Object
reads a file containing one regular expression per line.
-
#shift_end_tag(name, args) ⇒ Object
removes optional end tag.
Constructor Details
#initialize(matchspec) ⇒ ExpressionFactory
Returns a new instance of ExpressionFactory.
21 22 23 |
# File 'lib/glark/match/factory.rb', line 21 def initialize matchspec @regexp_factory = RegexpExpressionFactory.new matchspec end |
Instance Attribute Details
#expr ⇒ Object (readonly)
Returns the value of attribute expr.
19 20 21 |
# File 'lib/glark/match/factory.rb', line 19 def expr @expr end |
Instance Method Details
#make_and_distance(arg, args) ⇒ Object
95 96 97 98 |
# File 'lib/glark/match/factory.rb', line 95 def make_and_distance arg, args adist = AndDistance.new arg, args adist.distance end |
#make_and_expression(arg, args) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/glark/match/factory.rb', line 100 def make_and_expression arg, args dist = make_and_distance arg, args a1, a2 = make_two_expressions args, "and" AndExpression.new dist, a1, a2 end |
#make_expression(args, warn_option = false) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/glark/match/factory.rb', line 144 def make_expression args, warn_option = false arg = args[0] if arg case arg when "--or", "-o" args.shift make_or_expression args when "--xor" args.shift make_xor_expression args when %r{^\-\-and}, %r{^\-a} args.shift make_and_expression arg, args when '(' args.shift make_infix_expression arg, args else if warn_option && arg.index(%r{^\-{1,2}\w}) raise "option not understood: #{arg}" end args.shift make_regular_expression arg end else nil end end |
#make_expressions(args) ⇒ Object
creates two expressions and returns them.
51 52 53 54 55 56 |
# File 'lib/glark/match/factory.rb', line 51 def make_expressions args a1 = make_expression args a2 = make_expression args [ a1, a2 ] end |
#make_infix_expression(arg, args = []) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/glark/match/factory.rb', line 107 def make_infix_expression arg, args = [] expr = nil while arg case arg when '(' arg = args.shift expr = make_infix_expression arg, args when '--or', '-o' arg = args.shift rhs = make_infix_expression arg, args expr = InclusiveOrExpression.new expr, rhs when '--xor' arg = args.shift rhs = make_infix_expression arg, args expr = ExclusiveOrExpression.new expr, rhs when Regexp.new('^--and'), '-a' dist = make_and_distance arg, args arg = args.shift rhs = make_infix_expression arg, args expr = AndExpression.new dist, expr, rhs when ')' break else expr = make_regular_expression arg break end arg = args.shift end if !expr raise "No expression provided." end expr end |
#make_not_expression(args) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/glark/match/factory.rb', line 64 def make_not_expression args expr = make_regular_expression args, true unless expr raise "'not' expression takes one argument" end # explicit end tag is optional: shift_end_tag "not", args expr end |
#make_or_expression(args) ⇒ Object
85 86 87 88 |
# File 'lib/glark/match/factory.rb', line 85 def make_or_expression args a1, a2 = make_two_expressions args, "or" InclusiveOrExpression.new a1, a2 end |
#make_regular_expression(pattern, negated = false) ⇒ Object
46 47 48 |
# File 'lib/glark/match/factory.rb', line 46 def make_regular_expression pattern, negated = false @regexp_factory.create_expression pattern, negated end |
#make_two_expressions(args, type) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/glark/match/factory.rb', line 75 def make_two_expressions args, type a1, a2 = make_expressions args unless a1 && a2 raise "'" + type + "' expression takes two arguments" end shift_end_tag type, args [ a1, a2 ] end |
#make_xor_expression(args) ⇒ Object
90 91 92 93 |
# File 'lib/glark/match/factory.rb', line 90 def make_xor_expression args a1, a2 = make_two_expressions args, "xor" ExclusiveOrExpression.new a1, a2 end |
#read_file(fname) ⇒ Object
reads a file containing one regular expression per line.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/glark/match/factory.rb', line 26 def read_file fname expr = nil File.open(fname) do |file| file.each_line do |line| line.strip! next if line.empty? # flatten the or expression instead of nesting it, to avoid # stack overruns for very large files. re = make_regular_expression line.chomp if expr expr.ops << re else expr = InclusiveOrExpression.new re end end end expr end |
#shift_end_tag(name, args) ⇒ Object
removes optional end tag
59 60 61 62 |
# File 'lib/glark/match/factory.rb', line 59 def shift_end_tag name, args # explicit end tag is optional: args.shift if args[0] == ("--end-of-" + name) end |