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

📄 nesctreeparser.g

📁 plugin for eclipse
💻 G
📖 第 1 页 / 共 2 页
字号:
header {
package isis.anp.nesc;
}
{
import isis.anp.common.TNode;
import antlr.ASTPair;
import antlr.MismatchedTokenException;
import antlr.NoViableAltException;
import antlr.RecognitionException;
import antlr.collections.AST;
import antlr.collections.impl.BitSet;
}

                     
class NesCTreeParser extends GnuCTreeParser;

options
        {
        importVocab = NesC;
        buildAST = true;
        ASTLabelType = "TNode";

        // Copied following options from java grammar.
        codeGenMakeSwitchThreshold = 2;
        codeGenBitsetTestThreshold = 3;
        }

{
        int traceDepth = 0;
        public void reportError(RecognitionException ex) {
          if ( ex != null)   {
                System.err.println("ANTLR Tree Parsing RecognitionException Error: " + ex.getClass().getName() + " " + ex );
                ex.printStackTrace(System.err);
          }
        }
        public void reportError(NoViableAltException ex) {
                System.err.println("ANTLR Tree Parsing NoViableAltException Error: " + ex.toString());
                TNode.printTree( ex.node );
                ex.printStackTrace(System.err);
        }
        public void reportError(MismatchedTokenException ex) {
          if ( ex != null)   {
                TNode.printTree( ex.node );
                System.err.println("ANTLR Tree Parsing MismatchedTokenException Error: " + ex );
                ex.printStackTrace(System.err);
          }
        }
        public void reportError(String s) {
                System.err.println("ANTLR Error from String: " + s);
        }
        public void reportWarning(String s) {
                System.err.println("ANTLR Warning from String: " + s);
        }
        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;
        }


}

//******
// the translation unit is a NesC file
translationUnit  options {
  defaultErrorHandler=false;
}
    :


	includeFile
	| moduleFile
	| interfaceFile
	// TODO: implement configuration file rule
	//	| configurationFile


	;

/*
exception
catch [RecognitionException ex]
                        {
                        reportError(ex);
                        System.out.println("PROBLEM TREE:\n" 
                                                + _t.toStringList());
                        if (_t!=null) {_t = _t.getNextSibling();}
                        }
*/

//******
// Include file
includeFile
    :
	#( NIncludeFile
    	(externalList)?
	)
    ;


//******
// Interface file
interfaceFile
	:
	#(NInterfaceFile
	   	(includes)*
	    interfaceDeclaration
    )
	;

includes
	:
                #( i:NIncludes
                    //i:"includes"

                    includeFileNameList

                 )
	;

includeFileNameList
        :       (  includeFileName
                    (   c:COMMA              
                        includeFileName
                    )*
                  )
        ;


includeFileName
    :
                #( NIncludeFileName
                  i:ID
// TODO: optionally we can get the includeFileAST attribute of the ID node
// and start processing the AST of the included file here
                 )
    ;



interfaceDeclaration
	: #(lc:NInterface                          
	     i:"interface"                      
	     id:ID                               
	      	                                          
	     declarationList
	     rc:RCURLY                            
       )
	;


//******
// Module file
moduleFile
	:
	#(NModuleFile
	   	(includes)*
	   	module
	)
	;


module
	:           #( m:NModule

	                i:ID                     
                	interfaceDescription
	                moduleImplementation
                 )
	;

interfaceDescription
	:           #( lc:NInterfaceDescription
            	  (
                  uses
                  | provides
            
            	  )*
            	  rc:RCURLY                     
            	 )
	;

uses
    :           #( u:NUses            
			    interfaceDescriptionItemList
                 )
    ;


provides
    :           #( p:NProvides     
			    interfaceDescriptionItemList
                 )
    ;

interfaceDescriptionItemList
	: interfaceDescriptionItem
	| ( 
	    lc:LCURLY
	    (interfaceDescriptionItem)+       
	    rc:RCURLY
	  )
	;

interfaceDescriptionItem
	: declaration   
	| 
	     i:"interface"
	     interfaceIdentifier (interfaceParameters)?
	                  
	   
	;
	
interfaceIdentifier
	: (i:ID           

    	 (  a:"as"    
    	    r:ID      
    	 )?

// TODO: optionally we can get the interfaceFileAST attribute of the ID node
// and start processing the AST of the interface file here
    	 
	  )
	;

interfaceParameters
	: 
	l:LBRACKET                               
	parameterTypeList
	r:RBRACKET                               
	;

moduleImplementation
    : 
            #(  i:NImplementation			
                lc:LCURLY
                                            
                externalList                 
                rc:RCURLY                   
             )
    ;

externalList
        :       ( externalDef | textNode )+
        ;

textNode
		:		NText
		;

externalDef
        :       declaration
        |       functionDef
        |       asm_expr
        |       SEMI
        |       typelessDeclaration
        ;

