ComputeSimpleTermPosition.java :  » Parser » sablecc » org » sablecc » sablecc » Java Open Source

Java Open Source » Parser » sablecc 
sablecc » org » sablecc » sablecc » ComputeSimpleTermPosition.java
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of SableCC.                             *
 * See the file "LICENSE" for copyright information and the  *
 * terms and conditions for copying, distribution and        *
 * modification of SableCC.                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package org.sablecc.sablecc;

import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.util.*;
import java.io.*;

public class ComputeSimpleTermPosition extends DepthFirstAdapter
{
  String currentAlt;
  String currentProd;
  boolean processingParsedAlt;
  private ResolveIds ids;
  private int counter;

  public final Map positionsMap = new TypedHashMap(
                                    StringCast.instance,
                                    StringCast.instance);

  public final Map elems_position = new TypedHashMap(
                                      StringCast.instance,
                                      IntegerCast.instance);

  public ComputeSimpleTermPosition(ResolveIds ids)
  {
    this.ids = ids;
  }

  public void inAProd(AProd node)
  {
    currentProd = ids.name(node.getId().getText());
    ids.names.put(node, currentProd);
  }

  public void inAAlt(AAlt node)
  {
    counter = 0;
    processingParsedAlt = true;

    if(node.getAltName() != null)
    {
      currentAlt = "A" +
                   ids.name( node.getAltName().getText() ) +
                   currentProd;
    }
    else
    {
      currentAlt = "A" + currentProd;
    }

    ids.names.put(node, currentAlt);
  }

  public void inAElem(AElem node)
  {
    if(processingParsedAlt)
    {
      String currentElemName;
      if(node.getElemName() != null)
      {
        currentElemName = currentAlt + "." + node.getElemName().getText();
      }
      else
      {
        currentElemName = currentAlt + "." + node.getId().getText();
      }

      elems_position.put(currentElemName, new Integer(++counter));
    }

    if(node.getSpecifier() != null &&
        node.getSpecifier() instanceof ATokenSpecifier)
    {
      return;
    }

    String name = ids.name( node.getId().getText() );

    String elemType = (String)ids.elemTypes.get(node);
    if(processingParsedAlt && elemType.startsWith("P"))
    {
      String elemName;
      if(node.getElemName() != null)
      {
        elemName = node.getElemName().getText();
      }
      else
      {
        elemName = node.getId().getText();
      }

      positionsMap.put(currentAlt+"."+elemName, elemType);
    }
  }

  public void outAAlt(AAlt node)
  {
    processingParsedAlt = false;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.