parseArray function
Implementation
ValueIndex<ArrayNode>? parseArray(
String input, List<Token> tokenList, int index, Settings settings) {
// array: LEFT_BRACKET (value (COMMA value)*)? RIGHT_BRACKET
late Token startToken;
var array = ArrayNode();
var state = _ArrayState._START_;
Token token;
while (index < tokenList.length) {
token = tokenList[index];
switch (state) {
case _ArrayState._START_:
{
if (token.type == TokenType.LEFT_BRACKET) {
startToken = token;
state = _ArrayState.OPEN_ARRAY;
index++;
} else {
return null;
}
break;
}
case _ArrayState.OPEN_ARRAY:
{
if (token.type == TokenType.RIGHT_BRACKET) {
if (settings.loc) {
array = array.copyWith(
loc: Location.create(
startToken.loc!.start.line,
startToken.loc!.start.column,
startToken.loc!.start.offset,
token.loc!.end.line,
token.loc!.end.column,
token.loc!.end.offset,
settings.source));
}
return ValueIndex(array, index + 1);
} else {
final value = _parseValue<Node>(input, tokenList, index, settings);
index = value.index;
array.children.add(value.value);
state = _ArrayState.VALUE;
}
break;
}
case _ArrayState.VALUE:
{
if (token.type == TokenType.RIGHT_BRACKET) {
if (settings.loc) {
array = array.copyWith(
loc: Location.create(
startToken.loc!.start.line,
startToken.loc!.start.column,
startToken.loc!.start.offset,
token.loc!.end.line,
token.loc!.end.column,
token.loc!.end.offset,
settings.source));
}
return ValueIndex(array, index + 1);
} else if (token.type == TokenType.COMMA) {
state = _ArrayState.COMMA;
index++;
} else {
final msg = unexpectedToken(
substring(
input, token.loc!.start.offset!, token.loc!.end.offset),
settings.source,
token.loc!.start.line,
token.loc!.start.column);
throw JSONASTException(msg, input, settings.source,
token.loc!.start.line, token.loc!.start.column);
}
break;
}
case _ArrayState.COMMA:
{
final value = _parseValue<Node>(input, tokenList, index, settings);
index = value.index;
array.children.add(value.value);
state = _ArrayState.VALUE;
break;
}
}
}
throw errorEof(input, tokenList, settings);
}