Package net.gereon.jloom.semantics

Source Code of net.gereon.jloom.semantics.BlockSemantic

/*
This file is part of JLoom
Copyright (C) 2006  Gereon Fassbender
Homepage: jloom.sourceforge.net

JLoom is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You can find a copy of the GNU General Public License along with this program
in a file called COPYING. Information can also be found at www.fsf.org or
www.gnu.org or write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
created: 19.02.2006  Gereon Fassbender

$Revision$
$Date$
$Log$
*/

package net.gereon.jloom.semantics;

import static net.gereon.jloom.core.JLoomConstants.*;
import static net.gereon.jloom.util.GenerationTools.*;

import java.io.*;
import java.util.logging.*;

import net.gereon.jloom.core.*;
import net.gereon.jloom.problems.*;
import net.gereon.jloom.semantics.commands.*;
import net.gereon.jloom.syntax.*;
import net.gereon.jloom.syntax.impl.commands.MacroCommandDefinition;
import net.gereon.jloom.translation.translator.SimpleTranslationMapping;
import net.gereon.jloom.util.Log;



public class BlockSemantic implements Semantic
{
  private static boolean moveInBody(TranslationContext tc, GenerationContext gc, Block block, boolean inBody)
                                            throws IOException
  {
    assert block instanceof Template;
    Template template = (Template) block;
   
    if (!inBody) {
      newLine(gc, 3);
      String baseName = template.getGeneratorClassName();
      println(gc, "public class " + tc.getClassName() + " extends " + baseName + " {");
      gc.incIndent();
    }
   
    return true;
  }
 
 
  private static void addCheckpoint(TranslationContext tc, GenerationContext gc, int templatePos, boolean newToken)
  {
    CharArrayWriter w = (CharArrayWriter) gc.getOut();
    int generatorPos = w.size();
    if (!newToken) {
      generatorPos--;
    }
    SimpleTranslationMapping tm = (SimpleTranslationMapping) tc.getTranslationMapping();
    tm.addCheckpoint(templatePos, generatorPos, false);
  }
 
 
  private static void addCheckpoint(TranslationContext tc, GenerationContext gc, Element e, boolean newToken)
  {
    int templatePos = e.getFullPart().getInputRange().getStart();
    if (Log.log.isLoggable(Level.FINEST)) {
      Log.log.finest("element=" + e.getClass().getSimpleName() + "  templatePos=" + templatePos);
      System.err.println(e.getFullPart().getText());
      System.err.println();
    }
    addCheckpoint(tc, gc, templatePos, newToken);
  }
 
 
  public void translate(TranslationContext tc, GenerationContext gc, Block block) throws IOException
  {
    assert tc != null;
    assert gc != null;
    assert block != null;
   
    Block parent = block.getParent();
    boolean inBody = false;
   
    for (Element e : block.getElements())
    {
      addCheckpoint(tc, gc, e, true);
     
      if (e instanceof CommandBlock)
      {
        CommandBlock cb = (CommandBlock) e;
        if (cb.getCommand().getDefinition() instanceof MacroCommandDefinition) {
          Semantics.get().getSemantic(CustomCommandSemantic.class)
            .translate(tc, gc, (CommandBlock) e);
        }
        else {
          String name = cb.getCommand().getDefinition().getName();
          if (name.equals(JLOOM_COMMAND_NAME)) {
            Semantics.get().getSemantic(CommandSemantic_jloom.class)
              .translate(tc, gc, cb);
          }
          else if (name.equals(MAIN_COMMAND_NAME)) {
            inBody = moveInBody(tc, gc, block, inBody);
            Semantics.get().getSemantic(CommandSemantic_main.class)
              .translate(tc, gc, cb);
          }
          else if (name.equals(TAIL_COMMAND_NAME)) {
            Semantics.get().getSemantic(CommandSemantic_tail.class)
              .translate(tc, gc, cb);
          }
          else {
            throw new RuntimeException("Undefined semantic for command: " + name);
          }
        }
      }
      else if (e instanceof Content) {
        if (parent != null)
        {
          Semantics.get().getSemantic(ContentSemantic.class)
            .translate(tc, gc, (Content) e);
        }
        else {
          if (!((Content) e).getContentPart().getText().trim().equals("")) {
            String msg = "Content not allowed here.";
            TranslationProblem p = new SyntaxProblem(msg, ((Content) e).getContentPart().getInputRange());
            tc.handleProblem(p);
          }
        }
      }
      else if (e instanceof Expression) {
        Semantics.get().getSemantic(ExpressionSemantic.class)
          .translate(tc, gc, (Expression) e);
      }
      else if (e instanceof Declaration) {
        inBody = moveInBody(tc, gc, block, inBody);
        Semantics.get().getSemantic(ScriptletSemantic.class)
          .translate(tc, gc, (Scriptlet) e);
      }
      else if (e instanceof Scriptlet) {
        //nextTrim = true;
        Semantics.get().getSemantic(ScriptletSemantic.class)
          .translate(tc, gc, (Scriptlet) e);
      }
      else {
        throw new RuntimeException("Unknown element: " + e);
      }
     
      /*
      if ( !(e instanceof Block && ((Block) e).getElementCount() > 0) ) {
        int pos = e.getFullPart().getInputRange().getEnd();
        Log.log.finest("endpos: " + pos);
        addCheckpoint(tc, gc, pos, false);
      }
      */
    }
  }
}
TOP

Related Classes of net.gereon.jloom.semantics.BlockSemantic

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.