コード例 #1
0
        private TypeClause ParseSingleTypeClause()
        {
            if (_tokenizer.Token == "(")
            {
                _tokenizer.Advance();
                var clause = ParseTupleTypeClause();
                RequireToken(")");
                return(clause);
            }
            var name = ParseName();

            Assert(Char.IsLetter(name[0]), "Letter expected");

            if (Char.IsLower(name[0]))
            {
                return(new VariableTypeClause(name));
            }
            var list = new List <TypeClause>();

            while (_tokenizer.Token != null && !ReservedKeywords.Contains(_tokenizer.Token) && _tokenizer.Token != ")" &&
                   _tokenizer.Token != "," && _tokenizer.Token != "->")
            {
                list.Add(ParseSimpleTypeClause());
            }

            return(new NamedTypeClause(name, list));
        }
コード例 #2
0
        private Expression ParseDataTypeDeclarationExpression()
        {
            RequireToken("data");

            var name = ParseName();

            Assert(Char.IsUpper(name[0]), "Type names should start with an uppercase letter");

            var arguments = new List <string>();

            while (_tokenizer.Token != null && !ReservedKeywords.Contains(_tokenizer.Token))
            {
                var arg = ParseName();
                Assert(Char.IsLower(arg[0]), "Type variable names should start with an lowercase letter");
                arguments.Add(arg);
            }

            var constructors = new List <DataTypeConstructorDeclaration>();

            while (_tokenizer.Token == "case")
            {
                constructors.Add(ParseDataTypeConstructorDeclaration());
            }

            return(new DataTypeDeclarationExpression(name, arguments, constructors));
        }
コード例 #3
0
 private string ParseName()
 {
     Assert(_tokenizer.Token != null, "Name expected, but end-of-file found");
     Assert(Char.IsLetter(_tokenizer.Token[0]) || _tokenizer.Token[0] == '_',
            String.Format("Letter expected, but {0} found", _tokenizer.Token[0]));
     Assert(_tokenizer.Token.Skip(1).All(x => Char.IsLetterOrDigit(x) || x == '_'), "Invalid character");
     Assert(!ReservedKeywords.Contains(_tokenizer.Token),
            String.Format("Non-keyword expected, but {0} found", _tokenizer.Token));
     return(_tokenizer.Advance());
 }
コード例 #4
0
ファイル: EscapeUtils.cs プロジェクト: KrystianD/KDPgDriver
 public static string QuoteObjectName(string str)
 {
     if (str.ToHashSet().IsSubsetOf(ValidObjectNameChars) && !ReservedKeywords.Contains(str.ToLower()))
     {
         return(str);
     }
     else
     {
         return("\"" + str + "\"");
     }
 }
コード例 #5
0
        private Expression ParseApplication()
        {
            var res = ParseUnaryMinusExpression();

            while (_tokenizer.Token != null && !ReservedKeywords.Contains(_tokenizer.Token) &&
                   MayStartTerm(_tokenizer.Token[0]))
            {
                res = new ApplicationExpression(res, ParseTerm());
            }

            return(res);
        }
コード例 #6
0
 protected override IEnumerable <IInspectionResult> DoGetInspectionResults()
 {
     return(State.DeclarationFinder.UserDeclarations(DeclarationType.UserDefinedTypeMember)
            .Concat(State.DeclarationFinder.UserDeclarations(DeclarationType.EnumerationMember))
            .Where(m => ReservedKeywords.Any(k =>
                                             k.ToLowerInvariant().Equals(
                                                 m.IdentifierName.Trim().TrimStart('[').TrimEnd(']').ToLowerInvariant())))
            .Select(m => new DeclarationInspectionResult(
                        this,
                        InspectionResults.KeywordsUsedAsMemberInspection.
                        ThunderCodeFormat(m.IdentifierName),
                        m
                        )));
 }
コード例 #7
0
        /// <summary>
        /// Append @ to all reserved keywords that appear on the given name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string Mangle(string name)
        {
            var builder = new StringBuilder();

            string[] names = name.Split(Dot);
            for (int i = 0; i < names.Length; ++i)
            {
                if (ReservedKeywords.Contains(names[i]))
                {
                    builder.Append(At);
                }
                builder.Append(names[i]);
                builder.Append(Dot);
            }
            builder.Remove(builder.Length - 1, 1);
            return(builder.ToString());
        }
コード例 #8
0
        public string ResolveNameConflict(string tableNameHumanCase)
        {
            if (!_usedNames.ContainsKey(tableNameHumanCase))
            {
                _usedNames[tableNameHumanCase] = 0;
            }

            var toAdd = ReservedKeywords.Contains(tableNameHumanCase) || _usedNames[tableNameHumanCase] > 0;

            if (!toAdd)
            {
                return(tableNameHumanCase);
            }

            var c = _usedNames[tableNameHumanCase];

            _usedNames[tableNameHumanCase] = c + 1;
            return(tableNameHumanCase + c.ToString());
        }
