parseObject function
Implementation
ValueIndex<ObjectNode>? parseObject(
input, List<Token> tokenList, int index, Settings settings) {
// object: LEFT_BRACE (property (COMMA property)*)? RIGHT_BRACE
late Token startToken;
final object = new ObjectNode();
_ObjectState state = _ObjectState._START_;
while (index < tokenList.length) {
final token = tokenList[index];
switch (state) {
case _ObjectState._START_:
{
if (token.type == TokenType.LEFT_BRACE) {
startToken = token;
state = _ObjectState.OPEN_OBJECT;
index++;
} else {
return null;
}
break;
}
case _ObjectState.OPEN_OBJECT:
{
if (token.type == TokenType.RIGHT_BRACE) {
if (settings.loc) {
final src = settings.source ?? "";
object.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,
src);
}
return new ValueIndex(object, index + 1);
} else {
final property = parseProperty(input, tokenList, index, settings);
if (property != null) {
object.children.add(property.value);
state = _ObjectState.PROPERTY;
index = property.index;
} else {
final src = settings.source ?? "";
final msg = unexpectedToken(
substring(
input, token.loc!.start.offset, token.loc!.end.offset),
src,
token.loc!.start.line,
token.loc!.start.column);
throw new JSONASTException(msg, input, src, token.loc!.start.line,
token.loc!.start.column);
}
}
break;
}
case _ObjectState.PROPERTY:
{
if (token.type == TokenType.RIGHT_BRACE) {
if (settings.loc != null) {
final src = settings.source ?? "";
object.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,
src);
}
return new ValueIndex(object, index + 1);
} else if (token.type == TokenType.COMMA) {
state = _ObjectState.COMMA;
index++;
} else {
final src = settings.source ?? "";
final msg = unexpectedToken(
substring(
input, token.loc!.start.offset, token.loc!.end.offset),
src,
token.loc!.start.line,
token.loc!.start.column);
throw new JSONASTException(msg, input, src, token.loc!.start.line,
token.loc!.start.column);
}
break;
}
case _ObjectState.COMMA:
{
final property = parseProperty(input, tokenList, index, settings);
if (property != null) {
index = property.index;
object.children.add(property.value);
state = _ObjectState.PROPERTY;
} else {
final src = settings.source ?? "";
final msg = unexpectedToken(
substring(
input, token.loc!.start.offset, token.loc!.end.offset),
src,
token.loc!.start.line,
token.loc!.start.column);
throw new JSONASTException(msg, input, src, token.loc!.start.line,
token.loc!.start.column);
}
break;
}
}
}
throw errorEof(input, tokenList, settings);
}