Need advice on recognizing patterns in strings and replacing them with other patterns

Hi,

I am working on a project which involves reading lines from a file, one by
one, looking for certain patterns in each line, converting the patterns into
new
ruby code which may use some of the info in the recognized patterns. I think
this would be a good application for Ruby's built in regular expression and
pattern/substitution abilities. I would greatly appreciate any advice on a
nifty way to approach this. I have some examples of what I want to do below:

Input from my source file:

box2_34 = $CUR % ((box2_20 + (box2_21) + box2_22) - (box2_25 + box2_27 +
box2_29 + box2_31 + box2_33))

Convert the above to:

box2_34 = $CUR % (box2_20.to_f + box2_21.to_f + box2_22.to_f - (box2_25.to_f
+ box2_27.to_f + box2_29.to_f + box2_31.to_f + box2_33.to_f))

The above needs to maintain the arithematic integrity of the source line -
notice the minus sign. Also, I need to watch for any combination of
arithematic
operations, with varying numbers of boxes.

Another source line:

box1_205 = $CUR % ((box1_204) - (box1_203))

Convert the above to:

box1_205 = $CUR % (box1_204.to_f - box1_203.to_f)

Sometimes the box patterns are surrounded by parens, sometimes not. Note
that the extra space after $CUR has been removed, and all boxes have
'.to_f' appended to them.

Another example:

box1_32 = ('%0.0f') % (((box1_31) + (box1_32)) * ((1.75.to_s).percent))

Convert the above to:

box1_32 = '%0.0f' % ((box1_31.to_f + box1_32.to_f) * 0.0175)

The above could be a bunch of boxes added up, some subtracted, etc, with the
result multiplied by 1.75%.

Here is another example:

box1_44 = $CUR % (condition((box1_38).gt(((box1_41) + (box1_32)) +
(box1_43)), (((box1_38) - (box1_32)) - (box1_41)) - (box1_43), nil))

Convert the above to:

box1_44 = $CUR % (box1_38.to_f > (box1_41.to_f + box1_32.to_f +
box1_43.to_f) ? box1_38.to_f - box1_32.to_f - box1_41.to_f - box1_43.to_f :
0.0)

Again, the number of boxes vary, the type of math varies too.

Here is another:

box1_32 = $CUR % (condition(((box1_31).count).eq(0.to_s), '',
condition((box1_31).gt(0.to_s), (box1_31) * ((7.9.to_s).percent), nil)))
print 'box1_32', box1_32

Convert the above to:

box1_32 = $CUR % (box1_31.to_f > 0.0 ? box1_31.to_f * 0.079 : 0.0)
print 'box1_32', box1_32

I have a start on this, but if anybody has a handy way to do/approach this I
would sure appreciate any input.

Thanks alot,

Harry Truax

"Harry Truax" <htruax@stf.com> writes:

Hi,

I am working on a project which involves reading lines from a file, one by
one, looking for certain patterns in each line, converting the patterns into
new
ruby code which may use some of the info in the recognized patterns. I think
this would be a good application for Ruby's built in regular expression and
pattern/substitution abilities. I would greatly appreciate any advice on a
nifty way to approach this. I have some examples of what I want to do below:

Input from my source file:

box2_34 = $CUR % ((box2_20 + (box2_21) + box2_22) - (box2_25 + box2_27 +
box2_29 + box2_31 + box2_33))

Convert the above to:

box2_34 = $CUR % (box2_20.to_f + box2_21.to_f + box2_22.to_f - (box2_25.to_f
+ box2_27.to_f + box2_29.to_f + box2_31.to_f + box2_33.to_f))

Would it maybe easier to write a Domain-Specific Language and then
simply eval the source file? The input looks to me like syntactically
correct Ruby...

ยทยทยท

I have a start on this, but if anybody has a handy way to do/approach this I
would sure appreciate any input.

Thanks alot,

Harry Truax

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

"Christian Neukirchen" <chneukirchen@gmail.com> schrieb im Newsbeitrag news:m2fz0282jw.fsf@lilith.local...

"Harry Truax" <htruax@stf.com> writes:

Hi,

I am working on a project which involves reading lines from a file, one by
one, looking for certain patterns in each line, converting the patterns into
new
ruby code which may use some of the info in the recognized patterns. I think
this would be a good application for Ruby's built in regular expression and
pattern/substitution abilities. I would greatly appreciate any advice on a
nifty way to approach this. I have some examples of what I want to do below:

Input from my source file:

box2_34 = $CUR % ((box2_20 + (box2_21) + box2_22) - (box2_25 + box2_27 +
box2_29 + box2_31 + box2_33))

Convert the above to:

box2_34 = $CUR % (box2_20.to_f + box2_21.to_f + box2_22.to_f - (box2_25.to_f
+ box2_27.to_f + box2_29.to_f + box2_31.to_f + box2_33.to_f))

Would it maybe easier to write a Domain-Specific Language and then
simply eval the source file? The input looks to me like syntactically
correct Ruby...

Apart from that it does not exactly what he wants it to do. But I have thought of this also: if you make sure that all variables are float values the ".to_f"'s become superfluous and simply evaluating would work (apart from possible security issues).

IMHO the proper way to tackle this would be a parser of this domain language and a processor that works on the syntax tree to output the desired results.

Kind regards

    robert