Qt 4.8
Classes | Public Functions | Static Public Functions | Public Variables | List of all members
QDeclarativeScriptParser Class Reference

#include <qdeclarativescriptparser_p.h>

Classes

class  Import
 
class  JavaScriptMetaData
 
class  TypeReference
 

Public Functions

void clear ()
 
QList< QDeclarativeErrorerrors () const
 
TypeReferencefindOrCreateType (const QString &name)
 
QList< Importimports () const
 
bool parse (const QByteArray &data, const QUrl &url=QUrl())
 
 QDeclarativeScriptParser ()
 
QList< TypeReference * > referencedTypes () const
 
QString scriptFile () const
 
void setScriptFile (const QString &filename)
 
void setTree (QDeclarativeParser::Object *tree)
 
QDeclarativeParser::Objecttree () const
 
 ~QDeclarativeScriptParser ()
 

Static Public Functions

static JavaScriptMetaData extractMetaData (QString &)
 
static QDeclarativeParser::Object::ScriptBlock::Pragmas extractPragmas (QString &)
 

Public Variables

QList< QDeclarativeError_errors
 
QList< Import_imports
 
QList< TypeReference * > _refTypes
 
QString _scriptFile
 
QDeclarativeScriptParserJsASTDatadata
 
QDeclarativeParser::Objectroot
 

Detailed Description

Definition at line 70 of file qdeclarativescriptparser_p.h.

Constructors and Destructors

◆ QDeclarativeScriptParser()

QDeclarativeScriptParser::QDeclarativeScriptParser ( )

Definition at line 800 of file qdeclarativescriptparser.cpp.

801 : root(0), data(0)
802 {
803 
804 }
QDeclarativeScriptParserJsASTData * data
QDeclarativeParser::Object * root

◆ ~QDeclarativeScriptParser()

QDeclarativeScriptParser::~QDeclarativeScriptParser ( )

Definition at line 806 of file qdeclarativescriptparser.cpp.

807 {
808  clear();
809 }

Functions

◆ clear()

void QDeclarativeScriptParser::clear ( )

Definition at line 1164 of file qdeclarativescriptparser.cpp.

Referenced by QDeclarativeTypeData::done(), parse(), and ~QDeclarativeScriptParser().

1165 {
1166  if (root) {
1167  root->release();
1168  root = 0;
1169  }
1170  _imports.clear();
1172  _refTypes.clear();
1173  _errors.clear();
1174 
1175  if (data) {
1176  delete data;
1177  data = 0;
1178  }
1179 }
QList< TypeReference * > _refTypes
void clear()
Removes all items from the list.
Definition: qlist.h:764
QList< QDeclarativeError > _errors
QDeclarativeScriptParserJsASTData * data
QDeclarativeParser::Object * root
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319

◆ errors()

QList< QDeclarativeError > QDeclarativeScriptParser::errors ( ) const

Definition at line 886 of file qdeclarativescriptparser.cpp.

Referenced by QDeclarativeTypeData::dataReceived().

887 {
888  return _errors;
889 }
QList< QDeclarativeError > _errors

◆ extractMetaData()

QDeclarativeScriptParser::JavaScriptMetaData QDeclarativeScriptParser::extractMetaData ( QString script)
static

Definition at line 1007 of file qdeclarativescriptparser.cpp.

