All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.st.p2012.mind.idl.parser.JTBProcessor Maven / Gradle / Ivy

The newest version!
/**
 \file  com/st/p2012/mind/idl/parser/JTBProcessor.java
 \brief A JTBProcessor extension for adding documentation comments in semantic AST nodes.


 
 \n
 This file is part of the Platform 2012 program,
 a cooperation between STMicroelectronics and CEA.\n
 Redistribution of this file to outside parties is
 strictly prohibited without the written consent
 of the module owner indicated below.\n
 

 \par  Module owner: [email protected]


 \par  Copyright (C) 2009 STMicroelectronics

 \par  Authors: [email protected]

 \par  Id: $Id$
 \par  Date: $Date$
 \par  Revision: $Rev$

 */

package com.st.p2012.mind.idl.parser;

import static org.objectweb.fractal.adl.NodeUtil.castNodeError;

import java.util.List;

import org.objectweb.fractal.adl.Node;
import org.objectweb.fractal.adl.xml.XMLNodeFactory;

import org.objectweb.fractal.mind.idl.ast.Type;
import org.objectweb.fractal.mind.idl.ast.TypeCollectionContainer;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.Annotation;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.Annotations;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.EnumDefinition;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.EnumReference;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.EnumSpecification;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.InterfaceDefinition;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.MethodDefinition;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.NodeChoice;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.NodeToken;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.StructOrUnionDefinition;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.StructOrUnionReference;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.StructOrUnionSpecification;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.TypeDefName;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.TypeDefSpecification;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.TypeDefinition;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.TypeQualifier;
import org.objectweb.fractal.mind.idl.jtb.syntaxtree.TypeSpecifiers;

import com.st.p2012.mind.adl.parser.CommentProcessor;
import com.st.p2012.mind.ast.CommentDecoration;

public class JTBProcessor
    extends
      org.objectweb.fractal.mind.idl.parser.JTBProcessor {

  public JTBProcessor(final XMLNodeFactory nodeFactory, final String idlDtd,
      final String filename) {
    super(nodeFactory, idlDtd, filename);
  }

  /**
   * Gets the comment from a NodeToken.
   *
   * @param token
   * @return null if there is no comment associated to this token.
   */
  private String getComment(final NodeToken token) {
    if (token.specialTokens != null) {
      return CommentProcessor.processComment(token.getSpecialAt(0).tokenImage);
    }
    return null;
  }

  private NodeToken getCommentFromAnnotation(final Annotations annotation) {
    NodeToken commentToken = null;
    if(annotation.f0.present()) {
      commentToken = ((Annotation)annotation.f0.elementAt(0)).f0;
    }
    return commentToken;
  }

  @Override
  public Object visit(final InterfaceDefinition n, final Node argu) {
    final Node result = (Node) super.visit(n, argu);
    NodeToken commentToken = getCommentFromAnnotation(n.f0);
    if (commentToken == null){
      commentToken = n.f1;
    }
    CommentDecoration.setComment(result, getComment(commentToken));
    return result;
  }

  @Override
  public Object visit(final MethodDefinition n, final Node argu) {
    final Node result = (Node) super.visit(n, argu);

    NodeToken commentToken = getCommentFromAnnotation(n.f0);

    if(commentToken == null) {
      if(n.f1.f0.present()) { //type qualifier
        commentToken = (NodeToken) ((TypeQualifier) n.f1.f0.elementAt(0)).f0.choice;
      } else {
        final org.objectweb.fractal.mind.idl.jtb.syntaxtree.Node typeSpec = n.f1.f1.f0.choice;
        if(typeSpec instanceof TypeDefName) { //typedef name
          commentToken = ((TypeDefName) typeSpec).f0.f0;
        } else if (typeSpec instanceof StructOrUnionSpecification) { //struct or union specification
          final StructOrUnionSpecification structOrUnion = (StructOrUnionSpecification) typeSpec;
          if (structOrUnion.f0.choice instanceof StructOrUnionDefinition) { //struct or union definition
            commentToken = (NodeToken) ((StructOrUnionDefinition)structOrUnion.f0.choice).f0.f0.choice;
          } else { //struct or union reference
            commentToken = (NodeToken) ((StructOrUnionReference)structOrUnion.f0.choice).f0.f0.choice;
          }
        } else if(typeSpec instanceof EnumSpecification) { //Enum specification
          final EnumSpecification enumSpec = (EnumSpecification) typeSpec;
          if(enumSpec.f0.choice instanceof EnumDefinition) { //enum definition
            commentToken = ((EnumDefinition)enumSpec.f0.choice).f0;
          } else { //enum reference
            commentToken = ((EnumReference)enumSpec.f0.choice).f0;
          }
        } else { //type specifier
          commentToken = (NodeToken) ((NodeChoice)((TypeSpecifiers)typeSpec).f0.elementAt(0)).choice;
        }
      }
    }
    CommentDecoration.setComment(result, getComment(commentToken));
    return result;
  }

  @Override
  public Object visit(final TypeDefinition n, final Node argu) {
    assert argu != null;
    final TypeCollectionContainer container = castNodeError(argu,
        TypeCollectionContainer.class);

    final Object def = n.f0.accept(this, argu);

    if (def instanceof List) {
      for (final Object d : (List) def) {
        assert d instanceof Type;
        final Type type = (Type) d;
        container.addType(type);
        addTypeDefinitionComment(n, type);
        type.astSetDecoration("syntax-tree", n);
      }
    } else {
      assert def instanceof Type;
      final Type type = (Type) def;
      container.addType(type);
      addTypeDefinitionComment(n, type);
      type.astSetDecoration("syntax-tree", n);
    }
    return null;
  }

  private void addTypeDefinitionComment(final TypeDefinition n, final Type type) {
    final org.objectweb.fractal.mind.idl.jtb.syntaxtree.Node typeDefinition = n.f0.choice;

    final NodeToken commentToken;
    if(typeDefinition instanceof TypeDefSpecification) { //typedef name
      commentToken = ((TypeDefSpecification) typeDefinition).f0;
    } else if (typeDefinition instanceof StructOrUnionSpecification) { //struct or union specification
      final StructOrUnionSpecification structOrUnion = (StructOrUnionSpecification) typeDefinition;
      if (structOrUnion.f0.choice instanceof StructOrUnionDefinition) { //struct or union definition
        commentToken = (NodeToken) ((StructOrUnionDefinition)structOrUnion.f0.choice).f0.f0.choice;
      } else { //struct or union reference
        commentToken = (NodeToken) ((StructOrUnionReference)structOrUnion.f0.choice).f0.f0.choice;
      }
    } else {//Enum specification
      final EnumSpecification enumSpec = (EnumSpecification) typeDefinition;
      if(enumSpec.f0.choice instanceof EnumDefinition) { //enum definition
        commentToken = ((EnumDefinition)enumSpec.f0.choice).f0;
      } else { //enum reference
        commentToken = ((EnumReference)enumSpec.f0.choice).f0;
      }
    }
    CommentDecoration.setComment(type, getComment(commentToken));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy