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.
20 21 22 |
# File 'lib/glark/match/factory.rb', line 20 def initialize matchspec @regexp_factory = RegexpExpressionFactory.new matchspec end |
Instance Attribute Details
#expr ⇒ Object (readonly)
Returns the value of attribute expr.
18 19 20 |
# File 'lib/glark/match/factory.rb', line 18 def expr @expr end |
Instance Method Details
#make_and_distance(arg, args) ⇒ Object
94 95 96 97 |
# File 'lib/glark/match/factory.rb', line 94 def make_and_distance arg, args adist = AndDistance.new arg, args adist.distance end |
#make_and_expression(arg, args) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/glark/match/factory.rb', line 99 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
143 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 |
# File 'lib/glark/match/factory.rb', line 143 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.
50 51 52 53 54 55 |
# File 'lib/glark/match/factory.rb', line 50 def make_expressions args a1 = make_expression args a2 = make_expression args [ a1, a2 ] end |
#make_infix_expression(arg, args = []) ⇒ Object
106 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 |
# File 'lib/glark/match/factory.rb', line 106 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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/glark/match/factory.rb', line 63 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
84 85 86 87 |
# File 'lib/glark/match/factory.rb', line 84 def make_or_expression args a1, a2 = make_two_expressions args, "or" InclusiveOrExpression.new a1, a2 end |
#make_regular_expression(pattern, negated = false) ⇒ Object
45 46 47 |
# File 'lib/glark/match/factory.rb', line 45 def make_regular_expression pattern, negated = false @regexp_factory.create_expression pattern, negated end |
#make_two_expressions(args, type) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/glark/match/factory.rb', line 74 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
89 90 91 92 |
# File 'lib/glark/match/factory.rb', line 89 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.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/glark/match/factory.rb', line 25 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
58 59 60 61 |
# File 'lib/glark/match/factory.rb', line 58 def shift_end_tag name, args # explicit end tag is optional: args.shift if args[0] == ("--end-of-" + name) end |