typelessDeclaration
        :       #(NTypeMissing initDeclList SEMI)
        ;



asm_expr
        :       #( "asm" ( "volatile" )? LCURLY expr RCURLY ( SEMI )+ )
        ;


declaration
        :       #( NDeclaration
                    declSpecifiers
                    (                   
                        initDeclList
                    )?
                    ( SEMI )+
                )
        ;


declSpecifiers 
        :       ( storageClassSpecifier
                | typeQualifier
                | typeSpecifier
                )+
        ;

storageClassSpecifier
        :       a:"auto"                        
        |       b:"register"                    
        |       c:"typedef"                     
        |       d:"norace"                      
        |       functionStorageClassSpecifier
        ;


functionStorageClassSpecifier
        :       a:"extern"                      
        |       b:"static"                      
        |       c:"inline"                      
        |       d:"async"                       
        |       e:"default"                     
        |       f:"command"                     
        |       g:"event"                       
        |       h:"task"                        
        ;


typeQualifier
        :       "const"
        |       "volatile"
        ;


typeSpecifier
        :       "void"
        |       "char"
        |       "short"
        |       "int"
        |       "long"
        |       "float"
        |       "double"
        |       "signed"
        |       "unsigned"
        |       structSpecifier ( attributeDecl )*
        |       unionSpecifier  ( attributeDecl )*
        |       enumSpecifier
        |       typedefName
        |       #("typeof" LPAREN
                    ( (typeName )=> typeName 
                    | expr
                    )
                    RPAREN
                )
        |       "__complex"
        ;


typedefName
        :       #(NTypedefName ID)
        ;


structSpecifier
        :   #( "struct" structOrUnionBody )
        ;

unionSpecifier
        :   #( "union" structOrUnionBody )
        ;
   
structOrUnionBody
        :       ( (ID LCURLY) => ID LCURLY
                        ( structDeclarationList )?
                        RCURLY  
                |   LCURLY
                    ( structDeclarationList )?
                    RCURLY
                | ID
                )
        ;
/*
exception
catch [RecognitionException ex]
                        {
                        reportError(ex);
                        System.out.println("PROBLEM TREE:\n" 
                                                + _t.toStringList());
                        if (_t!=null) {_t = _t.getNextSibling();}
                        }
*/


structDeclarationList
        :       ( structDeclaration )+
        ;
/*
exception
catch [RecognitionException ex]
                        {
                        reportError(ex);
                        System.out.println("PROBLEM TREE:\n" 
                                                + _t.toStringList());
                        if (_t!=null) {_t = _t.getNextSibling();}
                        }
*/



structDeclaration
        :       specifierQualifierList structDeclaratorList
        ;
/*
exception
catch [RecognitionException ex]
                        {
                        reportError(ex);
                        System.out.println("PROBLEM TREE:\n" 
                                                + _t.toStringList());
                        if (_t!=null) {_t = _t.getNextSibling();}
                        }
*/



specifierQualifierList
        :       (
                typeSpecifier
                | typeQualifier
                )+
        ;
/*
exception
catch [RecognitionException ex]
                        {
                        reportError(ex);
                        System.out.println("PROBLEM TREE:\n" 
                                                + _t.toStringList());
                        if (_t!=null) {_t = _t.getNextSibling();}
                        }
*/



structDeclaratorList
        :       ( structDeclarator )+
        ;
/*
exception
catch [RecognitionException ex]
                        {
                        reportError(ex);
                        System.out.println("PROBLEM TREE:\n" 
                                                + _t.toStringList());
                        if (_t!=null) {_t = _t.getNextSibling();}
                        }
*/



structDeclarator
        :
        #( NStructDeclarator      
            ( declarator )?
            ( COLON expr )?
            ( attributeDecl )*
        )
        ;
/*
exception
catch [RecognitionException ex]
                        {
                        reportError(ex);
                        System.out.println("PROBLEM TREE:\n" 
                                                + _t.toStringList());
                        if (_t!=null) {_t = _t.getNextSibling();}
                        }
*/




enumSpecifier
        :   #(  "enum"
                ( ID )? 
                ( LCURLY enumList RCURLY )?
            )
            ( attributeDecl )*
        ;


enumList
        :       ( enumerator )+
        ;


enumerator
        :       ID ( ASSIGN expr )?
        ;



attributeDecl:
        #( "__attribute" (.)* )
        | #( NAsmAttribute LPAREN expr RPAREN )
        ;

initDeclList
        :       ( initDecl )+
        ;


initDecl
                                        { String declName = ""; }
        :       #( NInitDecl
                declarator
                ( attributeDecl )*
                ( ASSIGN initializer
                | COLON expr
                )?
                )
        ;


pointerGroup
        :       #( NPointerGroup ( STAR ( typeQualifier )* )+ )
        ;



⌨️ 快捷键说明

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