Exemplo n.º 1
0
 public static string AddTokenText(string prefix, string text, Token tok)
 {
     if (tok.IsBinary)
     {
         return string.Format("{0} ({1}, offset 0x{2}) {3}", prefix, TokenTypeString(tok.Type), tok.Offset.ToString("X"), text);
     }
     return string.Format("{0} ({1}, line {2}, col {3} ) {4}", prefix, TokenTypeString(tok.Type), tok.Line, tok.Column, text);
 }
Exemplo n.º 2
0
        public Element(Token keyToken, Parser parser)
        {
            this.keyToken = keyToken;

            Token n;
            do
            {
                n = parser.AdvanceToNextToken();
                if (n == null)
                {
                    throw (new Exception("unexpected end of file, expected closing bracket"));
                }
                if (n.Type == TokenType.Data)
                {
                    tokens.Add(n);
                    n = parser.AdvanceToNextToken();
                    if (n == null)
                    {
                        throw (new Exception("unexpected end of file, expected bracket, comma or key"));
                    }
                    TokenType ty = n.Type;
                    if (ty != TokenType.OpenBracket && ty != TokenType.CloseBracket && ty != TokenType.Comma && ty != TokenType.Key)
                    {
                        throw (new Exception("unexpected token; expected bracket, comma or key"));
                    }
                }
                if (n.Type == TokenType.OpenBracket)
                {
                    compound = new Scope(parser);

                    // current token should be a TOK_CLOSE_BRACKET
                    n = parser.CurrentToken;
                    Debug.Assert(n != null);

                    if (n.Type != TokenType.CloseBracket)
                    {
                        throw (new Exception("expected closing bracket"));
                    }
                    parser.AdvanceToNextToken();
                    return;
                }
            }
            while (n.Type != TokenType.Key && n.Type != TokenType.CloseBracket);
        }
Exemplo n.º 3
0
 /// <summary>
 /// wrapper around ParseTokenAsString() with ParseError handling
 /// </summary>
 public static string ParseTokenAsString(Token t)
 {
     string err;
     string i = ParseTokenAsString(t, out err);
     if (!string.IsNullOrEmpty(err))
     {
         throw (new ParseException(err, t));
     }
     return i;
 }
Exemplo n.º 4
0
        public static string ParseTokenAsString(Token t, out string errOut)
        {
            errOut = null;
            if (t.Type != TokenType.Data)
            {
                errOut = "expected TOK_DATA token";
                return "";
            }
            if (t.IsBinary)
            {
                var data = t.BinaryContents;
                if (data[0] != 'S')
                {
                    errOut = "failed to parse S(tring), unexpected data type (binary)";
                    return "";
                }

                // read string length
                int len = BitConverter.ToInt32(data, 1);

                Debug.Assert(t.End - t.Begin == 5 + len);
                return Encoding.Default.GetString(data, 5, len);
            }

            var length = t.End - t.Begin;
            if (length < 2)
            {
                errOut = "token is too short to hold a string";
                return "";
            }

            var s = t.BinaryContents;
            if (s[0] != '\"' || s[s.Length-1] != '\"')
            {
                errOut = "expected double quoted string";
                return "";
            }
            return Encoding.Default.GetString(s, 1, length - 2);
        }
Exemplo n.º 5
0
        public static long ParseTokenAsInt64(Token t, out string errOut)
        {
            errOut = null;
            if (t.Type != TokenType.Data)
            {
                errOut = "expected TOK_DATA token";
                return 0;
            }
            if (t.IsBinary)
            {
                byte[] data = t.BinaryContents;
                if (data[0] != 'L')
                {
                    errOut = "failed to parse Int64, unexpected data type";
                    return 0;
                }
                long id = BitConverter.ToInt64(data, 1);
                return id;
            }

            Debug.Assert(t.End - t.Begin > 0);

            return long.Parse(t.StringContents);
        }
Exemplo n.º 6
0
 public static int ParseTokenAsInt(Token t)
 {
     string err;
     int i = ParseTokenAsInt(t, out err);
     if (!string.IsNullOrEmpty(err))
     {
         throw (new ParseException(err, t));
     }
     return i;
 }
Exemplo n.º 7
0
        public static int ParseTokenAsInt(Token t, out string errOut)
        {
            errOut = null;
            if (t.Type != TokenType.Data)
            {
                errOut = "expected TOK_DATA token";
                return 0;
            }
            if (t.IsBinary)
            {
                byte[] data = t.BinaryContents;
                if (data[0] != 'I')
                {
                    errOut = "failed to parse I(nt), unexpected data type (binary)";
                    return 0;
                }
                int ival = BitConverter.ToInt32(data, 1);
                return ival;
            }

            Debug.Assert(t.End - t.Begin > 0);

            return int.Parse(t.StringContents);
        }
Exemplo n.º 8
0
 public static UInt64 ParseTokenAsID(Token t)
 {
     string err;
     ulong i = ParseTokenAsID(t, out err);
     if (!string.IsNullOrEmpty(err))
     {
         throw (new ParseException(err, t));
     }
     return i;
 }
Exemplo n.º 9
0
 public static ulong ParseTokenAsID(Token t, out string errOut)
 {
     errOut = null;
     if (t.Type != TokenType.Data)
     {
         errOut = "expected TOK_DATA token";
         return 0;
     }
     if (t.IsBinary)
     {
         var data = t.BinaryContents;
         if (data[0] != 'L')
         {
             errOut = "failed to parse ID, unexpected data type, expected L(ong) (binary)";
             return 0L;
         }
         ulong id = BitConverter.ToUInt64(data, 1);
         return id;
     }
     int length = t.End - t.Begin;
     Debug.Assert(length > 0);
     ulong result;
     if (ulong.TryParse(t.StringContents, out result))
     {
         return result;
     }
     else
     {
         errOut = "failed to parse ID (text)";
         return 0;
     }
 }
Exemplo n.º 10
0
        public static float ParseTokenAsFloat(Token t, out string errOut)
        {
            errOut = "";
            if (t.Type != TokenType.Data)
            {
                errOut = "expected TOK_DATA token";
                return 0.0f;
            }

            if (t.IsBinary)
            {
                var data = t.BinaryContents;
                if (data[0] != 'F' && data[0] != 'D')
                {
                    errOut = "failed to parse F(loat) or D(ouble), unexpected data type (binary)";
                    return 0.0f;
                }

                if (data[0] == 'F')
                {
                    return BitConverter.ToSingle(data, 1);
                }
                else
                {
                    return (float)BitConverter.ToDouble(data, 1);
                }
            }
            return float.Parse(t.StringContents);
        }
Exemplo n.º 11
0
 public static ulong ParseTokenAsDim(Token t, out string errOut)
 {
     errOut = null;
     if (t.Type != TokenType.Data)
     {
         errOut = "expected TOK_DATA token";
         return 0;
     }
     if (t.IsBinary)
     {
         var data = t.BinaryContents;
         if (data[0] != 'L')
         {
             errOut = "failed to parse ID, unexpected data type, expected L(ong) (binary)";
             return 0L;
         }
         ulong id = BitConverter.ToUInt64(data, 1);
         return id;
     }
     if (t.StringContents[0] != '*')
     {
         errOut = "expected asterisk before array dimension";
         return 0;
     }
     uint length = (uint)t.End - (uint)t.Begin;
     if (length == 0)
     {
         errOut = "expected valid integer number after asterisk";
         return 0;
     }
     return ulong.Parse(t.StringContents.Substring(1));
 }
Exemplo n.º 12
0
 public ParseException(string message, Token token)
     : base(Util.AddTokenText("FBX-Parsre",message,token))
 {
 }
Exemplo n.º 13
0
 public DomException(string message, Token token)
     : base(message)
 {
     throw (new Exception(Util.AddTokenText("FBX-DOM", message, token)));
 }