001    /*
002     * Copyright 2010-2012 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.js.compiler;
018    
019    import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor;
020    import com.google.dart.compiler.backend.js.ast.*;
021    import com.google.dart.compiler.util.TextOutput;
022    import org.jetbrains.annotations.Nullable;
023    
024    public class JsSourceGenerationVisitor extends JsToStringGenerationVisitor implements TextOutput.OutListener {
025        @Nullable
026        private final SourceMapBuilder sourceMapBuilder;
027    
028        private Object pendingSourceInfo;
029    
030        public JsSourceGenerationVisitor(TextOutput out, @Nullable SourceMapBuilder sourceMapBuilder) {
031            super(out);
032            this.sourceMapBuilder = sourceMapBuilder;
033            out.setOutListener(this);
034        }
035    
036        @Override
037        public void visitProgramFragment(JsProgramFragment x) {
038            x.acceptChildren(this);
039        }
040    
041        @Override
042        public void visitBlock(JsBlock x) {
043            printJsBlock(x, false, true);
044        }
045    
046        @Override
047        public void newLined() {
048            if (sourceMapBuilder != null) {
049                sourceMapBuilder.newLine();
050            }
051        }
052    
053        @Override
054        public void indentedAfterNewLine() {
055            if (pendingSourceInfo != null) {
056                assert sourceMapBuilder != null;
057                sourceMapBuilder.processSourceInfo(pendingSourceInfo);
058                pendingSourceInfo = null;
059            }
060        }
061    
062        @Override
063        public void accept(JsNode node) {
064            if (!(node instanceof JsNameRef)) {
065                mapSource(node);
066            }
067            super.accept(node);
068        }
069    
070        private void mapSource(JsNode node) {
071            if (sourceMapBuilder != null) {
072                Object sourceInfo = node.getSource();
073                if (sourceInfo != null) {
074                    assert pendingSourceInfo == null;
075                    if (p.isJustNewlined()) {
076                        pendingSourceInfo = sourceInfo;
077                    }
078                    else {
079                        sourceMapBuilder.processSourceInfo(sourceInfo);
080                    }
081                }
082            }
083        }
084    
085        @Override
086        protected void beforeNodePrinted(JsNode node) {
087            mapSource(node);
088        }
089    
090        @Override
091        public void visitProgram(JsProgram program) {
092            program.acceptChildren(this);
093            if (sourceMapBuilder != null) {
094                sourceMapBuilder.addLink();
095            }
096        }
097    }