private void AddEnum(ITypeDefinition enm, StringBuilder key) { if (enm.Namespace == "") { lock (Enums) { foreach (var typeArg in enm.TypeParameters) { foreach (var constraint in typeArg.DirectBaseTypes) { key.Append(constraint.Name); } } if (Enums.ContainsKey(key.ToString())) { return; } Enums.Add(key.ToString(), new EnumProperties(enm)); } } else { AddNamespace(key, enm); } }
private void AddEnumType(Type type) { if (!type.IsEnum) { throw new ArgumentOutOfRangeException(nameof(type)); } if (Enums.ContainsKey(type.Name)) { return; } Enums.Add(type.Name, Enum.GetNames(type)); }
internal string GetDotNetType(string typeName) { if (typeMappings.ContainsKey(typeName)) { return(typeMappings[typeName]); } if (Types.ContainsKey(typeName)) { return(Types[typeName].Name); } if (Enums.ContainsKey(typeName)) { return(typeName); } return(Inputs[typeName].Name); }
private void AddEnum(IType enm, StringBuilder key) { lock (Enums) { foreach (var typeArg in enm.TypeParameters) { foreach (var constraint in typeArg.Constraints) { key.Append(constraint.Name); } } if (Enums.ContainsKey(key.ToString())) { return; } Enums.Add(key.ToString(), new EnumProperties(enm)); } }
internal bool IsEnum(string typeName) { return(Enums.ContainsKey(typeName)); }
internal bool HasDotNetType(string typeName) { return(typeMappings.ContainsKey(typeName) || Types.ContainsKey(typeName) || Inputs.ContainsKey(typeName) || Enums.ContainsKey(typeName)); }
/// <summary> /// When an identifier is encountered, it could be a number of things. A single identifer by itself /// is considered a variable. The pattern identifier[.identifier]+ will consider the /// first identifier as a variable and the others as properties. Any identifier that is followed /// by an open parenthesis is considered to be a function call. Indexes are not handled yet, but /// should be handled in the future. If the identifier is "this" then a this reference is used. /// </summary> /// <param name="t"></param> /// <returns></returns> private CodeExpression ReadIdentifier(Tokenizer t) { CodeExpression ce = null; Token token = t.Current; if (token.Text == "this") { ce = new CodeThisReferenceExpression(); } else if (_RecognizedTypes.ContainsKey(token.Text)) { // In this case, the identifier is a recognized type. It is likely a cast expression. // We return a CodeTypeReferenceExpression. The parsing method will see this and // try to match it to open and close parentheses. It will then turn it into a // CodeCastExpression. The other possibility is that this method was called from // reading a typeof(), in which case the CodeTypeReferenceExpression is still valid. CodeTypeReferenceExpression ctre = new CodeTypeReferenceExpression(_RecognizedTypes[token.Text] as CodeTypeReference); t.GetNextToken(); return(ctre); } else if (Enums.ContainsKey(token.Text)) { // In this case, the identifier is a recognized enum. We'll return the value of this // immediately since enums are always in the same format. CodeTypeReferenceExpression ctre = new CodeTypeReferenceExpression(_Enums[token.Text] as CodeTypeReference); t.GetNextToken(); if (t.Current.Type != TokenType.Dot) { throw new Exception("Expected dot after enum type: " + token.Text); } t.GetNextToken(); if (t.Current.Type != TokenType.Identifier) { throw new Exception("Expected identifier token after enum type: " + token.Text); } ce = new CodeFieldReferenceExpression(ctre, t.Current.Text); t.GetNextToken(); return(ce); } else if (token.Text == "typeof") { token = t.GetNextToken(); if (token.Type != TokenType.OpenParens) { throw new Exception("Expected open parenthesis after typeof, instead found: " + token.Text); } t.GetNextToken(); ce = ReadIdentifier(t); if (!(ce is CodeTypeReferenceExpression)) { throw new Exception("Illegal argument in typeof. Expecting a type."); } if (t.Current.Type != TokenType.CloseParens) { throw new Exception("Expected close parenthesis after type in typeof expression, instead found: " + t.Current.Text); } CodeTypeOfExpression ctoe = new CodeTypeOfExpression((ce as CodeTypeReferenceExpression).Type); return(ctoe); } else { ce = new CodeVariableReferenceExpression(token.Text); } token = t.GetNextToken(); bool cont = true; while (cont) { if (token.Type == TokenType.Dot) { token = t.GetNextToken(); if (token.Type != TokenType.Identifier) { throw new Exception("Expected identifier after dot, instead found: " + token.Text); } if (_Fields.Contains(token.Text)) { ce = new CodeFieldReferenceExpression(ce, token.Text); } else { ce = new CodePropertyReferenceExpression(ce, token.Text); } token = t.GetNextToken(); if (token.Type != TokenType.OpenParens) { continue; } } else if (token.Type == TokenType.OpenBracket) { // If an open bracket follows, then we're dealing with an indexer. CodeIndexerExpression cie = new CodeIndexerExpression(); cie.TargetObject = ce; ce = cie; t.GetNextToken(); cie.Indices.Add(ReadExpression(t, TokenPriority.None)); if (t.Current.Type != TokenType.CloseBracket) { throw new Exception("Unexpected token: " + t.Current.Text); } token = t.GetNextToken(); continue; } if (token.Type == TokenType.OpenParens) { // An open parenthesis indicates a method, so we'll change from a variable or property reference into // a CodeMethodInvokeExpression. if (ce is CodeThisReferenceExpression) { throw new Exception("Cannot use parentheses after this keyword."); } CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(); if (ce is CodeVariableReferenceExpression) { cmie.Method = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), (ce as CodeVariableReferenceExpression).VariableName); } else if (ce is CodeFieldReferenceExpression) { CodeFieldReferenceExpression cfre = ce as CodeFieldReferenceExpression; cmie.Method = new CodeMethodReferenceExpression(cfre.TargetObject, cfre.FieldName); } else // must be a property reference { CodePropertyReferenceExpression cpre = ce as CodePropertyReferenceExpression; cmie.Method = new CodeMethodReferenceExpression(cpre.TargetObject, cpre.PropertyName); } ce = cmie; token = t.GetNextToken(); if (token.Type != TokenType.CloseParens) { cmie.Parameters.Add(ReadExpression(t, TokenPriority.None)); while (true) { token = t.Current; if (token.Type == TokenType.CloseParens) { t.GetNextToken(); break; } if (token.Type == TokenType.Comma) { t.GetNextToken(); cmie.Parameters.Add(ReadExpression(t, TokenPriority.None)); } else { throw new Exception("Unexpected token: " + token.Text); } } } else { token = t.GetNextToken(); } } else { cont = false; } } return(ce); }