readOptionalArgument method

MacroArgument? readOptionalArgument()

Reads […] if present (after optional spaces), balanced on braces.

Implementation

MacroArgument? readOptionalArgument() {
  skipRawSpaces();
  final open = peekRaw();
  if (!(open.kind == TokenKind.character && open.text == '[')) return null;
  popRaw();
  final tokens = <Token>[];
  var depth = 0;
  while (true) {
    final t = popRaw();
    if (t.isEof) {
      return MacroArgument(tokens, t.span.end, incomplete: true);
    }
    if (t.kind == TokenKind.character) {
      if (t.text == '{') depth++;
      if (t.text == '}') depth--;
      if (t.text == ']' && depth == 0) {
        return MacroArgument(tokens, t.span.end);
      }
    }
    tokens.add(t);
  }
}