Esempio n. 1
0
 private void ExecuteBegin()
 {
     Codigo.Add(".method static public void _principal() {");
     Codigo.Add("    .entrypoint");
     AddCode();
     Idents.Push(Ident);
 }
Esempio n. 2
0
 private void ExecuteEnd()
 {
     AddCode();
     AddCode("ret");
     AddCode("}");
     Idents.Pop();
     AddCode("}");
 }
Esempio n. 3
0
        /// <summary>
        /// Adds data from the specified <c>ident</c> or <c>rdg</c> element.
        /// The <see cref="Idents"/> list contains <c>ident</c>'s values with
        /// their <c>n</c> attribute, eventually followed by the value of the
        /// <c>n</c> attribute of the parent <c>rdg</c> element.
        /// This in turn comes from merging an overlapping <c>app</c> element
        /// into another one, and preserves the original from-to location of
        /// the overlapping <c>app</c>.
        /// </summary>
        /// <param name="identElem">The ident element.</param>
        /// <exception cref="ArgumentNullException">identElem</exception>
        public void AddIdent(XElement identElem)
        {
            if (identElem == null)
            {
                throw new ArgumentNullException(nameof(identElem));
            }

            if (identElem.Name.LocalName != "ident" &&
                identElem.Name.LocalName != "rdg")
            {
                return;
            }

            string identValue = identElem.Name.LocalName == "rdg"
                ? $"@{identElem.Attribute("n")?.Value}"
                : $"{identElem.Value.Trim()}#{identElem.Attribute("n")!.Value}";

            if (string.IsNullOrEmpty(identValue) || identValue == "@")
            {
                return;
            }

            string?rdgLoc = Idents.FirstOrDefault(
                i => i.StartsWith("@", StringComparison.Ordinal));

            if (rdgLoc != null)
            {
                if (identValue.StartsWith("@", StringComparison.Ordinal))
                {
                    // this should never happen
                    Logger?.LogError("Duplicate rdg@n attribute in idents list");
                    Idents.Add(identValue);
                }
                else
                {
                    Idents.Insert(Idents.IndexOf(rdgLoc), identValue);
                }
            }
            else
            {
                Idents.Add(identValue);
            }
        }
