Parse specified number of elements with OMeta

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Parse specified number of elements with OMeta

Yoshiki Ohshima-3
There may have been many similar attempts, but come to think of it,
adding a simple higher-order rule to OMeta that applies the given rule
for a given number of times is just straightforward.  For example, if
you want to only parse a string that represents fully padded
hexadecimal digits for a 8 or 4 byte quantity, you can write:

hex16or8 =
  numberOf(#hex. 16)
| numberOf(#hex. 8)

where #hex is something like:

hex :c =
   ?[(c between $0 and: $9) or: [c between: $A and: $F]


The numberOf rule looks like:

numberOf :rule :count =
    [0]:c [OrderedCollection new]:xs
    ([c+1]:c ?[c <= count] apply(rule):x [xs add: x])*
    ?[c = (count + 1)] -> [xs]

--
-- Yoshiki

Reply | Threaded
Open this post in threaded view
|

Re: Parse specified number of elements with OMeta

Frank Shearar-3
On 6 December 2012 06:15, Yoshiki Ohshima <[hidden email]> wrote:

> There may have been many similar attempts, but come to think of it,
> adding a simple higher-order rule to OMeta that applies the given rule
> for a given number of times is just straightforward.  For example, if
> you want to only parse a string that represents fully padded
> hexadecimal digits for a 8 or 4 byte quantity, you can write:
>
> hex16or8 =
>   numberOf(#hex. 16)
> | numberOf(#hex. 8)
>
> where #hex is something like:
>
> hex :c =
>    ?[(c between $0 and: $9) or: [c between: $A and: $F]

I think you're missing a trailing ] there.

> The numberOf rule looks like:
>
> numberOf :rule :count =
>     [0]:c [OrderedCollection new]:xs
>     ([c+1]:c ?[c <= count] apply(rule):x [xs add: x])*
>     ?[c = (count + 1)] -> [xs]

I may be misremembering my OMeta grammar, but I thought you'd need
some |s between those. Or is it that [0] binds to :count (calling it
c), which makes the initial collection. Then we apply some number of
times (the *) the rule that runs :rule, and finally we have a semantic
action returning the final collection?

frank

Reply | Threaded
Open this post in threaded view
|

Re: Parse specified number of elements with OMeta

Yoshiki Ohshima-3
On Thu, Dec 6, 2012 at 8:10 AM, Frank Shearar <[hidden email]> wrote:

> On 6 December 2012 06:15, Yoshiki Ohshima <[hidden email]> wrote:
>> There may have been many similar attempts, but come to think of it,
>> adding a simple higher-order rule to OMeta that applies the given rule
>> for a given number of times is just straightforward.  For example, if
>> you want to only parse a string that represents fully padded
>> hexadecimal digits for a 8 or 4 byte quantity, you can write:
>>
>> hex16or8 =
>>   numberOf(#hex. 16)
>> | numberOf(#hex. 8)
>>
>> where #hex is something like:
>>
>> hex :c =
>>    ?[(c between $0 and: $9) or: [c between: $A and: $F]
>
> I think you're missing a trailing ] there.

Thanks.  I typed this in above, as my actual implementation was even
uglier and made a mistake.

>> The numberOf rule looks like:
>>
>> numberOf :rule :count =
>>     [0]:c [OrderedCollection new]:xs
>>     ([c+1]:c ?[c <= count] apply(rule):x [xs add: x])*
>>     ?[c = (count + 1)] -> [xs]
>
> I may be misremembering my OMeta grammar, but I thought you'd need
> some |s between those.

No.

> Or is it that [0] binds to :count (calling it
> c), which makes the initial collection.

The numberOf rule takes two arguments, the rule to be applied and how
many times.

[0]:c binds 0 to c. And as a separate semantic action, a new
OrderedCollection is bound to xs.

> Then we apply some number of
> times (the *) the rule that runs :rule,

Yes.

> and finally we have a semantic
> action returning the final collection?

Yes, only after checking that the above loop terminated on the
condition, not failing to match rule by ?[c = (count + 1)].

--
-- Yoshiki