예제 #1
0
        private void TranslateTempFileContent(TranslationData data, TempFileFull fileData)
        {
            int tokenStart = data.CurrentToken;
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            TempClass contentData = null;
            while (iterator.Next(out token))
            {
                if (token.Term.Type != TermType.IdentifierKey && token.Term.Type != TermType.Key)
                {
                    continue;
                }

                JavaTermKey key = this.GetTermKey(token);
                switch (key)
                {
                    case JavaTermKey.Class:
                    case JavaTermKey.Interface:
                    case JavaTermKey.Enum:
                        {
                            contentData = new TempClass();
                            break;
                        }

                    case JavaTermKey.BraceRight:
                        {
                            return;
                        }
                }

                if (contentData != null)
                {
                    break;
                }
            }

            if (contentData != null)
            {
                data.CurrentToken = tokenStart;
                data.TranslationStack.Push(contentData);
                fileData.AddClass(contentData);
                return;
            }

            throw new InvalidDataException("Unexpected token list: ");
        }
예제 #2
0
        private void TranslateTempFile(TranslationData data, TempFileFull fileData)
        {
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            while (iterator.Next(out token))
            {
                switch (token.Term.Type)
                {
                    case TermType.IdentifierKey:
                        {
                            JavaTermKey key = this.GetTermKey(token);
                            switch (key)
                            {
                                case JavaTermKey.Package:
                                    {
                                        this.TranslateTempFilePackage(data, fileData);
                                        continue;
                                    }

                                case JavaTermKey.Import:
                                    {
                                        this.TranslateTempFileImport(data, fileData);
                                        continue;
                                    }

                                default:
                                    {
                                        // Rewind to current token and read the possible section
                                        data.CurrentToken--;
                                        this.TranslateTempFileContent(data, fileData);
                                        return;
                                    }
                            }
                        }
                }

                throw new InvalidDataException("Unexpected token: " + token);
            }
        }
예제 #3
0
        private void TranslateTempFileImport(TranslationData data, TempFileFull fileData)
        {
            var builder = new StringBuilder();
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            while (iterator.Next(out token))
            {
                if (token.Term.Type == TermType.Key)
                {
                    JavaTermKey key = this.GetTermKey(token);
                    if (key == JavaTermKey.Semicolon)
                    {
                        fileData.AddInclude(builder.ToString());
                        return;
                    }
                }

                builder.Append(token.Contents);
            }
        }
예제 #4
0
        private void TranslateTempFilePackage(TranslationData data, TempFileFull fileData)
        {
            if (!string.IsNullOrEmpty(fileData.Namespace))
            {
                throw new InvalidDataException("Possibly duplicate namespace!");
            }

            var builder = new StringBuilder();
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            while (iterator.Next(out token))
            {
                if (token.Term.Type == TermType.Key)
                {
                    JavaTermKey key = this.GetTermKey(token);
                    if (key == JavaTermKey.Semicolon)
                    {
                        fileData.Namespace = builder.ToString();
                        return;
                    }
                }

                builder.Append(token.Contents);
            }
        }
예제 #5
0
        private bool TranslateTemplateArgument(TranslationData data, out string argumentString)
        {
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            argumentString = string.Empty;
            while (iterator.Next(out token))
            {
                var segment = new TempDataSegment();

                switch (token.Term.Type)
                {
                    case TermType.Identifier:
                        {
                            argumentString += token.Contents;
                            break;
                        }

                    case TermType.Key:
                        {
                            JavaTermKey key = this.GetTermKey(token);
                            segment.Key = (int)key;
                            switch (key)
                            {
                                case JavaTermKey.Comma:
                                case JavaTermKey.Dot:
                                    {
                                        argumentString += token.Contents;
                                        break;
                                    }

                                case JavaTermKey.Greater:
                                    {
                                        return true;
                                    }

                                default:
                                    {
                                        throw new InvalidDataException("Unpexpected token in Template Argument: " + token.Contents);
                                    }
                            }

                            break;
                        }
                }
            }

            return false;
        }
