private void reportError(String message, CommonTree ast) { // Print an error message relating to the given // (sub)AST. int line = ast.getLine(), column = ast.getCharPositionInLine(); System.err.println("line " + line + ":" + column + " " + message); errorCount++; }
private VOpNode constructVBOpNode(VOperator vOp, CommonTree tree) { if (checkNode(tree)) { ExprNode expr = constructExprNode((CommonTree) tree.getChild(0)); return new VOpNode(vOp, expr, tree.getLine()); } return null; }
private VBOpNode constructVBOpNode(VBoolOperator vBOp, CommonTree tree) { if (!checkNode(tree)) { return null; } BExprNode bExpr = constructBExprNode((CommonTree) tree.getChild(0)); return new VBOpNode(vBOp, bExpr, tree.getLine()); }
private void checkInvocations(CommonTree node) { if (exceptionClass != "" && node.getType() == 4 && !forward && node.getText() != exceptionClass) { exceptionVariable = node.getText(); } if (exceptionVariable != "" && node.getType() == FORWARDCURLYBRACKET) { this.forward = true; } if ((forward) && (node.getType() == IDENTIFIER) && (node.getText()).equals(exceptionVariable)) { foundExceptionVariable = true; } if (foundExceptionVariable && node.getType() == DOT) { foundDot = true; } if (foundExceptionVariable && foundDot && node.getType() == IDENTIFIER) { foundDot = false; foundExceptionVariable = false; CSharpData data = new CSharpData(); data.setClassName(belongsToClass); data.setLineNumber(node.getLine()); data.setInvocationTo(exceptionClass); data.setInvocationName(node.getText()); allInvocations.add(data); } }
private AssignNode constructAssignNode(CommonTree tree) { if (!checkNode(tree)) { return null; } IdNode id = constructIdNode((CommonTree) tree.getChild(0)); ExprNode expr = constructExprNode((CommonTree) tree.getChild(1)); return new AssignNode(id, expr, tree.getLine()); }
private BinaryFunctionNode constructBinFuncNode(BinaryFunction binFunc, CommonTree tree) { if (!checkNode(tree)) { return null; } IdNode id = constructIdNode((CommonTree) tree.getChild(0)); ExprNode expr = constructExprNode((CommonTree) tree.getChild(1)); return new BinaryFunctionNode(binFunc, id, expr, tree.getLine()); }
private BooleanComparitorNode constructBooleanCompNode(ComparisonOperator comp, CommonTree tree) { if (!checkNode(tree)) { return null; } ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0)); ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1)); return new BooleanComparitorNode(comp, lExpr, rExpr, tree.getLine()); }
private BooleanBinOpNode constructBooleanBinOpNode(BooleanBinOperator binOp, CommonTree tree) { if (!checkNode(tree)) { return null; } BExprNode lBExpr = constructBExprNode((CommonTree) tree.getChild(0)); BExprNode rBExpr = constructBExprNode((CommonTree) tree.getChild(1)); return new BooleanBinOpNode(binOp, lBExpr, rBExpr, tree.getLine()); }
private BinOpNode constructBinOpNode(MathematicalOperator op, CommonTree tree) { if (checkNode(tree)) { ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0)); ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1)); return new BinOpNode(op, lExpr, rExpr, tree.getLine()); } return null; }
private BinaryPrimitiveNode constructBinPrimNode(BinaryPrimitive prim, CommonTree tree) { if (checkNode(tree)) { ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0)); ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1)); return new BinaryPrimitiveNode(prim, lExpr, rExpr, tree.getLine()); } return null; }
public WhileStatement(CommonTree MyTree, Model MyModel) { setLine(MyTree.getLine()); this.MyModel = MyModel; Comparison = ExpressionFactory.getExpression((CommonTree) MyTree.getChild(0), MyModel); if (MyTree.getChildCount() > 1) { for (int i = 1; i < MyTree.getChildCount(); i++) Statements.add(StatementFactory.getStatement((CommonTree) MyTree.getChild(i), MyModel)); } }
/** * @see org.coode.parsers.ErrorListener#incompatibleSymbols(org.antlr.runtime.tree.CommonTree, * org.antlr.runtime.tree.CommonTree[]) */ public void incompatibleSymbols(CommonTree parentExpression, CommonTree... trees) { List<String> symbols = new ArrayList<String>(trees.length); for (CommonTree commonTree : trees) { symbols.add(commonTree.getText()); } throw new IncompatibleSymbolsParsingException( parentExpression.getText(), parentExpression.getLine(), parentExpression.getCharPositionInLine(), symbols.toArray(new String[symbols.size()])); }
private NumNode constructNumNode(CommonTree tree) { if (checkNode(tree)) { String numString = tree.getToken().getText(); try { return new NumNode(Float.parseFloat(numString), tree.getLine()); } catch (NumberFormatException n) { exceptions.add(new TreeWalkerException(n.getMessage(), 0, 0)); } } return null; }
public void generateToDomain(CommonTree tree, String theClass) { this.lineNumber = tree.getLine(); this.fromClass = theClass; setExceptionClass(tree); if (isCatchedException(tree)) { this.exceptionType = "catch"; } else if (isThrowedException(tree)) { this.exceptionType = "throw"; } else { this.exceptionType = "throws"; } modelService.createException(fromClass, exceptionClass, lineNumber, exceptionType); }
private void getTypeOfParameter(CommonTree tree) { CommonTree typeOfParameterTree = CSharpGeneratorToolkit.getFirstDescendantWithType(tree, CSharpParser.TYPE); if (typeOfParameterTree != null) { CSharpInvocationGenerator cSharpInvocationGenerator = new CSharpInvocationGenerator(this.belongsToClass); this.declareType = cSharpInvocationGenerator.getCompleteToString( (CommonTree) typeOfParameterTree, belongsToClass, DependencySubTypes.DECL_PARAMETER); this.lineNumber = typeOfParameterTree.getLine(); if (this.declareType.endsWith(".")) { this.declareType = this.declareType.substring(0, this.declareType.length() - 1); // deleting the last point } if (!this.declareType.trim().equals("")) { this.declareTypeFound = true; this.paramTypesInSignature += !this.paramTypesInSignature.equals("") ? "," : ""; this.paramTypesInSignature += this.declareType; } } }
/** * Method used to create dummy objects * * <p>The Object name contains the filename and line number at which the undefined variable is * being accessed * * <p>A new property at_Dummy is introduced to mark this node * * <p>TODO: Should the dummy object be marked tainted?? * * @param pre * @return */ public Location createDummyObjectInHeap(CommonTree ast, SecurityType dummy_type) { String taint_val = dummy_type.getObjValue(); if (taint_val == null) { taint_val = "@High"; } Location dummy_loc = this.dummyObjectMap.get(taint_val); if (dummy_loc == null) { Location second_result; // Add the low security type to the dummy object JSObject dummy_obj = PredefinedObjectTemplates.new_object("Object", NativeObjects.ObjectProt, dummy_type); dummy_obj.put("at_Dummy", new ObjectValue(true)); // current filename sans the path // String[] fn = currentfilename.split("/"); second_result = new Location("dummy_" + ast.getLine()); this.put(second_result, dummy_obj); this.dummyObjectMap.put(taint_val, second_result); return second_result; } else { return dummy_loc; } }
public String generateParameterObjects( CommonTree allParametersTree, String belongsToMethod, String belongsToClass) { // allParametersTree = FORMAL_PARAMETER_LIST String returnvalue = ""; this.parameterQueue = new ArrayList<ArrayList<Object>>(); this.belongsToClass = belongsToClass; this.belongsToMethod = belongsToMethod; /* Test helper if (this.belongsToClass.contains("DeclarationParameter")){ //if (belongsToMethod.contains("performExternalScript")) { boolean breakpoint1 = true; //} } */ if (allParametersTree != null) { lineNumber = allParametersTree.getLine(); deriveParametersFromTree(allParametersTree); writeParametersToDomain(); returnvalue = paramTypesInSignature; } return returnvalue; }
// $ANTLR start "assignment" // DefRef.g:107:1: assignment : ^(eq= '=' member . ) ; public final void assignment() throws RecognitionException { CommonTree eq = null; Type member6 = null; try { // DefRef.g:108:5: ( ^(eq= '=' member . ) ) // DefRef.g:108:9: ^(eq= '=' member . ) { eq = (CommonTree) match(input, 27, FOLLOW_27_in_assignment473); if (state.failed) return; match(input, Token.DOWN, null); if (state.failed) return; pushFollow(FOLLOW_member_in_assignment475); member6 = member(); state._fsp--; if (state.failed) return; matchAny(input); if (state.failed) return; match(input, Token.UP, null); if (state.failed) return; if (state.backtracking == 1) { System.out.println("line " + eq.getLine() + ": assign to type " + member6.getName()); } } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { } return; }
// $ANTLR start "enterStruct" // DefRef.g:48:1: enterStruct : ^( 'struct' ID ( . )+ ) ; public final void enterStruct() throws RecognitionException { CommonTree ID1 = null; try { // DefRef.g:49:5: ( ^( 'struct' ID ( . )+ ) ) // DefRef.g:49:7: ^( 'struct' ID ( . )+ ) { match(input, 17, FOLLOW_17_in_enterStruct219); if (state.failed) return; match(input, Token.DOWN, null); if (state.failed) return; ID1 = (CommonTree) match(input, ID, FOLLOW_ID_in_enterStruct221); if (state.failed) return; // DefRef.g:49:21: ( . )+ int cnt3 = 0; loop3: do { int alt3 = 2; int LA3_0 = input.LA(1); if (((LA3_0 >= METHOD_DECL && LA3_0 <= 30))) { alt3 = 1; } else if ((LA3_0 == UP)) { alt3 = 2; } switch (alt3) { case 1: // DefRef.g:49:21: . { matchAny(input); if (state.failed) return; } break; default: if (cnt3 >= 1) break loop3; if (state.backtracking > 0) { state.failed = true; return; } EarlyExitException eee = new EarlyExitException(3, input); throw eee; } cnt3++; } while (true); match(input, Token.UP, null); if (state.failed) return; if (state.backtracking == 1) { System.out.println( "line " + ID1.getLine() + ": def struct " + (ID1 != null ? ID1.getText() : null)); StructSymbol ss = new StructSymbol((ID1 != null ? ID1.getText() : null), currentScope); currentScope.define(ss); // def struct in current scope currentScope = ss; // set current scope to struct scope } } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { } return; }
// $ANTLR start "varDeclaration" // DefRef.g:86:1: varDeclaration : ^( ( FIELD_DECL | VAR_DECL | ARG_DECL ) type ID ( . )? ) ; public final void varDeclaration() throws RecognitionException { CommonTree ID4 = null; DefRef.type_return type5 = null; try { // DefRef.g:87:5: ( ^( ( FIELD_DECL | VAR_DECL | ARG_DECL ) type ID ( . )? ) ) // DefRef.g:87:9: ^( ( FIELD_DECL | VAR_DECL | ARG_DECL ) type ID ( . )? ) { if (input.LA(1) == ARG_DECL || (input.LA(1) >= VAR_DECL && input.LA(1) <= FIELD_DECL)) { input.consume(); state.errorRecovery = false; state.failed = false; } else { if (state.backtracking > 0) { state.failed = true; return; } MismatchedSetException mse = new MismatchedSetException(null, input); throw mse; } match(input, Token.DOWN, null); if (state.failed) return; pushFollow(FOLLOW_type_in_varDeclaration363); type5 = type(); state._fsp--; if (state.failed) return; ID4 = (CommonTree) match(input, ID, FOLLOW_ID_in_varDeclaration365); if (state.failed) return; // DefRef.g:87:50: ( . )? int alt5 = 2; int LA5_0 = input.LA(1); if (((LA5_0 >= METHOD_DECL && LA5_0 <= 30))) { alt5 = 1; } switch (alt5) { case 1: // DefRef.g:87:50: . { matchAny(input); if (state.failed) return; } break; } match(input, Token.UP, null); if (state.failed) return; if (state.backtracking == 1) { System.out.println( "line " + ID4.getLine() + ": def " + (ID4 != null ? ID4.getText() : null)); VariableSymbol vs = new VariableSymbol( (ID4 != null ? ID4.getText() : null), (type5 != null ? type5.tsym : null)); currentScope.define(vs); } } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { } return; }
private ExprNode constructExprNode(CommonTree tree) { if (!checkNode(tree)) { return null; } Token token = tree.getToken(); ExprNode expr = null; int tokenType = token.getType(); UnaryPrimitive uPrim = SymbolSet.getUnaryPrimitiveOp(tokenType); if (uPrim != null) { ExprNode primExpr = constructExprNode((CommonTree) tree.getChild(0)); return new UnaryPrimNode(uPrim, primExpr, tree.getLine()); } switch (tokenType) { case (BACONParser.PLUS): { expr = constructBinOpNode(MathematicalOperator.PLUS, tree); break; } case (BACONParser.MINUS): { expr = constructBinOpNode(MathematicalOperator.MINUS, tree); break; } case (BACONParser.MUL): { expr = constructBinOpNode(MathematicalOperator.MULTIPLY, tree); break; } case (BACONParser.DIV): { expr = constructBinOpNode(MathematicalOperator.DIVIDE, tree); break; } case (BACONParser.POW): { expr = constructBinOpNode(MathematicalOperator.POWER, tree); break; } case (BACONParser.NEG): { ExprNode negExpr = constructExprNode((CommonTree) tree.getChild(0)); expr = new NegNode(negExpr, tree.getLine()); break; } case (BACONParser.IF): { BExprNode ifCondNode = constructBExprNode((CommonTree) tree.getChild(0)); ExprNode thenExpr = constructExprNode((CommonTree) tree.getChild(1)); ExprNode elseExpr = constructExprNode((CommonTree) tree.getChild(2)); expr = new IfExprNode(ifCondNode, thenExpr, elseExpr, tree.getLine()); break; } case (BACONParser.VAR): { expr = constructIdNode(tree); break; } case (BACONParser.FLOAT): { expr = constructNumNode(tree); break; } case (BACONParser.MAX): { expr = constructBinPrimNode(BinaryPrimitive.MAX, tree); break; } case (BACONParser.MIN): { expr = constructBinPrimNode(BinaryPrimitive.MIN, tree); break; } case (BACONParser.VARHIST): { IdNode id = constructIdNode((CommonTree) tree.getChild(0)); ExprNode histExpr = constructExprNode((CommonTree) tree.getChild(1)); expr = new VarHistNode(id, histExpr, tree.getLine()); break; } case (BACONParser.VAVERAGE): { expr = constructVBOpNode(VOperator.AVERAGE, tree); break; } case (BACONParser.VPRODUCT): { expr = constructVBOpNode(VOperator.PRODUCT, tree); break; } case (BACONParser.VSUM): { expr = constructVBOpNode(VOperator.SUM, tree); break; } default: { exceptions.add( new TreeWalkerException("Unknown expression token in bagging area", 999, 999)); } } return expr; }
// $ANTLR start "member" // DefRef.g:118:1: member returns [Type type] : ( ^( '.' m= member ID ) | ID ); public final Type member() throws RecognitionException { Type type = null; CommonTree ID7 = null; CommonTree ID8 = null; Type m = null; try { // DefRef.g:119:5: ( ^( '.' m= member ID ) | ID ) int alt6 = 2; int LA6_0 = input.LA(1); if ((LA6_0 == 30)) { alt6 = 1; } else if ((LA6_0 == ID)) { alt6 = 2; } else { if (state.backtracking > 0) { state.failed = true; return type; } NoViableAltException nvae = new NoViableAltException("", 6, 0, input); throw nvae; } switch (alt6) { case 1: // DefRef.g:119:7: ^( '.' m= member ID ) { match(input, 30, FOLLOW_30_in_member527); if (state.failed) return type; match(input, Token.DOWN, null); if (state.failed) return type; pushFollow(FOLLOW_member_in_member531); m = member(); state._fsp--; if (state.failed) return type; ID7 = (CommonTree) match(input, ID, FOLLOW_ID_in_member533); if (state.failed) return type; match(input, Token.UP, null); if (state.failed) return type; if (state.backtracking == 1) { StructSymbol scope = (StructSymbol) m; // get scope of expr Symbol s = scope.resolveMember((ID7 != null ? ID7.getText() : null)); // resolve ID in scope System.out.println( "line " + ID7.getLine() + ": ref " + m.getName() + "." + (ID7 != null ? ID7.getText() : null) + "=" + s); if (s != null) type = s.type; // return ID's type } } break; case 2: // DefRef.g:127:7: ID { ID8 = (CommonTree) match(input, ID, FOLLOW_ID_in_member550); if (state.failed) return type; if (state.backtracking == 1) { Symbol s = currentScope.resolve((ID8 != null ? ID8.getText() : null)); System.out.println( "line " + ID8.getLine() + ": ref " + (ID8 != null ? ID8.getText() : null) + "=" + s); if (s != null) type = s.type; } } break; } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { } return type; }
public static String getStartPosition(CommonTree node) { int pos = node.getCharPositionInLine(); return node.getLine() + ":" + pos; }
/** * @see org.coode.parsers.ErrorListener#illegalToken(org.antlr.runtime.tree.CommonTree, * java.lang.String) */ public void illegalToken(CommonTree t, String message) { throw new IllegalTokenParsingException( t.getText(), t.getLine(), t.getCharPositionInLine(), message); }
private IdNode constructIdNode(CommonTree tree) { if (checkNode(tree)) { return new IdNode(tree.getToken().getText(), tree.getLine()); } return null; }
/** * @see org.coode.parsers.ErrorListener#incompatibleSymbolType(org.antlr.runtime.tree.CommonTree, * org.coode.parsers.Type, org.antlr.runtime.tree.CommonTree) */ public void incompatibleSymbolType(CommonTree t, Type type, CommonTree expression) { throw new IncompatibleSymbolTypeParsingException( t.getText(), type, expression.getText(), t.getLine(), t.getCharPositionInLine()); }
// $ANTLR start "enterMethod" // DefRef.g:66:1: enterMethod : ^( METHOD_DECL type ID ( . )* ) ; public final void enterMethod() throws RecognitionException { CommonTree ID2 = null; DefRef.type_return type3 = null; try { // DefRef.g:67:5: ( ^( METHOD_DECL type ID ( . )* ) ) // DefRef.g:67:9: ^( METHOD_DECL type ID ( . )* ) { match(input, METHOD_DECL, FOLLOW_METHOD_DECL_in_enterMethod284); if (state.failed) return; match(input, Token.DOWN, null); if (state.failed) return; pushFollow(FOLLOW_type_in_enterMethod286); type3 = type(); state._fsp--; if (state.failed) return; ID2 = (CommonTree) match(input, ID, FOLLOW_ID_in_enterMethod288); if (state.failed) return; // DefRef.g:67:31: ( . )* loop4: do { int alt4 = 2; int LA4_0 = input.LA(1); if (((LA4_0 >= METHOD_DECL && LA4_0 <= 30))) { alt4 = 1; } else if ((LA4_0 == UP)) { alt4 = 2; } switch (alt4) { case 1: // DefRef.g:67:31: . { matchAny(input); if (state.failed) return; } break; default: break loop4; } } while (true); match(input, Token.UP, null); if (state.failed) return; if (state.backtracking == 1) { System.out.println( "line " + ID2.getLine() + ": def method " + (ID2 != null ? ID2.getText() : null)); MethodSymbol ms = new MethodSymbol( (ID2 != null ? ID2.getText() : null), (type3 != null ? type3.tsym : null), currentScope); currentScope.define(ms); // def method in globals currentScope = ms; // set current scope to method scope } } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { } return; }
/** @see org.coode.parsers.ErrorListener#unrecognisedSymbol(org.antlr.runtime.tree.CommonTree) */ public void unrecognisedSymbol(CommonTree t) { throw new UnrecognisedSymbolParsingException( t.getText(), t.getLine(), t.getCharPositionInLine()); }
public static DataNode buildDataNode(CommonTree t, MetaData meta, int type) { DataNode dNode = new DataNode(); // the type drives what data i want to pull switch (type) { case GraphConstants.TYPE_DIRECTORY: break; case GraphConstants.TYPE_FILE_INCLUDE: dNode.setFileName(meta.getFileName()); dNode.setFileInclude(getOneLevel(t)); break; case GraphConstants.TYPE_FILE: dNode.setFileName(meta.getFileName()); break; case GraphConstants.TYPE_CLASS: dNode.setClassName(getOneLevel(t)); dNode.setFileName(meta.getFileName()); dNode.setLine(t.getLine()); dNode.setLocation(Integer.toString(t.getCharPositionInLine())); break; case GraphConstants.TYPE_FUNCTION: dNode.setClassName(meta.getClassName()); dNode.setFileName(meta.getFileName()); dNode.setFunctionName(getOneLevel(t)); dNode.setLine(t.getLine()); dNode.setLocation(Integer.toString(t.getCharPositionInLine())); break; case GraphConstants.TYPE_FUNCTION_CALL: dNode.setClassName(meta.getClassName()); dNode.setFileName(meta.getFileName()); dNode.setFunctionCall(getOneLevel(t)); dNode.setLine(t.getLine()); dNode.setLocation(Integer.toString(t.getCharPositionInLine())); break; case GraphConstants.TYPE_VARIABLE: // set the var name StringBuilder sb = new StringBuilder(); String varName = getVariableName(t, sb); dNode.setVariableName(varName); dNode.setClassName(meta.getClassName()); dNode.setFileName(meta.getFileName()); dNode.setFunctionName(meta.getFunctionName()); dNode.setLine(t.getLine()); dNode.setLocation(Integer.toString(t.getCharPositionInLine())); dNode.setModified(meta.isModified()); dNode.setModifier(meta.isModifier()); dNode.setPossibleAttackVector(SurfaceVectorUtil.isPossibleSurfaceVector(varName)); break; default: } // pull global stuff no matter what dNode.setType(type); dNode.setRelativePath(meta.getRelativePath()); return dNode; }