Esempio n. 4
0
        public QueryLexerToken NextToken(QueryLexerTokenKind tokContext = QueryLexerTokenKind.UNKNOWN)
        {
            if (TokenCache.Kind != QueryLexerTokenKind.UNKNOWN)
            {
                QueryLexerToken tok = TokenCache;
                TokenCache = new QueryLexerToken();
                return(tok);
            }

            if (ArgI == ArgEnd)
            {
                if (!BeginEnumerator.MoveNext())
                {
                    return(new QueryLexerToken(QueryLexerTokenKind.END_REACHED));
                }
                else
                {
                    SetArg();
                }
            }

resume:

            switch (Arg[ArgI])
            {
            case '\0':
                throw new InvalidOperationException();

            case '\'':
            case '"':
            case '/':
            {
                string pat          = String.Empty;
                char   closing      = Arg[ArgI];
                bool   foundClosing = false;
                for (++ArgI; ArgI != ArgEnd; ++ArgI)
                {
                    if (Arg[ArgI] == '\\')
                    {
                        if (++ArgI == ArgEnd)
                        {
                            throw new ParseError(ParseError.ParseError_UnexpectedBackslashAtEndOfPattern);
                        }
                    }
                    else if (Arg[ArgI] == closing)
                    {
                        ++ArgI;
                        foundClosing = true;
                        break;
                    }
                    pat += Arg[ArgI];
                }
                if (!foundClosing)
                {
                    throw new ParseError(String.Format(ParseError.ParseError_ExpectedSmthAtEndOfPattern, closing));
                }
                if (String.IsNullOrEmpty(pat))
                {
                    throw new ParseError(ParseError.ParseError_MatchPatternIsEmpty);
                }

                return(new QueryLexerToken(QueryLexerTokenKind.TERM, pat));
            }
            }

            if (MultipleArgs && ConsumeNextArg)
            {
                ConsumeNextArg = false;
                QueryLexerToken tok = new QueryLexerToken(QueryLexerTokenKind.TERM, Arg.Substring(ArgI));
                PrevArgI = ArgI;
                ArgI     = ArgEnd;
                return(tok);
            }

            bool consumeNext = false;

            switch (Arg[ArgI])
            {
            case '\0':
                throw new InvalidOperationException();

            case ' ':
            case '\t':
            case '\r':
            case '\n':
                if (++ArgI == ArgEnd)
                {
                    return(NextToken(tokContext));
                }
                goto resume;

            case '(':
                ++ArgI;
                if (tokContext == QueryLexerTokenKind.TOK_EXPR)
                {
                    ConsumeWhitespace = true;
                }
                return(new QueryLexerToken(QueryLexerTokenKind.LPAREN));

            case ')':
                ++ArgI;
                if (tokContext == QueryLexerTokenKind.TOK_EXPR)
                {
                    ConsumeWhitespace = false;
                }
                return(new QueryLexerToken(QueryLexerTokenKind.RPAREN));

            case '&': ++ArgI; return(new QueryLexerToken(QueryLexerTokenKind.TOK_AND));

            case '|': ++ArgI; return(new QueryLexerToken(QueryLexerTokenKind.TOK_OR));

            case '!': ++ArgI; return(new QueryLexerToken(QueryLexerTokenKind.TOK_NOT));

            case '@': ++ArgI; return(new QueryLexerToken(QueryLexerTokenKind.TOK_PAYEE));

            case '#': ++ArgI; return(new QueryLexerToken(QueryLexerTokenKind.TOK_CODE));

            case '%': ++ArgI; return(new QueryLexerToken(QueryLexerTokenKind.TOK_META));

            case '=':
                ++ArgI;
                consumeNext = true;
                return(new QueryLexerToken(QueryLexerTokenKind.TOK_EQ));

            default:
            {
                if (Arg[ArgI] == '\\')
                {
                    ++ArgI;
                    consumeNext = true;
                }

                string ident = String.Empty;
                for (; ArgI != ArgEnd; ++ArgI)
                {
                    switch (Arg[ArgI])
                    {
                    case '\0':
                        throw new InvalidOperationException();

                    case ' ':
                    case '\t':
                    case '\n':
                    case '\r':
                        if (!MultipleArgs && !ConsumeWhitespace && !ConsumeNextArg)
                        {
                            goto test_ident;
                        }
                        else
                        {
                            ident += Arg[ArgI];
                        }
                        break;

                    case ')':
                        if (!consumeNext)              // DM - the second part of condition "... && tok_context == token_t::TOK_EXPR)" is redundant since it meets an opposite condition after falling through. See query.cc:166
                        {
                            goto test_ident;
                        }
                        ident += Arg[ArgI];
                        break;

                    case '(':
                    case '&':
                    case '|':
                    case '!':
                    case '@':
                    case '#':
                    case '%':
                    case '=':
                        if (!consumeNext && tokContext != QueryLexerTokenKind.TOK_EXPR)
                        {
                            goto test_ident;
                        }
                        ident += Arg[ArgI];
                        break;

                    default:
                        ident += Arg[ArgI];
                        break;
                    }
                }
                ConsumeWhitespace = false;

test_ident:

                QueryLexerTokenKind kind;
                if (Idents.TryGetValue(ident, out kind))
                {
                    if (kind == QueryLexerTokenKind.TOK_EXPR)
                    {
                        ConsumeNextArg = true;
                    }

                    return(new QueryLexerToken(kind));
                }
                return(new QueryLexerToken(QueryLexerTokenKind.TERM, ident));
            }
            }

            // Unreachable code... return new QueryLexerToken(QueryLexerTokenKind.UNKNOWN);
        }
