Made updates to JSBaker

This commit is contained in:
utkarshgautamnyu 2017-09-25 14:46:38 -07:00
parent 6c40d39f81
commit 0a57874cc2
2 changed files with 45 additions and 40 deletions

View file

@ -32,8 +32,8 @@ void JSBaker::bake() {
_tempDir = tempDir; _tempDir = tempDir;
_originalJSFilePath = _tempDir.filePath(_jsURL.fileName()); _originalJSFilePath = _tempDir.filePath(_jsURL.fileName());
qDebug() << "Made temporary dir " << _tempDir; qCDebug(js_baking) << "Made temporary dir " << _tempDir;
qDebug() << "Origin file path: " << _originalJSFilePath; qCDebug(js_baking) << "Origin file path: " << _originalJSFilePath;
// When source JS is loaded, trigger the importJS method // When source JS is loaded, trigger the importJS method
connect(this, &JSBaker::sourceJSLoaded, this, &JSBaker::importJS); connect(this, &JSBaker::sourceJSLoaded, this, &JSBaker::importJS);
@ -53,16 +53,14 @@ void JSBaker::loadSourceJS() {
} }
// make a copy of local file at _originalJSFilePath // make a copy of local file at _originalJSFilePath
qDebug() << "Local file url: " << _jsURL << _jsURL.toString() << _jsURL.toLocalFile() << ", copying to: " << _originalJSFilePath; qCDebug(js_baking) << "Local file url: " << _jsURL << _jsURL.toString() << _jsURL.toLocalFile() << ", copying to: " << _originalJSFilePath;
localJS.copy(_originalJSFilePath); localJS.copy(_originalJSFilePath);
// emit signal to indicate script is ready to import // emit signal to indicate script is ready to import
emit sourceJSLoaded(); emit sourceJSLoaded();
} }
void JSBaker::importJS() { void JSBaker::importJS() {
//qDebug() << "file path: " << _originalJSFilePath.toLocal8Bit().data() << QDir(_originalJSFilePath).exists();
// Import the file to be processed from _originalJSFilePath // Import the file to be processed from _originalJSFilePath
QFile jsFile(_originalJSFilePath); QFile jsFile(_originalJSFilePath);
if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
@ -99,21 +97,22 @@ void JSBaker::bakeJS(QFile* inputFile) {
while (!readCharacter.atEnd()) { while (!readCharacter.atEnd()) {
// input nextCharacter // input nextCharacter
readCharacter >> nextCharacter; readCharacter >> nextCharacter;
if (currentCharacter == '\r') { if (currentCharacter == '\r') {
writeCharacter << '\n'; writeCharacter << '\n';
} else if (currentCharacter == '/') { } else if (currentCharacter == '/') {
// Check if single line comment i.e. // // Check if single line comment i.e. //
if (nextCharacter == '/') { if (nextCharacter == '/') {
currentCharacter = handleSingleLineComments(&readCharacter); handleSingleLineComments(&readCharacter);
//writeCharacter << '\n';
//Start fresh after handling comments //Start fresh after handling comments
//previousCharacter = '\n'; previousCharacter = '\n';
//readCharacter >> currentCharacter; readCharacter >> currentCharacter;
continue; continue;
} else if (nextCharacter == '*') { } else if (nextCharacter == '*') {
// Check if multi line comment i.e. /* // Check if multi line comment i.e. /*
handleMultiLineComments(&readCharacter); handleMultiLineComments(&readCharacter);
//writeCharacter << ' ';
//Start fresh after handling comments //Start fresh after handling comments
previousCharacter = '\n'; previousCharacter = '\n';
readCharacter >> currentCharacter; readCharacter >> currentCharacter;
@ -122,21 +121,25 @@ void JSBaker::bakeJS(QFile* inputFile) {
// If '/' is not followed by '/' or '*' print '/' // If '/' is not followed by '/' or '*' print '/'
writeCharacter << currentCharacter; writeCharacter << currentCharacter;
} }
} else if (isControlCharacter(currentCharacter)) { // Check if white space or tab } else if (isControlCharacter(currentCharacter)) {
// Check if white space or tab
// Skip multiple spaces or tabs // Skip multiple spaces or tabs
if (isControlCharacter(nextCharacter)) { if (isControlCharacter(nextCharacter)) {
while (isControlCharacter(nextCharacter)) { while (isControlCharacter(nextCharacter)) {
readCharacter >> nextCharacter; readCharacter >> nextCharacter;
if (nextCharacter == '\n') if (nextCharacter == '\n') {
break; break;
}
} }
} }
// check if space can be omitted // check if space can be omitted
if (!canOmitSpace(previousCharacter,nextCharacter)) { if (!canOmitSpace(previousCharacter, nextCharacter)) {
writeCharacter << ' '; writeCharacter << ' ';
} }
} else if (currentCharacter == '\n') { // Check if new line } else if (currentCharacter == '\n') {
// Check if new line
//Skip multiple new lines //Skip multiple new lines
//Skip new line followed by space or tab //Skip new line followed by space or tab
@ -149,7 +152,7 @@ void JSBaker::bakeJS(QFile* inputFile) {
if (!canOmitNewLine(previousCharacter, nextCharacter)) { if (!canOmitNewLine(previousCharacter, nextCharacter)) {
writeCharacter << '\n'; writeCharacter << '\n';
} }
} else if (isQuote(currentCharacter)) { // If the currentCharacter is " or ' or ` } else if (isQuote(currentCharacter)) {
// Print the current quote and nextCharacter as is // Print the current quote and nextCharacter as is
writeCharacter << currentCharacter; writeCharacter << currentCharacter;
writeCharacter << nextCharacter; writeCharacter << nextCharacter;
@ -181,6 +184,7 @@ void JSBaker::bakeJS(QFile* inputFile) {
writeCharacter << currentCharacter; writeCharacter << currentCharacter;
} }
// Reading done. Closing the inputFile
inputFile->close(); inputFile->close();
if (hasErrors()) { if (hasErrors()) {
@ -213,20 +217,20 @@ void JSBaker::exportJS(QFile* bakedFile) {
qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath;
} }
//close the outputFile // Exporting done. Closing the outputFile
bakedFile->close(); bakedFile->close();
// emit signal to indicate the JS baking is finished // emit signal to indicate the JS baking is finished
emit finished(); emit finished();
} }
QChar JSBaker::handleSingleLineComments(QTextStream * readCharacter) { void JSBaker::handleSingleLineComments(QTextStream * readCharacter) {
QChar character; QChar character;
while (!readCharacter->atEnd()) { while (!readCharacter->atEnd()) {
*readCharacter >> character; *readCharacter >> character;
if (character <= '\n') if (character <= '\n')
break; break;
} }
return character; return;
} }
void JSBaker::handleMultiLineComments(QTextStream * readCharacter) { void JSBaker::handleMultiLineComments(QTextStream * readCharacter) {
@ -243,24 +247,24 @@ void JSBaker::handleMultiLineComments(QTextStream * readCharacter) {
} }
bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) {
if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacter(previousCharacter)) &&
if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialChar(previousCharacter)) && (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacter(nextCharacter))
(isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialChar(nextCharacter)) ) {
) {
return false; return false;
} }
return true; return true;
} }
bool JSBaker::canOmitNewLine(QChar previousCharacter, QChar nextCharacter) { bool JSBaker::canOmitNewLine(QChar previousCharacter, QChar nextCharacter) {
if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharPre(previousCharacter)) && if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacterPrevious(previousCharacter)) &&
(isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharPost(nextCharacter)) (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacterNext(nextCharacter))
) { ) {
return false; return false;
} }
return true; return true;
} }
//Check if character is alphabet, number or one of the following: '_', '$', '\\'
bool JSBaker::isAlphanum(QChar c) { bool JSBaker::isAlphanum(QChar c) {
return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')
|| c == '_' || c == '$' || c == '\\' || c > 126); || c == '_' || c == '$' || c == '\\' || c > 126);
@ -270,14 +274,14 @@ bool JSBaker::isNonAscii(QChar c) {
return ((int)c.toLatin1() > 127); return ((int)c.toLatin1() > 127);
} }
bool JSBaker::isSpecialChar(QChar c) { bool JSBaker::isSpecialCharacter(QChar c) {
if (c == '\'' || c == '$' || c == '_' || '/') { if (c == '\'' || c == '$' || c == '_' || c == '/') {
return true; return true;
} }
return false; return false;
} }
bool JSBaker::isSpecialCharPre(QChar c) { bool JSBaker::isSpecialCharacterPrevious(QChar c) {
if (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-' if (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-'
|| c == '"' || c == "'") { || c == '"' || c == "'") {
return true; return true;
@ -285,17 +289,19 @@ bool JSBaker::isSpecialCharPre(QChar c) {
return false; return false;
} }
bool JSBaker::isSpecialCharPost(QChar c) { bool JSBaker::isSpecialCharacterNext(QChar c) {
if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-' || c == '/') { if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-') {
return true; return true;
} }
return false; return false;
} }
// Check if white space or tab
bool JSBaker::isControlCharacter(QChar c) { bool JSBaker::isControlCharacter(QChar c) {
return (c == ' ' || (int)c.toLatin1() == 9); return (c == ' ' || (int)c.toLatin1() == 9);
} }
// Check If the currentCharacter is " or ' or `
bool JSBaker::isQuote(QChar c) { bool JSBaker::isQuote(QChar c) {
return (c == '"' || c == "'" || c == '`'); return (c == '"' || c == "'" || c == '`');
} }

