"""
The :mod:`stan.proc.proc_parse` module is the proc parser for SAS-like language.
"""

from stan.proc.proc_expr import RESERVED_KEYWORDS, PROC_
from stan.proc_functions import * 

def proc_parse(cstr):
    """proc parse converts procedure statements to python function equivalents
    
    Parameters
    ----------
    
    v_ls : list of tokens
    
    Notes 
    -----
    
    ``data`` and ``output``/``out`` are protected variables.    
    If you wish to use a DataFrame as an argument, prepend ``dt_`` for the parser to interpret this correctly
    """
    v_ls = PROC_.parseString(cstr)
    
    sls = []
    preprend = ''
        
    for ls in v_ls[1:]:        
        if len(ls[1:]) > 1:
            sls.append("%s=['%s']" % (ls[0], "','".join(ls[1:])))
        else:
            if ls[0].startswith('dt_') or ls[0] in ['data']: # hungarian notation if we want to use DataFrame as a variable
                sls.append("%s=%s" % (ls[0], ls[1]))
            elif ls[0] in ['output', 'out']:
                preprend += '%s=' % ls[1]
            else:
                sls.append("%s='%s'" % (ls[0], ls[1]))
    return '%s%s.%s(%s)' % (preprend, v_ls[0], v_ls[0], ','.join(sls)) # this statement is a bit dodgy




@@ -140,20 +140,22 @@
 , PROC_%0A
-from
+import
  stan.pr
@@ -171,17 +171,20 @@
 ons 
-import * 
+as proc_func
 %0A%0Ade
@@ -1123,16 +1123,286 @@
 %5D))%0A    
+            %0A    # try to find v_ls%5B0%5D in the %60proc_func%60 namespace...%0A    f_name = v_ls%5B0%5D.strip().lower()%0A    if f_name in proc_func.__dict__.keys(): # is there a better way?%0A        func_name = %22%25s.%25s%22 %25 (f_name, f_name)%0A    else:%0A        func_name = f_name%0A    %0A    
 return '
@@ -1403,19 +1403,16 @@
 turn '%25s
-%25s.
 %25s(%25s)' 
@@ -1428,24 +1428,17 @@
 nd, 
-v_ls%5B0%5D, v_ls%5B0%5D
+func_name
 , ',

change proc parse to allow for flexible procs