예제 #6
0
        private bool TranslateTempFunction(TranslationData data, TempFunction functionData)
        {
            bool inThrows = false;
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            while (iterator.Next(out token))
            {
                switch (token.Term.Type)
                {
                    case TermType.IdentifierKey:
                        {
                            JavaTermKey key = this.GetTermKey(token);
                            switch (key)
                            {
                                case JavaTermKey.Void:
                                    {
                                        // Return type stays null
                                        continue;
                                    }

                                case JavaTermKey.Throws:
                                    {
                                        inThrows = true;
                                        continue;
                                    }

                                case JavaTermKey.Enum:
                                case JavaTermKey.Class:
                                    {
                                        // We are declaring a class within a function
                                        System.Diagnostics.Trace.TraceWarning("{0} in class, this needs testing. ({1}", key, data.SourceFile);
                                        return false;
                                    }

                                default:
                                    {
                                        TempAttribute? attribute = this.GetIntermediateAttribute(key);
                                        if (attribute != null)
                                        {
                                            functionData.AddAttribute((int)attribute);
                                            continue;
                                        }

                                        break;
                                    }
                            }

                            if (!string.IsNullOrEmpty(functionData.ReturnType))
                            {
                                throw new InvalidDataException("Identifier key found but return was set already");
                            }

                            functionData.ReturnType = token.Contents;
                            continue;
                        }

                    case TermType.Key:
                        {
                            JavaTermKey key = this.GetTermKey(token);
                            switch (key)
                            {
                                case JavaTermKey.ParenthesisLeft:
                                    {
                                        functionData.Arguments = new TempDataBlock();
                                        this.TranslateParenthesisContent(data, functionData.Arguments);
                                        continue;
                                    }

                                case JavaTermKey.BraceLeft:
                                    {
                                        functionData.Contents = new TempDataBlock();
                                        this.TranslateBraceContent(data, functionData.Contents);
                                        return true;
                                    }

                                case JavaTermKey.BraceRight:
                                    {
                                        // We ran out of the function content scope
                                        return true;
                                    }

                                case JavaTermKey.Equal:
                                case JavaTermKey.Semicolon:
                                    {
                                        // This is not a function, rewind and return
                                        return false;
                                    }

                                case JavaTermKey.Dot:
                                    {
                                        this.HandleQualifiedTypeForClassContent(functionData, token);
                                        continue;
                                    }

                                case JavaTermKey.Comma:
                                    {
                                        if (!inThrows)
                                        {
                                            Diagnostic.Warning("Comma outside of throws, probably Enum definition");
                                            return true;
                                        }

                                        continue;
                                    }

                                case JavaTermKey.BracketLeft:
                                case JavaTermKey.BracketRight:
                                    {
                                        this.HandleBracketForClassContent(functionData, token);
                                        continue;
                                    }

                                case JavaTermKey.Less:
                                    {
                                        string templateArgument;
                                        if (this.TranslateTemplateArgument(data, out templateArgument))
                                        {
                                            functionData.TemplateArgument = templateArgument;
                                        }
                                        else
                                        {
                                            throw new InvalidDataException("Tried template argument but failed");
                                        }

                                        continue;
                                    }
                            }

                            break;
                        }

                    case TermType.Identifier:
                        {
                            this.HandleIdentifierForClassContent(functionData, token, inThrows);
                            continue;
                        }
                }

                throw new InvalidDataException("Unexpected token: " + token);
            }

            throw new InvalidOperationException("should not drop out here");
        }
예제 #7
0
        private void TranslateParenthesisContent(TranslationData data, TempDataBlock dataBlock)
        {
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            int level = 0;
            while (iterator.Next(out token))
            {
                var segment = new TempDataSegment();

                switch (token.Term.Type)
                {
                    case TermType.IdentifierKey:
                        {
                            segment.Key = (int)this.GetTermKey(token);
                            break;
                        }

                    case TermType.Key:
                        {
                            JavaTermKey key = this.GetTermKey(token);
                            segment.Key = (int)key;
                            switch (key)
                            {
                                case JavaTermKey.ParenthesisLeft:
                                    {
                                        level++;
                                        break;
                                    }

                                case JavaTermKey.ParenthesisRight:
                                    {
                                        level--;
                                        if (level <= 0)
                                        {
                                            return;
                                        }

                                        break;
                                    }
                            }

                            break;
                        }
                }

                if (segment.Key == null)
                {
                    segment.Contents = token.Contents;
                }

                dataBlock.AddContent(segment);
            }
        }
예제 #8
0
        private void TranslateStatementContent(TranslationData data, TempDataBlock dataBlock)
        {
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            while (iterator.Next(out token))
            {
                var segment = new TempDataSegment();

                switch (token.Term.Type)
                {
                    case TermType.IdentifierKey:
                        {
                            segment.Key = (int)this.GetTermKey(token);
                            break;
                        }

                    case TermType.Key:
                        {
                            JavaTermKey key = this.GetTermKey(token);
                            segment.Key = (int)key;
                            switch (key)
                            {
                                case JavaTermKey.Semicolon:
                                    {
                                        return;
                                    }
                            }

                            break;
                        }
                }

                if (segment.Key == null)
                {
                    segment.Contents = token.Contents;
                }

                dataBlock.AddContent(segment);
            }
        }
