Beispiel #1
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            if (enode.Name.Contains("Flag"))
            {
                sb.AppendLine(padding + "[Flags]");
            }

            if (enode.Type != null)
            {
                sb.AppendLine(padding + "public enum " + enode.Name + " : " + EmitType(enode.Type));
            }
            else
            {
                sb.AppendLine(padding + "public enum " + enode.Name);
            }

            sb.AppendLine(padding + "{");

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default);
                sb.AppendLine(padding + "\t" + prop.Name + " = " + lastValue + ",");
            }

            sb.AppendLine(padding + "}");
        }
Beispiel #2
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb)
        {
            sb.AppendLine();
            sb.AppendLine("Steam." + enode.Name + " = {");

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default);
                sb.Append("  " + prop.Name + ": " + lastValue + ",");
                if (prop.Obsolete != null)
                {
                    if (prop.Obsolete.Length > 0)
                    {
                        sb.Append(" // obsolete - " + prop.Obsolete);
                    }
                    else
                    {
                        sb.Append(" // obsolete");
                    }
                }
                sb.AppendLine();
            }

            sb.AppendLine("};");
        }
Beispiel #3
0
        public static int GetTypeSize(PropNode prop)
        {
            Symbol sym = prop.Type;

            // no static size for proto
            if (prop.Flags != null && prop.Flags == "proto")
            {
                return(0);
            }

            if (sym is WeakSymbol)
            {
                WeakSymbol wsym = sym as WeakSymbol;
                string     key  = wsym.Identifier;

                if (!weakTypeMap.ContainsKey(key))
                {
                    key = defaultType;
                }

                if (!String.IsNullOrEmpty(prop.FlagsOpt))
                {
                    return(Int32.Parse(prop.FlagsOpt));
                }

                return(weakTypeMap[key].Size);
            }
            else if (sym is StrongSymbol)
            {
                StrongSymbol ssym = sym as StrongSymbol;

                if (ssym.Class is EnumNode)
                {
                    EnumNode enode = ssym.Class as EnumNode;

                    if (enode.Type is WeakSymbol)
                    {
                        return(weakTypeMap[((WeakSymbol)enode.Type).Identifier].Size);
                    }
                    else
                    {
                        return(weakTypeMap[defaultType].Size);
                    }
                }
            }

            return(0);
        }
Beispiel #4
0
        protected override void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            if (enode.Flags == "flags")
            {
                sb.AppendLine(padding + "typedef NS_OPTIONS(NSUInteger, " + enode.Name + ") {");
            }
            else
            {
                sb.AppendLine(padding + "typedef NS_ENUM(NSUInteger, " + enode.Name + ") {");
            }

            string lastValue = "0";

            bool hasMax = false;

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default);
                sb.AppendLine(padding + "\t" + enode.Name + prop.Name + " = " + lastValue + ",");
                if (prop.Name.Equals("Max", StringComparison.OrdinalIgnoreCase))
                {
                    hasMax = true;
                }
            }

            long maxlong = 0;

            if (lastValue.StartsWith("0x"))
            {
                maxlong = Convert.ToInt64(lastValue.Substring(2, lastValue.Length - 2), 16);
            }
            else
            {
                maxlong = Int64.Parse(lastValue);
            }

            maxlong++;

            if (!hasMax && enode.Flags != "flags")
            {
                sb.AppendLine(padding + "\t" + enode.Name + "Max = " + maxlong + ",");
            }
            sb.AppendLine(padding + "};");
            sb.AppendLine();
        }
Beispiel #5
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            if (enode.Flags == "flags")
            {
                sb.AppendLine(padding + "[Flags]");
            }

            if (enode.Type != null)
            {
                sb.AppendLine(padding + "public enum " + enode.Name + " : " + EmitType(enode.Type));
            }
            else
            {
                sb.AppendLine(padding + "public enum " + enode.Name);
            }

            sb.AppendLine(padding + "{");

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                if (!prop.Emit)
                {
                    continue;
                }

                lastValue = EmitMultipleTypes(prop.Default);

                if (prop.Obsolete != null)
                {
                    if (prop.Obsolete.Length > 0)
                    {
                        sb.AppendLine(padding + "\t[Obsolete( \"" + prop.Obsolete + "\" )]");
                    }
                    else
                    {
                        sb.AppendLine(padding + "\t[Obsolete]");
                    }
                }
                sb.AppendLine(padding + "\t" + prop.Name + " = " + lastValue + ",");
            }

            sb.AppendLine(padding + "}");
        }
Beispiel #6
0
        protected override void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            if (enode.Flags == "flags")
            {
                sb.AppendLine(padding + "typedef NS_OPTIONS(NSUInteger, " + enode.Name + ") {");
            }
            else
            {
                sb.AppendLine(padding + "typedef NS_ENUM(NSUInteger, " + enode.Name + ") {");
            }

            string lastValue = "0";

            bool hasMax = false;

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default);
                sb.AppendLine(padding + "\t" + enode.Name + prop.Name + " = " + lastValue + ",");
                if (prop.Name.Equals("Max", StringComparison.OrdinalIgnoreCase))
                {
                    hasMax = true;
                }
            }

            long maxlong = 0;

            if (lastValue.StartsWith("0x"))
                maxlong = Convert.ToInt64(lastValue.Substring(2, lastValue.Length - 2), 16);
            else
                maxlong = Int64.Parse(lastValue);

            maxlong++;

            if (!hasMax && enode.Flags != "flags")
            {
                sb.AppendLine(padding + "\t" + enode.Name + "Max = " + maxlong + ",");
            }
            sb.AppendLine(padding + "};");
            sb.AppendLine();
        }
Beispiel #7
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb)
        {
            sb.AppendLine();
            sb.AppendLine("Steam." + enode.Name + " = {");

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default);
                sb.Append("  " + prop.Name + ": " + lastValue + ",");
                if (prop.Obsolete != null)
                {
                    if (prop.Obsolete.Length > 0)
                        sb.Append(" // obsolete - " + prop.Obsolete);
                    else
                        sb.Append(" // obsolete");
                }
                sb.AppendLine();
            }

            sb.AppendLine("};");
        }
Beispiel #8
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            sb.AppendLine(padding + "public enum " + enode.Name);
            sb.AppendLine(padding + "{");

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default.FirstOrDefault());
                sb.AppendLine(padding + "\t" + prop.Name + "(" + lastValue + "),");
            }

            int maxint = Int32.Parse(lastValue) + 1;

            sb.AppendLine(padding + "\tMax(" + maxint + ");");

            sb.AppendLine();
            sb.AppendLine(padding + "\tprivate int code;");

            sb.AppendLine(padding + "\tprivate " + enode.Name + "( int c ) { code = c; }");

            sb.AppendLine(padding + "\tpublic int getCode() {");
            sb.AppendLine(padding + "\t\treturn code;");
            sb.AppendLine(padding + "\t}");

            sb.AppendLine(padding + "\tpublic static " + enode.Name + " lookup( int code ) {");
            sb.AppendLine(padding + "\t\tfor ( " + enode.Name + " x : values() ) {");
            sb.AppendLine(padding + "\t\t\tif( x.getCode() == code ) return x;");
            sb.AppendLine(padding + "\t\t}");
            sb.AppendLine(padding + "\t\treturn Invalid;");
            sb.AppendLine(padding + "\t}");

            sb.AppendLine(padding + "}");
        }
Beispiel #9
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            if ( enode.Name.Contains( "Flag" ) )
                sb.AppendLine( padding + "[Flags]" );

            if ( enode.Type != null )
            {
                sb.AppendLine( padding + "public enum " + enode.Name + " : " + EmitType( enode.Type ) );
            }
            else
            {
                sb.AppendLine( padding + "public enum " + enode.Name );
            }

            sb.AppendLine( padding + "{" );

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default);
                sb.AppendLine(padding + "\t" + prop.Name + " = " + lastValue + ",");
            }

            sb.AppendLine(padding + "}");
        }
Beispiel #10
0
        private void EmitClassSerializer(ClassNode cnode, StringBuilder sb, int level, int baseSize)
        {
            string padding = new String('\t', level);

            sb.AppendLine();
            sb.AppendLine(padding + "public ByteBuffer serialize()");
            sb.AppendLine(padding + "{");


            // first emit variable length members
            List <String> varLengthProps = new List <String>();

            varLengthProps.Add(baseSize.ToString());

            if (cnode.Parent != null)
            {
                sb.AppendLine(padding + "\tByteBuffer bufHeader = Header.serialize();");
                varLengthProps.Add("bufHeader.limit()");

                sb.AppendLine();
            }

            foreach (PropNode prop in cnode.childNodes)
            {
                string typestr = EmitType(prop.Type);
                int    size    = CodeGenerator.GetTypeSize(prop);

                if (size == 0)
                {
                    if (prop.Flags != null && prop.Flags == "proto")
                    {
                        sb.AppendLine(padding + "\tByteBuffer buf" + GetUpperName(prop.Name) + " = " + prop.Name + ".toByteString().asReadOnlyByteBuffer();");

                        if (prop.FlagsOpt != null)
                        {
                            sb.AppendLine(padding + "\t" + prop.FlagsOpt + " = buf" + GetUpperName(prop.Name) + ".limit();");
                        }

                        if (baseSize == 0)
                        {
                            // early exit
                            sb.AppendLine(padding + "\treturn buf" + GetUpperName(prop.Name) + ";");
                            sb.AppendLine(padding + "}");
                            return;
                        }
                    }
                    else
                    {
                        sb.AppendLine(padding + "\tByteBuffer buf" + GetUpperName(prop.Name) + " = " + GetUpperName(prop.Name) + ".serialize();");
                    }

                    varLengthProps.Add("buf" + GetUpperName(prop.Name) + ".limit()");
                }
            }

            sb.AppendLine(padding + "\tByteBuffer buffer = ByteBuffer.allocate( " + String.Join(" + ", varLengthProps.ToArray()) + " );");
            sb.AppendLine(padding + "\tbuffer.order( ByteOrder.LITTLE_ENDIAN );");
            sb.AppendLine();

            if (cnode.Parent != null)
            {
                sb.AppendLine(padding + "\tbuffer.put( bufHeader );");
            }

            // next emit writers
            foreach (PropNode prop in cnode.childNodes)
            {
                string typestr  = EmitType(prop.Type);
                string typecast = "";
                string propName = prop.Name;
                int    size     = CodeGenerator.GetTypeSize(prop);

                if (prop.Type is StrongSymbol && ((StrongSymbol)prop.Type).Class is EnumNode)
                {
                    EnumNode enode = ((StrongSymbol)prop.Type).Class as EnumNode;

                    if (enode.Type is WeakSymbol)
                    {
                        typecast = "(" + ((WeakSymbol)enode.Type).Identifier + ")";
                    }
                    else
                    {
                        typecast = "(int)";
                    }

                    propName = propName + ".getCode()";
                }

                if (!readerTypeMap.ContainsKey(typestr))
                {
                    typestr = CodeGenerator.GetTypeOfSize(size, SupportsUnsignedTypes());
                }

                if (prop.Flags != null)
                {
                    if (prop.Flags == "steamidmarshal")
                    {
                    }
                    else if (prop.Flags == "proto")
                    {
                        sb.AppendLine(padding + "\tbuffer.put( buf" + GetUpperName(propName) + " );");
                        continue;
                    }
                    else if (prop.Flags == "const")
                    {
                        continue;
                    }
                }

                if (prop.Flags == "protomask")
                {
                    propName = "MsgUtil.MakeMsg( " + propName + ", true )";
                }

                sb.AppendLine(padding + "\tbuffer.put" + readerTypeMap[typestr] + "( " + typecast + propName + " );");
            }

            sb.AppendLine();

            sb.AppendLine();
            sb.AppendLine(padding + "\tbuffer.flip();");
            sb.AppendLine(padding + "\treturn buffer;");
            sb.AppendLine(padding + "}");
        }
Beispiel #11
0
        public static Node Analyze( Queue<Token> tokens )
        {
            Node root = new Node();

            while (tokens.Count > 0)
            {
                Token cur = tokens.Dequeue();

                switch (cur.Name)
                {
                    case "EOF":
                        break;
                    case "preprocess":
                        Token text = Expect(tokens, "string");

                        if (cur.Value == "import")
                        {
                            Queue<Token> parentTokens = LanguageParser.TokenizeString( File.ReadAllText( text.Value ) );

                            Node newRoot = Analyze( parentTokens );

                            foreach (Node child in newRoot.childNodes)
                            {
                                root.childNodes.Add(child);
                            }
                        }
                        break;
                    case "identifier":
                        switch (cur.Value)
                        {
                            case "class":
                                {
                                    Token name = Expect(tokens, "identifier");
                                    Token ident = null, parent = null;

                                    Token op1 = Optional(tokens, "operator", "<");
                                    if (op1 != null)
                                    {
                                        ident = Expect(tokens, "identifier");
                                        Token op2 = Expect(tokens, "operator", ">");
                                    }

                                    Token expect = Optional(tokens, "identifier", "expects");
                                    if (expect != null)
                                    {
                                        parent = Expect(tokens, "identifier");
                                    }

                                    ClassNode cnode = new ClassNode();
                                    cnode.Name = name.Value;

                                    if (ident != null)
                                    {
                                        cnode.Ident = SymbolLocator.LookupSymbol(root, ident.Value, false);
                                    }

                                    if (parent != null)
                                    {
                                        //cnode.Parent = SymbolLocator.LookupSymbol(root, parent.Value, true);
                                    }

                                    root.childNodes.Add(cnode);
                                    ParseInnerScope(tokens, cnode, root);
                                }
                                break;
                            case "enum":
                                {
                                    Token name = Expect(tokens, "identifier");
                                    Token datatype = null;

                                    Token op1 = Optional(tokens, "operator", "<");
                                    if (op1 != null)
                                    {
                                        datatype = Expect(tokens, "identifier");
                                        Token op2 = Expect(tokens, "operator", ">");
                                    }

                                    Token flag = Optional( tokens, "identifier", "flags" );

                                    EnumNode enode = new EnumNode();
                                    enode.Name = name.Value;

                                    if ( flag != null )
                                    {
                                        enode.Flags = flag.Value;
                                    }

                                    if (datatype != null)
                                    {
                                        enode.Type = SymbolLocator.LookupSymbol(root, datatype.Value, false);
                                    }


                                    root.childNodes.Add(enode);
                                    ParseInnerScope(tokens, enode, root);
                                }
                                break;
                        }
                        break;
                }
            }

            return root;
        }
Beispiel #12
0
        private long EvalEnum(EnumNode enode, PropNode prop)
        {
            long number = 0;
            foreach (Symbol sym in prop.Default) {
                long n = 0;
                var val = (sym as WeakSymbol).Identifier;

                if (new Regex("^[-0-9]+$|^0x[-0-9a-fA-F]+$").IsMatch(val)) {
                    int bas = 10;
                    if (val.StartsWith("0x", StringComparison.Ordinal)) {
                        bas = 16;
                        val = val.Substring(2);
                    }
                    n = Convert.ToInt64(val, bas);
                } else {
                    var otherNode = enode.childNodes.Single(o => o is PropNode && (o as PropNode).Name == val) as PropNode;
                    n = EvalEnum(enode, otherNode);
                }
                number = number | n;
            }
            return number;
        }
Beispiel #13
0
        private void EmitEnumStringer(EnumNode enode, StringBuilder sb)
        {
            sb.AppendLine("var " + enode.Name + "_name = map[" + enode.Name + "]string{");

            // duplicate elements don't really make sense here, so we filter them
            var uniqueNodes = new List<PropNode>();
            {
                var allValues = new List<long>();
                foreach (var node in enode.childNodes) {
                    if (!(node is PropNode))
                        continue;
                    var prop = node as PropNode;
                    long val = EvalEnum(enode, prop);
                    if (allValues.Contains(val))
                        continue;
                    allValues.Add(val);
                    uniqueNodes.Add(prop);
                }
            }

            foreach (PropNode prop in uniqueNodes) {
                sb.Append("    " + EvalEnum(enode, prop) + ": ");
                sb.AppendLine("\"" + enode.Name + "_" + prop.Name + "\",");
            }

            sb.AppendLine("}");
            sb.AppendLine();

            sb.AppendLine("func (e " + enode.Name + ") String() string {");
            sb.AppendLine("    if s, ok := " + enode.Name + "_name[e]; ok {");
            sb.AppendLine("         return s");
            sb.AppendLine("    }");
            sb.AppendLine("    var flags []string");
            sb.AppendLine("    for k, v := range " + enode.Name + "_name {");
            sb.AppendLine("        	if e&k != 0 {");
            sb.AppendLine("        		flags = append(flags, v)");
            sb.AppendLine("	        }");
            sb.AppendLine("    }");
            sb.AppendLine("    if len(flags) == 0 {");
            sb.AppendLine("        return fmt.Sprintf(\"%d\", e)");
            sb.AppendLine("    }");
            sb.AppendLine("    sort.Strings(flags)");
            sb.AppendLine("    return strings.Join(flags, \" | \")");
            sb.AppendLine("}");
            sb.AppendLine();
        }
 protected override void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
 {
     // Do nothing. Enums are already defined in the @interface generator.
 }
        private void EmitClassSerializer(ClassNode cnode, StringBuilder sb, int level, int baseSize)
        {
            string padding = new String('\t', level);

            sb.AppendLine();
            sb.AppendLine(padding + "- (void) serialize:(NSMutableData *)_data");
            sb.AppendLine(padding + "{");
            sb.AppendLine();

            if (cnode.Parent != null)
            {
                sb.AppendLine(padding + "\t[self.header serialize:data];");
                sb.AppendLine();
            }

            foreach (PropNode prop in cnode.childNodes)
            {
                string typestr = EmitType(prop.Type);
                int    size    = CodeGenerator.GetTypeSize(prop);

                if (size == 0)
                {
                    if (prop.Flags != null && prop.Flags == "proto")
                    {
                        //sb.AppendLine(padding + "\t[_data appendProtobuf:" + GetPropertyName(prop.Name) + "];");
                    }
                    else
                    {
                        sb.AppendLine(padding + "\tMemoryStream ms" + GetUpperName(prop.Name) + " = " + GetUpperName(prop.Name) + ".serialize();");
                    }
                }
            }

            if (cnode.Parent != null)
            {
                sb.AppendLine(padding + "\tmsHeader.CopyTo( msBuffer );");
            }

            // next emit writers
            foreach (PropNode prop in cnode.childNodes)
            {
                string typecast = "";
                string propName = GetUpperName(prop.Name);

                if (prop.Type is StrongSymbol && ((StrongSymbol)prop.Type).Class is EnumNode)
                {
                    EnumNode enode = ((StrongSymbol)prop.Type).Class as EnumNode;

                    if (enode.Type is WeakSymbol)
                    {
                        typecast = "(" + ((WeakSymbol)enode.Type).Identifier + ")";
                    }
                    else
                    {
                        typecast = "(int)";
                    }
                }

                if (prop.Flags != null)
                {
                    if (prop.Flags == "steamidmarshal" || prop.Flags == "gameidmarshal" || prop.Flags == "boolmarshal")
                    {
                        propName = prop.Name;
                    }
                    else if (prop.Flags == "proto")
                    {
                        sb.AppendLine(padding + "\t[_data appendData:[" + GetPropertyName(propName) + " data]];");
                        continue;
                    }
                    else if (prop.Flags == "const")
                    {
                        continue;
                    }
                }

                if (prop.Flags == "protomask")
                {
                    if (prop.Default is StrongSymbol && (prop.Default as StrongSymbol).Class.Name == "EGCMsg")
                    {
                        propName = "[_SKMsgUtil makeGCMsg:" + GetPropertyName(propName) + " isProtobuf:YES]";
                    }
                    else
                    {
                        propName = "[_SKMsgUtil makeMsg:" + GetPropertyName(propName) + " isProtobuf:YES]";
                    }
                }

                int    size    = CodeGenerator.GetTypeSize(prop);
                string typestr = CodeGenerator.GetTypeOfSize(size, SupportsUnsignedTypes());

                if (!String.IsNullOrEmpty(prop.FlagsOpt))
                {
                    sb.AppendLine(padding + "\t[_data appendData:" + GetPropertyName(propName) + "];");
                }
                else
                {
                    sb.AppendLine(padding + "\t[_data cr_append" + readerTypeMap[typestr] + ":" + GetPropertyName(propName) + "];");
                }

                //sb.AppendLine(padding + "\tbw.Write( " + typecast + propName + " );");
            }

            sb.AppendLine();

            //sb.AppendLine();
            //sb.AppendLine(padding + "\tmsBuffer.Seek( 0, SeekOrigin.Begin );");
            sb.AppendLine(padding + "}");
        }
 protected override void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
 {
     // Do nothing. Enums are already defined in the @interface generator.
 }
Beispiel #17
0
 protected virtual void EmitEnumNode(EnumNode n, StringBuilder sb, int level)
 {
 }
Beispiel #18
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            if ( enode.Flags == "flags" )
                sb.AppendLine( padding + "[Flags]" );

            if ( enode.Type != null )
            {
                sb.AppendLine( padding + "public enum " + enode.Name + " : " + EmitType( enode.Type ) );
            }
            else
            {
                sb.AppendLine( padding + "public enum " + enode.Name );
            }

            sb.AppendLine( padding + "{" );

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitMultipleTypes(prop.Default);

                if ( prop.Obsolete != null )
                {
                    if ( prop.Obsolete.Length > 0 )
                        sb.AppendLine( padding + "\t[Obsolete( \"" + prop.Obsolete + "\" )]" );
                    else
                        sb.AppendLine( padding + "\t[Obsolete]" );
                }
                sb.AppendLine(padding + "\t" + prop.Name + " = " + lastValue + ",");
            }

            sb.AppendLine(padding + "}");
        }
Beispiel #19
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb, int level)
        {
            string padding = new String('\t', level);

            sb.AppendLine(padding + "public enum " + enode.Name);
            sb.AppendLine(padding + "{");

            string lastValue = "0";

            foreach (PropNode prop in enode.childNodes)
            {
                lastValue = EmitType(prop.Default);
                sb.AppendLine(padding + "\t" + prop.Name + "(" + lastValue + "),");
            }

            int maxint = Int32.Parse(lastValue) + 1;

            sb.AppendLine(padding + "\tMax(" + maxint + ");");

            sb.AppendLine();
            sb.AppendLine(padding + "\tprivate int code;");

            sb.AppendLine(padding + "\tprivate " + enode.Name + "( int c ) { code = c; }");

            sb.AppendLine(padding + "\tpublic int getCode() {");
            sb.AppendLine(padding + "\t\treturn code;");
            sb.AppendLine(padding + "\t}");
    
            sb.AppendLine(padding + "\tpublic static " + enode.Name + " lookup( int code ) {");
            sb.AppendLine(padding + "\t\tfor ( " + enode.Name + " x : values() ) {");
            sb.AppendLine(padding + "\t\t\tif( x.getCode() == code ) return x;");
            sb.AppendLine(padding + "\t\t}");
            sb.AppendLine(padding + "\t\treturn Invalid;");
            sb.AppendLine(padding + "\t}");

            sb.AppendLine(padding + "}");
        }
Beispiel #20
0
        public static Node Analyze(Queue <Token> tokens)
        {
            Node root = new Node();

            while (tokens.Count > 0)
            {
                Token cur = tokens.Dequeue();

                switch (cur.Name)
                {
                case "EOF":
                    break;

                case "preprocess":
                    Token text = Expect(tokens, "string");

                    if (cur.Value == "import")
                    {
                        Queue <Token> parentTokens = LanguageParser.TokenizeString(File.ReadAllText(text.Value), text.Value);

                        Node newRoot = Analyze(parentTokens);

                        foreach (Node child in newRoot.childNodes)
                        {
                            root.childNodes.Add(child);
                        }
                    }
                    break;

                case "identifier":
                    switch (cur.Value)
                    {
                    case "class":
                    {
                        Token name = Expect(tokens, "identifier");
                        Token ident = null, parent = null;

                        Token op1 = Optional(tokens, "operator", "<");
                        if (op1 != null)
                        {
                            ident = Expect(tokens, "identifier");
                            Token op2 = Expect(tokens, "operator", ">");
                        }

                        Token expect = Optional(tokens, "identifier", "expects");
                        if (expect != null)
                        {
                            parent = Expect(tokens, "identifier");
                        }

                        Token removed = Optional(tokens, "identifier", "removed");

                        ClassNode cnode = new ClassNode();
                        cnode.Name = name.Value;

                        if (ident != null)
                        {
                            cnode.Ident = SymbolLocator.LookupSymbol(root, ident.Value, false);
                        }

                        if (parent != null)
                        {
                            //cnode.Parent = SymbolLocator.LookupSymbol(root, parent.Value, true);
                        }

                        if (removed != null)
                        {
                            cnode.Emit = false;
                        }
                        else
                        {
                            cnode.Emit = true;
                        }

                        root.childNodes.Add(cnode);
                        ParseInnerScope(tokens, cnode, root);
                    }
                    break;

                    case "enum":
                    {
                        Token name     = Expect(tokens, "identifier");
                        Token datatype = null;

                        Token op1 = Optional(tokens, "operator", "<");
                        if (op1 != null)
                        {
                            datatype = Expect(tokens, "identifier");
                            Token op2 = Expect(tokens, "operator", ">");
                        }

                        Token flag = Optional(tokens, "identifier", "flags");

                        EnumNode enode = new EnumNode();
                        enode.Name = name.Value;

                        if (flag != null)
                        {
                            enode.Flags = flag.Value;
                        }

                        if (datatype != null)
                        {
                            enode.Type = SymbolLocator.LookupSymbol(root, datatype.Value, false);
                        }


                        root.childNodes.Add(enode);
                        ParseInnerScope(tokens, enode, root);
                    }
                    break;
                    }
                    break;
                }
            }

            return(root);
        }
Beispiel #21
0
        private void EmitEnumStringer(EnumNode enode, StringBuilder sb)
        {
            sb.AppendLine("func (e " + enode.Name + ") String() string {");
            sb.AppendLine("    switch e {");

            // go does not allow duplicate cases, so we filter duplicte ones out
            var uniqueNodes = new List<PropNode>();
            {
                var allValues = new List<long>();
                for (int i = 0; i < enode.childNodes.Count; i++) {
                    Node node = enode.childNodes[i];
                    if (!(node is PropNode))
                        continue;
                    PropNode prop = node as PropNode;
                    long val = EvalEnum(enode, prop);
                    if (allValues.Contains(val))
                        continue;
                    allValues.Add(val);
                    uniqueNodes.Add(prop);
                }
            }

            foreach (PropNode prop in uniqueNodes) {
                sb.AppendLine("    case " + enode.Name + "_" + prop.Name + ":");
                sb.AppendLine("        return \"" + enode.Name + "_" + prop.Name + "\"");
            }
            sb.AppendLine("    default:");
            sb.AppendLine("        return \"INVALID\"");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            sb.AppendLine();
        }
Beispiel #22
0
        private void EmitEnumNode(EnumNode enode, StringBuilder sb)
        {
            string type = enode.Type != null ? EmitType(enode.Type) : "int32";
            enumTypes.Add(enode.Name, type);

            sb.AppendLine("type " + enode.Name + " " + type);
            sb.AppendLine();

            sb.AppendLine("const (");
            bool first = true;
            foreach (PropNode prop in enode.childNodes) {
                string val = String.Join(" | ", prop.Default.Select(item => {
                    var name = EmitSymbol(item);
                    // if this is an element of this enum, make sure to prefix it with its name
                    return (enode.childNodes.Exists(node => node.Name == name) ? enode.Name + "_" : "") + name;
                }));

                sb.Append("    " + enode.Name + "_" + prop.Name + " " + enode.Name + " = " + val);

                if (prop.Obsolete != null) {
                    if (prop.Obsolete.Length > 0)
                        sb.Append(" // Deprecated: " + prop.Obsolete);
                    else
                        sb.Append(" // Deprecated");
                }

                sb.AppendLine();

                if (first)
                    first = false;
            }
            sb.AppendLine(")");
            sb.AppendLine();

            if (debug)
                EmitEnumStringer(enode, sb);
        }
