⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nescobjecttreebuilder.g

📁 plugin for eclipse
💻 G
📖 第 1 页 / 共 5 页
字号:
header {
package isis.anp.nesc;
}
{
import isis.anp.common.CodeLocation;
import isis.anp.common.ParserMessage;
import isis.anp.common.TNode;
import isis.anp.nesc.common.NesCObjectTreeBuilderContext;
import isis.anp.nesc.ot.AbstractDeclarator;
import isis.anp.nesc.ot.ArrayConstructor;
import isis.anp.nesc.ot.AsmExpr;
import isis.anp.nesc.ot.Attribute;
import isis.anp.nesc.ot.BreakStatement;
import isis.anp.nesc.ot.CaseStatement;
import isis.anp.nesc.ot.ComponentAlias;
import isis.anp.nesc.ot.CompoundStatement;
import isis.anp.nesc.ot.Configuration;
import isis.anp.nesc.ot.ConfigurationImplementation;
import isis.anp.nesc.ot.Connection;
import isis.anp.nesc.ot.ContinueStatement;
import isis.anp.nesc.ot.Declaration;
import isis.anp.nesc.ot.Declarator;
import isis.anp.nesc.ot.DefaultCaseStatement;
import isis.anp.nesc.ot.DirectionSpecifier;
import isis.anp.nesc.ot.DoStatement;
import isis.anp.nesc.ot.EnumSpecifier;
import isis.anp.nesc.ot.Enumerator;
import isis.anp.nesc.ot.Expression;
import isis.anp.nesc.ot.ExpressionStatement;
import isis.anp.nesc.ot.ForStatement;
import isis.anp.nesc.ot.FunctionDeclaration;
import isis.anp.nesc.ot.FunctionDefinition;
import isis.anp.nesc.ot.FunctionPort;
import isis.anp.nesc.ot.GotoStatement;
import isis.anp.nesc.ot.IfStatement;
import isis.anp.nesc.ot.Interface;
import isis.anp.nesc.ot.InterfacePort;
import isis.anp.nesc.ot.LabeledStatement;
import isis.anp.nesc.ot.Module;
import isis.anp.nesc.ot.ModuleImplementation;
import isis.anp.nesc.ot.NameConflictException;
import isis.anp.nesc.ot.NesCComponentFile;
import isis.anp.nesc.ot.NesCConfigurationFile;
import isis.anp.nesc.ot.NesCFile;
import isis.anp.nesc.ot.NesCIncludeFile;
import isis.anp.nesc.ot.NesCInterfaceFile;
import isis.anp.nesc.ot.NesCModuleFile;
import isis.anp.nesc.ot.ParameterDeclaration;
import isis.anp.nesc.ot.ParameterTypeList;
import isis.anp.nesc.ot.PointerGroup;
import isis.anp.nesc.ot.Port;
import isis.anp.nesc.ot.ReturnStatement;
import isis.anp.nesc.ot.Statement;
import isis.anp.nesc.ot.StorageClassSpecifier;
import isis.anp.nesc.ot.StructOrUnionSpecifier;
import isis.anp.nesc.ot.StructSpecifier;
import isis.anp.nesc.ot.SwitchStatement;
import isis.anp.nesc.ot.SynchronySpecifier;
import isis.anp.nesc.ot.TypeQualifier;
import isis.anp.nesc.ot.TypeResolutionException;
import isis.anp.nesc.ot.TypeSpecifier;
import isis.anp.nesc.ot.TypedefName;
import isis.anp.nesc.ot.TypelessDeclaration;
import isis.anp.nesc.ot.UnionSpecifier;
import isis.anp.nesc.ot.WhileStatement;
import isis.anp.nesc.ot.scope.Scope;
import isis.anp.nesc.ot.types.FunctionType;
import isis.anp.nesc.ot.types.TypeBuilder;
import isis.commons.fs.CanonicalPath;

import java.util.ArrayList;
import java.util.List;

import antlr.MismatchedTokenException;
import antlr.NoViableAltException;
import antlr.RecognitionException;
import antlr.collections.AST;
import antlr.collections.impl.BitSet;
}


class NesCObjectTreeBuilder extends TreeParser;

options {
	importVocab= NesC;
	buildAST=false;
	ASTLabelType= "TNode";
	codeGenMakeSwitchThreshold= 2;
	codeGenBitsetTestThreshold= 3;
}

