Пример #1
0
        public static List<smcMesh> ReadFile(String FileName, out String SmcName)
        {
            List<smcMesh> ret = new List<smcMesh>();

            String lines = File.ReadAllText(FileName);
            SmcName = String.Empty;

            var defs = new TokenDefinition[]
            {
                // Thanks to [steven levithan][2] for this great quoted string
                // regex
                new TokenDefinition(@"([""'])(?:\\\1|.)*?\1", "QUOTED-STRING"),
                // Thanks to http://www.regular-expressions.info/floatingpoint.html
                new TokenDefinition(@"[-+]?\d*\.\d+([eE][-+]?\d+)?", "FLOAT"),
                new TokenDefinition(@"[-+]?\d+", "INT"),
                new TokenDefinition(@"#t", "TRUE"),
                new TokenDefinition(@"#f", "FALSE"),
                new TokenDefinition(@"[*<>\?\-+/A-Za-z->!]+", "SYMBOL"),
                new TokenDefinition(@"\.", "DOT"),
                new TokenDefinition(@"\(", "LEFT"),
                new TokenDefinition(@"\)", "RIGHT"),
                new TokenDefinition(@"\s", "SPACE"),
                new TokenDefinition(@",", "TICKMARK"),
                new TokenDefinition(@";", "SEMICOLON"),
                new TokenDefinition(@"{", "LEFT_BRACK"),
                new TokenDefinition(@"}", "RIGHT_BRACK")
            };

            TextReader r = new StringReader(lines);

            Lexer l = new Lexer(r, defs);

            String LastSymbol = "";
            int texIndex = 0;

            smcMesh mesh = new smcMesh();

            while (l.Next())
            {
                if ((String)l.Token == "SYMBOL" && l.TokenContents != "TFNM")
                    LastSymbol = l.TokenContents;

                if (LastSymbol == "NAME" && l.TokenContents != "TFNM")
                {
                    if ((String)l.Token == "QUOTED-STRING")
                    {
                        SmcName = l.TokenContents.Replace("\"", "");
                    }
                }

                if (LastSymbol == "MESH" && l.TokenContents != "TFNM")
                {
                    if ((String)l.Token == "QUOTED-STRING" && l.TokenContents != "}" && l.TokenContents != "{")
                    {
                        if (mesh.Object != null)
                            ret.Add(mesh);

                        mesh = new smcMesh(l.TokenContents.Replace("\"", ""));
                    }
                }

                if (LastSymbol == "TEXTURES" && l.TokenContents != "TFNM")
                {
                    if ((String)l.Token == "QUOTED-STRING" && l.TokenContents != "}" && l.TokenContents != "{")
                    {
                        if (texIndex == 0)
                        {
                            mesh.Object.Add(new smcObject());
                            mesh.Object.Last().Name = l.TokenContents.Replace("\"", "");
                            texIndex++;
                        }
                        else
                        {
                            mesh.Object.Last().Texture = l.TokenContents.Replace("\"", "");
                            texIndex = 0;
                        }
                    }
                }
            }

            if (mesh.Object != null)
                ret.Add(mesh);

            return ret;
        }
Пример #2
0
 public Lexer(TextReader reader, TokenDefinition[] tokenDefinitions)
 {
     this.reader = reader;
     this.tokenDefinitions = tokenDefinitions;
     nextLine();
 }