29 : lexer(new
SimpleLexer(src)), acceptSingleValue(acceptSingleValue), accceptJsStyleKeys(accceptJsStyleKeys), previousTokenEndOffset(-1, -1, -1)
33 lexer->addRule(
eWhitespace,
"Whitespace",
"[\\r\\n\\s]+");
38 lexer->addRule(
eComma,
",",
",");
39 lexer->addRule(
eColon,
":",
"\\:");
40 lexer->addRule(
eNumber,
"Number",
"-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?");
41 lexer->addRule(
eString,
"String",
"\"([^\\\\\\\"]|\\\\.)*\"");
42 lexer->addRule(
eBoolean,
"Boolean",
"true|false");
43 lexer->addRule(
eNull,
"Null",
"null");
44 lexer->addRule(
eId,
"ID",
"[_A-Za-z]\\w*");
66 if (lexer->currentTokenId != lexer->tokenFin)
68 error(
"end of string");
73 void StructuralJsonParser::next()
76 previousTokenEndOffset = lexer->getPositionInfo(lexer->pos);
86 if (acceptSingleValue)
100 return error(
"{ or [");
107 o->setLexerStartOffset(lexer->getPositionInfo(lexer->pos));
113 o->setLexerEndOffset(previousTokenEndOffset);
117 if (!accceptJsStyleKeys && lexer->currentTokenId !=
eString)
119 return error(
"String or '}'", o);
121 else if (accceptJsStyleKeys && lexer->currentTokenId !=
eString && lexer->currentTokenId !=
eId)
123 return error(
"String, ID or '}'", o);
133 while (lexer->currentTokenId ==
eComma)
147 o->setLexerEndOffset(previousTokenEndOffset);
152 return error(
"',' or '}'", o);
158 if (!accceptJsStyleKeys && lexer->currentTokenId !=
eString)
163 else if (accceptJsStyleKeys && lexer->currentTokenId !=
eString && lexer->currentTokenId !=
eId)
165 error(
"String or ID");
171 if (accceptJsStyleKeys && lexer->currentTokenId ==
eId)
173 key = lexer->currentTokenValue;
183 if (lexer->currentTokenId !=
eColon)
204 a->setLexerStartOffset(lexer->getPositionInfo(lexer->pos));
210 a->setLexerEndOffset(previousTokenEndOffset);
214 int posBeforeItem = lexer->pos;
220 if (posBeforeItem == lexer->pos)
223 return error(
"']', " + expected,
a);
231 while (lexer->currentTokenId ==
eComma)
246 a->setLexerEndOffset(previousTokenEndOffset);
251 return error(
", or ]",
a);
258 switch (lexer->currentTokenId)
274 return error(
"'{', '[', String, Number, 'true', 'false' or 'null'");
284 switch (lexer->currentTokenId)
290 return stringerror(val);
309 return error(
"String, Number, 'true', 'false' or 'null'");
312 value->setLexerStartOffset(lexer->getPositionInfo(lexer->pos));
314 value->setLexerEndOffset(previousTokenEndOffset);
320 this->err =
"Unexpected Token " + lexer->tokenIdToName(lexer->currentTokenId) +
" at " +
getlongerrposstr() +
". Expecting " + expected +
".";
321 this->expected = expected;
328 this->expected = expected;
334 return err.length() > 0;
344 lexer->positionToLineAndColumn(lexer->lastPos, line, col);
350 lexer->positionToLineAndColumn(lexer->lastPos, line, col);
357 lexer->positionToLineAndColumn(lexer->lastPos, line, col);
365 std::string::const_iterator it =
s.begin();
366 std::string::const_iterator end =
s.end() - 1;
368 if (
s.length() < 1 || *it !=
'"')
370 res =
"Missing starting \"";
376 if (
s.length() < 2 || *end !=
'"')
378 res =
"Missing ending \"";
386 if (
c ==
'\\' && it !=
s.end())
423 res =
"\\u is not supported";
427 res =
"invalid escape sequence";