{
		// We need a few member variables to share information between rules. (Obviously, this could have been done
		// with parameter passing, however, using globals is the easy way)
		int paramCount = 0;			// number of params in a param list

		NesCObjectTreeBuilderContext ctx = null;		// the current project
		
		public void setContext(NesCObjectTreeBuilderContext ctx) {
			this.ctx = ctx;
		}

        int traceDepth = 0;

        public void reportError(TypeResolutionException ex) {
          if ( ex != null)   {
          		ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(ex.node), ex));
          }
        }

        public void reportError(NameConflictException ex) {
          if ( ex != null)   {
          		ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(ex.getNode()), ex));
          }
        }

        public void reportError(RecognitionException ex) {
          if ( ex != null)   {
          		ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(ex.getFilename(), ex.getLine(), ex.getColumn()), ex));
          }
        }
        
	     public void reportError(NoViableAltException ex) {
          if ( ex != null)   {
          		ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(ex.getFilename(), ex.getLine(), ex.getColumn()), ex));
          }
        }
        
        public void reportError(MismatchedTokenException ex) {
          if ( ex != null)   {
          		ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(ex.getFilename(), ex.getLine(), ex.getColumn()), ex));
          }
        }
        
        public void reportError(String s) {        	
          	ctx.addMsg(new ParserMessage(ParserMessage.ERROR, s, null, null));
        }
        

        protected void match(AST t, int ttype) throws MismatchedTokenException {
                //System.out.println("match("+ttype+"); cursor is "+t);
                super.match(t, ttype);
        }
        public void match(AST t, BitSet b) throws MismatchedTokenException {
                //System.out.println("match("+b+"); cursor is "+t);
                super.match(t, b);
        }
        protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
                //System.out.println("matchNot("+ttype+"); cursor is "+t);
                super.matchNot(t, ttype);
                }
        public void traceIn(String rname, AST t) {
          traceDepth += 1;
          for (int x=0; x<traceDepth; x++) System.out.print(" ");
          super.traceIn(rname, t);   
        }
        public void traceOut(String rname, AST t) {
          for (int x=0; x<traceDepth; x++) System.out.print(" ");
          super.traceOut(rname, t);
          traceDepth -= 1;
        }

}

translationUnit[CanonicalPath cp] returns [NesCFile nesCFileObj]
options {
	defaultErrorHandler=false;
}
														{
															nesCFileObj = null;
														}
		: nesCFileObj = includeFile[cp]
		| nesCFileObj = moduleFile[cp]
		| nesCFileObj = interfaceFile[cp]
		| nesCFileObj = configurationFile[cp]
	;

includeFile[CanonicalPath cp] returns [NesCIncludeFile includeFileObj]
														{	includeFileObj = null; }
		:#( n:NIncludeFile								{
															includeFileObj = new NesCIncludeFile();
															includeFileObj.setDefNode(n);
															ctx.addProcessedObjectTree(cp,includeFileObj);															
														}
    	(externalList[includeFileObj])?
	)
    ;

interfaceFile[CanonicalPath cp] returns [NesCInterfaceFile interfaceFileObj]
														{	interfaceFileObj = null;
															List includeFileObjList = null;
															Interface interfaceObj = null;
														}
		:#(n:NInterfaceFile								{	interfaceFileObj = new NesCInterfaceFile();
															interfaceFileObj.setDefNode(n);
															ctx.addProcessedObjectTree(cp, interfaceFileObj);															
															
														}
	   	(includeFileObjList  = includes					{	interfaceFileObj.addIncludeFiles(includeFileObjList); }
	   	)*
	    interfaceObj = interfaceDeclaration				{	interfaceFileObj.setInterface(interfaceObj); }
    )
	;

includes returns [List includeFileObjList] 
														{	includeFileObjList = null; }
		:#( i:NIncludes
              includeFileObjList = includeFileNameList
          )
	;

includeFileNameList returns [List includeFileObjList]
														{	includeFileObjList = new ArrayList(); 
															NesCIncludeFile includeFileObj = null;
														}
		:( includeFileObj = includeFileName				{	includeFileObjList.add(includeFileObj); }
           ( c:COMMA              
             includeFileObj = includeFileName			{	includeFileObjList.add(includeFileObj); }
           )*
         )
        ;

