0

I'm looking for a function that returns different integer values by matching an input string for different expressions.

Here is how it could be, but syntax:

package main

func MatchType(data []byte) int {
  %% machine scanner;
  %% write data;

  cs, p, pe, eof := 0, 0, len(data), len(data)

  _ = eof

  %%{
    main := (  0..120 '-first-' ) @{ return 1 } ;
    main := ( 0..120 '-second-' ) @{ return 2 } ;

    write init;
    write exec;
  }%%

  return -1
}

Can anybody please advise?

1 Answers1

0
  %%{

    action a1 { return 8; }
    action a2 { return 10; }

    main := ( 0..120 '-first-' $a1  | 0..120 '-second-' $a2 ) @{ return 0 } ;

    write init;
    write exec;
  }%%
Wojciech Kaczmarek
  • 2,173
  • 4
  • 23
  • 40
  • Btw, why to invoke these actions via $ ? I guess @ would be better.. – Wojciech Kaczmarek Dec 15 '22 at 14:11
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Dec 16 '22 at 00:26