void QmlCodeParser::parseSourceFile(const Location& location, const QString& filePath, Tree *tree) { QFile in(filePath); if (!in.open(QIODevice::ReadOnly)) { location.error(tr("Cannot open QML file '%1'").arg(filePath)); return; } QString document = in.readAll(); in.close(); Location fileLocation(filePath); QString newCode = document; extractPragmas(newCode); lexer->setCode(newCode, 1); QSet<QString> topicCommandsAllowed = topicCommands(); QSet<QString> otherMetacommandsAllowed = otherMetaCommands(); QSet<QString> metacommandsAllowed = topicCommandsAllowed + otherMetacommandsAllowed; QDeclarativeJS::NodePool m_nodePool(filePath, &engine); if (parser->parse()) { QDeclarativeJS::AST::UiProgram *ast = parser->ast(); QmlDocVisitor visitor(filePath, newCode, &engine, tree, metacommandsAllowed); QDeclarativeJS::AST::Node::accept(ast, &visitor); } }
QString JsCodeMarker::addMarkUp(const QString &code, const Node * /* relative */, const Location &location) { QDeclarativeJS::Engine engine; QDeclarativeJS::Lexer lexer(&engine); QString newCode = code; QList<QDeclarativeJS::AST::SourceLocation> pragmas = extractPragmas(newCode); lexer.setCode(newCode, 1); QDeclarativeJS::Parser parser(&engine); QDeclarativeJS::NodePool m_nodePool("<JsCodeMarker::addMarkUp>", &engine); QString output; if (parser.parseProgram()) { QDeclarativeJS::AST::Node *ast = parser.rootNode(); // Pass the unmodified code to the visitor so that pragmas and other // unhandled source text can be output. QmlMarkupVisitor visitor(code, pragmas, &engine); QDeclarativeJS::AST::Node::accept(ast, &visitor); output = visitor.markedUpCode(); } else { location.warning(tr("Unable to parse JavaScript: \"%1\" at line %2, column %3").arg( parser.errorMessage()).arg(parser.errorLineNumber()).arg( parser.errorColumnNumber())); output = protect(code); } return output; }
/*! Returns true if the \a code is recognized by the parser. */ bool JsCodeMarker::recognizeCode(const QString &code) { QDeclarativeJS::Engine engine; QDeclarativeJS::Lexer lexer(&engine); QDeclarativeJS::Parser parser(&engine); QDeclarativeJS::NodePool m_nodePool("<JsCodeMarker::recognizeCode>", &engine); QString newCode = code; QList<QDeclarativeJS::AST::SourceLocation> pragmas = extractPragmas(newCode); lexer.setCode(newCode, 1); return parser.parseProgram(); }
/*! Returns true if the \a code is recognized by the parser. */ bool QmlCodeMarker::recognizeCode(const QString &code) { QDeclarativeJS::Engine engine; QDeclarativeJS::Lexer lexer(&engine); QDeclarativeJS::Parser parser(&engine); QDeclarativeJS::NodePool m_nodePool("<QmlCodeMarker::recognizeCode>", &engine); QString newCode = code; extractPragmas(newCode); lexer.setCode(newCode, 1); return parser.parse(); }