Parse Nested Message Types
Problem statement
Parse a valid message-type expression and return its canonical representation.
A type is an identifier made of letters or digits, optionally followed by angle brackets containing one or more comma-separated child types. ASCII whitespace may appear anywhere outside an identifier. A leaf remains its identifier; a parameterized type is returned as Name(child1,child2,...), recursively preserving child order.
Function
canonicalizeMessageType(expression: String) → StringExamples
Example 1
expression = "Map<String,List<Int>>"return = "Map(String,List(Int))"The nested angle-bracket grammar becomes a canonical parenthesized tree.
Example 2
expression = " Envelope < Message < String > , Metadata > "return = "Envelope(Message(String),Metadata)"Whitespace is ignored while child order is preserved.
Constraints
1 <= expression.length <= 10^5- The expression is valid under the stated grammar.
- Nesting depth is at most 500.