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

📄 expandednescparser.g

📁 plugin for eclipse
💻 G
📖 第 1 页 / 共 4 页
字号:
										    {       
												String inputFileName = i.getText()+".nc";			
												AST componentFileAST = null;
										
												ParserMessage container = new ParserMessage(ParserMessage.INFO, "Component file: "+inputFileName, new CodeLocation((CToken)i), null);
												getParserContext().addMsg(container);
												getParserContext().pushMessages(container);
												try {
													componentFileAST = getParserContext().getAST(inputFileName);
													##.setAttribute("componentFileAST", componentFileAST);	        
												} catch(ParserMessage m) {
													getParserContext().addMsg(m);
												} finally {
													getParserContext().popMessages();				
												}
										    }
		
	("as" ID)?
	;

connectionList :(
		( (endpoint ASSIGN) => endpoint ASSIGN endpoint
		| (endpoint PTR) => endpoint PTR endpoint
		| endpoint BACKPTR endpoint
		)
		(SEMI)+
	   )*
		                                                { ## = #( #[NConnectionList], ## ); }
	;

endpoint :ID (DOT ID)* (interfaceParams)?
	;

declarator[boolean isFunctionDefinition] returns [String declName]{ declName = ""; }
:( pointerGroup )?               
    
	( 
    	(
            id:ID               				{ declName = id.getText(); }
                (DOT id2:ID						{ declName = declName + "." + id2.getText(); }
                )?    
        )
        | LPAREN declName = declarator[false] RPAREN
    )

	( {isFunctionDefinition}? (
								(interfaceParameterList)?
								declaratorParamaterList[isFunctionDefinition, declName]
							   )
							   

							   |(
									declaratorParamaterList[isFunctionDefinition, declName]
					    			|LBRACKET ( expr )? RBRACKET
					    	     )*
	)

                                                { ## = #( #[NDeclarator], ## ); }
    ;

interfaceParameterList :(
	                LBRACKET^
	                
	                (                           
	                        (declSpecifiers)=> parameterTypeList
	                        | (idList)?
	                )
	
	                RBRACKET					{ ##.setType(NInterfaceParameterList); }      
	                )?

;

declaratorParamaterList[boolean isFunctionDefinition, String declName] :LPAREN^
                                                { 
                                                    if (isFunctionDefinition) {
                                                        pushScope(declName);
                                                    }
                                                    else {
                                                        pushScope("!"+declName); 
                                                    }
                                                }
                (                           
                        (declSpecifiers)=> parameterTypeList
                        | (idList)?
                )
                                                {
                                                popScope();
                                                }    
                ( COMMA! )?
                RPAREN       
                                                { ##.setType(NParameterTypeList); }      
        ;

statement :(	
        		("atomic") => "atomic" gnuCStatement	{	// System.out.println("atomic keyword found.");
        												}
        		
        		| gnuCStatement
        	)
        ;

gnuCStatement :SEMI                    // Empty statements
        
        |       compoundStatement[getAScopeName()]       // Group of statements

        |       expr SEMI!               { ## = #( #[NStatementExpr], ## );} // Expressions

// Iteration statements:

        |       "while"^ LPAREN! expr RPAREN! statement
        |       "do"^ statement "while"! LPAREN! expr RPAREN! SEMI!
        |!       "for"
                LPAREN ( e1:expr )? SEMI ( e2:expr )? SEMI ( e3:expr )? RPAREN
                s:statement
                                    {
                                        if ( #e1 == null) { #e1 = #[ NEmptyExpression ]; }
                                        if ( #e2 == null) { #e2 = #[ NEmptyExpression ]; }
                                        if ( #e3 == null) { #e3 = #[ NEmptyExpression ]; }
                                        ## = #( #[LITERAL_for, "for"], #e1, #e2, #e3, #s );
                                    }


// Jump statements:

        |       "goto"^ expr SEMI!
        |       "continue" SEMI!
        |       "break" SEMI!
        |       "return"^ ( expr )? SEMI!


        |       ID COLON! (options {warnWhenFollowAmbig=false;}: statement)?  { ## = #( #[NLabel], ## ); }
// GNU allows range expressions in case statements
        |       "case"^ ((constExpr VARARGS)=> rangeExpr | constExpr) COLON! ( options{warnWhenFollowAmbig=false;}:statement )?
        |       "default"^ COLON! ( options{warnWhenFollowAmbig=false;}: statement )?

// Selection statements:

        |       "if"^
                 LPAREN! expr RPAREN! statement  
                ( //standard if-else ambiguity
                        options {
                            warnWhenFollowAmbig = false;
                        } :
                "else" statement )?
        |       "switch"^ LPAREN! expr RPAREN! statement
        ;

storageClassSpecifier :"auto"                  
        |       "register"              
        |       "typedef"               
        |       "norace"
        |       "volatile"        
        |       functionStorageClassSpecifier
        ;

functionStorageClassSpecifier :"extern"
        |       "static"
        |       "inline"
        |       "async"
        |       "default"
        |       "command"
        |       "event"
        |       "task"
        ;

postfixExpr :primaryExpr
                ( 
                postfixSuffix                   {## = #( #[NPostfixExpr], ## );} 
                )?
    | ( "call" | "signal" ) ID (DOT ID)?
                (interfaceParams
                )?
                ( 
                functionCall                    {## = #( #[NPostfixExpr], ## );} 
                )

    | "post" primaryExpr
                ( 
                functionCall                    {## = #( #[NPostfixExpr], ## );} 
                )
    ;

interfaceParams :LBRACKET^ (a:argExprList) RBRACKET
                        {
                        ##.setType( NInterfaceParamArgs );
                        }
        ;

functionDef { String declName; }
:( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
                |  //epsilon
                )
                declName = d:declarator[true]
                            {
                            AST d2, ds2;
                            d2 = astFactory.dupList(#d);
                            ds2 = astFactory.dupList(#ds);
                            getParserContext().getSymbolTable().add(declName, #(null, ds2, d2));
                            pushScope(declName);
                            }
                ( attributeDecl )*                            
                ( declaration )* (VARARGS)? ( SEMI! )*
                            { popScope(); }
                compoundStatement[declName]
                            { ## = #( #[NFunctionDef], ## );}
        ;

functionPrefix { String declName; }
:( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
                |  //epsilon
                )
                declName = d:declarator[true]
                ( attributeDecl )*                                            
                ( declaration )* (VARARGS)? ( SEMI )*
                LCURLY
        ;

parameterDeclaration { String declName; }
:ds:declSpecifiers
                ( ( declarator[false] )=> declName = d:declarator[false]
                            {
                            AST d2, ds2;
                            d2 = astFactory.dupList(#d);
                            ds2 = astFactory.dupList(#ds);
                            getParserContext().getSymbolTable().add(declName, #(null, ds2, d2));
                            }
                | nonemptyAbstractDeclarator
                )?
                            {
                            ## = #( #[NParameterDeclaration], ## );
                            }
        ;

enumSpecifier :"enum"^
                ( ( ID LCURLY )=> i:ID LCURLY enumList[i.getText()] RCURLY
                | LCURLY enumList["anonymous"] RCURLY
                	( attributeDecl )*
                | ID
                )
        ;

enumerator[String enumName] :i:ID                { getParserContext().getSymbolTable().add(  i.getText(),
                                                        #(   null,
                                                            #[LITERAL_enum, "enum"],
                                                            #[ ID, enumName]
                                                         )
                                                     );
                                    }
                (ASSIGN constExpr)?
        ;

initDecl[AST declarationSpecifiers] { String declName = ""; }
:declName = d:declarator[false]
                                        {   AST ds1, d1;
                                            ds1 = astFactory.dupList(declarationSpecifiers);
                                            d1 = astFactory.dupList(#d);
                                            getParserContext().getSymbolTable().add(declName, #(null, ds1, d1) );
                                        }
                ( attributeDecl )*
                ( ASSIGN initializer
                | COLON expr
                )?
                                        { ## = #( #[NInitDecl], ## ); }
        ;

nestedFunctionDef { String declName; }
:( "auto" )? //only for nested functions
                ( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
                )?
                declName = d:declarator[false]
                            {
                            AST d2, ds2;
                            d2 = astFactory.dupList(#d);
                            ds2 = astFactory.dupList(#ds);
                            getParserContext().getSymbolTable().add(declName, #(null, ds2, d2));
                            pushScope(declName);
                            }
                ( declaration )*
                            { popScope(); }
                compoundStatement[declName]
                            { ## = #( #[NFunctionDef], ## );}
        ;

nescDummy :NInterfaceParamArgs
    | NModule
    | NModuleFile
    | NInterface
    | NInterfaceFile
    | NConfiguration
    | NConfigurationFile
    | NUses
    | NProvides
    | NImplementation
    | NInterfaceDescription
    | NIncludes
    | NIncludeFile
    | NIncludeFileName
    | NText
	| NInterfaceParameterList    
	| NComponentList
	| NConnectionList
    ;

// inherited from grammar GnuCParser
asm_expr :"asm"^ 
                ("volatile")? LCURLY expr RCURLY ( SEMI )+
        ;

// inherited from grammar GnuCParser
idList :ID ( options{warnWhenFollowAmbig=false;}: COMMA ID )*
        ;

// inherited from grammar GnuCParser
externalDef :( "typedef" | declaration )=> declaration
        |       ( functionPrefix )=> functionDef
        |       typelessDeclaration
        |       asm_expr
        |       SEMI
        ;

// inherited from grammar GnuCParser
typelessDeclaration { AST typeMissing = #[NTypeMissing]; }
:initDeclList[typeMissing] SEMI          { ## = #( #[NTypeMissing], ##); }
        ;

// inherited from grammar GnuCParser
initializer :( ( ( (initializerElementLabel)=> initializerElementLabel )?
                ( assignExpr | lcurlyInitializer )  { ## = #( #[NInitializer], ## ); }
              )
              | lcurlyInitializer
              )
        ;

// inherited from grammar GnuCParser
initializerElementLabel :(   ( LBRACKET ((constExpr VARARGS)=> rangeExpr | constExpr) RBRACKET (ASSIGN)? )
                | ID COLON
                | DOT ID ASSIGN
            )
                                    { ## = #( #[NInitializerElementLabel], ##) ; }
        ;

// inherited from grammar GnuCParser
lcurlyInitializer :LCURLY^ (initializerList ( COMMA! )? )? RCURLY
                            { ##.setType( NLcurlyInitializer ); }
        ;

// inherited from grammar GnuCParser
initializerList :initializer ( options{warnWhenFollowAmbig=false;}:COMMA! initializer )*
        ;

// inherited from grammar GnuCParser
parameterTypeList :parameterDeclaration
                (   options {
                            warnWhenFollowAmbig = false;
                        } : 
                  ( COMMA | SEMI )  
                  parameterDeclaration
                )*
                ( ( COMMA | SEMI ) 
                  VARARGS
                )?
        ;

// inherited from grammar GnuCParser
declarationList :(               options {   // this loop properly aborts when
                                            // it finds a non-typedefName ID MBZ
                                            warnWhenFollowAmbig = false;
                                        } :
    
                localLabelDeclaration
                |  ( declarationPredictor )=> declaration
                )+
        ;

// inherited from grammar GnuCParser
localLabelDeclaration :( //GNU note:  any __label__ declarations must come before regular declarations.
                "__label__"^ ID (options{warnWhenFollowAmbig=false;}: COMMA! ID)* ( COMMA! )? ( SEMI! )+
                )
        ;

// inherited from grammar GnuCParser
declaration { AST ds1 = null; }
:ds:declSpecifiers       { ds1 = astFactory.dupList(#ds); }
                (                       
                    initDeclList[ds1]
                )?
                ( SEMI )+
                                        { ## = #( #[NDeclaration], ##); }
                
        ;

// inherited from grammar GnuCParser
typeSpecifier[int specCount] returns [int retSpecCount]{ retSpecCount = specCount + 1; }
:( "void"
        |       "char"
        |       "short"
        |       "int"
        |       "long"
        |       "float"
        |       "double"
        |       "signed"
        |       "unsigned"
        |       structOrUnionSpecifier  ( options{warnWhenFollowAmbig=false;}: attributeDecl )*
        |       enumSpecifier
        |       { specCount==0 }? typedefName
        |       "typeof"^ LPAREN
                ( ( typeName )=> typeName
                | expr
                )
                RPAREN
        |       "__complex"
        )
        ;

// inherited from grammar GnuCParser
structOrUnionSpecifier { String scopeName; }
:sou:structOrUnion!
                ( ( ID LCURLY )=> i:ID l:LCURLY
                                            {
                                            scopeName = #sou.getText() + " " + #i.getText();
                                            #l.setText(scopeName);
                                            pushScope(scopeName);
                                            }
                        ( structDeclarationList )?
                                            { popScope();}
                        RCURLY
                |   l1:LCURLY
                                            {
                                            scopeName = getAScopeName();
                                            #l1.setText(scopeName);
                                            pushScope(scopeName);
                                            }
                    ( structDeclarationList )?
                                            { popScope(); }
                    RCURLY
                | ID
                )
                                            {
                                            ## = #( #sou, ## );
                                            }
        ;

// inherited from grammar GnuCParser
structDeclaration :specifierQualifierList structDeclaratorList ( COMMA! )? ( SEMI! )+
        ;

// inherited from grammar GnuCParser
structDeclaratorList :structDeclarator ( options{warnWhenFollowAmbig=false;}: COMMA! structDeclarator )*
        ;

// inherited from grammar GnuCParser
structDeclarator :( declarator[false] )?
                ( COLON constExpr )?
                ( attributeDecl )*
                                    { ## = #( #[NStructDeclarator], ##); }
        ;

// inherited from grammar GnuCParser
enumList[String enumName] :enumerator[enumName] ( options{warnWhenFollowAmbig=false;}: COMMA! enumerator[enumName] )* ( COMMA! )?
        ;

// inherited from grammar GnuCParser
initDeclList[AST declarationSpecifiers] :initDecl[declarationSpecifiers] 
                ( options{warnWhenFollowAmbig=false;}: COMMA! initDecl[declarationSpecifiers] )*
                ( COMMA! )?
        ;

// inherited from grammar GnuCParser
attributeDecl :"__attribute"^ LPAREN LPAREN attributeList RPAREN RPAREN
                | "asm"^ LPAREN stringConst RPAREN { ##.setType( NAsmAttribute ); }
        ;

⌨️ 快捷键说明

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