1008 {
1009  JavaScriptMetaData rv;
1010 
1011  QDeclarativeParser::Object::ScriptBlock::Pragmas &pragmas = rv.pragmas;
1012 
1013  const QString pragma(QLatin1String("pragma"));
1014  const QString js(QLatin1String(".js"));
1015  const QString library(QLatin1String("library"));
1016 
1018  l.setCode(script, 0);
1019 
1020  int token = l.lex();
1021 
1022  while (true) {
1023  if (token != QDeclarativeJSGrammar::T_DOT)
1024  return rv;
1025 
1026  int startOffset = l.tokenOffset();
1027  int startLine = l.currentLineNo();
1028 
1029  token = l.lex();
1030 
1031  CHECK_LINE;
1032 
1033  if (token == QDeclarativeJSGrammar::T_IMPORT) {
1034 
1035  // .import <URI> <Version> as <Identifier>
1036  // .import <file.js> as <Identifier>
1037 
1038  token = l.lex();
1039 
1040  CHECK_LINE;
1041 
1043 
1044  QString file(l.characterBuffer(), l.characterCount());
1045  if (!file.endsWith(js))
1046  return rv;
1047 
1048  token = l.lex();
1049 
1050  CHECK_TOKEN(T_AS);
1051  CHECK_LINE;
1052 
1053  token = l.lex();
1054 
1055  CHECK_TOKEN(T_IDENTIFIER);
1056  CHECK_LINE;
1057 
1058  int endOffset = l.tokenLength() + l.tokenOffset();
1059 
1060  QString importId = script.mid(l.tokenOffset(), l.tokenLength());
1061 
1062  if (!importId.at(0).isUpper())
1063  return rv;
1064 
1065  token = l.lex();
1066  if (l.currentLineNo() == startLine)
1067  return rv;
1068 
1069  replaceWithSpace(script, startOffset, endOffset - startOffset);
1070 
1071  Import import;
1072  import.type = Import::Script;
1073  import.uri = file;
1074  import.qualifier = importId;
1075 
1076  rv.imports << import;
1077 
1078  } else {
1079  // URI
1080  QString uri;
1081  QString version;
1082 
1083  while (true) {
1084  if (!isUriToken(token))
1085  return rv;
1086 
1087  uri.append(QString(l.characterBuffer(), l.characterCount()));
1088 
1089  token = l.lex();
1090  CHECK_LINE;
1091  if (token != QDeclarativeJSGrammar::T_DOT)
1092  break;
1093 
1094  uri.append(QLatin1Char('.'));
1095 
1096  token = l.lex();
1097  CHECK_LINE;
1098  }
1099 
1100  CHECK_TOKEN(T_NUMERIC_LITERAL);
1101  version = script.mid(l.tokenOffset(), l.tokenLength());
1102 
1103  token = l.lex();
1104 
1105  CHECK_TOKEN(T_AS);
1106  CHECK_LINE;
1107 
1108  token = l.lex();
1109 
1110  CHECK_TOKEN(T_IDENTIFIER);
1111  CHECK_LINE;
1112 
1113  int endOffset = l.tokenLength() + l.tokenOffset();
1114 
1115  QString importId = script.mid(l.tokenOffset(), l.tokenLength());
1116 
1117  if (!importId.at(0).isUpper())
1118  return rv;
1119 
1120  token = l.lex();
1121  if (l.currentLineNo() == startLine)
1122  return rv;
1123 
1124  replaceWithSpace(script, startOffset, endOffset - startOffset);
1125 
1126  Import import;
1127  import.type = Import::Library;
1128  import.uri = uri;
1129  import.version = version;
1130  import.qualifier = importId;
1131 
1132  rv.imports << import;
1133  }
1134 
1135  } else if (token == QDeclarativeJSGrammar::T_IDENTIFIER &&
1136  script.mid(l.tokenOffset(), l.tokenLength()) == pragma) {
1137 
1138  token = l.lex();
1139 
1140  CHECK_TOKEN(T_IDENTIFIER);
1141  CHECK_LINE;
1142 
1143  QString pragmaValue = script.mid(l.tokenOffset(), l.tokenLength());
1144  int endOffset = l.tokenLength() + l.tokenOffset();
1145 
1146  if (pragmaValue == QLatin1String("library")) {
1148  replaceWithSpace(script, startOffset, endOffset - startOffset);
1149  } else {
1150  return rv;
1151  }
1152 
1153  token = l.lex();
1154  if (l.currentLineNo() == startLine)
1155  return rv;
1156 
1157  } else {
1158  return rv;
1159  }
1160  }
1161  return rv;
1162 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
static bool isUriToken(int token)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define CHECK_TOKEN(t)
bool isUpper() const
Returns true if the character is an uppercase letter, i.
Definition: qchar.h:273
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QString & append(QChar c)
Definition: qstring.cpp:1777
#define CHECK_LINE
QFactoryLoader * l
static void replaceWithSpace(QString &str, int idx, int n)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ extractPragmas()

QDeclarativeParser::Object::ScriptBlock::Pragmas QDeclarativeScriptParser::extractPragmas ( QString script)
static

Definition at line 904 of file qdeclarativescriptparser.cpp.

Referenced by QDeclarativeScriptData::dataReceived(), QDeclarativeInclude::finished(), QDeclarativeInclude::include(), QDeclarativeWorkerScriptEnginePrivate::processLoad(), and QDeclarativeInclude::worker_include().

905 {
906  QDeclarativeParser::Object::ScriptBlock::Pragmas rv = QDeclarativeParser::Object::ScriptBlock::None;
907 
908  const QString pragma(QLatin1String("pragma"));
909  const QString library(QLatin1String("library"));
910 
912  l.setCode(script, 0);
913 
914  int token = l.lex();
915 
916  while (true) {
917  if (token != QDeclarativeJSGrammar::T_DOT)
918  return rv;
919 
920  int startOffset = l.tokenOffset();
921  int startLine = l.currentLineNo();
922 
923  token = l.lex();
924 
926  l.currentLineNo() != startLine ||
927  script.mid(l.tokenOffset(), l.tokenLength()) != pragma)
928  return rv;
929 
930  token = l.lex();
931 
933  l.currentLineNo() != startLine)
934  return rv;
935 
936  QString pragmaValue = script.mid(l.tokenOffset(), l.tokenLength());
937  int endOffset = l.tokenLength() + l.tokenOffset();
938 
939  token = l.lex();
940  if (l.currentLineNo() == startLine)
941  return rv;
942 
943  if (pragmaValue == library) {
945  replaceWithSpace(script, startOffset, endOffset - startOffset);
946  } else {
947  return rv;
948  }
949  }
950  return rv;
951 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QFactoryLoader * l
static void replaceWithSpace(QString &str, int idx, int n)

◆ findOrCreateType()

QDeclarativeScriptParser::TypeReference * QDeclarativeScriptParser::findOrCreateType ( const QString name)

