Strscan

Just got acquainted with strscan, and want to create some sort of sub-matching. For now I’ve introduced my variant of groups.

Next I want a trigger that would set off a new group recursively.

But I code as I think and I think as I drink.. so, let’s see. I also have this anti-social-media thing, where I avoid groups, chats, communities and such. I have to figure it out / look it up myself.

I realize I apologize (a very interesting word!) for my code being in such a state.. Well, I haven’t come further than this.

Oh! That reminds me: idris2. I was so hooked on getting into it. And some little shitty thing threw me off. Could have been the color-choices on the Doc-site. I can never tell. It’s on … or off.

require 'strscan'

txt = <<-TEXT
  hej
  # apa
  .
TEXT

@sc = StringScanner.new txt


@rules = {
  set1: {
    whitespace:   %r{ [ \c\r\n\t]+ }x,
    greeting:     'hej'
  },
  set2: {
    comment:      %r{ \#.*$ }x
  }
}

def nxt(*rules)
  return if @sc.eos?
  
  rules.each {|name|
    @rules[name].each {|k,v|
      if (res = @sc.scan(v))
        return [k, res]
      end
    }
  }
  [:unknown, @sc.getch]
end

while rep = nxt(:set1, :set2);  p rep end