From d2e14e3e1f0b5a9b96bcb1ac3e68bffc2fd9ba8d Mon Sep 17 00:00:00 2001 From: Roberto Raggi <roberto.raggi@nokia.com> Date: Thu, 2 Apr 2009 09:50:17 +0200 Subject: [PATCH] Fixed the automatic insertion of `;' tokens and improved the QtScriptParser. --- .../qtscripteditor/parser/javascript.g | 46 ++++++++------- .../parser/javascriptparser.cpp | 40 ++++++------- .../parser/javascriptparser_p.h | 6 ++ src/plugins/qtscripteditor/qtscripteditor.cpp | 58 +++++++++++-------- 4 files changed, 84 insertions(+), 66 deletions(-) diff --git a/src/plugins/qtscripteditor/parser/javascript.g b/src/plugins/qtscripteditor/parser/javascript.g index dc23055ae26..f762498ee4c 100644 --- a/src/plugins/qtscripteditor/parser/javascript.g +++ b/src/plugins/qtscripteditor/parser/javascript.g @@ -269,6 +269,12 @@ public: DiagnosticMessage(Kind kind, int line, int column, const QString &message) : kind(kind), line(line), column(column), message(message) {} + bool isWarning() const + { return kind == Warning; } + + bool isError() const + { return kind == Error; } + Kind kind; int line; int column; @@ -2083,17 +2089,15 @@ PropertyNameAndValueListOpt: PropertyNameAndValueList ; tk.dval = yylval; tk.loc = yylloc; -#if 0 const QString msg = QString::fromUtf8("Missing `;'"); diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Warning, yylloc.startLine, yylloc.startColumn, msg)); -#endif first_token = &token_buffer[0]; last_token = &token_buffer[1]; - yytoken = T_AUTOMATIC_SEMICOLON; + yytoken = T_SEMICOLON; yylval = 0; action = errorState; @@ -2103,24 +2107,6 @@ PropertyNameAndValueListOpt: PropertyNameAndValueList ; hadErrors = true; - static int tokens[] = { - T_PLUS, - T_EQ, - - T_COMMA, - T_COLON, - T_SEMICOLON, - - T_RPAREN, T_RBRACKET, T_RBRACE, - - T_NUMERIC_LITERAL, - T_IDENTIFIER, - - T_LPAREN, T_LBRACKET, T_LBRACE, - - EOF_SYMBOL - }; - token_buffer[0].token = yytoken; token_buffer[0].dval = yylval; token_buffer[0].loc = yylloc; @@ -2139,6 +2125,24 @@ PropertyNameAndValueListOpt: PropertyNameAndValueList ; goto _Lcheck_token; } + static int tokens[] = { + T_PLUS, + T_EQ, + + T_COMMA, + T_COLON, + T_SEMICOLON, + + T_RPAREN, T_RBRACKET, T_RBRACE, + + T_NUMERIC_LITERAL, + T_IDENTIFIER, + + T_LPAREN, T_LBRACKET, T_LBRACE, + + EOF_SYMBOL + }; + for (int *tk = tokens; *tk != EOF_SYMBOL; ++tk) { int a = t_action(errorState, *tk); if (a > 0 && t_action(a, yytoken)) { diff --git a/src/plugins/qtscripteditor/parser/javascriptparser.cpp b/src/plugins/qtscripteditor/parser/javascriptparser.cpp index b1d1508b604..794c416b763 100644 --- a/src/plugins/qtscripteditor/parser/javascriptparser.cpp +++ b/src/plugins/qtscripteditor/parser/javascriptparser.cpp @@ -1094,17 +1094,15 @@ case 266: { tk.dval = yylval; tk.loc = yylloc; -#if 0 const QString msg = QString::fromUtf8("Missing `;'"); diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Warning, yylloc.startLine, yylloc.startColumn, msg)); -#endif first_token = &token_buffer[0]; last_token = &token_buffer[1]; - yytoken = T_AUTOMATIC_SEMICOLON; + yytoken = T_SEMICOLON; yylval = 0; action = errorState; @@ -1114,24 +1112,6 @@ case 266: { hadErrors = true; - static int tokens[] = { - T_PLUS, - T_EQ, - - T_COMMA, - T_COLON, - T_SEMICOLON, - - T_RPAREN, T_RBRACKET, T_RBRACE, - - T_NUMERIC_LITERAL, - T_IDENTIFIER, - - T_LPAREN, T_LBRACKET, T_LBRACE, - - EOF_SYMBOL - }; - token_buffer[0].token = yytoken; token_buffer[0].dval = yylval; token_buffer[0].loc = yylloc; @@ -1150,6 +1130,24 @@ case 266: { goto _Lcheck_token; } + static int tokens[] = { + T_PLUS, + T_EQ, + + T_COMMA, + T_COLON, + T_SEMICOLON, + + T_RPAREN, T_RBRACKET, T_RBRACE, + + T_NUMERIC_LITERAL, + T_IDENTIFIER, + + T_LPAREN, T_LBRACKET, T_LBRACE, + + EOF_SYMBOL + }; + for (int *tk = tokens; *tk != EOF_SYMBOL; ++tk) { int a = t_action(errorState, *tk); if (a > 0 && t_action(a, yytoken)) { diff --git a/src/plugins/qtscripteditor/parser/javascriptparser_p.h b/src/plugins/qtscripteditor/parser/javascriptparser_p.h index 17a1f250adc..d3b4e70208f 100644 --- a/src/plugins/qtscripteditor/parser/javascriptparser_p.h +++ b/src/plugins/qtscripteditor/parser/javascriptparser_p.h @@ -120,6 +120,12 @@ public: DiagnosticMessage(Kind kind, int line, int column, const QString &message) : kind(kind), line(line), column(column), message(message) {} + bool isWarning() const + { return kind == Warning; } + + bool isError() const + { return kind == Error; } + Kind kind; int line; int column; diff --git a/src/plugins/qtscripteditor/qtscripteditor.cpp b/src/plugins/qtscripteditor/qtscripteditor.cpp index e6968645041..916567797ed 100644 --- a/src/plugins/qtscripteditor/qtscripteditor.cpp +++ b/src/plugins/qtscripteditor/qtscripteditor.cpp @@ -86,11 +86,15 @@ protected: virtual bool visit(FunctionDeclaration *ast) { + if (! ast->name) + return false; + QString text = ast->name->asString(); text += QLatin1Char('('); for (FormalParameterList *it = ast->formals; it; it = it->next) { - text += it->name->asString(); + if (it->name) + text += it->name->asString(); if (it->next) text += QLatin1String(", "); @@ -100,7 +104,7 @@ protected: Declaration d; d.text = text; - d.startLine= ast->startLine; + d.startLine = ast->startLine; d.startColumn = ast->startColumn; d.endLine = ast->endLine; d.endColumn = ast->endColumn; @@ -112,6 +116,9 @@ protected: virtual bool visit(VariableDeclaration *ast) { + if (! ast->name) + return false; + Declaration d; d.text = ast->name->asString(); d.startLine= ast->startLine; @@ -201,20 +208,23 @@ void ScriptEditor::updateDocumentNow() lexer.setCode(code, /*line = */ 1); driver.setLexer(&lexer); - parser.parse(&driver); + if (parser.parse(&driver)) { + JavaScript::AST::Visitor v; + driver.ast()->accept(&v); - FindDeclarations decls; - m_declarations = decls.accept(driver.ast()); + FindDeclarations decls; + m_declarations = decls.accept(driver.ast()); - QStringList items; - items.append(tr("<Select Symbol>")); + QStringList items; + items.append(tr("<Select Symbol>")); - foreach (Declaration decl, m_declarations) - items.append(decl.text); + foreach (Declaration decl, m_declarations) + items.append(decl.text); - m_methodCombo->clear(); - m_methodCombo->addItems(items); - updateMethodBoxIndex(); + m_methodCombo->clear(); + m_methodCombo->addItems(items); + updateMethodBoxIndex(); + } QList<QTextEdit::ExtraSelection> selections; @@ -229,21 +239,20 @@ void ScriptEditor::updateDocumentNow() QTextEdit::ExtraSelection sel; foreach (const JavaScriptParser::DiagnosticMessage &d, parser.diagnosticMessages()) { - const int line = d.line; + if (d.isWarning()) + continue; - QTextCursor c(document()->findBlockByNumber(line - 1)); - sel.cursor = c; + int line = d.line; + int column = d.column; - if (parser.errorColumnNumber() > 1) - sel.cursor.setPosition(c.position() + d.column - 1); + if (column == 0) + column = 1; - if (d.kind == JavaScriptParser::DiagnosticMessage::Warning) { - sel.format = warningFormat; - sel.cursor.movePosition(QTextCursor::StartOfWord); - } else { - sel.format = errorFormat; - } + sel.format = errorFormat; + QTextCursor c(document()->findBlockByNumber(line - 1)); + sel.cursor = c; + sel.cursor.setPosition(c.position() + column - 1); sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); selections.append(sel); @@ -328,7 +337,8 @@ static void indentScriptBlock(const TextEditor::TabSettings &ts, { typedef typename SharedTools::Indenter<Iterator> Indenter ; Indenter &indenter = Indenter::instance(); - indenter.setIndentSize(ts.m_tabSize); + indenter.setTabSize(ts.m_tabSize); + indenter.setIndentSize(ts.m_indentSize); const TextEditor::TextBlockIterator current(block); const int indent = indenter.indentForBottomLine(current, programBegin, programEnd, typedChar); -- GitLab