use of lucee.commons.lang.ParserString in project Lucee by lucee.
the class SelectParser method selectExpressions.
// { (selectStatement) [AS] label | tableName [AS] label}
private void selectExpressions(ParserString raw, Select select) throws SQLParserException {
Expression exp = null;
do {
raw.removeSpace();
exp = expression(raw);
if (exp == null)
throw new SQLParserException("missing expression in select part of query");
raw.removeSpace();
if (raw.forwardIfCurrent("as ")) {
String alias = identifier(raw, new RefBooleanImpl(false));
if (alias == null)
throw new SQLParserException("missing alias in select part");
exp.setAlias(alias);
} else {
int start = raw.getPos();
RefBoolean hb = new RefBooleanImpl(false);
String alias = identifier(raw, hb);
if (!hb.toBooleanValue() && "from".equalsIgnoreCase(alias))
raw.setPos(start);
else if (alias != null)
exp.setAlias(alias);
}
select.addSelectExpression(exp);
raw.removeSpace();
} while (raw.forwardIfCurrent(','));
}
use of lucee.commons.lang.ParserString in project Lucee by lucee.
the class SQLPrettyfier method prettyfie.
public static String prettyfie(String sql, boolean validZql) {
ParserString ps = new ParserString(sql.trim());
boolean insideString = false;
// short insideKlammer=0;
StringBuilder sb = new StringBuilder(sql.length());
outer: while (!ps.isAfterLast()) {
if (insideString) {
if (ps.isCurrent('\'')) {
if (!ps.hasNext() || !ps.isNext('\''))
insideString = false;
}
} else {
if (ps.isCurrent('\''))
insideString = true;
else if (ps.isCurrent('?')) {
sb.append(" " + PLACEHOLDER_QUESTION + " ");
ps.next();
continue;
} else if (ps.isCurrent('{')) {
StringBuilder date = new StringBuilder();
int pos = ps.getPos();
while (true) {
if (ps.isAfterLast()) {
ps.setPos(pos);
break;
} else if (ps.isCurrent('}')) {
date.append('}');
DateTime d;
try {
d = DateCaster.toDateAdvanced(date.toString(), null);
} catch (PageException e) {
ps.setPos(pos);
break;
}
sb.append('\'');
sb.append(new DateFormat(Locale.US).format(d, "yyyy-mm-dd"));
sb.append(' ');
sb.append(new TimeFormat(Locale.US).format(d, "HH:mm:ss"));
sb.append('\'');
ps.next();
continue outer;
} else {
date.append(ps.getCurrent());
ps.next();
}
}
} else if (ps.isCurrent('*')) {
sb.append(" " + PLACEHOLDER_ASTERIX + " ");
ps.next();
// last=ps.getCurrent();
continue;
} else if (validZql && ps.isCurrent('a')) {
if (ps.isPreviousWhiteSpace() && ps.isNext('s') && ps.isNextNextWhiteSpace()) {
ps.next();
ps.next();
ps.removeSpace();
continue;
}
}
/*for(int i=0;i<reseved_words.length;i++) {
if(ps.isCurrent(reseved_words[i])) {
int pos=ps.getPos();
ps.setPos(pos+4);
if(ps.isCurrentWhiteSpace()) {
sb.append(" placeholder_"+reseved_words[i]+" ");
continue;
}
if(ps.isCurrent(',')) {
sb.append(" placeholder_"+reseved_words[i]+",");
continue;
}
ps.setPos(pos);
}
}*/
/*if(ps.isCurrent("char")) {
int pos=ps.getPos();
ps.setPos(pos+4);
if(ps.isCurrentWhiteSpace()) {
sb.append(" "+PLACEHOLDER_CHAR+" ");
continue;
}
if(ps.isCurrent(',')) {
sb.append(" "+PLACEHOLDER_CHAR+",");
continue;
}
ps.setPos(pos);
}*/
}
sb.append(ps.getCurrent());
ps.next();
}
;
if (!ps.isLast(';'))
sb.append(';');
// print.err("---------------------------------------------------------------------------------");
return sb.toString();
}
use of lucee.commons.lang.ParserString in project Lucee by lucee.
the class CFMLExpressionInterpreter method interpret.
public Object interpret(PageContext pc, String str, boolean preciseMath) throws PageException {
this.cfml = new ParserString(str);
this.preciseMath = preciseMath;
init(pc);
if (LITERAL_ARRAY == null)
LITERAL_ARRAY = fld.getFunction("_literalArray");
if (LITERAL_STRUCT == null)
LITERAL_STRUCT = fld.getFunction("_literalStruct");
if (JSON_ARRAY == null)
JSON_ARRAY = fld.getFunction("_jsonArray");
if (JSON_STRUCT == null)
JSON_STRUCT = fld.getFunction("_jsonStruct");
if (LITERAL_ORDERED_STRUCT == null)
LITERAL_ORDERED_STRUCT = fld.getFunction("_literalOrderedStruct");
cfml.removeSpace();
Ref ref = assignOp();
cfml.removeSpace();
if (cfml.isAfterLast()) {
// data.put(str+":"+preciseMath,ref);
return ref.getValue(pc);
}
throw new InterpreterException("Syntax Error, invalid Expression [" + cfml.toString() + "]");
}
use of lucee.commons.lang.ParserString in project Lucee by lucee.
the class VariableInterpreter method parse.
/*
public static boolean isDefined(PageContext pc,String var) {
StringList list = parse(pc,new ParserString(var));
if(list==null) return false;
int scope=scopeString2Int(list.next());
Object coll =NULL;
if(scope==Scope.SCOPE_UNDEFINED) {
coll=pc.undefinedScope().get(list.current(),NULL);
if(coll==NULL) return false;
}
else {
try {
coll=pc.scope(scope);
} catch (PageException e) {
return false;
}
}
while(list.hasNext()) {
coll=pc.getVariableUtil().get(pc,coll,list.next(),NULL);
//print.out(coll);
if(coll==NULL) return false;
}
return true;
}
*/
/**
* parse a Literal variable String and return result as String List
* @param pc Page Context
* @param ps ParserString to read
* @return Variable Definition in a String List
*/
private static StringList parse(PageContext pc, ParserString ps, boolean doLowerCase) {
String id = readIdentifier(ps, doLowerCase);
if (id == null)
return null;
StringList list = new StringList(id);
CFMLExpressionInterpreter interpreter = null;
while (true) {
if (ps.forwardIfCurrent('.')) {
id = readIdentifier(ps, doLowerCase);
if (id == null)
return null;
list.add(id);
} else if (ps.forwardIfCurrent('[')) {
if (interpreter == null)
interpreter = new CFMLExpressionInterpreter(false);
try {
list.add(Caster.toString(interpreter.interpretPart(pc, ps)));
} catch (PageException e) {
return null;
}
if (!ps.forwardIfCurrent(']'))
return null;
ps.removeSpace();
} else
break;
}
if (ps.isValidIndex())
return null;
list.reset();
return list;
}
use of lucee.commons.lang.ParserString in project Lucee by lucee.
the class VariableInterpreter method parse.
public static StringList parse(String var, boolean doLowerCase) {
ParserString ps = new ParserString(var);
String id = readIdentifier(ps, doLowerCase);
if (id == null)
return null;
StringList list = new StringList(id);
while (true) {
if (ps.forwardIfCurrent('.')) {
id = readIdentifier(ps, doLowerCase);
if (id == null)
return null;
list.add(id);
} else
break;
}
if (ps.isValidIndex())
return null;
list.reset();
return list;
}
Aggregations