tree grammar PearGrammerTree;

options
{
    tokenVocab		= PearGrammer;
    language		= C;
	ASTLabelType	= pANTLR3_BASE_TREE;
	output = AST;
}
  


@includes
{
	#include "pearIR.h"
	#include <iostream>
	#include <vector>
	#include <map>
}


@header
{	
	#define	ANTLR3_INLINE_INPUT_ASCII
 	#include "PearGrammerLexer.h"
	using namespace pear;
	using namespace std;
	
	typedef map<string,Binding> SymbolTable;
	
}


@members
{

   
PearIr          gIr;
Transformation  gTransform;
Conversion 	    gConvert;
vector<Parameter>  gParamStack;
vector<Binding> gBindings;
Binding 	    gBinding;

ANTLR3_COMMON_TREE_struct gTree;

SymbolTable     gSymbolTable;



 int main(int argc, char * argv[])
 {

    pANTLR3_INPUT_STREAM           input;
    pPearGrammerLexer               lex;
    pANTLR3_COMMON_TOKEN_STREAM    tokens;
    pPearGrammerParser              parser;

    input  = antlr3AsciiFileStreamNew          ((pANTLR3_UINT8)argv[1]);
    lex    = PearGrammerLexerNew                (input);
    tokens = antlr3CommonTokenStreamSourceNew  (ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
    parser = PearGrammerParserNew               (tokens);

    parser.program_return r = parser->program(parser);
    altlr3CommonTree tree = 

    // Must manually clean up
    //
    parser ->free(parser);
    tokens ->free(tokens);
    lex    ->free(lex);
    input  ->close(input);
    
    cout << "successfully parsed:" << endl;
    
    // make an archive
        // make an archive
   // std::ofstream ofs("log");
    //boost::archive::text_oarchive oa(ofs);
    //oa << gIr;

	boost::archive::text_oarchive oa(cout);
	cout << "parsed expressions:" << endl;
	oa << gIr;


    return 0;
   }
 }
 
 
/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

 
roi_type 
	:  
	 ROI_FOR | ROI_WHILE | ROI_DO | ROI_STRUCT
	| ROI_FIRST | ROI_SECOND | ROI_THIRD | ROI_LAST
	| ROI_CALL | OPERATOR_FUNCTION
	;
 	
transform_type
	: TRANS_CLASS | TRANS_FUNC | TRANS_LOOP
	;
	
target_function 
	: ^(IDENTIFIER param_list)
	;
 	
roi_identifier
	: roi_type | IDENTIFIER
	;
	
roi_expr
	: ^(roi_identifier)*
	;
 

	
	
data_scope_expr
	: ^(IDENTIFIER)*
	;

parameter //[ ParameterType ptype]
	//: v=STRING_LITERAL
		: STRING_LITERAL | IDENTIFIER
//	-> ^($v)
/*
	{
		gParamStack.push_back(Parameter((ParameterType)ptype, (char*)($v.text->chars)));
		//printf("found parameter \%s\n", (char*)($v.text->chars));
		 
	}
	*/
	;


param_list  
	: ^(parameter)*
	;

input_param_list
	: ^ (PARAM_IN param_list)
	;
	
output_param_list
	: ^ (PARAM_OUT param_list)
	;
	
binding_params
	: (input_param_list output_param_list)
	| ( output_param_list)
	| (input_param_list)
	;


binding_function
	: ^(BIND_FUNC (OPERATOR_FUNCTION | IDENTIFIER) binding_params?)
/*
	{
		gBinding.name = (char*)($name.text->chars);
		gBinding.tree = $binding_function.tree;
		gBinding.parameters = gParamStack;
		gParamStack.clear();
	}
*/
	;
	
data_scope_list
	: ^(LIST data_scope_expr)
	;
	
data_scope_elem
	: ^(ELEMENT data_scope_expr)
	;
	
lifted_data_expr
	: (data_scope_elem | data_scope_list)
	;
	
conversion_expr
	: ^(CONVERSION lifted_data_expr)
	;

transform_expr
	: ^(TRANSFORM transform_type roi_expr target_function)
	/*
	{
		gIr.transforms.push_back(gTransform);
		gTransform.clear();
		printf("done transform_expr \%s\n", $transform_expr.text->chars);
	}
	*/
	;
/*
bind_constants
	: 'None' | 'Default'
	;
*/	
function_symbol
	//: FUNC_EXEC | IDENTIFIER
	: IDENTIFIER
	/*
	{		 
		//do lookup for bound functions
		SymbolTable::iterator it = gSymbolTable.find(string((char*)($IDENTIFIER.text->chars)));
		if (it == gSymbolTable.end()) {
			//keep default binding
			//???throw exception for unbound symbol
			printf("found undefined symbol \%s\n", $IDENTIFIER.text->chars);
		}
		else {
		    printf("binding symbol \%s\n", $IDENTIFIER.text->chars);
			gBinding = it->second;  //substitute for bound function
		}
	}
	*/
	;
	
bind_function_type
	: binding_function | 'None' | IDENTIFIER
	;

/*	
execute_function
	: 'execute' '=' bind_function_type
	//->^ (FUNC_EXEC 
	{
		printf("binding execute function \%s\n", $bind_function_type.text->chars);
		gBinding.type = EXEC;
    	//pANTLR3_BASE_TREE	    (*newFromTree)	(struct ANTLR3_ARBORETUM_struct * factory, pANTLR3_COMMON_TREE tree);
		
		//($bind_function_type.dupNode)(&gTree);
	
		
	}
	;

start_function
	: FUNC_START '=' bind_function_type
	{
		gBinding.type = START;
	}
	;
	
stop_function
	: FUNC_STOP '=' bind_function_type
	{
		gBinding.type = STOP;
	}
	;

control_function
	: FUNC_CONTROL '=' bind_function_type
	//-> ^(FUNC_CONTROL $v)
	{
		gBinding.type = CONTROL;
	}
	;
	
semantic_function_binding
	: (execute_function | start_function | stop_function | control_function)
	//-> ^(FUNC_BIND $v)
	;
*/

null_function 
	: 'void'
	;

/*
???It's probably better to have no c code here
???in which case we can't use a symbol table
???how can we save the functions so we can discriminate them???
*/
function_binding
	: ^(FUNCTION IDENTIFIER IDENTIFIER binding_params?)
	//->^ (IDENTIFIER 
	;

data_binding
	: ^(DATA data_scope_expr*)
	;

binding_expr
	: data_binding | function_binding
	/*
	{	
		printf("adding binding \%s\n", $binding_expr.text->chars);
		//add to symbol table
		gSymbolTable[gBinding.name] = gBinding;
		gBindings.push_back(gBinding);
		gBinding.clear();
	}
	*/
	;
	
//identifier_list
//	: IDENTIFIER*
//	;
	
binding_list
	: 'where' binding_expr ('&' binding_expr)*
	^(binding_expr*)
	;

//function 
//	: (operator_function | IDENTIFIER) '(' param_list? ')'
//	;


expression  
	: (conversion_expr | transform_expr ) binding_list? ';'
	;
	
program
	: expression* EOF!
	;
	

	
    
