@Test public final void testUnionClause() { ParseTree tree = analyzer.parse(setClauses[0]).getParseTree(); assertEquals(StatementType.UNION, tree.getType()); SetStmt union = (SetStmt) tree; assertEquals(StatementType.SELECT, union.getLeftTree().getType()); assertEquals(StatementType.SELECT, union.getRightTree().getType()); QueryBlock left = (QueryBlock) union.getLeftTree(); assertEquals("student", left.getFromTables()[0].getTableName()); QueryBlock right = (QueryBlock) union.getRightTree(); assertEquals("branch", right.getFromTables()[0].getTableName()); // multiple set statements tree = analyzer.parse(setClauses[1]).getParseTree(); assertEquals(StatementType.UNION, tree.getType()); union = (SetStmt) tree; assertEquals(StatementType.SELECT, union.getLeftTree().getType()); assertEquals(StatementType.INTERSECT, union.getRightTree().getType()); left = (QueryBlock) union.getLeftTree(); assertEquals("student", left.getFromTables()[0].getTableName()); SetStmt rightSet = (SetStmt) union.getRightTree(); left = (QueryBlock) rightSet.getLeftTree(); assertEquals("branch", left.getFromTables()[0].getTableName()); right = (QueryBlock) rightSet.getRightTree(); assertEquals("b", right.getFromTables()[0].getAlias()); }
public void print(int offset) { for (int i = 0; i < offset; i++) System.err.print(" "); System.err.println(rule); for (ParseTree subT : children) { subT.print(offset + 1); } }
@Test public final void testEmptyGroupSetStatement() { ParseTree tree = analyzer.parse(GROUP_BY[4]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertTrue(block.getGroupByClause().isEmptyGroupSet()); }
public void run() { ParseTreeMaker maker = new ParseTreeMaker(); ParseTree pt = maker.getParseTree(); new Verifier(maker.getSymbols()).verify(pt); pt = new PTOptimizer(pt).getOptimizedPT(); pt.print(0); new PTGenerator(maker.getSymbols()).genCode(maker.getParseTree()).print(); }
public static ParseTree getParseTree(ParseTree child, FileOptions fo, Target t) { CFunction ac = new CFunction(new __autoconcat__().getName(), t); ParseTree tree = new ParseTree(ac, fo); List<ParseTree> children = new ArrayList<ParseTree>(); children.add(child); tree.setChildren(children); return tree; }
public String asJava() { try { return this.tryAsJava(); } catch (Throwable t) { ParseTree pt = ParseTree.getParseTree(this.context); pt.handleTypeResolveException(pt, this.getOriginalCode(), t, false); return UntranslatedExpression.createUntranslatedExpression(this.getOriginalCode(), context) .asJava(); } }
public ParseTree parseCondition(String e) throws ParseError { ParseTree ret = null; ParseTree ev = parse(e); if (ev.isBoolean()) { ret = ev; } else { E.error("not a condition: " + e); } return ret; }
public ParseTree parseExpression(String e) throws ParseError { ParseTree ret = null; ParseTree ev = parse(e); if (ev.isFloat()) { ret = ev; } else { E.error("not an expression: " + e); } return ret; }
@Test public final void testGroupByStatement() { ParseTree tree = analyzer.parse(GROUP_BY[0]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertEquals(1, block.getGroupByClause().getGroupSet().size()); assertEquals( "age", block.getGroupByClause().getGroupSet().get(0).getColumns()[0].getColumnName()); assertTrue(block.hasHavingCond()); assertEquals(Type.GTH, block.getHavingCond().getType()); }
@Test public final void testRollUpStatement() { ParseTree tree = analyzer.parse(GROUP_BY[2]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertEquals(1, block.getGroupByClause().getGroupSet().size()); assertEquals(GroupType.ROLLUP, block.getGroupByClause().getGroupSet().get(0).getType()); List<GroupElement> groups = block.getGroupByClause().getGroupSet(); assertEquals("people.name", groups.get(0).getColumns()[0].getQualifiedName()); assertEquals("people.age", groups.get(0).getColumns()[1].getQualifiedName()); }
@Test public void testInterpret() throws Exception { ParseTree stmt = new ParseTree(LL1Grammar.NonTerminal.STMT_USE_KEYSPACE, null); stmt.add( new ParseTree(Terminal.KEY_USE, new Token(TokenType.KEYWORD, "USE")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test"))); IInterpreter interpreter = new UseKeySpaceInterpreter(); UseKeySpaceCommand cmd = (UseKeySpaceCommand) interpreter.interpret(stmt, new Context()); assertEquals(cmd.getName(), "Test"); assertTrue(cmd.hasContext()); }
@Test public void testCaseWhen() { ParseTree tree = analyzer .parse( "select case when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) " + "when p_type = 'MOCC' then l_extendedprice - 100 else 0 end as cond from lineitem, part") .getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.getTargetList()[0].hasAlias()); assertEquals("cond", block.getTargetList()[0].getAlias()); assertEquals(DataType.DOUBLE, block.getTargetList()[0].getEvalTree().getValueType()[0]); }
private ParseTree createTree(String lhs) { ParseTree ret = new ParseTree(); ret.rule = in.nextLine(); createSymbol(ret.rule); if (!terminals.contains(lhs)) { String[] arr = ret.rule.split(" "); for (int i = 1; i < arr.length; i++) { ret.children.add(createTree(arr[i])); } } return ret; }
private static Map<String, String> generatePOSTags(Question q) { ParseTree parse = new ParseTree(); DEPTree tree = parse.process(q); // TODO this is horribly wrong, the same label CAN have different pos // tags Map<String, String> label2pos = Maps.newHashMap(); Stack<DEPNode> stack = new Stack<DEPNode>(); stack.push(tree.getFirstRoot()); while (!stack.isEmpty()) { DEPNode tmp = stack.pop(); label2pos.put(tmp.form, tmp.pos); for (DEPNode child : tmp.getDependentNodeList()) { stack.push(child); } } return label2pos; }
@Test public final void testSetQulaifier() { ParseTree tree = analyzer.parse(setQualifier[0]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertFalse(block.isDistinct()); tree = analyzer.parse(setQualifier[1]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); block = (QueryBlock) tree; assertTrue(block.isDistinct()); tree = analyzer.parse(setQualifier[2]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); block = (QueryBlock) tree; assertFalse(block.isDistinct()); }
@Test public final void testMixedGroupByStatement() { ParseTree tree = analyzer.parse(GROUP_BY[3]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertEquals(3, block.getGroupByClause().getGroupSet().size()); Iterator<GroupElement> it = block.getGroupByClause().getGroupSet().iterator(); GroupElement group = it.next(); assertEquals(GroupType.CUBE, group.getType()); assertEquals("people.name", group.getColumns()[0].getQualifiedName()); group = it.next(); assertEquals(GroupType.ROLLUP, group.getType()); assertEquals("people.age", group.getColumns()[0].getQualifiedName()); group = it.next(); assertEquals(GroupType.GROUPBY, group.getType()); assertEquals("people.id", group.getColumns()[0].getQualifiedName()); }
public JavaProgram createJavaProgram( List sourceFiles, String libraryPath, String mainClassname, String projectType, String policyType) throws Exception { // Debug.setOn(true); long l = Debug.getTime(); LibraryData libraryData = new LibraryData(new File(libraryPath), projectType, language, false); Library library = new Library(libraryData); Parser parser = Parser.createParser(this.language, sourceFiles, library, new TranslationPolicy(policyType)); long lll = Debug.getTime(); System.out.println("Library Loaded in " + (lll - l) + "ms"); ParseTree tree = parser.parse(); TranslationReport report = tree.getTranslationReport(); long ll = Debug.getTime(); System.out.println("Code parsed in " + (ll - lll) + "ms"); Debug.setOn(true); Translator tt = new Translator(this.language); JavaProgram jp = tt.createJavaProgram(tree, mainClassname, projectType); if (report.hasTypeResolveErrors() || report.hasTranslationWarnings()) { List errors = new ArrayList(); errors.addAll(report.getTypeResolveExceptions()); report.doReport(errors, report.getTranslationWarnings()); } l = Debug.getTime(); System.out.println("Translated in " + (l - ll) + "ms"); jp.setTypeResolved(!report.hasTypeResolveErrors()); // resolved if no errors return jp; }
private void fold(ParseTree t, String val) { ParseTree term = new ParseTree(); term.rule = "term factor"; ParseTree factor = new ParseTree(); ParseTree deepest = new ParseTree(); t.rule = "expr term"; t.children.clear(); t.children.add(term); term.children.add(factor); if (val.equals("NULL")) { factor.rule = "factor NULL"; deepest.rule = "NULL null"; } else { factor.rule = "factor NUM"; deepest.rule = "NUM " + val; } factor.children.add(deepest); }
public Construct eval(ParseTree c, final Environment env) throws CancelCommandException { final Construct m = c.getData(); CurrentEnv = env; // TODO: Reevaluate if this line is needed. The script doesn't know the label inherently, the // environment does, and setting it this way taints the environment. CurrentEnv.getEnv(GlobalEnv.class).SetLabel(this.label); if (m.getCType() == ConstructType.FUNCTION) { env.getEnv(GlobalEnv.class).SetScript(this); if (m.val().matches("^_[^_].*")) { // Not really a function, so we can't put it in Function. Procedure p = getProc(m.val()); if (p == null) { throw new ConfigRuntimeException( "Unknown procedure \"" + m.val() + "\"", ExceptionType.InvalidProcedureException, m.getTarget()); } Environment newEnv = env; try { newEnv = env.clone(); } catch (Exception e) { } ProfilePoint pp = env.getEnv(GlobalEnv.class).GetProfiler().start(m.val() + " execution", LogLevel.INFO); Construct ret = p.cexecute(c.getChildren(), newEnv, m.getTarget()); pp.stop(); return ret; } final Function f; try { f = (Function) FunctionList.getFunction(m); } catch (ConfigCompileException e) { // Turn it into a config runtime exception. This shouldn't ever happen though. throw new ConfigRuntimeException("Unable to find function " + m.val(), m.getTarget()); } // We have special handling for loop and other control flow functions if (f instanceof assign) { if (c.getChildAt(0).getData() instanceof CFunction) { CFunction test = (CFunction) c.getChildAt(0).getData(); if (test.val().equals("array_get")) { env.getEnv(GlobalEnv.class).SetFlag("array_get_alt_mode", true); Construct arrayAndIndex = eval(c.getChildAt(0), env); env.getEnv(GlobalEnv.class).ClearFlag("array_get_alt_mode"); return ((assign) f) .array_assign(m.getTarget(), env, arrayAndIndex, eval(c.getChildAt(1), env)); } } } if (f.useSpecialExec()) { ProfilePoint p = null; if (f.shouldProfile() && env.getEnv(GlobalEnv.class).GetProfiler() != null && env.getEnv(GlobalEnv.class).GetProfiler().isLoggable(f.profileAt())) { p = env.getEnv(GlobalEnv.class) .GetProfiler() .start(f.profileMessageS(c.getChildren()), f.profileAt()); } Construct ret = f.execs(m.getTarget(), env, this, c.getChildren().toArray(new ParseTree[] {})); if (p != null) { p.stop(); } return ret; } ArrayList<Construct> args = new ArrayList<Construct>(); for (ParseTree c2 : c.getChildren()) { args.add(eval(c2, env)); } if (f.isRestricted()) { boolean perm = Static.hasCHPermission(f.getName(), env); if (!perm) { throw new ConfigRuntimeException( "You do not have permission to use the " + f.getName() + " function.", ExceptionType.InsufficientPermissionException, m.getTarget()); } } Object[] a = args.toArray(); Construct[] ca = new Construct[a.length]; for (int i = 0; i < a.length; i++) { ca[i] = (Construct) a[i]; // CArray, CBoolean, CDouble, CInt, CNull, CString, CVoid, CEntry, CLabel (only to sconcat). if (!(ca[i] instanceof CArray || ca[i] instanceof CBoolean || ca[i] instanceof CDouble || ca[i] instanceof CInt || ca[i] instanceof CNull || ca[i] instanceof CString || ca[i] instanceof CVoid || ca[i] instanceof IVariable || ca[i] instanceof CEntry || ca[i] instanceof CLabel) && (!f.getName().equals("__autoconcat__") && (ca[i] instanceof CLabel))) { throw new ConfigRuntimeException( "Invalid Construct (" + ca[i].getClass() + ") being passed as an argument to a function (" + f.getName() + ")", null, m.getTarget()); } if (env.getEnv(GlobalEnv.class).GetFlag("array_get_alt_mode") == Boolean.TRUE && i == 0) { continue; } while (f.preResolveVariables() && ca[i] instanceof IVariable) { IVariable cur = (IVariable) ca[i]; ca[i] = env.getEnv(GlobalEnv.class).GetVarList().get(cur.getName(), cur.getTarget()).ival(); } } { // It takes a moment to generate the toString of some things, so lets not do it // if we actually aren't going to profile ProfilePoint p = null; if (f.shouldProfile() && env.getEnv(GlobalEnv.class).GetProfiler() != null && env.getEnv(GlobalEnv.class).GetProfiler().isLoggable(f.profileAt())) { p = env.getEnv(GlobalEnv.class).GetProfiler().start(f.profileMessage(ca), f.profileAt()); } Construct ret = f.exec(m.getTarget(), env, ca); if (p != null) { p.stop(); } return ret; } } else if (m.getCType() == ConstructType.VARIABLE) { return new CString(m.val(), m.getTarget()); } else { return m; } }
/** * __autoconcat__ has special optimization techniques needed, since it's really a part of the * compiler itself, and not so much a function. It being a function is merely a convenience, so * we can defer processing until after parsing. While it is tightly coupled with the compiler, * this is ok, since it's really a compiler mechanism more than a function. * * @param t * @param list * @return */ public ParseTree optimizeSpecial(List<ParseTree> list, boolean returnSConcat) throws ConfigCompileException { // If any of our nodes are CSymbols, we have different behavior boolean inSymbolMode = false; // caching this can save Xn // Assignment // Note that we are walking the array in reverse, because multiple assignments, // say @a = @b = 1 will break if they look like assign(assign(@a, @b), 1), // they need to be assign(@a, assign(@b, 1)). As a variation, we also have // to support something like 1 + @a = 2, which will turn into add(1, assign(@a, 2), // and 1 + @a = @b + 3 would turn into add(1, assign(@a, add(@b, 3))). for (int i = list.size() - 2; i >= 0; i--) { ParseTree node = list.get(i + 1); if (node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isAssignment()) { CSymbol sy = (CSymbol) node.getData(); String conversionFunction = sy.convertAssignment(); ParseTree lhs = list.get(i); if (conversionFunction != null) { ParseTree conversion = new ParseTree( new CFunction(conversionFunction, node.getTarget()), node.getFileOptions()); // grab the entire right side, and turn it into an operation with the left side. // We have to take the entire right up to the next construct not followed by an // operator (or the end) try { ParseTree rhs; if (i < list.size() - 3) { // Need to autoconcat ParseTree ac = new ParseTree( new CFunction("__autoconcat__", Target.UNKNOWN), lhs.getFileOptions()); int index = i + 2; ac.addChild(list.get(index)); list.remove(index); while (true) { if (list.size() > index && list.get(index).getData() instanceof CSymbol) { // Add the next two children, (the symbol then the item) // and continue. ac.addChild(list.get(index)); ac.addChild(list.get(index + 1)); list.remove(index); list.remove(index); continue; } else { break; } } // Set this subset into the correct slot, the rest of the // code will grab it correctly that way. list.add(i + 2, ac); } rhs = list.get(i + 2); conversion.addChild(lhs); conversion.addChild(rhs); list.set(i + 2, conversion); } catch (IndexOutOfBoundsException e) { throw new ConfigCompileException("Invalid symbol listed", node.getTarget()); } } // Simple assignment now ParseTree assign = new ParseTree(new CFunction("assign", node.getTarget()), node.getFileOptions()); ParseTree rhs; if (i < list.size() - 3) { // Need to autoconcat ParseTree ac = new ParseTree( new CFunction("__autoconcat__", Target.UNKNOWN), lhs.getFileOptions()); int index = i + 2; ac.addChild(list.get(index)); list.remove(index); while (true) { if (list.size() > index && list.get(index).getData() instanceof CSymbol) { // Add the next two children, (the symbol then the item) // and continue. ac.addChild(list.get(index)); ac.addChild(list.get(index + 1)); list.remove(index); list.remove(index); continue; } else { break; } } // Set this subset into the correct slot, the rest of the // code will grab it correctly that way. list.add(i + 2, ac); } rhs = list.get(i + 2); assign.addChild(lhs); assign.addChild(rhs); list.set(i, assign); list.remove(i + 1); list.remove(i + 1); } } // postfix for (int i = 0; i < list.size(); i++) { ParseTree node = list.get(i); if (node.getData() instanceof CSymbol) { inSymbolMode = true; } if (node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isPostfix()) { if (i - 1 >= 0) { // && list.get(i - 1).getData() instanceof IVariable) { CSymbol sy = (CSymbol) node.getData(); ParseTree conversion; if (sy.val().equals("++")) { conversion = new ParseTree(new CFunction("postinc", node.getTarget()), node.getFileOptions()); } else { conversion = new ParseTree(new CFunction("postdec", node.getTarget()), node.getFileOptions()); } conversion.addChild(list.get(i - 1)); list.set(i - 1, conversion); list.remove(i); i--; } } } if (inSymbolMode) { try { // look for unary operators for (int i = 0; i < list.size() - 1; i++) { ParseTree node = list.get(i); if (node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isUnary()) { ParseTree conversion; if (node.getData().val().equals("-") || node.getData().val().equals("+")) { // These are special, because if the values to the left isn't a symbol, // it's not unary if ((i == 0 || list.get(i - 1).getData() instanceof CSymbol) && !(list.get(i + 1).getData() instanceof CSymbol)) { if (node.getData().val().equals("-")) { // We have to negate it conversion = new ParseTree( new CFunction("neg", node.getTarget()), node.getFileOptions()); } else { conversion = new ParseTree(new CFunction("p", node.getTarget()), node.getFileOptions()); } } else { continue; } } else { conversion = new ParseTree( new CFunction(((CSymbol) node.getData()).convert(), node.getTarget()), node.getFileOptions()); } conversion.addChild(list.get(i + 1)); list.set(i, conversion); list.remove(i + 1); i--; } } for (int i = 0; i < list.size() - 1; i++) { ParseTree next = list.get(i + 1); if (next.getData() instanceof CSymbol) { if (((CSymbol) next.getData()).isExponential()) { ParseTree conversion = new ParseTree( new CFunction(((CSymbol) next.getData()).convert(), next.getTarget()), next.getFileOptions()); conversion.addChild(list.get(i)); conversion.addChild(list.get(i + 2)); list.set(i, conversion); list.remove(i + 1); list.remove(i + 1); i--; } } } // Multiplicative for (int i = 0; i < list.size() - 1; i++) { ParseTree next = list.get(i + 1); if (next.getData() instanceof CSymbol) { CSymbol nextData = (CSymbol) next.getData(); if (nextData.isMultaplicative() && !nextData.isAssignment()) { ParseTree conversion = new ParseTree( new CFunction(((CSymbol) next.getData()).convert(), next.getTarget()), next.getFileOptions()); conversion.addChild(list.get(i)); conversion.addChild(list.get(i + 2)); list.set(i, conversion); list.remove(i + 1); list.remove(i + 1); i--; } } } // Additive for (int i = 0; i < list.size() - 1; i++) { ParseTree next = list.get(i + 1); if (next.getData() instanceof CSymbol && ((CSymbol) next.getData()).isAdditive() && !((CSymbol) next.getData()).isAssignment()) { ParseTree conversion = new ParseTree( new CFunction(((CSymbol) next.getData()).convert(), next.getTarget()), next.getFileOptions()); conversion.addChild(list.get(i)); conversion.addChild(list.get(i + 2)); list.set(i, conversion); list.remove(i + 1); list.remove(i + 1); i--; } } // relational for (int i = 0; i < list.size() - 1; i++) { ParseTree node = list.get(i + 1); if (node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isRelational()) { CSymbol sy = (CSymbol) node.getData(); ParseTree conversion = new ParseTree( new CFunction(sy.convert(), node.getTarget()), node.getFileOptions()); conversion.addChild(list.get(i)); conversion.addChild(list.get(i + 2)); list.set(i, conversion); list.remove(i + 1); list.remove(i + 1); i--; } } // equality for (int i = 0; i < list.size() - 1; i++) { ParseTree node = list.get(i + 1); if (node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isEquality()) { CSymbol sy = (CSymbol) node.getData(); ParseTree conversion = new ParseTree( new CFunction(sy.convert(), node.getTarget()), node.getFileOptions()); conversion.addChild(list.get(i)); conversion.addChild(list.get(i + 2)); list.set(i, conversion); list.remove(i + 1); list.remove(i + 1); i--; } } // logical and for (int i = 0; i < list.size() - 1; i++) { ParseTree node = list.get(i + 1); if (node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isLogicalAnd()) { CSymbol sy = (CSymbol) node.getData(); ParseTree conversion = new ParseTree( new CFunction(sy.convert(), node.getTarget()), node.getFileOptions()); conversion.addChild(list.get(i)); conversion.addChild(list.get(i + 2)); list.set(i, conversion); list.remove(i + 1); list.remove(i + 1); i--; } } // logical or for (int i = 0; i < list.size() - 1; i++) { ParseTree node = list.get(i + 1); if (node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isLogicalOr()) { CSymbol sy = (CSymbol) node.getData(); ParseTree conversion = new ParseTree( new CFunction(sy.convert(), node.getTarget()), node.getFileOptions()); conversion.addChild(list.get(i)); conversion.addChild(list.get(i + 2)); list.set(i, conversion); list.remove(i + 1); list.remove(i + 1); i--; } } } catch (IndexOutOfBoundsException e) { throw new ConfigCompileException( "Unexpected symbol (" + list.get(list.size() - 1).getData().val() + "). Did you forget to quote your symbols?", list.get(list.size() - 1).getTarget()); } } // Look for a CEntry here if (list.size() >= 1) { ParseTree node = list.get(0); if (node.getData() instanceof CLabel) { ParseTree value = new ParseTree( new CFunction("__autoconcat__", node.getTarget()), node.getFileOptions()); for (int i = 1; i < list.size(); i++) { value.addChild(list.get(i)); } ParseTree ce = new ParseTree(new CFunction("centry", node.getTarget()), node.getFileOptions()); ce.addChild(node); ce.addChild(value); return ce; } } // We've eliminated the need for __autoconcat__ either way, however, if there are still // arguments // left, it needs to go to sconcat, which MAY be able to be further optimized, but that will // be handled in MethodScriptCompiler's optimize function. Also, we must scan for // CPreIdentifiers, // which may be turned into a function if (list.size() == 1) { return list.get(0); } else { for (int i = 0; i < list.size(); i++) { if (list.get(i).getData().getCType() == Construct.ConstructType.IDENTIFIER) { if (i == 0) { // Yup, it's an identifier CFunction identifier = new CFunction(list.get(i).getData().val(), list.get(i).getTarget()); list.remove(0); ParseTree child = list.get(0); if (list.size() > 1) { child = new ParseTree(new CFunction("sconcat", Target.UNKNOWN), child.getFileOptions()); child.setChildren(list); } try { Function f = (Function) FunctionList.getFunction(identifier); ParseTree node = new ParseTree( f.execs(identifier.getTarget(), null, null, child), child.getFileOptions()); return node; } catch (Exception e) { throw new Error("Unknown function " + identifier.val() + "?"); } } else { // Hmm, this is weird. I'm not sure what condition this can happen in throw new ConfigCompileException( "Unexpected IDENTIFIER? O.o Please report a bug," + " and include the script you used to get this error.", Target.UNKNOWN); } } } ParseTree tree; FileOptions options = new FileOptions(new HashMap<String, String>()); if (!list.isEmpty()) { options = list.get(0).getFileOptions(); } if (returnSConcat) { tree = new ParseTree(new CFunction("sconcat", Target.UNKNOWN), options); } else { tree = new ParseTree(new CFunction("concat", Target.UNKNOWN), options); } tree.setChildren(list); return tree; } }
@Test public void testInterpret() throws Exception { ParseTree[] input = new ParseTree[10]; input[0] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt0 = new ParseTree(LL1Grammar.NonTerminal.STMT_CREATE_KEYSPACE, null); input[0].add(stmt0); stmt0.add( new ParseTree(Terminal.KEY_KEYSPACE, new Token(TokenType.KEYWORD, "KEYSPACE")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test"))); input[1] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt1 = new ParseTree(LL1Grammar.NonTerminal.STMT_DROP_KEYSPACE, null); input[1].add(stmt1); stmt1.add( new ParseTree(Terminal.KEY_KILL, new Token(TokenType.KEYWORD, "KILL")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test"))); input[2] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt2 = new ParseTree(LL1Grammar.NonTerminal.STMT_USE_KEYSPACE, null); input[2].add(stmt2); stmt2.add( new ParseTree(Terminal.KEY_USE, new Token(TokenType.KEYWORD, "USE")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test"))); input[3] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt3 = new ParseTree(LL1Grammar.NonTerminal.STMT_CREATE_COLUMNFAMILY, null); input[3].add(stmt3); stmt3.add( new ParseTree(Terminal.KEY_CREATE, new Token(TokenType.KEYWORD, "CREATE")), new ParseTree(Terminal.KEY_COLUMNFAMILY, new Token(TokenType.KEYWORD, "COLUMNFAMILY")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test2"))); input[4] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt4 = new ParseTree(LL1Grammar.NonTerminal.STMT_DROP_COLUMNFAMILY, null); input[4].add(stmt4); stmt4.add( new ParseTree(Terminal.KEY_DROP, new Token(TokenType.KEYWORD, "DROP")), new ParseTree(Terminal.KEY_COLUMNFAMILY, new Token(TokenType.KEYWORD, "COLUMNFAMILY")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test2"))); input[5] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt5 = new ParseTree(LL1Grammar.NonTerminal.STMT_INSERT, null); input[5].add(stmt5); ParseTree keyValue = new ParseTree(LL1Grammar.NonTerminal.KEY_VALUES_LIST, null); ParseTree nextValue = new ParseTree(LL1Grammar.NonTerminal.KEY_VALUES_LIST_PRIM, null); nextValue.add( new ParseTree(Terminal.SYMBOL_COMMA, new Token(TokenType.COMMA)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "name")), new ParseTree(Terminal.SYMBOL_EQUAL, new Token(TokenType.EQUAL)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Piotr")), new ParseTree(LL1Grammar.NonTerminal.KEY_VALUES_LIST_PRIM, null)); keyValue.add( new ParseTree(Terminal.KEY_KEY, new Token(TokenType.KEYWORD, "KEY")), new ParseTree(Terminal.SYMBOL_EQUAL, new Token(TokenType.EQUAL)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "pepe")), nextValue); stmt5.add( new ParseTree(Terminal.KEY_INSERT, new Token(TokenType.KEYWORD, "INSERT")), new ParseTree(Terminal.KEY_INTO, new Token(TokenType.KEYWORD, "INTO")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test2")), new ParseTree(Terminal.SYMBOL_LPAREN, new Token(TokenType.LPAREN)), keyValue, new ParseTree(Terminal.SYMBOL_RPAREN, new Token(TokenType.RPAREN))); input[6] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt6 = new ParseTree(LL1Grammar.NonTerminal.STMT_UPDATE, null); input[6].add(stmt6); ParseTree keyValue2 = new ParseTree(LL1Grammar.NonTerminal.KEY_VALUES_LIST, null); ParseTree nextValue2 = new ParseTree(LL1Grammar.NonTerminal.KEY_VALUES_LIST_PRIM, null); nextValue2.add( new ParseTree(Terminal.SYMBOL_COMMA, new Token(TokenType.COMMA)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "name")), new ParseTree(Terminal.SYMBOL_EQUAL, new Token(TokenType.EQUAL)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Piotr")), new ParseTree(LL1Grammar.NonTerminal.KEY_VALUES_LIST_PRIM, null)); keyValue2.add( new ParseTree(Terminal.KEY_KEY, new Token(TokenType.KEYWORD, "KEY")), new ParseTree(Terminal.SYMBOL_EQUAL, new Token(TokenType.EQUAL)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "pepe")), nextValue2); stmt6.add( new ParseTree(Terminal.KEY_UPDATE, new Token(TokenType.KEYWORD, "UPDATE")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test2")), new ParseTree(Terminal.KEY_SET, new Token(TokenType.KEYWORD, "SET")), keyValue2); input[7] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt7 = new ParseTree(LL1Grammar.NonTerminal.STMT_SELECT, null); input[7].add(stmt7); ParseTree where = new ParseTree(LL1Grammar.NonTerminal.WHERE, null); where.add( new ParseTree(Terminal.KEY_WHERE, new Token(TokenType.KEYWORD, "WHERE")), new ParseTree(Terminal.KEY_KEY, new Token(TokenType.KEYWORD, "KEY")), new ParseTree(Terminal.SYMBOL_EQUAL, new Token(TokenType.EQUAL)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "pepe"))); stmt7.add( new ParseTree(Terminal.KEY_SELECT, new Token(TokenType.KEYWORD, "SELECT")), new ParseTree(Terminal.KEY_FROM, new Token(TokenType.KEYWORD, "FROM")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test2")), where); input[8] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt8 = new ParseTree(LL1Grammar.NonTerminal.STMT_DELETE, null); input[8].add(stmt8); ParseTree where2 = new ParseTree(LL1Grammar.NonTerminal.WHERE, null); where2.add( new ParseTree(Terminal.KEY_WHERE, new Token(TokenType.KEYWORD, "WHERE")), new ParseTree(Terminal.KEY_KEY, new Token(TokenType.KEYWORD, "KEY")), new ParseTree(Terminal.SYMBOL_EQUAL, new Token(TokenType.EQUAL)), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "pepe"))); stmt8.add( new ParseTree(Terminal.KEY_DELETE, new Token(TokenType.KEYWORD, "DELETE")), new ParseTree(Terminal.KEY_FROM, new Token(TokenType.KEYWORD, "FROM")), new ParseTree(Terminal.VAR, new Token(TokenType.VARIABLE, "Test2")), where2); input[9] = new ParseTree(LL1Grammar.NonTerminal.STMT_START, null); ParseTree stmt9 = new ParseTree(LL1Grammar.NonTerminal.WHERE, null); input[9].add(stmt9); IDataModel model = new HashMapDataModel(); IKeySpace keySpace = model.createKeySpace("Test2"); Context ctx = new Context(); DB.INSTANCE.addKeySpace(keySpace); ctx.setActualKeySpace("Test2"); StartStmtInterpreter interpreter = new StartStmtInterpreter(); assertTrue(interpreter.interpret(input[0], ctx) instanceof CreateKeySpaceCommand); assertTrue(interpreter.interpret(input[1], ctx) instanceof DropKeySpaceCommand); assertTrue(interpreter.interpret(input[2], ctx) instanceof UseKeySpaceCommand); assertTrue(interpreter.interpret(input[3], ctx) instanceof CreateColumnFamilyCommand); assertTrue(interpreter.interpret(input[4], ctx) instanceof DropColumnFamilyCommand); assertTrue(interpreter.interpret(input[5], ctx) instanceof InsertCommand); assertTrue(interpreter.interpret(input[6], ctx) instanceof UpdateCommand); assertTrue(interpreter.interpret(input[7], ctx) instanceof SelectCommand); assertTrue(interpreter.interpret(input[8], ctx) instanceof DeleteCommand); // check for syntax error boolean wasThrown = false; try { interpreter.interpret(input[9], ctx); } catch (PythiaException e) { wasThrown = true; } assertTrue(wasThrown); }
/** Parse the query string. */ public ZimbraQuery(OperationContext octxt, SoapProtocol proto, Mailbox mbox, SearchParams params) throws ServiceException { this.octxt = octxt; this.protocol = proto; this.params = params; this.mailbox = mbox; // Parse the text using the JavaCC parser. try { QueryParser parser = new QueryParser(mbox, mbox.index.getAnalyzer()); parser.setDefaultField(params.getDefaultField()); parser.setTypes(params.getTypes()); parser.setTimeZone(params.getTimeZone()); parser.setLocale(params.getLocale()); parser.setQuick(params.isQuick()); clauses = parser.parse(params.getQueryString()); if (parser.getSortBy() != null) { SortBy sort = SortBy.of(parser.getSortBy()); if (sort == null) { throw ServiceException.PARSE_ERROR("INVALID_SORTBY: " + sort, null); } params.setSortBy(sort); } } catch (Error e) { throw ServiceException.PARSE_ERROR("PARSER_ERROR", e); } ZimbraLog.search.debug("%s,types=%s,sort=%s", this, params.getTypes(), params.getSortBy()); // Build a parse tree and push all the "NOT's" down to the bottom level. // This is because we cannot invert result sets. parseTree = ParseTree.build(clauses).simplify(); parseTree.pushNotsDown(); // Check sort compatibility. switch (params.getSortBy().getKey()) { case RCPT: case ATTACHMENT: case FLAG: case PRIORITY: // We don't store these in Lucene. if (hasTextOperation()) { throw ServiceException.INVALID_REQUEST( "Sort '" + params.getSortBy().name() + "' can't be used with text query.", null); } break; default: break; } SearchParams.Cursor cursor = params.getCursor(); if (cursor != null) { // Check cursor compatibility if (params.getCursor().isIncludeOffset() && hasTextOperation()) { throw ServiceException.INVALID_REQUEST( "cursor.includeOffset can't be used with text query.", null); } // Supplement sortValue if (cursor.getSortValue() == null) { ZimbraLog.search.debug( "Supplementing sortValue sort=%s,id=%s", params.getSortBy(), cursor.getItemId()); try { MailItem item = mailbox.getItemById(null, cursor.getItemId().getId(), MailItem.Type.UNKNOWN); switch (params.getSortBy().getKey()) { case NAME: cursor.setSortValue(item.getName()); break; case RCPT: cursor.setSortValue(item.getSortRecipients()); break; case SENDER: cursor.setSortValue(item.getSortSender()); break; case SIZE: cursor.setSortValue(String.valueOf(item.getSize())); break; case SUBJECT: cursor.setSortValue(item.getSortSubject()); break; case DATE: default: cursor.setSortValue(String.valueOf(item.getDate())); break; } } catch (NoSuchItemException e) { params.setCursor(null); // clear cursor } } } }
public void run(final List<Variable> vars, Environment myEnv, final MethodScriptComplete done) { // Some things, such as the label are determined at compile time this.CurrentEnv = myEnv; this.CurrentEnv.getEnv(GlobalEnv.class).SetLabel(this.label); MCCommandSender p = myEnv.getEnv(CommandHelperEnvironment.class).GetCommandSender(); if (!hasBeenCompiled || compilerError) { Target target = Target.UNKNOWN; if (left.size() >= 1) { try { target = new Target(left.get(0).line_num, left.get(0).file, left.get(0).column); } catch (NullPointerException e) { // Oh well, we tried to get more information } } throw new ConfigRuntimeException( "Unable to run command, script not yet compiled, or a compiler error occured for that command." + " To see the compile error, run /reloadaliases", null, target); } if (p instanceof MCPlayer) { if (CurrentEnv.getEnv(GlobalEnv.class).GetLabel() != null) { PermissionsResolver perms = CurrentEnv.getEnv(GlobalEnv.class).GetPermissionsResolver(); String[] groups = CurrentEnv.getEnv(GlobalEnv.class).GetLabel().split("/"); for (String group : groups) { if (group.startsWith("-") && perms.inGroup(((MCPlayer) p).getName(), group.substring(1))) { // negative permission throw new ConfigRuntimeException( "You do not have permission to use that command", ExceptionType.InsufficientPermissionException, Target.UNKNOWN); } else if (perms.inGroup(((MCPlayer) p).getName(), group)) { // They do have permission. break; } } } } try { for (ParseTree rootNode : cright) { for (Construct tempNode : rootNode.getAllData()) { if (tempNode instanceof Variable) { if (left_vars == null) { throw new ConfigRuntimeException( "$variables may not be used in this context. Only @variables may be.", null, tempNode.getTarget()); } ((Variable) tempNode) .setVal( new CString( Static.resolveDollarVar( left_vars.get(((Variable) tempNode).getName()), vars) .toString(), tempNode.getTarget())); } } MethodScriptCompiler.registerAutoIncludes(CurrentEnv, this); MethodScriptCompiler.execute(rootNode, CurrentEnv, done, this); } } catch (ConfigRuntimeException ex) { // We don't know how to handle this really, so let's pass it up the chain. throw ex; } catch (CancelCommandException e) { // p.sendMessage(e.getMessage()); // The message in the exception is actually empty } catch (LoopBreakException e) { if (p != null) { p.sendMessage("The break() function must be used inside a for() or foreach() loop"); } System.out.println("The break() function must be used inside a for() or foreach() loop"); } catch (LoopContinueException e) { if (p != null) { p.sendMessage("The continue() function must be used inside a for() or foreach() loop"); } System.out.println("The continue() function must be used inside a for() or foreach() loop"); } catch (FunctionReturnException e) { if (myEnv.getEnv(CommandHelperEnvironment.class).GetEvent() != null) { // Oh, we're running in an event handler. Those know how to catch it too. throw e; } if (p != null) { p.sendMessage("The return() function must be used inside a procedure."); } System.out.println("The return() function must be used inside a procedure."); } catch (Throwable t) { System.out.println("An unexpected exception occured during the execution of a script."); t.printStackTrace(); if (p != null) { p.sendMessage( "An unexpected exception occured during the execution of your script. Please check the console for more information."); } } if (done != null) { done.done(null); } }