Appearance
| 1 | namespace Syntax.Parsers.Expressions is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use Source | |
| 5 | ||
| 6 | class LIST(expression_parser: Parser[Trees.Expressions.Expression]): Base[Trees.Expressions.LIST] is | |
| 7 | super() | |
| 8 | ||
| 9 | description: string => "expression list" | |
| 10 | ||
| 11 | parse(context: CONTEXT) -> Trees.Expressions.LIST is | |
| 12 | let start = context.location | |
| 13 | ||
| 14 | let first = parse_element(context) | |
| 15 | ||
| 16 | if !first? then | |
| 17 | return Trees.Expressions.LIST(start::context.location, Collections.LIST[Trees.Expressions.Expression]()) | |
| 18 | fi | |
| 19 | ||
| 20 | return parse_after(context, start, first) | |
| 21 | si | |
| 22 | ||
| 23 | parse_element(context: CONTEXT) -> Trees.Expressions.Expression? is | |
| 24 | context.allow_tuple_element = true | |
| 25 | let expression = expression_parser.parse(context) | |
| 26 | context.allow_tuple_element = false | |
| 27 | ||
| 28 | return expression | |
| 29 | si | |
| 30 | ||
| 31 | // The rest of a list whose first element the caller has already | |
| 32 | // parsed - a list literal looks at the token after its first | |
| 33 | // element before deciding it is a list at all. | |
| 34 | parse_after(context: CONTEXT, start: LOCATION, first: Trees.Expressions.Expression) -> Trees.Expressions.LIST is | |
| 35 | let expressions = Collections.LIST[Trees.Expressions.Expression]() | |
| 36 | let expression mut = first | |
| 37 | ||
| 38 | do | |
| 39 | expressions.add(expression) | |
| 40 | ||
| 41 | if context.current.token != Lexical.TOKEN.COMMA then | |
| 42 | return Trees.Expressions.LIST(start::expression.location, expressions) | |
| 43 | fi | |
| 44 | ||
| 45 | context.next_token() | |
| 46 | ||
| 47 | // Trailing comma: stop when a closing bracket follows. | |
| 48 | // The same parser drives list literals (`[a, b,]`), | |
| 49 | // tuple literals (`(a, b,)`) and call argument lists | |
| 50 | // (`f(a, b,)`), so one check covers all three. | |
| 51 | if | |
| 52 | context.current.token == Lexical.TOKEN.SQUARE_CLOSE \/ | |
| 53 | context.current.token == Lexical.TOKEN.PAREN_CLOSE | |
| 54 | then | |
| 55 | return Trees.Expressions.LIST(start::context.location, expressions) | |
| 56 | fi | |
| 57 | ||
| 58 | // The expression parser chain bottoms out at a poisoned | |
| 59 | // placeholder rather than null, but that's an invariant of | |
| 60 | // the current wiring, not a contract: guard here so a | |
| 61 | // future element parser without that override can't put | |
| 62 | // a null in the list. | |
| 63 | let next = parse_element(context) | |
| 64 | ||
| 65 | if !next? then | |
| 66 | break | |
| 67 | fi | |
| 68 | ||
| 69 | expression = next | |
| 70 | od | |
| 71 | ||
| 72 | return Trees.Expressions.LIST(start::context.location, expressions) | |
| 73 | si | |
| 74 | si | |
| 75 | si |