コード例 #9
0
        private Pattern ParseBracketedPatternWithoutType()
        {
            var list = new List <Pattern>();

            Assert(!ReservedKeywords.Contains(_tokenizer.Token), "Unexpected token: " + _tokenizer.Token);

            if (_tokenizer.Token == ")" || _tokenizer.Token == ":")
            {
                return(new TuplePattern(list));
            }

            list.Add(ParseNestedPattern());

            while (_tokenizer.Token == ",")
            {
                _tokenizer.Advance();
                list.Add(ParsePattern());
            }

            return(list.Count == 1 ? list[0] : new TuplePattern(list));
        }
コード例 #10
0
        private DataTypeConstructorDeclaration ParseDataTypeConstructorDeclaration()
        {
            RequireToken("case");

            var name = ParseName();

            Assert(Char.IsUpper(name[0]), "Constructor names should start with an uppercase letter");

            if (_tokenizer.Token != "of")
            {
                return(new DataTypeConstructorDeclaration(name, new List <TypeClause>()));
            }

            RequireToken("of");

            var arguments = new List <TypeClause>();

            while (_tokenizer.Token != null && !ReservedKeywords.Contains(_tokenizer.Token))
            {
                arguments.Add(ParseTypeClause());
            }

            return(new DataTypeConstructorDeclaration(name, arguments));
        }
コード例 #11
0
 private static string GetIdentifierName(string possibleIdentifier)
 {
     return(ReservedKeywords.Contains(possibleIdentifier, StringComparer.Ordinal)
         ? $"@{possibleIdentifier}"
         : possibleIdentifier);
 }