includeFileName returns [NesCIncludeFile includeFileObj]
														{	includeFileObj = null; }
		:#( n:NIncludeFileName
                  i:ID
														{															
															// get the include file's AST (the parser stores it as an attribute of the NIncludeFileName node)
															TNode includeFileAST = (TNode)i.getAttribute("includeFileAST");
//															ASTFrameFactory.create(includeFileAST, "include file AST: "+i.getText());
															//String includeFileName = (String)includeFileAST.getAttribute("source");
															String includeFileName = i.getText()+".h";
															
															// try getting the include file object tree from the context
															ParserMessage container = new ParserMessage(ParserMessage.INFO, "OT: Include file: "+includeFileName, new CodeLocation(i), null);
															ctx.addMsg(container);
															ctx.pushMessages(container);
															NesCFile nesCFileObj = null;
															try {
																nesCFileObj = ctx.getObjectTree(includeFileName);
															} catch(ParserMessage m) {
																ctx.addMsg(m);																	
															} finally {
																ctx.popMessages();				
															}
/*															
															if(nesCFileObj == null || !(nesCFileObj instanceof NesCInterfaceFile)) {
																// build the object tree
																includeFileObj = includeFile(includeFileAST);
																// register the parsed object tree with the project
																ctx.setFileObjectTree(includeFileName, includeFileObj);
															} else {
																includeFileObj = (NesCIncludeFile) nesCFileObj;
															}
*/
															includeFileObj = (NesCIncludeFile) nesCFileObj;		
														}
                 )
    ;

interfaceDeclaration returns [Interface interfaceObj]
														{	interfaceObj = null; 
															FunctionDeclaration decl = null;
														}
		: #( lc:NInterface                          	{	interfaceObj = new Interface(); 
															interfaceObj.setDefNode(lc);
														}
	     i:"interface"                      
	     id:ID                               			{	interfaceObj.setNameNode(id); }
	      	                                          
	     (decl = interfaceMemberFunctionDeclaration		{	if(decl instanceof FunctionDeclaration) {
	     														interfaceObj.addFunctionDeclaration(decl);
	     													} else {
	     														TypeResolutionException ex = new TypeResolutionException("Error adding declaration to interface: not function declaration", decl.getDefNode());
	     														reportError(ex);
	     													}
	     												}
	     )+
	     rc:RCURLY                            
       )
	;

interfaceMemberFunctionDeclaration returns [FunctionDeclaration fdeclnObj = null]
														{	
															SynchronySpecifier ssObj = null;
															DirectionSpecifier dsObj = null;
															TypeQualifier tqObj = null;
															TypeSpecifier tsObj = null;
															Declarator fdeclrObj = null;
															TypeBuilder builderObj = null;
														}
	: #( dn:NDeclaration								{	fdeclnObj = new FunctionDeclaration(); 
															fdeclnObj.setDefNode(dn);
															builderObj = new TypeBuilder();
														}
	
		( ssObj = synchronySpecifier					{	fdeclnObj.addSynchronySpecifier(ssObj); }
		| dsObj = directionSpecifier					{	fdeclnObj.addDirectionSpecifier(dsObj); }
		)*
		( tqObj = typeQualifier							{	builderObj.addTypeQualifier(tqObj); }
		)*
		( tsObj = typeSpecifier							{	builderObj.addTypeSpecifier(tsObj);
														}
		)+ 
		
		#( i:NInitDecl
		
			fdeclrObj = interfaceMemberFunctionDeclarator
														{
															try {
															
																builderObj.addDeclarator(fdeclrObj);
	                											if(builderObj.getDeclarator() instanceof Declarator) {
	                												Declarator namedDeclObj = (Declarator)builderObj.getDeclarator();
	                												fdeclnObj.setNameNode(namedDeclObj.getNameNode());
	                												
	                											}
																fdeclnObj.setType(builderObj.getType());
																fdeclnObj.addToScope(ctx);             												
	            												Scope scope = new Scope("function " + fdeclnObj.getName(), ctx.getCurrentScope());
	            												ctx.pushScope(scope);
	            												fdeclnObj.addParametersToScope(ctx);
	            												ctx.popScope();            												
															} catch (TypeResolutionException ex) {
																ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(fdeclrObj.getDefNode()), ex));
															}
														}
		) (SEMI)+
	  )
	;
//    exception
//    catch [TypeResolutionException ex] {
//       reportError(ex);
//    }
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -