private static TypeReference ParseLiteralType(Parser parser, IAbstractSyntaxTree lexerNode) { switch (lexerNode.Type) { case Lexer.TokenType.Long: return(parser.Int64); case Lexer.TokenType.Integer: return(parser.Int32); case Lexer.TokenType.CharLiteral: return(parser.Char); case Lexer.TokenType.StringLiteral: return(parser.String); case Lexer.TokenType.Float: return(parser.Float); case Lexer.TokenType.True: case Lexer.TokenType.False: return(parser.Bool); case Lexer.TokenType.Double: return(parser.Double); default: ContractsHelper.AssumeUnreachable("Unknown literal type"); return(null); //unreachable } }
private static LiteralNode ParseInteger(ProjectParser parser, string value, TypeReference requestedType, SequencePoint point) { try { var parsed = BigInteger.Parse(value, CultureInfo.InvariantCulture); var type = GetLowestConversion(parser, parsed); if (type == null) { ErrorCode.IntegerOverflow.ReportAndThrow(point, "Cannot fit {0} into an integer, use BigInteger.Parse", value); } else { //return what we parsed, if requested type can't fit if (type.IsAssignableTo(requestedType)) { type = requestedType; } } if (parsed.Sign >= 0) { return(new LiteralNode((ulong)parsed, type, point)); } else { return(new LiteralNode((long)parsed, type, point)); } } catch (FormatException) { ContractsHelper.AssumeUnreachable("Error parsing {0} as integer", value); } return(Utils.Utils.Fail <LiteralNode>()); }
private static LiteralNode ParseRational(ProjectParser parser, string value, TypeReference requestedType, SequencePoint point) { try { switch (requestedType.MetadataType) { case MetadataType.Single: return(new LiteralNode(Convert.ToSingle(value, CultureInfo.InvariantCulture), requestedType, point)); case MetadataType.Double: return(new LiteralNode(Convert.ToDouble(value, CultureInfo.InvariantCulture), requestedType, point)); default: ContractsHelper.AssertUnreachable("Unexpected type {0} in ParseRational", requestedType.MetadataType); return(Utils.Utils.Fail <LiteralNode>()); } } catch (OverflowException) { ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, overflow", value, requestedType.FullName); return(Utils.Utils.Fail <LiteralNode>()); } catch (FormatException) { ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, format error", value, requestedType.FullName); return(Utils.Utils.Fail <LiteralNode>()); } }
public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode, TypeReference expectedType = null) { Contract.Ensures(Contract.Result <ExpressionNode>() != null); ExpressionNode ret = null; switch (lexerNode.Type) { case Lexer.TokenType.FullSymbol: case Lexer.TokenType.Symbol: ret = DotOperatorNode.Parse(context, lexerNode); break; case Lexer.TokenType.LiteralNode: ret = LiteralNode.Parse(context, lexerNode); break; case Lexer.TokenType.Value: ret = ExpressionNode.Parse(context, lexerNode.Children[0], expectedType); break; case Lexer.TokenType.Function: ret = MethodNode.Parse(context, lexerNode); break; case Lexer.TokenType.InfixNode: ret = InfixParser.Parse(context, lexerNode); break; case Lexer.TokenType.PostfixNode: case Lexer.TokenType.PrefixNode: ret = UnaryOperators.Parse(context, lexerNode); break; case Lexer.TokenType.ParenthesesNode: ret = ExpressionNode.Parse(context, lexerNode.Children[1], expectedType); break; case Lexer.TokenType.ArrayLiteral: ret = ArrayCreationNode.Parse(context, lexerNode); break; case Lexer.TokenType.Null: ret = NullNode.Parse(context, lexerNode); break; default: ContractsHelper.AssumeUnreachable("Unknown expression type {0}", lexerNode.Type); break; } if (!(expectedType == null || expectedType.IsAuto())) { var ambiguous = ret as IAmbiguousNode; if (ambiguous != null) { ret = ambiguous.RemoveAmbiguity(context, expectedType); } } return(ret); }
private static LiteralNode ParseValue(ProjectParser parser, string value, TypeReference type, SequencePoint point) { try { switch (type.MetadataType) { case MetadataType.String: case MetadataType.Boolean: return(new LiteralNode(value, type, point)); case MetadataType.SByte: case MetadataType.Byte: case MetadataType.Int16: case MetadataType.UInt16: case MetadataType.Int32: case MetadataType.UInt32: case MetadataType.Int64: case MetadataType.UInt64: return(ParseInteger(parser, value, type, point)); case MetadataType.Char: { if (value.Length > 1) { ErrorCode.MultipleCharacterLiteral.ReportAndThrow(point, "Character literal must not be longer than one character (found: '{0}').", value); } return(new LiteralNode(value, type, point)); } case MetadataType.Single: case MetadataType.Double: return(ParseRational(parser, value, type, point)); default: ContractsHelper.AssumeUnreachable(String.Format("Unexpected literal type {0}", type.FullName)); return(null); //unreachable } } catch (OverflowException) { ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, overflow", value, type.FullName); return(null);//unreachable } catch (FormatException) { ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, format error", value, type.FullName); return(null);//unreachable } }
public void Release(VariableDefinition variable) { Contract.Requires(variable != null); for (int i = 0; i < temporaryVariables.Count; i++) { if (temporaryVariables[i].variable == variable) { temporaryVariables[i] = new TempVariable(variable, false); return; } } ContractsHelper.AssertUnreachable("Invalid variable was passed to TemporaryVairables.Release()."); }
private static string ModuleKindToExtension(ModuleKind moduleKind) { Contract.Requires(moduleKind != ModuleKind.NetModule); switch (moduleKind) { case ModuleKind.Console: case ModuleKind.Windows: return(".exe"); case ModuleKind.Dll: return(".dll"); default: ContractsHelper.AssertUnreachable(string.Format("Unknown module kind: {0}.", moduleKind)); return(string.Empty); } }
private static ModuleKind ParseModuleKinds(IEnumerable <string> moduleKinds) { Contract.Ensures(Contract.Result <ModuleKind>() != ModuleKind.NetModule); var moduleKindCount = moduleKinds.Count(); if (moduleKindCount > 1) { var errorMessage = new StringBuilder("More than one module kind specified:" + Environment.NewLine); foreach (var moduleKind in moduleKinds) { errorMessage.AppendFormat("\t{0}{1}", moduleKind, Environment.NewLine); } Errors.Report(ErrorCode.MoreThanOneModuleKind, errorMessage.ToString()); return(default(ModuleKind)); } else if (moduleKindCount == 0) { return(ModuleKind.Console); } else { switch (moduleKinds.Single().ToLowerInvariant()) { case "/console": return(ModuleKind.Console); case "/windows": return(ModuleKind.Windows); case "/dll": return(ModuleKind.Dll); default: ContractsHelper.AssumeUnreachable(string.Format("Unknown module kind: \"{0}\".", moduleKinds.First())); return(default(ModuleKind)); } } }
private static ExpressionNode ParsePrefix(ContextNode context, IAbstractSyntaxTree lexerNode) { var opType = lexerNode.Children[1].Type; if (opType == Lexer.TokenType.CastOperator) { return(CastNode.Parse(context, lexerNode)); } else { InternalUnaryOperatorType op; switch (opType) { case Lexer.TokenType.PlusPlus: op = InternalUnaryOperatorType.PreIncrement; break; case Lexer.TokenType.MinusMinus: op = InternalUnaryOperatorType.PreDecrement; break; case Lexer.TokenType.Not: op = InternalUnaryOperatorType.LogicalNot; break; case Lexer.TokenType.BitwiseComplement: op = InternalUnaryOperatorType.BinaryNot; break; case Lexer.TokenType.Minus: op = InternalUnaryOperatorType.Negation; break; default: ContractsHelper.AssertUnreachable("Unknown prefix op {0}", opType); return(null); //unreachable } return(Create(context, ExpressionNode.Parse(context, lexerNode.Children[0]), op)); } }
public CodeBlockNode AddNode(ParserNode node) { var returning = node as IReturningNode; if (returning != null && returning.Returns) { Returns = true; } var expression = node as ExpressionNode; if (expression != null) { AddExpression(expression); return(this); } var declaration = node as SymbolDeclarationNode; if (declaration != null) { AddDeclaration(declaration); return(this); } // no special action if (node is CodeBlockNode || node is ConditionBlockNode || node is WhileBlock || node is ReturnNode || node is ForLoopNode || node is ForEachNode) { nodes.Add(node); return(this); } ContractsHelper.AssumeUnreachable("Unexpected node {0} in while parsing code block", node.GetType().FullName); return(Utils.Utils.Fail <CodeBlockNode>()); }
public static ExpressionNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode) { Contract.Requires(lexerNode.Type == TokenType.InfixNode); var op = lexerNode.Children[2].Type; if (op.IsBinaryOp()) { return(BinaryOperatorNode.Parse(context, lexerNode)); } if (op.IsAssignmentOp()) { return(AssignmentOperatorNode.Parse(context, lexerNode)); } if (op.IsPeriodOp()) { return(DotOperatorNode.Parse(context, lexerNode)); } ContractsHelper.AssertUnreachable("Unknown infix operator {0}", op); return(null);//unreachable }
private static ExpressionNode ParseSuffix(ContextNode context, IAbstractSyntaxTree lexerNode) { var opType = lexerNode.Children[1].Type; switch (opType) { case Lexer.TokenType.PlusPlus: return(Create(context, ExpressionNode.Parse(context, lexerNode.Children[0]), InternalUnaryOperatorType.PostIncrement)); case Lexer.TokenType.MinusMinus: return(Create(context, ExpressionNode.Parse(context, lexerNode.Children[0]), InternalUnaryOperatorType.PostDecrement)); case Lexer.TokenType.FunctionArgumentsList: return(MethodCallNode.Parse(context, lexerNode)); case Lexer.TokenType.IndexNode: return(ArrayAccessNode.Parse(context, lexerNode)); default: ContractsHelper.AssertUnreachable("Unknown postfix op {0}", opType); return(null); //unreachable } }
private static List <ExpressionNode> ParseArgList(ContextNode parent, IAbstractSyntaxTree lexerNode) { var args = new List <ExpressionNode>(); foreach (var node in lexerNode.Children) { switch (node.Type) { case Lexer.TokenType.LeftParenthesis: case Lexer.TokenType.RightParenthesis: case Lexer.TokenType.Comma: break; case Lexer.TokenType.Value: args.Add(ExpressionNode.Parse(parent, node)); break; default: ContractsHelper.AssertUnreachable("Unexpected node {0} in call", node.Type); break; } } return(args); }
public ContentResult GetContent(string fileName) { return(Content(ContractsHelper.GetContractContent(fileName))); }
// GET: Contracts public ActionResult Index() { ViewData["CSharpTemplates"] = ContractsHelper.GetContractList(SourceLanguage.CSharp); ViewData["PythonTemplates"] = ContractsHelper.GetContractList(SourceLanguage.Python); return(View()); }
public async Task <OperationResult> Import(ImportModel importModel) { try { JToken rawJson = JToken.Parse(importModel.ImportList); int eFormId = importModel.eFormId; JToken rawHeadersJson = JToken.Parse(importModel.Headers); JToken headers = rawHeadersJson; IEnumerable <JToken> rentableObjects = rawJson.Skip(2); Core core = await _coreHelper.GetCore(); foreach (JToken rentableObject in rentableObjects) { Customer customer = null; bool companyNameExists = int.TryParse(headers[1]["headerValue"].ToString(), out int companyNameColumn); if (companyNameExists) { customer = await FindCustomer(companyNameExists, companyNameColumn, headers, rentableObject); if (customer == null) { CustomerFullModel customerModel = CustomerHelper.ComposeValues(new CustomerFullModel(), headers, rentableObject); customer = new Customer { CompanyName = customerModel.CompanyName }; await customer.Create(_customerDbContext); } } RentableItem rentableItem = null; bool brandExists = int.TryParse(headers[2]["headerValue"].ToString(), out int brandColumn); bool modelExists = int.TryParse(headers[3]["headerValue"].ToString(), out int modelColumn); bool registrationDateExists = int.TryParse(headers[4]["headerValue"].ToString(), out int registrationDateColumn); bool vinNumberExists = int.TryParse(headers[5]["headerValue"].ToString(), out int vinNumberColumn); if (brandExists && modelExists && registrationDateExists && vinNumberExists) { rentableItem = await FindRentableItem(brandExists, brandColumn, modelExists, modelColumn, registrationDateExists, registrationDateColumn, vinNumberExists, vinNumberColumn, headers, rentableObject); if (rentableItem == null) { RentableItemModel rentableItemModel = RentableItemHelper.ComposeValues(new RentableItemModel(), headers, rentableObject); rentableItem = new RentableItem { Brand = rentableItemModel.Brand, ModelName = rentableItemModel.ModelName, RegistrationDate = rentableItemModel.RegistrationDate, VinNumber = rentableItemModel.VinNumber, eFormId = eFormId }; await rentableItem.Create(_dbContext); } } Contract contract = null; bool contractNumberExists = int.TryParse(headers[0]["headerValue"].ToString(), out int contractNumberColumn); bool contractStartExists = int.TryParse(headers[6]["headerValue"].ToString(), out int contractStartColumn); bool contractEndExists = int.TryParse(headers[7]["headerValue"].ToString(), out int contractEndColumn); int customerID = customer.Id; if (contractNumberExists && contractStartExists && contractEndExists && customerID != null) { contract = await FindContract(contractNumberExists, contractNumberColumn, contractStartExists, contractStartColumn, contractEndExists, contractEndColumn, customerID, headers, rentableObject); if (contract == null) { ContractModel contractModel = ContractsHelper.ComposeValues(new ContractModel(), headers, rentableObject); contract = new Contract { CustomerId = customerID, ContractEnd = contractModel.ContractEnd, ContractStart = contractModel.ContractStart, ContractNr = contractModel.ContractNr }; await contract.Create(_dbContext); } } ContractRentableItem contractRentableItem = null; if (contract.Id != null && rentableItem.Id != null) { contractRentableItem = await FindtContractRentableItem(contract.Id, rentableItem.Id); if (contractRentableItem == null) { contractRentableItem = new ContractRentableItem { ContractId = contract.Id, RentableItemId = rentableItem.Id }; await contractRentableItem.Create(_dbContext); } } } return(new OperationResult(true, "Import Successful")); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(false, "You goofed up")); } }