View file

@ -18,7 +18,7 @@
static const QString BAKED_JS_EXTENSION = ".baked.js"; static const QString BAKED_JS_EXTENSION = ".baked.js";
class JSBaker : public Baker { class JSBaker : public Baker {
//Q_OBJECT Q_OBJECT
public: public:
JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); JSBaker(const QUrl& jsURL, const QString& bakedOutputDir);
@ -32,7 +32,6 @@ private slots:
void importJS(); void importJS();
private : private :
QUrl _jsURL; QUrl _jsURL;
QString _bakedOutputDir; QString _bakedOutputDir;
QDir _tempDir; QDir _tempDir;
@ -44,7 +43,7 @@ private :
void bakeJS(QFile*); void bakeJS(QFile*);
void exportJS(QFile*); void exportJS(QFile*);
QChar handleSingleLineComments(QTextStream*); void handleSingleLineComments(QTextStream*);
void handleMultiLineComments(QTextStream*); void handleMultiLineComments(QTextStream*);
bool canOmitSpace(QChar, QChar); bool canOmitSpace(QChar, QChar);
@ -52,11 +51,11 @@ private :
bool isAlphanum(QChar); bool isAlphanum(QChar);
bool isNonAscii(QChar c); bool isNonAscii(QChar c);
bool isSpecialChar(QChar c); bool isSpecialCharacter(QChar c);
bool isSpecialCharPre(QChar c); bool isSpecialCharacterPrevious(QChar c);
bool isSpecialCharPost(QChar c); bool isSpecialCharacterNext(QChar c);
bool isControlCharacter(QChar); bool isControlCharacter(QChar);
bool isQuote(QChar); bool isQuote(QChar);
}; };
#endif // !hifi_JSBaker_h #endif // !hifi_JSBaker_h