Regular Expressions 101

Community Patterns

Golang function metadata parsing

0

Regular Expression
PCRE (PHP <7.3)

/
(?x)^ (?<StatementDesc>(?: (?: [\/\/].+(?=\n)\n)* )* ) (?: (?: # Begin Function Declaration func[[:space:]]+ # Datatype Method? (?: \( (?<funcRecvrVar>[a-zA-Z_]+) [[:space:]]+ (?<funcRecvrTypePointerChar>\*?) (?<funcRecvrType>[^[[:space:]\)]+) (?<funcRecvrErr>.*?(?=[\)])) \) )? [[:space:]]* # Function Name (?<funcName>[a-zA-Z]+) [[:space:]]* # Parameters \((?<funcParamBody> # <funcParam> is optional (?<funcParam> (?<funcParam1> (?<funcParam1Name> [A-Za-z_\d]+ (?: [[:space:]]+ (?<funcParam1Type> [A-Za-z_\d]+ [[:space:]]* )? | [[:space:]]* )? ) ) # Next Parameter recursion (?: # Check if comma (?=,)[,] [[:space:]]* (?<funcParam2> (?<funcParam2Name> [A-Za-z_\d]+ (?: [[:space:]]+ (?<funcParam2Type> [A-Za-z_\d]+ [[:space:]]* )? | [[:space:]]* )? ) ) # Next Parameter recursion (?: # Check if comma (?=,)[,] [[:space:]]* (?<funcParam3> (?<funcParam3Name> [A-Za-z_\d]+ (?: [[:space:]]+ (?<funcParam3Type> [A-Za-z_\d]+ [[:space:]]* )? | [[:space:]]* )? ) ) )? )? (?<funcParamRest>[^\)]*) )? )\) [[:space:]]* # Return Value (?: # Return inside parens (?:[\(] (?<funcReturn1>[^\),[:space:]]+) (?: [[:space:]]*[,][[:space:]]* (?<funcReturn2>[^\),[:space:]]+) (?: [[:space:]]*[,][[:space:]]* (?<funcReturn3>[^\),[:space:]]+) )? )? [\)] ) | # Single Return Outside Parens (?<funcReturnSingle>[^\{[:space:]]+) | )? [[:space:]]* [[:space:]]* ) # Remove Non-relevant lines (?<removed> (?: # At new line, check if its blank || begins with spaces or not `func` (?= (?: $ | [[:space:]\t].+\n | (?!\/\/|func) ) ) .+\n+(?=(?![\/]|func)) )+ )? )
/
gmixX

Description

Created out of hubris while working on golang source to lua EmmyLua annotations

TODO: add struct parsing

Submitted by anonymous - 4 years ago