Package net.gereon.jloom.translation.core

Source Code of net.gereon.jloom.translation.core.AbstractToken

/*
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: 27.12.2005  Gereon Fassbender

$Revision$
$Date$
$Log$
*/

package net.gereon.jloom.translation.core;

import java.util.*;

import net.gereon.jloom.problems.*;
import net.gereon.jloom.syntax.*;
import net.gereon.jloom.syntax.impl.TranslationPartImpl;




public abstract class AbstractToken implements Token
{
  private Token parent;
  private Collection<Token> tokens = new ArrayList<Token>();
  private Collection<TranslationProblem> problems = new ArrayList<TranslationProblem>();
  private String text;
  // TODO waer hier CharSequence nicht gut -
  //      und dann die Untertokens als SubSequence
 
  private Range range;
  private TranslationPart part;
  private int start;
  private int length;
  private ParsePattern pattern;
  //private Parser parser;
 
 
 
  public AbstractToken(Token parent, String text, int start, ParsePattern pattern)
  {
    assert text != null;
    assert start >= 0; // && end > start;
    //assert pattern != null;   bei root null
    assert parent != null || start == 0;
   
    this.parent = parent;
    this.text = text;
    this.start = start;
    this.length = text.length();
    this.pattern = pattern;
    //parser = new Parser(text);
    this.range = new Range(getStartPosAbsolute(), length);
    this.part = new TranslationPartImpl(text, range);
  }
 
 
  public void addSubToken(Token t)
  {
    assert t != null;
    tokens.add(t);
  }
 
 
  public Collection<Token> getSubTokens()
  {
    return Collections.unmodifiableCollection(tokens);
  }
 
 
  public String getText()
  {
    return text;
  }
 
 
  public int getStartPos()
  {
    return start;
  }
 
 
  public int getStartPosAbsolute()
  {
    if (parent == null) {
      return start;
    }
    else {
      return start + parent.getStartPosAbsolute();
    }
  }
 

  public int getLength()
  {
    return length;
  }
 
 
  public Range getRange()
  {
    return range;
  }
 
 
  public TranslationPart getTranslationPart()
  {
    return part;
  }
 

  public ParsePattern getPattern()
  {
    return pattern;
  }
 
 
  public void addProblem(TranslationProblem p)
  {
    assert p != null;
    problems.add(p);
  }
 
 
  public void createProblem(String message, boolean fullToken)
  {
    assert message != null;
    TokenSyntaxProblem p = new TokenSyntaxProblem(message, this, fullToken);
    addProblem(p);
  }
 
 
  private <T> void copy(Collection<T> from, Collection<T> to)
  {
    for (T e : from) {
      to.add(e);
    }
  }
 

  public Collection<TranslationProblem> getProblems()
  {
    Collection<TranslationProblem> p = new ArrayList<TranslationProblem>();
    copy(problems, p); // TODO addAll() ?
    for (Token t : tokens) {
      copy(t.getProblems(), p);
    }
    return p;
  }

 
  /*public Token parseToken(ParsePattern... tokenPatterns)
  {
    return parser.parseToken(tokenPatterns);
  }*/
 
 
  String toString(boolean useNewline)
  {
    //String separatorLeft = depth < 1 : ""
    StringBuilder s = new StringBuilder();
   
    if (!useNewline) { s.append('{'); }
   
    //Collection<Token> tokens = getSubTokens();
    if (tokens.isEmpty()) {
      s.append(getText());
    }
    else {
      for (Token t: tokens) {
        s.append(t.toString());
        if (useNewline) { s.append("\n\n"); }
      }
    }
   
    if (!useNewline) { s.append('}'); }
    return s.toString();
  }
 
 
  public String toString()
  {
    return toString(false);
  }
}
TOP

Related Classes of net.gereon.jloom.translation.core.AbstractToken

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.