Definition at line 1181 of file qdeclarativescriptparser.cpp.

1182 {
1183  TypeReference *type = 0;
1184  int i = 0;
1185  for (; i < _refTypes.size(); ++i) {
1186  if (_refTypes.at(i)->name == name) {
1187  type = _refTypes.at(i);
1188  break;
1189  }
1190  }
1191  if (!type) {
1192  type = new TypeReference(i, name);
1193  _refTypes.append(type);
1194  }
1195 
1196  return type;
1197 }
int type
Definition: qmetatype.cpp:239
QList< TypeReference * > _refTypes
const char * name

◆ imports()

QList< QDeclarativeScriptParser::Import > QDeclarativeScriptParser::imports ( ) const

◆ parse()

bool QDeclarativeScriptParser::parse ( const QByteArray data,
const QUrl url = QUrl() 
)

Definition at line 821 of file qdeclarativescriptparser.cpp.

Referenced by QDeclarativeTypeData::dataReceived().

822 {
823  clear();
824 
825  const QString fileName = url.toString();
827 
829 #ifndef QT_NO_TEXTCODEC
830  stream.setCodec("UTF-8");
831 #endif
832  const QString code = stream.readAll();
833 
834  data = new QDeclarativeScriptParserJsASTData(fileName);
835 
836  Lexer lexer(&data->engine);
837  lexer.setCode(code, /*line = */ 1);
838 
839  Parser parser(&data->engine);
840 
841  if (! parser.parse() || !_errors.isEmpty()) {
842 
843  // Extract errors from the parser
844  foreach (const DiagnosticMessage &m, parser.diagnosticMessages()) {
845 
846  if (m.isWarning())
847  continue;
848 
850  error.setUrl(url);
851  error.setDescription(m.message);
852  error.setLine(m.loc.startLine);
853  error.setColumn(m.loc.startColumn);
854  _errors << error;
855 
856  }
857  }
858 
859  if (_errors.isEmpty()) {
860  ProcessAST process(this);
861  process(code, parser.ast());
862 
863  // Set the url for process errors
864  for(int ii = 0; ii < _errors.count(); ++ii)
865  _errors[ii].setUrl(url);
866  }
867 
868  return _errors.isEmpty();
869 }
void setDescription(const QString &)
Sets the error description.
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
#define error(msg)
void setColumn(int)
Sets the error column number.
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
static FILE * stream
The QDeclarativeError class encapsulates a QML error.
void setLine(int)
Sets the error line number.
QList< QDeclarativeError > _errors
void setUrl(const QUrl &)
Sets the url for the file that caused this error.
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
static QString fileName(const QString &fileUrl)

◆ referencedTypes()

QList< QDeclarativeScriptParser::TypeReference * > QDeclarativeScriptParser::referencedTypes ( ) const

◆ scriptFile()

QString QDeclarativeScriptParser::scriptFile ( ) const
inline

Definition at line 132 of file qdeclarativescriptparser_p.h.

◆ setScriptFile()

void QDeclarativeScriptParser::setScriptFile ( const QString filename)
inline

Definition at line 131 of file qdeclarativescriptparser_p.h.

◆ setTree()

void QDeclarativeScriptParser::setTree ( QDeclarativeParser::Object tree)

Definition at line 1199 of file qdeclarativescriptparser.cpp.

1200 {
1201  Q_ASSERT(! root);
1202 
1203  root = tree;
1204 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QDeclarativeParser::Object * root
QDeclarativeParser::Object * tree() const

◆ tree()

QDeclarativeParser::Object * QDeclarativeScriptParser::tree ( ) const

Definition at line 876 of file qdeclarativescriptparser.cpp.

Referenced by QDeclarativeCompiler::compile(), QDeclarativeDomDocument::load(), and setTree().

877 {
878  return root;
879 }
QDeclarativeParser::Object * root

Properties

◆ _errors

QList<QDeclarativeError> QDeclarativeScriptParser::_errors

Definition at line 135 of file qdeclarativescriptparser_p.h.

Referenced by clear(), errors(), and parse().

◆ _imports

QList<Import> QDeclarativeScriptParser::_imports

Definition at line 138 of file qdeclarativescriptparser_p.h.

Referenced by clear(), and imports().

◆ _refTypes

QList<TypeReference*> QDeclarativeScriptParser::_refTypes

Definition at line 139 of file qdeclarativescriptparser_p.h.

Referenced by clear(), findOrCreateType(), and referencedTypes().

◆ _scriptFile

QString QDeclarativeScriptParser::_scriptFile

Definition at line 140 of file qdeclarativescriptparser_p.h.

Referenced by parse(), scriptFile(), and setScriptFile().

◆ data

QDeclarativeScriptParserJsASTData* QDeclarativeScriptParser::data

Definition at line 141 of file qdeclarativescriptparser_p.h.

Referenced by clear(), parse(), and replaceWithSpace().

◆ root

QDeclarativeParser::Object* QDeclarativeScriptParser::root

Definition at line 137 of file qdeclarativescriptparser_p.h.

Referenced by clear(), setTree(), and tree().


The documentation for this class was generated from the following files: