public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the While byte */
            ms.ReadByte();

            byte nextByte = Peek(ms);
            while (nextByte != 45) /* new line */
            {
                Condition.Add(Element.GetNextElement(ms, state, IndentLevel));
                nextByte = Peek(ms);
            }

            /* eat the new line */
            ms.ReadByte();

            nextByte = Peek(ms);
            state.AlternateBreak = 38;
            while (nextByte != 38) /* end-while */
            {
                Body.Add(Element.GetNextElement(ms, state, IndentLevel,true));
                nextByte = Peek(ms);
            }

            /* eat the end while */
            ms.ReadByte();

            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            int numBytes = 18;
            /* skip the marker */
            ms.ReadByte();
            int firstByte = ms.ReadByte();
            int decimalPlace = ms.ReadByte();

            BigInteger value = BigInteger.Zero;
            BigInteger fact = BigInteger.One;
            for (var i = 0; i < (numBytes - 4); i++)
            {
                value = BigInteger.Add(value, BigInteger.Multiply(fact, new BigInteger(ms.ReadByte())));
                fact = BigInteger.Multiply(fact, new BigInteger(256));
            }
            string number = value.ToString();

            if (decimalPlace > 0)
            {
                while (number.Length < decimalPlace)
                {
                    number = "0" + number;
                }
                number = number.Insert(number.Length - (decimalPlace), ".");
            }
            /* skip last 2 bytes */
            ms.ReadByte();
            ms.ReadByte();
            this.Value = number;
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the declare byte */
            ms.ReadByte();

            StringBuilder sb = new StringBuilder();
            sb.Append("Declare Function ");
            ms.Position += 1;

            Element.GetNextElement(ms, state, -1).Write(sb);

            sb.Append(" ");

            /* eat the "peoplecode" byte */
            ms.ReadByte();

            sb.Append("PeopleCode ");

            Element.GetNextElement(ms, state, -1).Write(sb);

            sb.Append(" ");

            Element.GetNextElement(ms, state, -1).Write(sb);

            sb.Append(";\r\n");

            Value = sb.ToString();

            /* eat 2 bytes */
            ms.Position += 2;
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the try byte */
            ms.ReadByte();
            state.AlternateBreak = 102;
            while (Peek(ms) != 102)
            {
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel, true);
                Body.Add(nextElement);
            }

            while (Peek(ms) != 103)
            {
                CatchElement catchElem = new CatchElement();
                catchElem.IndentLevel = IndentLevel;
                catchElem.Parse(ms, state);
                Catches.Add(catchElem);
            }

            /* eat the end try */
            ms.ReadByte();

            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the Local byte */
            byte variableType = (byte)ms.ReadByte();

            switch(variableType)
            {
                case 68:
                    declareType = DeclareType.LOCAL;
                    break;
                case 69:
                    declareType = DeclareType.GLOBAL;
                    break;
                case 84:
                    declareType = DeclareType.COMPONENT;
                    break;
                case 86:
                    declareType = DeclareType.CONSTANT;
                    break;
                case 98:
                    declareType = DeclareType.INSTANCE;
                    break;
            }

            byte nextByte = Peek(ms);
            Element stringElement = new PureStringElement();
            StringBuilder sb = new StringBuilder();
            if (declareType != DeclareType.CONSTANT)
            {
                while (nextByte != 1)
                {
                    var e = Element.GetNextElement(ms, state, -1);

                    if (e.Value == ":" && sb[sb.Length - 1] == ' ')
                    {
                        sb.Length--;
                    }

                    e.Write(sb);
                    if (e.Value != ":" && sb[sb.Length-1] != ':')
                    {
                        sb.Append(" ");
                    }

                    nextByte = Peek(ms);
                }

                VariableType = sb.ToString().Trim();
            }
            while (nextByte != 21) /* semicolon */
            {
                Declaration.Add(GetNextElement(ms, state, 0));
                nextByte = Peek(ms);
            }
            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the opening Function byte */
            ms.ReadByte();

            /* function name */
            var stringElement = new PureStringElement();
            stringElement.Parse(ms, state);
            MethodName = stringElement.Value;

            byte nextByte = Peek(ms);

            if (nextByte == 11)
            {
                /* function has parameters */
                /* eat open parens */
                ms.ReadByte();
                nextByte = Peek(ms);
                while (nextByte != 20) /* close paren */
                {
                    stringElement.Parse(ms, state);
                    var paramName = stringElement.Value;
                    /* eat the "as" */
                    ms.ReadByte();
                    stringElement.Parse(ms, state);
                    var paramType = stringElement.Value;
                    Params.Add(new Parameter() { Name = paramName, Type = paramType });

                    nextByte = Peek(ms);
                    if (nextByte == 3) /* comma */
                    {
                        ms.ReadByte();
                        nextByte = Peek(ms);
                    }
                }
                /* eat close parens */
                ms.ReadByte();
            }

            /* eat the newline */
            ms.ReadByte();

            while (nextByte != 55) /* end function */
            {
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel,true);
                Body.Add(nextElement);
                nextByte = Peek(ms);
            }

            /* eat end-function */
            ms.ReadByte();

            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the method byte */
            ms.ReadByte();

            Element stringElement = new PureStringElement();
            stringElement.Parse(ms, state);
            MethodName = stringElement.Value;

            /* eat the open paren */
            ms.ReadByte();
            byte nextByte = Peek(ms);

            while (nextByte != 20) /* close paren */
            {
                stringElement.Parse(ms, state);
                var paramName = stringElement.Value;
                /* eat the "as" */
                ms.ReadByte();
                stringElement.Parse(ms, state);
                var paramType = stringElement.Value;
                Params.Add(new Parameter() { Name = paramName, Type = paramType });

                nextByte = Peek(ms);
                if (nextByte == 3)
                {
                    /* eat the comma */
                    ms.ReadByte();
                    nextByte = Peek(ms);
                }
            }

            /* eat the close paren */
            ms.ReadByte();

            if (Peek(ms) == 57)
            {
                /* has a return value */
                /* eat the "Returns" */
                ms.ReadByte();

                nextByte = Peek(ms);

                while (nextByte != 21) /* semicolon */
                {
                    stringElement.Parse(ms, state);
                    ReturnType += stringElement.Value + " ";
                    nextByte = Peek(ms);
                }
                ReturnType = ReturnType.Trim();

            }
            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the throw byte */
            ms.ReadByte();

            Value = "";
            //while (Peek(ms) != 21 && Peek(ms) != state.AlternateBreak)
            //{
                var elem = Element.GetNextElement(ms, state, -1,true);
                Value += elem.ToString();
            //}
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat import byte */
            ms.ReadByte();

            while (Peek(ms) != 21) /* semicolon */
            {
                Import += Element.GetNextElement(ms, state, 0).Value;
            }
            /* eat semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the return byte */
            ms.ReadByte();

            Value = "";
            StringBuilder sb = new StringBuilder();
            while (Peek(ms) != 21 && Peek(ms) != 26 && Peek(ms) != 25 && Peek(ms) != 100)
            {
                var elem = Element.GetNextElement(ms, state, -1);
                elem.Write(sb);
                Value += sb.ToString();
                sb.Length = 0;
            }
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            IndentLevel = -1;
            /* skip the header */
            byte[] header = new byte[37];
            ms.Read(header, 0, 37);

            byte nextByte = Peek(ms);
            while (nextByte != 7)
            {
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel, true);
                Statements.Add(nextElement);
                nextByte = Peek(ms);
            }
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the marker */
            var marker = ms.ReadByte();

            MemoryStream bytesRead = new MemoryStream();
            byte[] currentChar = new byte[2];

            ms.Read(currentChar, 0, 2);
            while (currentChar[0] + currentChar[1] > 0)
            {
                bytesRead.Write(currentChar, 0, 2);
                ms.Read(currentChar, 0, 2);
            }

            Value = System.Text.Encoding.Unicode.GetString(bytesRead.ToArray());
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat comment marker */
            byte commentType = (byte) ms.ReadByte();

            if (commentType == 36)
            {
                Type = CommentType.SLASH_STAR;
            }
            else if (commentType == 78)
            {
                //TODO: make a global string writer to use instead of each element making its own string builder
                Type = CommentType.SLASH_STAR_SAME_LINE;
            }
            else if (commentType == 85)
            {
                Type = CommentType.ANGLE_STAR;
            }
            else if (commentType == 109)
            {
                Type = CommentType.SLASH_PLUS;
            }
            if (Type == CommentType.SLASH_PLUS)
            {
                ms.Seek(-1, SeekOrigin.Current);
                PureStringElement commentText = new PureStringElement();
                commentText.Parse(ms, state);
                this.Value = "/+ " + commentText.Value + " +/";
            } else
            {
                byte[] bShort = new byte[2];
                bShort[0] = (byte)ms.ReadByte();
                bShort[1] = (byte)ms.ReadByte();

                int commentLength = BitConverter.ToInt16(bShort, 0);

                byte[] commentData = new byte[commentLength];
                ms.Read(commentData, 0, commentLength);

                string s = Encoding.Unicode.GetString(commentData);
                s = s.Replace("\r\n", "\n"); // CRLF to LF
                s = s.Replace("\n", "\r\n"); // LF to CRLF
                this.Value = s;
            }
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the Get byte */
            ms.ReadByte();

            /* eat the space */
            ms.ReadByte();

            PropertyName = Element.GetNextElement(ms, state, 0).Value;

            while (Peek(ms) != 106)
            {
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel, true);
                Body.Add(nextElement);
            }

            /* eat the end get */
            ms.ReadByte();

            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the Property byte */
            byte variableType = (byte)ms.ReadByte();

            byte nextByte = Peek(ms);
            Element stringElement = new PureStringElement();

            while (nextByte != 10)
            {
                stringElement.Parse(ms, state);
                VariableType += stringElement.Value + " ";
                nextByte = Peek(ms);
            }
            VariableType = VariableType.Trim();

            while (nextByte != 21) /* semicolon */
            {
                if (nextByte == 95)
                {
                    HasGet = true;
                    ms.ReadByte();
                }

                else if (nextByte == 73)
                {
                    HasSet = true;
                    ms.ReadByte();
                }
                else
                {
                    Declaration.Add(GetNextElement(ms, state, 0));
                }

                nextByte = Peek(ms);
            }
            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the catch byte */
            ms.ReadByte();
            Element nextElement = null;
            state.AlternateBreak = 45;

            StringBuilder sb1 = new StringBuilder();

            var exceptionType = Element.GetNextElement(ms, state, -1,false);

            exceptionType.Write(sb1);
            while (Peek(ms) == 87)
            {
                exceptionType = Element.GetNextElement(ms, state, -1, false);
                exceptionType.Write(sb1);
                exceptionType = Element.GetNextElement(ms, state, -1, false);
                exceptionType.Write(sb1);
            }
            var variableName = Element.GetNextElement(ms, state, -1, false);

            StringBuilder sb = new StringBuilder();
            sb.Append(sb1.ToString());
            sb.Append(" ");
            variableName.Write(sb);

            CatchClause = sb.ToString();

            /* eat the newline */
            ms.ReadByte();

            state.AlternateBreak = 103;
            while (Peek(ms) != 103)
            {
                nextElement = Element.GetNextElement(ms, state, IndentLevel, true);
                Body.Add(nextElement);
            }
        }
Beispiel #17
0
        public static PeopleCode.ProgramElement ParsePPC(string filename, string refsFile, ParseOptions opts = null)
        {
            byte[] ppcBytes = File.ReadAllBytes(filename);
            string refsText = File.ReadAllText(refsFile);

            JArray refs = JArray.Parse(refsText);
            ParseState state = new ParseState();
            if (opts != null)
            {
                state.Options = opts;
            } else
            {
                state.Options = new ParseOptions();
            }
            state.References = refs;

            MemoryStream ms = new MemoryStream(ppcBytes);

            ProgramElement p = new ProgramElement();
            p.Parse(ms, state);

            return p;
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the method byte */
            ms.ReadByte();

            /* eat the 65 byte ? */
            ms.ReadByte();

            Element stringElement = new PureStringElement();
            stringElement.Parse(ms, state);
            MethodName = stringElement.Value;

            if (MethodName == "createObject")
            {
                //Debugger.Break();
            }

            /* eat new line */
            Element.GetNextElement(ms, state, IndentLevel, false);

            while (Peek(ms) != 100)
            {
                state.AlternateBreak = 100;
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel,true);
                Body.Add(nextElement);
                if (Peek(ms) == 21)
                {
                    ms.ReadByte();
                }
            }

            /* eat the end method */
            ms.ReadByte();

            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat reference marker */
            referenceType = (byte) ms.ReadByte();
            byte[] bShort = new byte[2];
            bShort[0] = (byte)ms.ReadByte();
            bShort[1] = (byte)ms.ReadByte();

            int refNum = BitConverter.ToInt16(bShort, 0);
            JObject reference = (JObject)state.References[refNum];
            RecordName = reference["RECNAME"].ToString();
            ReferenceName = reference["REFNAME"].ToString();
            PackageRoot = reference["PACKAGEROOT"].ToString();
            QualifyPath = reference["QUALIFYPATH"].ToString();

            foreach (String keyword in refKeywords)
            {
                if (RecordName.Equals(keyword.ToUpper()))
                {
                    RecordName = keyword;
                    break;
                }
            }
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the "Evaluate" byte */
            ms.ReadByte();

            StringBuilder sb = new StringBuilder();
            while (Peek(ms) != 61)
            {
                Element.GetNextElement(ms, state, -1).Write(sb);
                Variable += sb.ToString();
                sb.Length = 0;
            }

            byte nextByte = Peek(ms);

            while (nextByte == 61 || nextByte == 62)
            {
                WhenElement nextElement = new WhenElement();
                nextElement.IndentLevel = this.IndentLevel;
                nextElement.Parse(ms, state);

                Cases.Add(nextElement);
                /* extra spaces after "Break;" and next "When" */
                while (Peek(ms) == 79)
                {
                    ms.ReadByte();
                }
                nextByte = Peek(ms);
            }

            /* eat the end evaluate */
            ms.ReadByte();

            /* eat the semicolon */
            ms.ReadByte();
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* read the byte */
            byte type = (byte) ms.ReadByte();
            switch(type)
            {
                case 24:
                    Type = BooleanType.AND;
                    break;
                case 29:
                    Type = BooleanType.NOT;
                    break;
                case 30:
                    Type = BooleanType.OR;
                    break;
                case 47:
                    Type = BooleanType.TRUE;
                    break;
                case 48:
                    Type = BooleanType.FALSE;
                    break;

            }
        }
 public override void Parse(MemoryStream ms, ParseState state)
 {
     /* eat the new line byte */
     ms.ReadByte();
 }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            byte b = (byte)ms.ReadByte();

            switch (b)
            {
                case 3:
                    Value = ",";
                    Spacing = ElementSpacing.AFTER;
                    break;
                case 4:
                    Value = "/";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 5:
                    Value = ".";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 6:
                    Value = "=";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 8:
                    Value = ">=";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 9:
                    Value = ">";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 11:
                    Value = "(";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 12:
                    Value = "<=";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 13:
                    Value = "<";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 14:
                    Value = "-";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 15:
                    Value = "*";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 16:
                    Value = "<>";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 19:
                    Value = "+";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 20:
                    Value = ")";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 21:
                    Value = ";";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 35:
                    Value = "|";
                    Spacing = ElementSpacing.BOTH;
                    break;
                case 46:
                    Value = "Break";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 67:
                    Value = "Exit";
                    Spacing = ElementSpacing.AFTER;
                    break;
                case 75:
                    Value = "Null";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 76:
                    Value = "[";
                    Spacing = ElementSpacing.BEFORE;
                    break;
                case 77:
                    Value = "]";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 87:
                    Value = ":";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 89:
                    Value = "*";
                    Spacing = ElementSpacing.NONE;
                    break;
                case 105:
                    Value = "create";
                    Spacing = ElementSpacing.AFTER;
                    break;

            }
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            Element stringElement = new PureStringElement();
            /* eat the class byte */
            ms.ReadByte();
            state.InClassDefn = true;
            stringElement.Parse(ms, state);
            ClassName = stringElement.Value;

            byte nextByte = Peek(ms);

            if (nextByte == 114) /* implements */
            {
                /* eat implements byte */
                ms.ReadByte();
                do
                {
                    if (Peek(ms) == 87)
                    {
                        Implements += Element.GetNextElement(ms, state, 0).Value;
                    }
                    Implements += Element.GetNextElement(ms, state, 0).Value;
                } while (Peek(ms) == 87);
            }

            while (nextByte != 91 && nextByte != 97 && nextByte != 115)
            {
                Element nextElement = Element.GetNextElement(ms, state,IndentLevel);
                Public.Add(nextElement);
                nextByte = Peek(ms);
            }
            if (nextByte == 115) /* has protected section */
            {
                /* eat protected byte */
                ms.ReadByte();
                while (nextByte != 91 && nextByte != 97)
                {
                    Element nextElement = Element.GetNextElement(ms, state, IndentLevel);
                    Protected.Add(nextElement);
                    nextByte = Peek(ms);
                }
            }
            if (nextByte == 97) /* has private section */
            {
                /* eat private byte */
                ms.ReadByte();
                while (nextByte != 91)
                {
                    Element nextElement = Element.GetNextElement(ms, state, IndentLevel);
                    Private.Add(nextElement);
                    nextByte = Peek(ms);
                }
            }
            /* eat the end-class */
            ms.ReadByte();

            state.InClassDefn = false;

            /* eat the semicolon */
            ms.ReadByte();

            /* class defn has been parsed, now for the body */

            while (Peek(ms) != 7)
            {
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel - 1);
                Body.Add(nextElement);
            }

            nextByte = Peek(ms);

            /* handle parse options */
            if (state.Options.AlphabetizeMethodDeclarations)
            {
                List<MethodDeclarationElement> methods = new List<MethodDeclarationElement>();
                List<int> indexes = new List<int>();
                /* process Public */
                for (var x = 0; x < Public.Count; x++)
                {
                    if (Public[x] is MethodDeclarationElement)
                    {
                        methods.Add((MethodDeclarationElement)Public[x]);
                        indexes.Add(x);
                    }
                }
                methods = methods.OrderBy(p => p.MethodName).ToList<MethodDeclarationElement>();
                for (var x = 0; x < indexes.Count; x++)
                {
                    Public[indexes[x]] = methods[x];
                }
                methods.Clear();
                indexes.Clear();

                /* process Protected */
                for (var x = 0; x < Protected.Count; x++)
                {
                    if (Protected[x] is MethodDeclarationElement)
                    {
                        methods.Add((MethodDeclarationElement)Protected[x]);
                        indexes.Add(x);
                    }
                }
                methods = methods.OrderBy(p => p.MethodName).ToList<MethodDeclarationElement>();
                for (var x = 0; x < indexes.Count; x++)
                {
                    Protected[indexes[x]] = methods[x];
                }
                methods.Clear();
                indexes.Clear();

                /* process Private */
                for (var x = 0; x < Private.Count; x++)
                {
                    if (Private[x] is MethodDeclarationElement)
                    {
                        methods.Add((MethodDeclarationElement)Private[x]);
                        indexes.Add(x);
                    }
                }
                methods = methods.OrderBy(p => p.MethodName).ToList<MethodDeclarationElement>();
                for (var x = 0; x < indexes.Count; x++)
                {
                    Private[indexes[x]] = methods[x];
                }
                methods.Clear();
                indexes.Clear();
            }

            if (state.Options.MatchMethodDeclarationOrder)
            {
                /* get ordered list of all declares */
                List<MethodDeclarationElement> declares = new List<MethodDeclarationElement>();
                foreach(var e in Public.Concat(Protected).Concat(Private).ToList())
                {
                    if (e is MethodDeclarationElement)
                    {
                        declares.Add((MethodDeclarationElement)e);
                    }
                }

                /* search body for all methods */
                List<MethodElement> methods = new List<MethodElement>();
                List<int> methodIndexes = new List<int>();

                for (var x = 0; x < Body.Count; x++)
                {
                    if (Body[x] is MethodElement)
                    {
                        methods.Add((MethodElement)Body[x]);
                        methodIndexes.Add(x);
                    }
                }

                for (var x = 0; x < declares.Count; x++)
                {
                    MethodElement currMethod = methods.Where(m => m.MethodName == declares[x].MethodName).First();
                    Body[methodIndexes[x]] = currMethod;
                }
            }

            if (state.Options.PairGetSets)
            {
                List<GetterElement> getters = new List<GetterElement>();
                List<SetterElement> setters = new List<SetterElement>();
                List<int> indexes = new List<int>();

                for(var x = 0; x < Body.Count; x++)
                {
                    if (Body[x] is GetterElement)
                    {
                        getters.Add((GetterElement)Body[x]);
                        indexes.Add(x);
                    }
                    if (Body[x] is SetterElement)
                    {
                        setters.Add((SetterElement)Body[x]);
                        indexes.Add(x);
                    }
                }

                List<Tuple<GetterElement, SetterElement>> pairs = new List<Tuple<GetterElement, SetterElement>>();

                foreach (var g in getters)
                {
                    var s = setters.Where(p => p.PropertyName == g.PropertyName).FirstOrDefault();
                    if (s != null) {
                        pairs.Add(new Tuple<GetterElement, SetterElement>(g, s));
                    }
                }

                /* remove paired items from other lists */
                foreach (var t in pairs)
                {
                    getters.Remove(t.Item1);
                    setters.Remove(t.Item2);
                }
                var y = 0;
                for (var x = 0; x < pairs.Count; x+=2)
                {
                    Body[indexes[x]] = pairs[y].Item1;
                    Body[indexes[x + 1]] = pairs[y++].Item2;
                }

                /* add leftover getters */
                for (var x = pairs.Count; x < getters.Count; x++)
                {
                    Body[indexes[x]] = getters[x];
                }

                /* add leftover setters */
                for (var x = pairs.Count + getters.Count; x < setters.Count; x++)
                {
                    Body[indexes[x]] = setters[x];
                }
            }
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the If byte */
            ms.ReadByte();

            byte nextByte = Peek(ms);
            while (nextByte != 31) /* then */
            {
                Condition.Add(Element.GetNextElement(ms, state, IndentLevel));
                nextByte = Peek(ms);
            }

            if (nextByte == 66)
            {
                /* skip this byte */
                ms.ReadByte();
            }

            /* eat the "then" */
            ms.ReadByte();

            if (Peek(ms) == 21) /* semicolon */
            {
                ms.ReadByte();
            }

            nextByte = Peek(ms);

            while (nextByte != 25 && nextByte != 26) /* not an "else" or an "end-if" */
            {
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel,true);
                Body.Add(nextElement);
                nextByte = Peek(ms);
                if (Peek(ms) == 21)
                {
                    ms.ReadByte();
                }
            }

            if (nextByte == 25)
            {
                ElseBody = new List<Element>();
                /* eat the else */
                var elseMarker = ms.ReadByte();
                while (nextByte != 26 ) /* not an "else" or an "end-if" */
                {
                    Element nextElement = Element.GetNextElement(ms, state, IndentLevel,true);
                    ElseBody.Add(nextElement);
                    nextByte = Peek(ms);
                    if (Peek(ms) == 21)
                    {
                        ms.ReadByte();
                    }
                }

            }

            /* eat the end-if */
            ms.ReadByte();

            /* eat the semicolon */
            ms.ReadByte();
        }
        public static Element GetNextElement(MemoryStream ms, ParseState state, int indentationLevel, bool collapsePrimitives = false)
        {
            bool isPrimitive = false;
            byte nextByte = Peek(ms);

            /* bytes that we should just skip */
            if (nextByte == 65)
            {
                /* this is for "AND" inside an IF */
                ms.ReadByte();
                nextByte = Peek(ms);
            }
            Element nextElement;

            switch (nextByte)
            {
                case 0:
                case 1:
                case 10:
                case 18:
                    isPrimitive = true;
                    nextElement = new PureStringElement();
                    break;
                case 3:
                case 4:
                case 5:
                case 6:
                case 8:
                case 9:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 19:
                case 20:
                case 21:
                case 35:
                case 46:
                case 67:
                case 75:
                case 76:
                case 77:
                case 87:
                case 105:
                    isPrimitive = true;
                    nextElement = new OperatorElement();
                    break;
                case 22:
                    isPrimitive = true;
                    nextElement = new QuotedStringElement();
                    break;
                case 24:
                case 29:
                case 30:
                case 47:
                case 48:
                    isPrimitive = true;
                    nextElement = new BooleanLogicElement();
                    break;
                case 28:
                    nextElement = new IfElement();
                    break;
                case 33:
                case 74:
                    isPrimitive = true;
                    nextElement = new ReferenceElement();
                    break;
                case 36:
                case 78:
                case 85:
                case 109:
                    nextElement = new CommentElement();
                    break;
                case 37:
                    nextElement = new WhileElement();
                    break;
                case 41:
                    nextElement = new ForElement();
                    break;
                case 45:
                case 79:
                    nextElement = new NewLineElement();
                    break;
                case 49:
                    nextElement = new DeclareFunctionElement();
                    break;
                case 50:
                    nextElement = new FunctionElement();
                    break;
                case 56:
                    isPrimitive = true;
                    nextElement = new ReturnElement();
                    break;
                case 60:
                    nextElement = new EvaluateElement();
                    break;
                case 64:
                    nextElement = new PureStringElement();
                    break;
                case 66:
                    nextElement = new PureStringElement();
                    ms.ReadByte();
                    nextElement.Value = "";
                    return nextElement;
                case 68:
                case 69:
                case 84:
                case 86:
                case 98:
                    nextElement = new VariableDeclarationElement();
                    break;
                case 80:
                    isPrimitive = true;
                    nextElement = new NumberElement();
                    break;
                case 88:
                    nextElement = new ImportElement();
                    break;
                case 89:
                    nextElement = new OperatorElement();
                    break;
                case 90:
                    nextElement = new ClassElement();
                    break;
                case 94:
                    nextElement = new PropertyElement();
                    break;
                case 95:
                    nextElement = new GetterElement();
                    break;
                case 73:
                    nextElement = new SetterElement();
                    break;
                case 99:
                    if (state.InClassDefn)
                    {
                        nextElement = new MethodDeclarationElement();
                    } else
                    {
                        nextElement = new MethodElement();
                        //nextElement = new MethodElement();
                    }

                    break;
                case 101:
                    nextElement = new TryElement();
                    break;
                case 104:
                    nextElement = new ThrowElement();
                    break;
                default:
                    return null;
            }
            nextElement.IndentLevel = indentationLevel + 1;
            nextElement.Parse(ms, state);

            if (isPrimitive && collapsePrimitives && Peek(ms) != state.AlternateBreak) {
                StringBuilder sb = new StringBuilder();
                nextElement.Write(sb);
                /* we found a "primitive" (non-control struct), lets collapse until we see a semicolon */
                var tempElement = GetNextElement(ms, state, indentationLevel, false);
                while (tempElement != null && (tempElement.Value == null || tempElement.Value.Equals(";") == false || tempElement is NewLineElement))
                {
                    tempElement.Write(sb);
                    if (Peek(ms) == state.AlternateBreak)
                    {
                        break;
                    }
                    tempElement = GetNextElement(ms, state, -1, false);

                }
                string finalLine = sb.ToString().Trim() + ";\r\n";

                nextElement = new PureStringElement();
                nextElement.IndentLevel = indentationLevel + 1;
                nextElement.Value = finalLine;
            }
            return nextElement;
        }
 public abstract void Parse(MemoryStream ms, ParseState state);
 public override void Parse(MemoryStream ms, ParseState state)
 {
     Element stringElement = new PureStringElement();
     stringElement.Parse(ms, state);
     Value = "\"" + stringElement.Value.Replace("\"","\"\"") + "\"";
 }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            var whenType = ms.ReadByte();

            if (whenType == 61)
            {
                /* will have a criteria */
                Condition = new List<Element>();
                byte nextByte = Peek(ms);
                while (nextByte != 45) /* new line */
                {
                    state.AlternateBreak = 45;
                    Condition.Add(Element.GetNextElement(ms, state, 0,true));
                    nextByte = Peek(ms);
                }

                /* eat the newline */
                ms.ReadByte();

                nextByte = Peek(ms);

                while (nextByte != 46)
                {
                    state.AlternateBreak = 46;
                    Element nextElement = Element.GetNextElement(ms, state, IndentLevel,true);
                    Body.Add(nextElement);
                    nextByte = Peek(ms);
                    if (Peek(ms) == 21)
                    {
                        ms.ReadByte();
                    }
                }

                /* eat the break */
                ms.ReadByte();

                /* eat the semicolon */
                ms.ReadByte();
            } else if (whenType == 62)
            {
                WhenOther = true;

                /* eat the when-other */

                while (Peek(ms) != 46 && Peek(ms) != 63)
                {
                    state.AlternateBreak = 46;
                    Element nextElement = Element.GetNextElement(ms, state, IndentLevel, true);
                    Body.Add(nextElement);
                }
                if (Peek(ms) == 46)
                {
                    /* eat the break */
                    ms.ReadByte();

                    /* eat the semicolon */
                    ms.ReadByte();
                }

            }
        }
        public override void Parse(MemoryStream ms, ParseState state)
        {
            /* eat the "For" byte */
            ms.ReadByte();

            byte nextByte = Peek(ms);

            while (nextByte != 42) /* "To" marker */
            {
                Predicate.Add(Element.GetNextElement(ms, state, IndentLevel));
                nextByte = Peek(ms);
            }

            /* eat the "To" */
            ms.ReadByte();

            while (nextByte != 43 && nextByte != 45) /* "Step" or newline marker */
            {
                To.Add(Element.GetNextElement(ms, state, IndentLevel));
                nextByte = Peek(ms);
            }

            if (Peek(ms) == 21) /*semicolon */
            {
                ms.ReadByte();
            }

            if (nextByte == 43)
            {
                // there's a "Step";
                ms.ReadByte();
                while (nextByte != 45)
                {
                    Step.Add(Element.GetNextElement(ms, state, IndentLevel));
                    nextByte = Peek(ms);
                }
            }
            if (Peek(ms) == 21) /*semicolon */
            {
                ms.ReadByte();
            }
            /* eat the newline */
            nextByte = (byte)ms.ReadByte();
            nextByte = Peek(ms);

            if (nextByte == 21) /*semicolon */
            {
                nextByte = (byte)ms.ReadByte();
                nextByte = Peek(ms);
            }

            state.AlternateBreak = 44;
            while (nextByte != 44) /* end-for */
            {
                Element nextElement = Element.GetNextElement(ms, state, IndentLevel, true);
                Body.Add(nextElement);
                nextByte = Peek(ms);
            }

            /* eat the end-for */
            ms.ReadByte();
            /* eat the semicolon */
            ms.ReadByte();
        }