Forum: Ruby Defining a "followed by" operator or function in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cea563880417182e24d2b9bbe8ec0de0?d=identicon&s=25 Andy Elvey (latte)
on 2009-03-23 07:27
Hi all -
 As part of doing a parsing lib in Ruby, I want to create a "followed
by" operator or function. In other words, I want to be able to specify
that (say) "foo" must be followed by n or more occurrences of "bar".

Something like this -
"foo".(0,+) "bar" would match zero or more occurrences of "foo" followed
by "bar". So, it would match "foo" , "foo" "bar", "foo" "bar" "bar" ...
and so on.

"foo".(0,1) "bar" would match zero or one occurrences of "foo" followed
by "bar".

 So, how could this be done?
 Very many thanks in advance!
 - Andy
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-23 14:40
> Something like this -
> "foo".(0,+) "bar" would match zero or more occurrences of "foo" followed
> by "bar". So, it would match "foo" , "foo" "bar", "foo" "bar" "bar" ...
> and so on.
>
> "foo".(0,1) "bar" would match zero or one occurrences of "foo" followed
> by "bar".

Are you talking about regular expression syntax?

  (foo){0,}(bar){1,}         # or (foo)?(bar)+
  (foo){0,1}bar              # or (foo)?bar

See http://www.ruby-doc.org/docs/ProgrammingRuby/html/...

Otherwise, if you are writing a parser for some language of your own
devising, then you are free to implement whatever syntax you wish. Buy a
good book on language and compiler design.
Cea563880417182e24d2b9bbe8ec0de0?d=identicon&s=25 Andy Elvey (latte)
on 2009-03-24 06:35
Brian Candler wrote:
>> Something like this -
>> "foo".(0,+) "bar" would match zero or more occurrences of "foo" followed
>> by "bar". So, it would match "foo" , "foo" "bar", "foo" "bar" "bar" ...
>> and so on.
>>
>> "foo".(0,1) "bar" would match zero or one occurrences of "foo" followed
>> by "bar".
>
> Are you talking about regular expression syntax?
>
>   (foo){0,}(bar){1,}         # or (foo)?(bar)+
>   (foo){0,1}bar              # or (foo)?bar
>
> See http://www.ruby-doc.org/docs/ProgrammingRuby/html/...
>
> Otherwise, if you are writing a parser for some language of your own
> devising, then you are free to implement whatever syntax you wish. Buy a
> good book on language and compiler design.
Hi Brian -
 Thanks very much for that - it should be a big help!
Bye for now -
 - Andy
This topic is locked and can not be replied to.