Beispiel #23
0
        private void EmitClassSerializer(ClassNode cnode, StringBuilder sb, int level, int baseSize)
        {
            string padding = new String('\t', level);

            sb.AppendLine();
            sb.AppendLine(padding + "public void Serialize(Stream stream)");
            sb.AppendLine(padding + "{");


            // first emit variable length members
            List <String> varLengthProps = new List <String>();
            List <String> openedStreams  = new List <String>();

            varLengthProps.Add(baseSize.ToString());

            if (cnode.Parent != null)
            {
                sb.AppendLine(padding + "\tHeader.Serialize(stream);");
                varLengthProps.Add("(int)msHeader.Length");
                openedStreams.Add("msHeader");

                sb.AppendLine();
            }

            foreach (PropNode prop in cnode.childNodes)
            {
                string typestr = EmitType(prop.Type);
                int    size    = CodeGenerator.GetTypeSize(prop);

                if (size == 0)
                {
                    if (prop.Flags != null && prop.Flags == "proto")
                    {
                        if (baseSize == 0)
                        {
                            // early exit
                            sb.AppendLine(padding + "\tProtoBuf.Serializer.Serialize<" + typestr + ">(stream, " + GetUpperName(prop.Name) + ");");
                            sb.AppendLine(padding + "}");
                            return;
                        }

                        sb.AppendLine(padding + "\tMemoryStream ms" + GetUpperName(prop.Name) + " = new MemoryStream();");
                        sb.AppendLine(padding + "\tProtoBuf.Serializer.Serialize<" + typestr + ">(ms" + GetUpperName(prop.Name) + ", " + GetUpperName(prop.Name) + ");");

                        if (prop.FlagsOpt != null)
                        {
                            sb.AppendLine(padding + "\t" + GetUpperName(prop.FlagsOpt) + " = (int)ms" + GetUpperName(prop.Name) + ".Length;");
                        }

                        //sb.AppendLine(padding + "\tms" + GetUpperName(prop.Name) + ".Seek( 0, SeekOrigin.Begin );");
                    }
                    else
                    {
                        sb.AppendLine(padding + "\tMemoryStream ms" + GetUpperName(prop.Name) + " = " + GetUpperName(prop.Name) + ".serialize();");
                    }

                    varLengthProps.Add("(int)ms" + GetUpperName(prop.Name) + ".Length");
                    openedStreams.Add("ms" + GetUpperName(prop.Name));
                }
            }

            //sb.AppendLine(padding + "\tBinaryWriterEx bw = new BinaryWriterEx( stream );");
            //sb.AppendLine();
            sb.AppendLine(padding + "\tBinaryWriter bw = new BinaryWriter( stream );");
            sb.AppendLine();

            if (cnode.Parent != null)
            {
                sb.AppendLine(padding + "\tmsHeader.CopyTo( msBuffer );");
            }

            // next emit writers
            foreach (PropNode prop in cnode.childNodes)
            {
                string typecast = "";
                string propName = GetUpperName(prop.Name);

                if (prop.Type is StrongSymbol && ((StrongSymbol)prop.Type).Class is EnumNode)
                {
                    EnumNode enode = ((StrongSymbol)prop.Type).Class as EnumNode;

                    if (enode.Type is WeakSymbol)
                    {
                        typecast = "(" + ((WeakSymbol)enode.Type).Identifier + ")";
                    }
                    else
                    {
                        typecast = "(int)";
                    }
                }

                if (prop.Flags != null)
                {
                    if (prop.Flags == "steamidmarshal" || prop.Flags == "gameidmarshal" || prop.Flags == "boolmarshal")
                    {
                        propName = prop.Name;
                    }
                    else if (prop.Flags == "proto")
                    {
                        sb.AppendLine(padding + "\tbw.Write( ms" + propName + ".ToArray() );");
                        continue;
                    }
                    else if (prop.Flags == "const")
                    {
                        continue;
                    }
                }

                if (prop.Flags == "protomask")
                {
                    propName = "MsgUtil.MakeMsg( " + propName + ", true )";
                }
                else if (prop.Flags == "protomaskgc")
                {
                    propName = "MsgUtil.MakeGCMsg( " + propName + ", true )";
                }

                sb.AppendLine(padding + "\tbw.Write( " + typecast + propName + " );");
            }

            sb.AppendLine();

            foreach (String stream in openedStreams)
            {
                sb.AppendLine(padding + "\t" + stream + ".Close();");
            }

            //sb.AppendLine();
            //sb.AppendLine(padding + "\tmsBuffer.Seek( 0, SeekOrigin.Begin );");
            sb.AppendLine(padding + "}");
        }
Beispiel #24
0
 protected virtual void EmitEnumNode(EnumNode n, StringBuilder sb, int level) { }