Esempio n. 5
0
        private void ParseRecursively(List <mValue> ValueList, int chunkOffset, int chunkNumber)
        {
            for (int i = 0; i < ValueList.Count; i++)
            {
                object name;
                switch (ValueList[i].Attributes)
                {
                case mValue.ObjectAttributes.TagBlock:
                {
                    if ((chunkOffset + ValueList[i].Offset) <= ((int)Map.IO.In.BaseStream.Length))
                    {
                        break;
                    }
                    continue;
                }

                case mValue.ObjectAttributes.TagData:
                {
                    if ((chunkOffset + ValueList[i].Offset) <= ((int)Map.IO.In.BaseStream.Length))
                    {
                        goto Label_0224;
                    }
                    continue;
                }

                case mValue.ObjectAttributes.TagReference:
                {
                    if ((chunkOffset + ValueList[i].Offset) <= ((int)Map.IO.In.BaseStream.Length))
                    {
                        goto Label_03B7;
                    }
                    continue;
                }

                case mValue.ObjectAttributes.StringID:
                {
                    if ((chunkOffset + ValueList[i].Offset) <= ((int)Map.IO.In.BaseStream.Length))
                    {
                        goto Label_05E5;
                    }
                    continue;
                }

                default:
                {
                    continue;
                }
                }
                Map.IO.In.BaseStream.Position = chunkOffset + ValueList[i].Offset;
                mTagBlock block = (mTagBlock)ValueList[i];
                Structure item  = new Structure {
                    Name = block.Name
                };
                if (chunkNumber != -1)
                {
                    name      = item.Name;
                    item.Name = string.Concat(new object[] { name, " [", chunkNumber, "]" });
                }
                item.Size    = block.Size;
                item.Count   = Map.IO.In.ReadInt32();
                item.Pointer = Map.IO.In.ReadInt32() - Map.Map_Header.mapMagic;
                if (!(((item.Count <= 0) | (item.Pointer <= 0)) | (item.Count > 0x186a0)))
                {
                    item.Offset = chunkOffset + block.Offset;
                    Structures.Add(item);
                    for (int j = 0; j < item.Count; j++)
                    {
                        ParseRecursively(block.Values, item.Pointer + (j * item.Size), j);
                    }
                }
                continue;
Label_0224:
                Map.IO.In.BaseStream.Position = chunkOffset + ValueList[i].Offset;
                mTagData data  = (mTagData)ValueList[i];
                TagData  data2 = new TagData {
                    Name = data.Name
                };
                if (chunkNumber != -1)
                {
                    name       = data2.Name;
                    data2.Name = string.Concat(new object[] { name, " [", chunkNumber, "]" });
                }
                data2.Size = Map.IO.In.ReadInt32();
                Stream baseStream = Map.IO.In.BaseStream;
                baseStream.Position += 8L;
                data2.Pointer        = Map.IO.In.ReadInt32() - Map.Map_Header.mapMagic;
                if (!((data2.Size <= 0) | (data2.Pointer <= 0)))
                {
                    data2.Offset = chunkOffset + data.Offset;
                    Tag_Data_Blocks.Add(data2);
                }
                continue;
Label_03B7:
                Map.IO.In.BaseStream.Position = (chunkOffset + ValueList[i].Offset) + 12;
                mTagReference reference = (mTagReference)ValueList[i];
                Ident         ident     = new Ident {
                    Name = reference.Name
                };
                if (chunkNumber != -1)
                {
                    name       = ident.Name;
                    ident.Name = string.Concat(new object[] { name, " [", chunkNumber, "]" });
                }
                ident.Offset = chunkOffset + reference.Offset;
                ident.ID     = Map.IO.In.ReadInt32();
                int tagIndexByIdent = Map.GetTagIndexByIdent(ident.ID);
                if (tagIndexByIdent != -1)
                {
                    ident.TagClass = Map.Index_Items[tagIndexByIdent].Class;
                }
                else
                {
                    ident.TagClass = "Null";
                }
                if (tagIndexByIdent != -1)
                {
                    int    key = Map.Index_Items[tagIndexByIdent].Ident;
                    string str = Map.Index_Items[tagIndexByIdent].Name;
                    if (Map.tagNameList.TagPaths.ContainsKey(key))
                    {
                        try
                        {
                            str = Map.tagNameList.TagPaths[key];
                        }
                        catch
                        {
                        }
                    }
                    ident.TagName = str + "." + Map.Index_Items[tagIndexByIdent].Class;
                }
                else
                {
                    ident.TagName = "Null";
                }
                Idents.Add(ident);
                continue;
Label_05E5:
                Map.IO.In.BaseStream.Position = chunkOffset + ValueList[i].Offset;
                mStringID        gid        = (mStringID)ValueList[i];
                StringIdentifier identifier = new StringIdentifier {
                    Name = gid.Name
                };
                if (chunkNumber != -1)
                {
                    name            = identifier.Name;
                    identifier.Name = string.Concat(new object[] { name, " [", chunkNumber, "]" });
                }
                identifier.Offset      = chunkOffset + gid.Offset;
                identifier.Identifier  = Map.IO.In.ReadInt32();
                tagIndexByIdent        = Map.String_Table.GetStringItemIndexByID(Map, identifier.Identifier);
                identifier.StringIndex = tagIndexByIdent;
                try
                {
                    identifier.StringName = Map.String_Table.StringItems[tagIndexByIdent].Name;
                }
                catch
                {
                    identifier.StringName = "<<invalid sid>>";
                }
                Strings.Add(identifier);
            }
        }
