Пример #1
0
        public static string Preprocess(
            string effectCode, string filePath, IDictionary<string, string> defines, List<string> dependencies,
            IEffectCompilerOutput output)
        {
            var fullPath = Path.GetFullPath(filePath);

            var pp = new CppNet.Preprocessor();

            pp.EmitExtraLineInfo = false;
            pp.addFeature(Feature.LINEMARKERS);
            pp.setListener(new MGErrorListener(output));
            pp.setFileSystem(new MGFileSystem(dependencies));
            pp.setQuoteIncludePath(new List<string> { Path.GetDirectoryName(fullPath) });

            foreach (var define in defines)
                pp.addMacro(define.Key, define.Value);

            pp.addInput(new MGStringLexerSource(effectCode, true, fullPath));

            var result = new StringBuilder();

            var endOfStream = false;
            while (!endOfStream)
            {
                var token = pp.token();
                switch (token.getType())
                {
                    case CppNet.Token.EOF:
                        endOfStream = true;
                        break;
                    case CppNet.Token.CPPCOMMENT:
                        break;
                    case CppNet.Token.CCOMMENT:
                    {
                        var tokenText = token.getText();
                        if (tokenText != null)
                        {
                            // Need to preserve line breaks so that line numbers are correct.
                            foreach (var c in tokenText)
                                if (c == '\n')
                                    result.Append(c);
                        }
                        break;
                    }
                    default:
                    {
                        var tokenText = token.getText();
                        if (tokenText != null)
                            result.Append(tokenText);
                        break;
                    }
                }
            }

            return result.ToString();
        }
Пример #2
0
        public static string Preprocess(
            string effectCode, string filePath, IDictionary <string, string> defines, List <string> dependencies,
            IEffectCompilerOutput output)
        {
            var fullPath = Path.GetFullPath(filePath);

            var pp = new CppNet.Preprocessor();

            pp.EmitExtraLineInfo = false;
            pp.addFeature(Feature.LINEMARKERS);
            pp.setListener(new MGErrorListener(fullPath, output));
            pp.setFileSystem(new MGFileSystem(dependencies));
            pp.setQuoteIncludePath(new List <string> {
                Path.GetDirectoryName(fullPath)
            });

            foreach (var define in defines)
            {
                pp.addMacro(define.Key, define.Value);
            }

            pp.addInput(new StringLexerSource(effectCode, true, fullPath));

            var result = new StringBuilder();

            var endOfStream = false;

            while (!endOfStream)
            {
                var token = pp.token();
                switch (token.getType())
                {
                case CppNet.Token.EOF:
                    endOfStream = true;
                    break;

                case CppNet.Token.CCOMMENT:
                case CppNet.Token.CPPCOMMENT:
                    break;

                default:
                    var tokenText = token.getText();
                    if (tokenText != null)
                    {
                        result.Append(tokenText);
                    }
                    break;
                }
            }

            return(result.ToString());
        }
Пример #3
0
        public static string Preprocess(
            string effectCode, string filePath, IDictionary <string, string> defines, List <string> dependencies,
            IEffectCompilerOutput output)
        {
            var fullPath = Path.GetFullPath(filePath);

            var pp = new CppNet.Preprocessor();

            pp.EmitExtraLineInfo = false;
            pp.addFeature(Feature.LINEMARKERS);
            pp.setListener(new MGErrorListener(output));
            pp.setFileSystem(new MGFileSystem(dependencies));
            pp.setQuoteIncludePath(new List <string> {
                Path.GetDirectoryName(fullPath)
            });

            foreach (var define in defines)
            {
                pp.addMacro(define.Key, define.Value);
            }

            effectCode = effectCode.Replace("#line", "//--WORKAROUND#line");

            pp.addInput(new MGStringLexerSource(effectCode, true, fullPath));

            var result = new StringBuilder();

            var endOfStream = false;

            while (!endOfStream)
            {
                var token = pp.token();
                switch (token.getType())
                {
                case CppNet.Token.EOF:
                    endOfStream = true;
                    break;

                case CppNet.Token.CPPCOMMENT:
                    if (token.getText().StartsWith("//--WORKAROUND#line"))
                    {
                        result.Append(token.getText().Replace("//--WORKAROUND#line", "#line"));
                    }
                    break;

                case CppNet.Token.CCOMMENT:
                {
                    var tokenText = token.getText();
                    if (tokenText != null)
                    {
                        // Need to preserve line breaks so that line numbers are correct.
                        foreach (var c in tokenText)
                        {
                            if (c == '\n')
                            {
                                result.Append(c);
                            }
                        }
                    }
                    break;
                }

                default:
                {
                    var tokenText = token.getText();
                    if (tokenText != null)
                    {
                        result.Append(tokenText);
                    }
                    break;
                }
                }
            }

            return(result.ToString());
        }