SymbolArray parseParameterList(CharacterSource* source) { SymbolList list; Symbol parameter = parseParameter(source); if (!parameter.valid()) return list; list.add(parameter); Span span; while (Space::parseCharacter(source, ',', &span)) { Symbol parameter = parseParameter(source); if (!parameter.valid()) source->location().throwError("Expected parameter"); list.add(parameter); } return list; }
Symbol parseCase(CharacterSource* source) { static String caseKeyword("case"); static String defaultKeyword("default"); SymbolList expressions; bool defaultType; Span span; Span span2; if (Space::parseKeyword(source, caseKeyword, &span)) { defaultType = false; do { Symbol expression = parseExpressionOrFail(source); expressions.add(expression); if (!Space::parseCharacter(source, ',', &span2)) break; } while (true); } else { defaultType = true; if (!Space::parseKeyword(source, defaultKeyword, &span)) source->location().throwError("Expected case or default"); } Space::assertCharacter(source, ':', &span2); Symbol statement = parseStatementOrFail(source); SpanCache* cache = newSpan(span + spanOf(statement)); if (defaultType) return Symbol(atomDefaultCase, statement, cache); return Symbol(atomCase, SymbolArray(expressions), statement, cache); }
Symbol parseSwitchStatement(CharacterSource* source) { Span startSpan; static String switchKeyword("switch"); if (!Space::parseKeyword(source, switchKeyword, &startSpan)) return Symbol(); Span span; Space::assertCharacter(source, '(', &span); Symbol expression = parseExpressionOrFail(source); Space::assertCharacter(source, ')', &span); Space::assertCharacter(source, '{', &span); Symbol defaultCase; CharacterSource s = *source; SymbolList cases; do { Symbol c = parseCase(source); if (!c.valid()) break; if (c.atom() == atomDefaultCase) { if (defaultCase.valid()) s.location().throwError( "This switch statement already has a default case"); defaultCase = c; } else cases.add(c); } while (true); Space::assertCharacter(source, '}', &span); return Symbol(atomSwitchStatement, expression, defaultCase, SymbolArray(cases), newSpan(startSpan + span)); }
SymbolArray parseStatementSequence(CharacterSource* source) { SymbolList list; do { Symbol statement = parseStatement(source); if (!statement.valid()) return list; list.add(statement); } while (true); }