コード例 #12
0
ファイル: ContractGenerator.cs プロジェクト: zutobg/Meadow
        string GenerateFunction(Abi methodAbi)
        {
            var callDataParams = new List <string>();

            string functionSig = AbiSignature.GetFullSignature(methodAbi);

            callDataParams.Add($"\"{functionSig}\"");

            string inputConstructorArg = string.Empty;
            bool   hasInputs           = methodAbi.Inputs.Length > 0;

            if (hasInputs)
            {
                var inputs = GenerateInputs(methodAbi.Inputs);
                inputConstructorArg = GenerateInputString(inputs);

                for (var i = 0; i < inputs.Length; i++)
                {
                    var    input = inputs[i];
                    string encoderLine;
                    if (input.AbiType.IsMultiDimensionalArray)
                    {
                        encoderLine = $"EncoderFactory.LoadMultiDimArrayEncoder(\"{input.SolidityTypeName}\", {input.Identifier})";
                    }
                    else if (input.AbiType.IsArrayType)
                    {
                        string arrayElementType = GetArrayElementClrTypeName(input.AbiType);
                        string arrayItemEncoder = $"EncoderFactory.LoadEncoder(\"{input.AbiType.ArrayItemInfo.SolidityName}\", default({arrayElementType}))";
                        encoderLine = $"EncoderFactory.LoadEncoder(\"{input.SolidityTypeName}\", {input.Identifier}, {arrayItemEncoder})";
                    }
                    else
                    {
                        encoderLine = $"EncoderFactory.LoadEncoder(\"{input.SolidityTypeName}\", {input.Identifier})";
                    }

                    callDataParams.Add(encoderLine);
                }
            }

            string callDataString = string.Join(", ", callDataParams);

            var    outputs        = GenerateOutputs(methodAbi.Outputs);
            string outputParams   = GenerateOutputString(outputs);
            string outputClrTypes = string.Empty;

            if (outputs.Length > 0)
            {
                outputClrTypes = "<" + string.Join(", ", outputs.Select(s => s.ClrTypeName)) + ">";
            }

            string returnType;

            if (outputs.Length == 0)
            {
                returnType = string.Empty;
            }
            else if (outputs.Length == 1)
            {
                returnType = $"<{outputs[0].ClrTypeName}>";
            }
            else
            {
                returnType = $"<({outputParams})>";
            }

            string[] decoderParams = new string[outputs.Length];
            for (var i = 0; i < outputs.Length; i++)
            {
                string decoder;
                if (outputs[i].AbiType.IsMultiDimensionalArray)
                {
                    var clrType = GetArrayElementClrTypeName(outputs[i].AbiType);
                    decoder = $"DecoderFactory.GetMultiDimArrayDecoder<{clrType}>(\"{outputs[i].AbiType.SolidityName}\")";
                }
                else if (outputs[i].AbiType.IsArrayType)
                {
                    string arrayElementType = GetArrayElementClrTypeName(outputs[i].AbiType);
                    decoder = $"DecoderFactory.GetArrayDecoder(EncoderFactory.LoadEncoder(\"{outputs[i].AbiType.ArrayItemInfo.SolidityName}\", default({arrayElementType})))";
                }
                else
                {
                    decoder = "DecoderFactory.Decode";
                }

                decoderParams[i] = $"\"{methodAbi.Outputs[i].Type}\", {decoder}";
            }

            string decoderStr;

            if (outputs.Length > 0)
            {
                decoderStr = "this, callData, " + string.Join(", ", decoderParams);
            }
            else
            {
                decoderStr = "this, callData";
            }

            var methodName = ReservedKeywords.EscapeIdentifier(methodAbi.Name);

            var(summaryDoc, paramsDoc) = GetSummaryXmlDoc(methodAbi);

            return($@"
                /// <summary>{summaryDoc}</summary>
                {paramsDoc}
                public EthFunc{returnType} {methodName}({inputConstructorArg})
                {{
                    var callData = {typeof(EncoderUtil).FullName}.GetFunctionCallBytes({callDataString});

                    return EthFunc.Create{outputClrTypes}({decoderStr});
                }}
            ");
        }
コード例 #13
0
        /*
         *
         * NOTE WHEN YOU ARE MAPPING THE XML TO C# TYPES
         * [SCEHMANAME]:NIL = "TRUE" means that the property is nullable and you should mapping correctly to the nullable type
         *
         */

        public string Write(IEnumerable <Class> classInfo, string rootName)
        {
            StringBuilder sb = new StringBuilder();

            // Add Top Comment
            if (!string.IsNullOrEmpty(Comment))
            {
                if (!UsePascalCase)
                {
                    if (ReservedKeywords.Contains(rootName)) // Check for reserved words in case no pascal
                    {
                        sb.AppendLine(string.Format(Comment, "@" + rootName));
                    }
                    else
                    {
                        sb.AppendLine(string.Format(Comment, rootName));
                    }
                }
                else
                {
                    sb.AppendLine(string.Format(Comment, rootName.ToTitleCase()));
                }
            }

            foreach (var @class in classInfo)
            {
                if (!RemoveXMLAttributes)
                {
                    if (AddNamespaceAttributes)
                    {
                        sb.AppendFormat("[XmlRoot(ElementName=\"{0}\", Namespace=\"{1}\")]{2}", @class.XmlName, @class.Namespace, Environment.NewLine);
                    }
                    else
                    {
                        sb.AppendFormat("[XmlRoot(ElementName=\"{0}\")]{1}", @class.XmlName, Environment.NewLine);
                    }
                }

                if (UsePascalCase)
                {
                    @class.Name = @class.Name.ToTitleCase();
                }

                if (!UsePascalCase) // Check for reserved words in case no pascal
                {
                    if (ReservedKeywords.Contains(@class.Name))
                    {
                        @class.Name = "@" + @class.Name;
                    }
                }

                sb.AppendFormat("public class {0} {{ {1}", @class.Name, Environment.NewLine);
                foreach (var field in @class.Fields)
                {
                    if (!RemoveXMLAttributes)
                    {
                        if (AddNamespaceAttributes)
                        {
                            sb.AppendFormat("{3}\t[Xml{0}({0}Name=\"{1}\", Namespace=\"{2}\")] {3}", field.XmlType, field.XmlName, field.Namespace, Environment.NewLine);
                        }
                        else
                        {
                            sb.AppendFormat("{2}\t[Xml{0}({0}Name=\"{1}\")] {2}", field.XmlType, field.XmlName, Environment.NewLine);
                        }
                    }

                    if (UsePascalCase)
                    {
                        field.Name = field.Name.ToTitleCase();

                        if (field.Type.ToLower() == field.Name.ToLower())
                        {
                            field.Type = field.Name;
                        }
                    }

                    if (!UsePascalCase) // Check for reserved words in case no pascal
                    {
                        if (ReservedKeywords.Contains(field.Name))
                        {
                            field.Name = "@" + field.Name;
                        }
                    }

                    if (UseFields)
                    {
                        sb.AppendFormat("\tpublic {0} {1}; {2}", field.Type, field.Name, Environment.NewLine);
                    }
                    else
                    {
                        sb.AppendFormat("\tpublic {0} {1} {{ get; set; }} {2}", field.Type, field.Name, Environment.NewLine);
                    }
                }
                sb.AppendLine("}");
                sb.AppendLine("");
            }

            return(sb.ToString());
        }
コード例 #14
0
 public bool IsReservedKeyword(string text) => ReservedKeywords.Contains(text);