Browse Source

doAssignment() improvements in expression parser

master
Odilitime 4 years ago
parent
commit
6e63ac0fec
1 changed files with 68 additions and 32 deletions
  1. 68
    32
      src/parsers/scripting/javascript/JSParser.cpp

+ 68
- 32
src/parsers/scripting/javascript/JSParser.cpp View File

@@ -182,7 +182,7 @@ bool doAssignment(js_scope &rootScope, std::string token) {
// *, /, %
// ?, >>, <<

if (token[cursor] == '"') {
if (token[cursor] == '\'') {
quoteStart = cursor;
state = 4;
} else
@@ -194,100 +194,128 @@ bool doAssignment(js_scope &rootScope, std::string token) {
parenStart = cursor;
parenLevel++;
state = 8;
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
if (last != cursor) {
expression_parts.push_back(token.substr(last, cursor - last - 1));
}
last = cursor + 1;
expression_parts.push_back("(");
} else
if (token[cursor] == '{') {
parenStart = cursor;
parenLevel++;
state = 1;
//std::cout << "last " << last << " cursor " << cursor << std::endl;
if (last != cursor) {
expression_parts.push_back(token.substr(last, cursor - last - 1));
}
last = cursor + 1;
expression_parts.push_back("{");
}
// single =
if (token[cursor] == '=' && token.length() > cursor + 1 && token[cursor + 1] != '=') {
//state = 1;
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
//std::cout << "starting = at " << cursor << " last: " << last << std::endl;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("=");
}
// ||
if (token[cursor] == '|' && token.length() > cursor + 1 && token[cursor + 1] == '|') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("||");
}
if (token[cursor] == '&' && token.length() > cursor + 1 && token[cursor + 1] == '&') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("&&");
}
if (token[cursor] == '>' && token.length() > cursor + 1 && token[cursor + 1] != '=' && token[cursor + 1] != '>') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back(">");
}
if (token[cursor] == '<' && token.length() > cursor + 1 && token[cursor + 1] != '=' && token[cursor + 1] != '<') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("<");
}
if (token[cursor] == '<' && token.length() > cursor + 1 && token[cursor + 1] == '&') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("<=");
}
if (token[cursor] == '>' && token.length() > cursor + 1 && token[cursor + 1] == '&') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back(">=");
}
if (token[cursor] == '=' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] != '=') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("==");
}
if (token[cursor] == '=' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] == '=') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("===");
}
if (token[cursor] == '!' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] != '=') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("!=");
}
if (token[cursor] == '!' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] == '=') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("!==");
}
// +
if (token[cursor] == '+') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("+");
}
} else
if (token[cursor] == '-') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("+");
}
} else
if (token[cursor] == '*') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("*");
}
} else
if (token[cursor] == '/') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("/");
}
} else
if (token[cursor] == '%') {
expression_parts.push_back(token.substr(last, cursor - 1)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("%");
}
} else
if (token[cursor] == '?') {
expression_parts.push_back(token.substr(last, cursor)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back("?");
trinaryLevel++;
state = 9;
}

} else if (state == 1) {
if (token[cursor] == '{') {
parenLevel++;
} else
if (token[cursor] == '}') {
parenLevel--;
if (!parenLevel) {
expression_parts.push_back(token.substr(last, cursor - last));
last = cursor + 1;
expression_parts.push_back("}");
state = 0;
}
}
} else if (state == 4) {
if (token[cursor] == '\'') {
if (token[cursor - 1] != '\\') {
std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
expression_parts.push_back(quote);
//std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
//expression_parts.push_back(quote);
//std::cout << "single quote: " << quote << std::endl;
state = 0;
}
}
} else if (state == 5) {
if (token[cursor] == '\'') {
if (token[cursor] == '"') {
if (token[cursor - 1] != '\\') {
std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
expression_parts.push_back(quote);
//std::cout << "single quote: " << quote << std::endl;
//std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
//expression_parts.push_back(quote);
//std::cout << "double quote: " << quote << std::endl;
state = 0;
}
}
@@ -298,7 +326,7 @@ bool doAssignment(js_scope &rootScope, std::string token) {
if (token[cursor] == ')') {
parenLevel--;
if (!parenLevel) {
expression_parts.push_back(token.substr(last, cursor)); last = cursor;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor;
expression_parts.push_back(")");
state = 0;
}
@@ -310,17 +338,24 @@ bool doAssignment(js_scope &rootScope, std::string token) {
if (token[cursor] == ':') {
trinaryLevel--;
if (!trinaryLevel) {
expression_parts.push_back(token.substr(last, cursor)); last = cursor + 1;
expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
expression_parts.push_back(":");
state = 0;
}
}
}
}
std::cout << "ending state " << state << std::endl;
if (last != token.length()) {
expression_parts.push_back(token.substr(last, token.length()));
}
std::cout << "expression token[" << token << "]" << std::endl;
std::cout << "expression debug" << std::endl;
for(auto it : expression_parts) {
// probably should trim these
std::cout << "[" << it << "]" << std::endl;
// { starts JSON capture (should be exactly one block before the } token)
}
std::cout << "expression end" << std::endl;
@@ -399,6 +434,7 @@ std::shared_ptr<JavaScript> JSParser::parse(const std::string &source) const {
auto varList = parseSepButNotBetween(listStr, ",", opens, closes);
//std::cout << "has " << varList.size() << " variables" << std::endl;
for(auto it2 : varList) {
//std::cout << "var processing [" << it2 << "]" << std::endl;
/*
// FIXME: make sure = isn't in quotes or JSON?
// FIXME: double or triple equal differentiation

Loading…
Cancel
Save