237 auto leave = [
this](
bool statementDone =
false) { this->
leaveState(statementDone); };
241 qCDebug(formatterLog) <<
"Starting to look at " <<
line;
245 const int kind = currentToken.lexKind;
257 case StateType::TopmostIntro:
259 case QQmlJSGrammar::T_IDENTIFIER:
260 enter(StateType::ObjectdefinitionOrJs);
262 case QQmlJSGrammar::T_IMPORT:
263 enter(StateType::TopQml);
265 case QQmlJSGrammar::T_LBRACE:
266 enter(StateType::TopJs);
267 enter(StateType::Expression);
270 enter(StateType::TopJs);
275 case StateType::TopQml:
277 case QQmlJSGrammar::T_IMPORT:
278 enter(StateType::ImportStart);
280 case QQmlJSGrammar::T_IDENTIFIER:
281 enter(StateType::BindingOrObjectdefinition);
285 enter(StateType::BindingOrObjectdefinition);
290 case StateType::TopJs:
294 case StateType::ObjectdefinitionOrJs:
296 case QQmlJSGrammar::T_DOT:
298 case QQmlJSGrammar::T_LBRACE:
299 turnInto(StateType::BindingOrObjectdefinition);
303 turnInto(StateType::TopJs);
309 case StateType::ImportStart:
310 enter(StateType::ImportMaybeDotOrVersionOrAs);
313 case StateType::ImportMaybeDotOrVersionOrAs:
315 case QQmlJSGrammar::T_DOT:
316 turnInto(StateType::ImportDot);
318 case QQmlJSGrammar::T_AS:
319 turnInto(StateType::ImportAs);
321 case QQmlJSGrammar::T_NUMERIC_LITERAL:
322 case QQmlJSGrammar::T_VERSION_NUMBER:
323 turnInto(StateType::ImportMaybeAs);
332 case StateType::ImportMaybeAs:
334 case QQmlJSGrammar::T_AS:
335 turnInto(StateType::ImportAs);
344 case StateType::ImportDot:
346 turnInto(StateType::ImportMaybeDotOrVersionOrAs);
354 case StateType::ImportAs:
361 case StateType::BindingOrObjectdefinition:
363 case QQmlJSGrammar::T_COLON:
364 enter(StateType::BindingAssignment);
366 case QQmlJSGrammar::T_LBRACE:
367 enter(StateType::ObjectdefinitionOpen);
372 case StateType::BindingAssignment:
374 case QQmlJSGrammar::T_AUTOMATIC_SEMICOLON:
375 case QQmlJSGrammar::T_COMPATIBILITY_SEMICOLON:
376 case QQmlJSGrammar::T_SEMICOLON:
379 case QQmlJSGrammar::T_IF:
380 enter(StateType::IfStatement);
382 case QQmlJSGrammar::T_WITH:
383 enter(StateType::StatementWithCondition);
385 case QQmlJSGrammar::T_TRY:
386 enter(StateType::TryStatement);
388 case QQmlJSGrammar::T_SWITCH:
389 enter(StateType::SwitchStatement);
391 case QQmlJSGrammar::T_LBRACE:
392 enter(StateType::JsblockOpen);
394 case QQmlJSGrammar::T_ON:
395 case QQmlJSGrammar::T_AS:
396 case QQmlJSGrammar::T_IMPORT:
397 case QQmlJSGrammar::T_SIGNAL:
398 case QQmlJSGrammar::T_PROPERTY:
399 case QQmlJSGrammar::T_REQUIRED:
400 case QQmlJSGrammar::T_READONLY:
401 case QQmlJSGrammar::T_IDENTIFIER:
402 enter(StateType::ExpressionOrObjectdefinition);
406 case QQmlJSGrammar::T_RBRACKET:
407 case QQmlJSGrammar::T_RPAREN:
412 enter(StateType::Expression);
417 case StateType::ObjectdefinitionOpen:
419 case QQmlJSGrammar::T_RBRACE:
422 case QQmlJSGrammar::T_DEFAULT:
423 case QQmlJSGrammar::T_READONLY:
424 enter(StateType::PropertyModifiers);
426 case QQmlJSGrammar::T_PROPERTY:
427 enter(StateType::PropertyStart);
429 case QQmlJSGrammar::T_REQUIRED:
430 enter(StateType::RequiredProperty);
432 case QQmlJSGrammar::T_COMPONENT:
433 enter(StateType::ComponentStart);
435 case QQmlJSGrammar::T_FUNCTION:
436 case QQmlJSGrammar::T_FUNCTION_STAR:
437 enter(StateType::FunctionStart);
439 case QQmlJSGrammar::T_SIGNAL:
440 enter(StateType::SignalStart);
442 case QQmlJSGrammar::T_ENUM:
443 enter(StateType::EnumStart);
445 case QQmlJSGrammar::T_ON:
446 case QQmlJSGrammar::T_AS:
447 case QQmlJSGrammar::T_IMPORT:
448 enter(StateType::BindingOrObjectdefinition);
452 enter(StateType::BindingOrObjectdefinition);
457 case StateType::PropertyModifiers:
459 case QQmlJSGrammar::T_PROPERTY:
460 turnInto(StateType::PropertyStart);
462 case QQmlJSGrammar::T_DEFAULT:
463 case QQmlJSGrammar::T_READONLY:
465 case QQmlJSGrammar::T_REQUIRED:
466 turnInto(StateType::RequiredProperty);
474 case StateType::PropertyStart:
476 case QQmlJSGrammar::T_COLON:
477 enter(StateType::BindingAssignment);
479 case QQmlJSGrammar::T_VAR:
480 case QQmlJSGrammar::T_IDENTIFIER:
481 enter(StateType::PropertyName);
485 enter(StateType::PropertyListOpen);
493 case StateType::RequiredProperty:
495 case QQmlJSGrammar::T_PROPERTY:
496 turnInto(StateType::PropertyStart);
498 case QQmlJSGrammar::T_DEFAULT:
499 case QQmlJSGrammar::T_READONLY:
500 turnInto(StateType::PropertyModifiers);
502 case QQmlJSGrammar::T_IDENTIFIER:
511 case StateType::ComponentStart:
513 case QQmlJSGrammar::T_IDENTIFIER:
514 turnInto(StateType::ComponentName);
522 case StateType::ComponentName:
524 case QQmlJSGrammar::T_COLON:
525 enter(StateType::BindingAssignment);
533 case StateType::PropertyName:
534 turnInto(StateType::PropertyMaybeInitializer);
537 case StateType::PropertyListOpen: {
540 turnInto(StateType::PropertyName);
543 case StateType::PropertyMaybeInitializer:
545 case QQmlJSGrammar::T_COLON:
546 turnInto(StateType::BindingAssignment);
554 case StateType::EnumStart:
556 case QQmlJSGrammar::T_LBRACE:
557 enter(StateType::ObjectliteralOpen);
562 case StateType::SignalStart:
564 case QQmlJSGrammar::T_COLON:
565 enter(StateType::BindingAssignment);
568 enter(StateType::SignalMaybeArglist);
573 case StateType::SignalMaybeArglist:
575 case QQmlJSGrammar::T_LPAREN:
576 turnInto(StateType::SignalArglistOpen);
584 case StateType::SignalArglistOpen:
586 case QQmlJSGrammar::T_RPAREN:
592 case StateType::FunctionStart:
594 case QQmlJSGrammar::T_LPAREN:
595 enter(StateType::FunctionArglistOpen);
600 case StateType::FunctionArglistOpen:
602 case QQmlJSGrammar::T_COLON:
603 enter(StateType::TypeAnnotation);
605 case QQmlJSGrammar::T_RPAREN:
606 turnInto(StateType::FunctionArglistClosed);
611 case StateType::FunctionArglistClosed:
613 case QQmlJSGrammar::T_COLON:
614 enter(StateType::TypeAnnotation);
616 case QQmlJSGrammar::T_LBRACE:
617 turnInto(StateType::JsblockOpen);
625 case StateType::TypeAnnotation:
627 case QQmlJSGrammar::T_IDENTIFIER:
628 case QQmlJSGrammar::T_DOT:
630 case QQmlJSGrammar::T_LT:
631 turnInto(StateType::TypeParameter);
639 case StateType::TypeParameter:
641 case QQmlJSGrammar::T_LT:
642 enter(StateType::TypeParameter);
644 case QQmlJSGrammar::T_GT:
650 case StateType::ExpressionOrObjectdefinition:
652 case QQmlJSGrammar::T_DOT:
655 case QQmlJSGrammar::T_LBRACE:
656 turnInto(StateType::ObjectdefinitionOpen);
660 case QQmlJSGrammar::T_RBRACKET:
661 case QQmlJSGrammar::T_RPAREN:
669 enter(StateType::Expression);
674 case StateType::ExpressionOrLabel:
676 case QQmlJSGrammar::T_COLON:
677 turnInto(StateType::LabelledStatement);
681 case QQmlJSGrammar::T_RBRACKET:
682 case QQmlJSGrammar::T_RPAREN:
687 enter(StateType::Expression);
692 case StateType::TernaryOp:
693 if (kind == QQmlJSGrammar::T_COLON) {
694 enter(StateType::TernaryOpAfterColon);
695 enter(StateType::ExpressionContinuation);
699 case StateType::TernaryOpAfterColon:
700 case StateType::Expression:
704 case QQmlJSGrammar::T_COMMA:
707 case QQmlJSGrammar::T_RBRACKET:
708 case QQmlJSGrammar::T_RPAREN:
711 case QQmlJSGrammar::T_RBRACE:
714 case QQmlJSGrammar::T_AUTOMATIC_SEMICOLON:
715 case QQmlJSGrammar::T_COMPATIBILITY_SEMICOLON:
716 case QQmlJSGrammar::T_SEMICOLON:
721 enter(StateType::ExpressionContinuation);
726 case StateType::ExpressionContinuation:
730 case StateType::ExpressionMaybeContinuation:
732 case QQmlJSGrammar::T_QUESTION:
733 case QQmlJSGrammar::T_LBRACKET:
734 case QQmlJSGrammar::T_LPAREN:
735 case QQmlJSGrammar::T_LBRACE:
744 case StateType::ParenOpen:
748 case QQmlJSGrammar::T_RPAREN:
754 case StateType::BracketOpen:
758 case QQmlJSGrammar::T_COMMA:
759 enter(StateType::BracketElementStart);
761 case QQmlJSGrammar::T_RBRACKET:
767 case StateType::ObjectliteralOpen:
771 case QQmlJSGrammar::T_COLON:
772 enter(StateType::ObjectliteralAssignment);
774 case QQmlJSGrammar::T_RBRACKET:
775 case QQmlJSGrammar::T_RPAREN:
778 case QQmlJSGrammar::T_RBRACE:
785 case StateType::ObjectliteralAssignment:
789 case QQmlJSGrammar::T_COMMA:
792 case QQmlJSGrammar::T_RBRACKET:
793 case QQmlJSGrammar::T_RPAREN:
796 case QQmlJSGrammar::T_RBRACE:
801 enter(StateType::ExpressionContinuation);
806 case StateType::BracketElementStart:
808 turnInto(StateType::BracketElementMaybeObjectdefinition);
815 case StateType::BracketElementMaybeObjectdefinition:
817 case QQmlJSGrammar::T_LBRACE:
818 turnInto(StateType::ObjectdefinitionOpen);
826 case StateType::JsblockOpen:
827 case StateType::SubstatementOpen:
831 case QQmlJSGrammar::T_RBRACE:
837 case StateType::LabelledStatement:
843 case StateType::Substatement:
845 if (kind != QQmlJSGrammar::T_LBRACE) {
850 case QQmlJSGrammar::T_LBRACE:
851 turnInto(StateType::SubstatementOpen);
856 case StateType::IfStatement:
858 case QQmlJSGrammar::T_LPAREN:
859 enter(StateType::ConditionOpen);
867 case StateType::MaybeElse:
869 case QQmlJSGrammar::T_ELSE:
870 turnInto(StateType::ElseClause);
871 enter(StateType::Substatement);
879 case StateType::MaybeCatchOrFinally:
881 case QQmlJSGrammar::T_CATCH:
882 turnInto(StateType::CatchStatement);
884 case QQmlJSGrammar::T_FINALLY:
885 turnInto(StateType::FinallyStatement);
893 case StateType::ElseClause:
900 case StateType::ConditionOpen:
904 case QQmlJSGrammar::T_RPAREN:
905 turnInto(StateType::Substatement);
910 case StateType::SwitchStatement:
911 case StateType::CatchStatement:
912 case StateType::StatementWithCondition:
914 case QQmlJSGrammar::T_LPAREN:
915 enter(StateType::StatementWithConditionParenOpen);
922 case StateType::StatementWithConditionParenOpen:
926 case QQmlJSGrammar::T_RPAREN:
927 turnInto(StateType::Substatement);
932 case StateType::TryStatement:
933 case StateType::FinallyStatement:
935 case QQmlJSGrammar::T_LBRACE:
936 enter(StateType::JsblockOpen);
944 case StateType::DoStatement:
946 case QQmlJSGrammar::T_WHILE:
948 case QQmlJSGrammar::T_LPAREN:
949 enter(StateType::DoStatementWhileParenOpen);
957 case StateType::DoStatementWhileParenOpen:
961 case QQmlJSGrammar::T_RPAREN:
968 case StateType::BreakcontinueStatement:
969 if (Token ::lexKindIsIdentifier(kind)) {
977 case StateType::CaseStart:
979 case QQmlJSGrammar::T_COLON:
980 turnInto(StateType::CaseCont);
985 case StateType::CaseCont:
986 if (kind != QQmlJSGrammar::T_CASE && kind != QQmlJSGrammar::T_DEFAULT &&
tryStatement())
989 case QQmlJSGrammar::T_RBRACE:
992 case QQmlJSGrammar::T_DEFAULT:
993 case QQmlJSGrammar::T_CASE:
999 case StateType::MultilineCommentStart:
1000 case StateType::MultilineCommentCont:
1009 turnInto(StateType::MultilineCommentCont);
1024 if (topState == StateType::ExpressionOrLabel)
1027 else if (topState == StateType::BreakcontinueStatement)
1033 if (topState == StateType::Expression || topState == StateType::ExpressionOrObjectdefinition
1034 || topState == StateType::ObjectliteralAssignment
1035 || topState == StateType::TernaryOpAfterColon) {
1036 enterState(StateType::ExpressionMaybeContinuation);
1039 if (topState != StateType::MultilineCommentStart && topState != StateType::MultilineCommentCont
1041 enterState(StateType::MultilineCommentStart);
1056 if (topState.type == StateType::MultilineCommentStart
1057 || topState.type == StateType::MultilineCommentCont) {
1069 switch (tokenKind) {
1070 case QQmlJSGrammar::T_LBRACE:
1071 if (topState.type == StateType::Substatement
1072 || topState.type == StateType::BindingAssignment
1073 || topState.type == StateType::CaseCont) {
1074 return topState.savedIndentDepth;
1077 case QQmlJSGrammar::T_RBRACE: {
1078 if (topState.type == StateType::JsblockOpen && previousState.
type == StateType::CaseCont) {
1081 for (
int i = 0; oldStatus.
state(
i).
type != StateType::TopmostIntro; ++
i) {
1083 if (
type == StateType::ObjectdefinitionOpen ||
type == StateType::JsblockOpen
1084 ||
type == StateType::SubstatementOpen ||
type == StateType::ObjectliteralOpen) {
1090 case QQmlJSGrammar::T_RBRACKET:
1091 for (
int i = 0; oldStatus.
state(
i).
type != StateType::TopmostIntro; ++
i) {
1093 if (
type == StateType::BracketOpen) {
1098 case QQmlJSGrammar::T_LBRACKET:
1099 case QQmlJSGrammar::T_LPAREN:
1100 if (topState.type == StateType::ExpressionMaybeContinuation)
1101 return topState.savedIndentDepth;
1103 case QQmlJSGrammar::T_ELSE:
1104 if (topState.type == StateType::MaybeElse) {
1106 }
else if (topState.type == StateType::ExpressionMaybeContinuation) {
1107 bool hasElse =
false;
1108 for (
int i = 1; oldStatus.
state(
i).
type != StateType::TopmostIntro; ++
i) {
1110 if (
type == StateType::ElseClause)
1112 if (
type == StateType::IfStatement) {
1122 case QQmlJSGrammar::T_CATCH:
1123 case QQmlJSGrammar::T_FINALLY:
1124 if (topState.type == StateType::MaybeCatchOrFinally)
1127 case QQmlJSGrammar::T_COLON:
1128 if (topState.type == StateType::TernaryOp)
1129 return indentDepth - 2;
1131 case QQmlJSGrammar::T_QUESTION:
1132 if (topState.type == StateType::ExpressionMaybeContinuation)
1133 return topState.savedIndentDepth;
1136 case QQmlJSGrammar::T_DEFAULT:
1137 case QQmlJSGrammar::T_CASE:
1138 for (
int i = 0; oldStatus.
state(
i).
type != StateType::TopmostIntro; ++
i) {
1140 if (
type == StateType::SwitchStatement ||
type == StateType::CaseCont) {
1142 }
else if (
type == StateType::TopmostIntro) {
1149 && topState.type == StateType::ExpressionMaybeContinuation)
1150 return topState.savedIndentDepth;
1204 int *savedIndentDepth)
const
1208 const int tokenPosition =
column(tk.begin());
1213 case StateType::ObjectdefinitionOpen: {
1215 if (parentState.type == StateType::BindingAssignment)
1219 *savedIndentDepth = tokenPosition;
1225 case StateType::BindingOrObjectdefinition:
1227 *indentDepth = *savedIndentDepth = tokenPosition;
1230 case StateType::BindingAssignment:
1231 case StateType::ObjectliteralAssignment:
1238 case StateType::ExpressionOrObjectdefinition:
1239 *indentDepth = tokenPosition;
1242 case StateType::ExpressionOrLabel:
1243 if (*indentDepth == tokenPosition)
1246 *indentDepth = tokenPosition;
1249 case StateType::Expression:
1250 if (*indentDepth == tokenPosition) {
1253 if (parentState.type != StateType::ExpressionOrObjectdefinition
1254 && parentState.type != StateType::ExpressionOrLabel
1255 && parentState.type != StateType::BindingAssignment) {
1261 else if (parentState.type != StateType::ExpressionOrObjectdefinition
1262 && parentState.type != StateType::ExpressionOrLabel) {
1263 *indentDepth = tokenPosition;
1267 case StateType::ExpressionMaybeContinuation:
1279 case StateType::BracketOpen:
1280 if (parentState.type == StateType::Expression
1284 }
else if (parentState.type == StateType::ObjectliteralAssignment) {
1285 *savedIndentDepth = parentState.savedIndentDepth;
1287 }
else if (!lastToken) {
1288 *indentDepth = tokenPosition + 1;
1294 case StateType::FunctionStart:
1299 case StateType::DoStatementWhileParenOpen:
1300 case StateType::StatementWithConditionParenOpen:
1301 case StateType::SignalArglistOpen:
1302 case StateType::FunctionArglistOpen:
1303 case StateType::ParenOpen:
1305 *indentDepth = tokenPosition + 1;
1310 case StateType::TernaryOp:
1312 *indentDepth = tokenPosition + tk.length + 1;
1317 case StateType::JsblockOpen:
1319 if (parentState.type == StateType::CaseCont) {
1320 *savedIndentDepth = parentState.savedIndentDepth;
1324 case StateType::SubstatementOpen:
1326 if (parentState.type == StateType::BindingAssignment)
1331 case StateType::Substatement:
1335 case StateType::ObjectliteralOpen:
1336 if (parentState.type == StateType::Expression
1337 || parentState.type == StateType::ObjectliteralAssignment) {
1342 *indentDepth = parentState.savedIndentDepth;
1343 *savedIndentDepth = *indentDepth;
1348 case StateType::StatementWithCondition:
1349 case StateType::TryStatement:
1350 case StateType::CatchStatement:
1351 case StateType::FinallyStatement:
1352 case StateType::IfStatement:
1353 case StateType::DoStatement:
1354 case StateType::SwitchStatement:
1355 if (firstToken || parentState.type == StateType::BindingAssignment)
1356 *savedIndentDepth = tokenPosition;
1358 *indentDepth = *savedIndentDepth;
1360 if (!firstToken &&
newState == StateType::IfStatement
1361 && parentState.type == StateType::Substatement
1364 *savedIndentDepth = *indentDepth;
1368 case StateType::MaybeElse:
1369 case StateType::MaybeCatchOrFinally: {
1371 int lastNonEndState = 0;
1379 case StateType::ConditionOpen:
1384 *indentDepth = tokenPosition + 1;
1387 case StateType::CaseStart:
1388 *savedIndentDepth = tokenPosition;
1391 case StateType::CaseCont:
1395 case StateType::MultilineCommentStart:
1396 *indentDepth = tokenPosition + 2;
1399 case StateType::MultilineCommentCont:
1400 *indentDepth = tokenPosition;