예제 #9
0
        private void TranslateBraceContent(TranslationData data, TempDataBlock dataBlock)
        {
            TranslationDataIterator iterator = data.GetIterator();
            Token token;
            while (iterator.Next(out token))
            {
                var segment = new TempDataSegment();

                switch (token.Term.Type)
                {
                    case TermType.IdentifierKey:
                        {
                            segment.Key = (int)this.GetTermKey(token);
                            break;
                        }

                    case TermType.Key:
                        {
                            JavaTermKey key = this.GetTermKey(token);
                            segment.Key = (int)key;
                            switch (key)
                            {
                                case JavaTermKey.BraceLeft:
                                    {
                                        segment.Key = null;
                                        segment.BlockContents = new TempDataBlock();
                                        this.TranslateBraceContent(data, segment.BlockContents);
                                        dataBlock.AddContent(segment);
                                        continue;
                                    }

                                case JavaTermKey.BraceRight:
                                    {
                                        return;
                                    }
                            }

                            break;
                        }
                }

                // Only need the content if we don't know what this token is
                if (segment.Key == null)
                {
                    segment.Contents = token.Contents;
                }

                dataBlock.AddContent(segment);
            }
        }
예제 #10
0
        private TempFileFull TranslateToTempFile(TranslationData data)
        {
            data.CurrentToken = 0;

            // Create the root data element
            var file = new TempFileFull { Name = data.TargetFile.FileName, StructureType = TempFileStructureType.Java };
            data.TranslationStack.Push(file);

            while (data.CurrentToken < data.Tokens.Count)
            {
                TempData currentData = data.TranslationStack.Peek();
                switch (currentData.Type)
                {
                    case TempDataType.File:
                        {
                            this.TranslateTempFile(data, (TempFileFull)currentData);
                            break;
                        }

                    case TempDataType.Class:
                        {
                            this.TranslateTempClass(data, (TempClass)currentData);
                            break;
                        }

                    case TempDataType.Function:
                        {
                            this.TranslateTempFunction(data, (TempFunction)currentData);
                            break;
                        }

                    case TempDataType.Member:
                        {
                            this.TranslateTempMember(data);
                            break;
                        }

                    default:
                        {
                            throw new NotImplementedException("Unknown translation data type: " + currentData.Type);
                        }
                }
            }

            return file;
        }
예제 #11
0
        // -------------------------------------------------------------------
        // Private
        // -------------------------------------------------------------------
        private void TranslateFile(TempProject project, CarbonFile sourceFile, CarbonFile targetFile)
        {
            Diagnostic.RegisterThread("Translate." + sourceFile.FileName);

            CarbonDirectory targetRelativeSubDir = targetFile.GetDirectory() ?? new CarbonDirectory(string.Empty);
            CarbonDirectory fullTargetPath = this.targetDirectory.ToDirectory(this.config.Current.IntermediateSubDirectory, targetRelativeSubDir);
            CarbonFile fullTargetFile = fullTargetPath.ToFile(targetFile.FileName + TempExtension);
            CarbonFile relativeTargetFile = this.config.Current.IntermediateSubDirectory.ToDirectory(targetRelativeSubDir).ToFile(targetFile.FileName + TempExtension);

            if (this.config.Current.Verbose)
            {
                System.Diagnostics.Trace.TraceInformation("Translating {0} -> {1}", sourceFile, fullTargetFile);
            }

            IList<Token> tokens = this.TokenizeFile(sourceFile);
            if (tokens == null)
            {
                throw new InvalidDataException();
            }

            project.AddStat(TempProjectStat.Files);
            project.AddStat(TempProjectStat.Tokens, tokens.Count);
            var fileEntry = new TempProjectFileEntry
                                {
                                    IsCompressed = this.config.Current.CompressIntermediate,
                                    RelativeFile = new CarbonFile(targetFile.FileName),
                                    RelativePath = targetFile.GetDirectory(),
                                    File = relativeTargetFile,
                                };

            var data = new TranslationData(tokens)
                           {
                               ProjectData = project,
                               SourceFile = sourceFile,
                               TargetFile = fullTargetFile,
                               FileEntry = fileEntry
                           };

            // Register the file entry into the project
            project.AddFile(fileEntry);

            try
            {
                TempFileFull file = this.TranslateToTempFile(data);
                this.SaveTempFile(data, file);
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.TraceError("Failed to translate {0}: {1}", fileEntry.File, e);
                this.SaveTempFile(data, new TempFileFull() { Name = data.TargetFile.FileName });
            }

            Diagnostic.UnregisterThread();
        }
예제 #12
0
 private void SaveTempFile(TranslationData data, TempFileFull file)
 {
     if (this.config.Current.CompressIntermediate)
     {
         JsonExtensions.SaveToFile(data.TargetFile, file);
     }
     else
     {
         JsonExtensions.SaveToFile(data.TargetFile, file, false, Formatting.Indented);
     }
 }
 public TranslationDataIterator(TranslationData data)
 {
     this.data = data;
 }