parsing – Capturing function parameters and optional type hints using regex

I’m trying to capture the optional type hints that come after parameter names for my own toy language so I can make them a different color.

I think I’m getting very close, Spaces aren’t allowed for now just to make debugging easier:-

\b(func)\b\s+\b(\w+)\b\((\w+(:(\w+))?(,(\w+)(:(\w+))?)*)?\)\s*->\s*\b(\w+)\b

I based that regex off of my parser grammer:-

funDecl        → "func" function ("->" IDENTIFIER)? ;
function       → IDENTIFIER "(" parameters? ")" block ;
parameters     → IDENTIFIER (":" IDENTIFIER)? ( "," IDENTIFIER (":" IDENTIFIER)?)* ;

Here’s the current strange results:-
regex

Read more here: Source link