Esempio n. 6
0
        private void ParseRecursively(List <mValue> ValueList, int chunkOffset, int chunkNumber)
        {
            //Loop through values
            for (int i = 0; i < ValueList.Count; i++)
            {
                //Determine the value type
                switch (ValueList[i].Attributes)
                {
                case mValue.ObjectAttributes.Reflexive:
                {
                    //If the offset is rediculous..
                    if (chunkOffset + ValueList[i].Offset > (int)Map.IO.In.BaseStream.Length)
                    {
                        break;
                    }
                    //Go to the reflexive offset
                    Map.IO.In.BaseStream.Position = chunkOffset + ValueList[i].Offset;

                    //Get the reflexive
                    mReflexive mreflexive = (mReflexive)ValueList[i];
                    //Initialize our instance of structure
                    Structure structure = new Structure();
                    //Assign the name
                    structure.Name = mreflexive.Name;
                    //Add chunk modifier
                    if (chunkNumber != -1)
                    {
                        structure.Name += " [" + chunkNumber + "]";
                    }
                    //Assign the size
                    structure.Size = mreflexive.Size;
                    //Read the count
                    structure.Count = Map.IO.In.ReadInt32();
                    //Read the pointer
                    structure.Pointer = Map.IO.In.ReadInt32() - Map.MapHeader.mapMagic;

                    //If the count or pointer are bad, break out and dont add it to the list
                    if (structure.Count <= 0 | structure.Pointer <= 0 | structure.Count > 100000)
                    {
                        break;
                    }

                    //Assign the offset
                    structure.Offset = chunkOffset + mreflexive.Offset;
                    //Add it to the list
                    Structures.Add(structure);
                    //Loop through all the chunks
                    for (int z = 0; z < structure.Count; z++)
                    {
                        //Parse recursively
                        ParseRecursively(mreflexive.Values, structure.Pointer + (z * structure.Size), z);
                    }
                    break;
                }

                case mValue.ObjectAttributes.Ident:
                {
                    //If the offset is rediculous..
                    if (chunkOffset + ValueList[i].Offset > (int)Map.IO.In.BaseStream.Length)
                    {
                        break;
                    }

                    //Go to the ident offset
                    Map.IO.In.BaseStream.Position = chunkOffset + ValueList[i].Offset;

                    //Get the ident instance
                    mIdent mident = (mIdent)ValueList[i];
                    //Initialize our instance of the ident
                    Ident ident = new Ident();
                    //Assign the name
                    ident.Name = mident.Name;
                    if (chunkNumber != -1)
                    {
                        ident.Name += " [" + chunkNumber + "]";
                    }
                    //Assign the offset
                    ident.Offset = chunkOffset + mident.Offset - 12;
                    //Assign the Ident
                    ident.ID = Map.IO.In.ReadInt32();
                    //Get the tag index
                    int index = Map.GetTagIndexByIdent(ident.ID);
                    //Assign the class
                    if (index != -1)
                    {
                        ident.TagClass = Map.IndexItems[index].Class;
                    }
                    else
                    {
                        ident.TagClass = "Null";
                    }
                    //Assign the name
                    if (index != -1)
                    {
                        ident.TagName = Map.IndexItems[index].Name;
                    }
                    else
                    {
                        ident.TagName = "Null";
                    }
                    //Add to the ident to the list
                    Idents.Add(ident);
                    break;
                }

                case mValue.ObjectAttributes.StringID:
                {
                    //If the offset is rediculous..
                    if (chunkOffset + ValueList[i].Offset > (int)Map.IO.In.BaseStream.Length)
                    {
                        break;
                    }

                    //Go to the ident offset
                    Map.IO.In.BaseStream.Position = chunkOffset + ValueList[i].Offset;

                    //Get the ident instance
                    mStringID mstringid = (mStringID)ValueList[i];
                    //Initialize our instance of the string id.
                    StringIdentifier str = new StringIdentifier();
                    //Assign the name
                    str.Name = mstringid.Name;
                    if (chunkNumber != -1)
                    {
                        str.Name += " [" + chunkNumber + "]";
                    }
                    //Assign the offset
                    str.Offset = chunkOffset + mstringid.Offset;
                    //Assign the Ident
                    str.Identifier = Map.IO.In.ReadInt32();
                    //Get the string index
                    int index = Map.StringTable.GetStringItemIndexByID(Map, str.Identifier);
                    //Assign the string index
                    str.StringIndex = index;
                    //Try to..
                    try
                    {
                        //Assign the string name
                        str.StringName = Map.StringTable.StringItems[index].Name;
                    }
                    catch
                    {
                        //Assign our invalid name
                        str.StringName = "<<invalid sid>>";
                    }
                    //Add to the string to the list
                    Strings.Add(str);
                    break;
                }
                }
            }
        }