Exemple #1
0
scopedIdent = ident + OneOrMore( Literal("::").suppress() + ident )
scopedIdent.setParseAction(lambda t: "_".join(t))

print("(replace namespace-scoped names with C-compatible names)")
print(scopedIdent.transformString( testData ))
    
    
# or a crude pre-processor (use parse actions to replace matching text)
def substituteMacro(s,l,t):
    if t[0] in macros:
        return macros[t[0]]
ident.setParseAction( substituteMacro )
ident.ignore(macroDef)

print("(simulate #define pre-processor)")
print(ident.transformString( testData ))



#################
print("Example of a stripper")
print("----------------------")

from pyparsing import dblQuotedString, LineStart

# remove all string macro definitions (after extracting to a string resource table?)
stringMacroDef = Literal("#define") + ident + "=" + dblQuotedString + LineStart()
stringMacroDef.setParseAction( replaceWith("") )

print(stringMacroDef.transformString( testData ))
Exemple #2
0
scopedIdent.setParseAction(lambda t: "_".join(t))

print("(replace namespace-scoped names with C-compatible names)")
print(scopedIdent.transformString(testData))


# or a crude pre-processor (use parse actions to replace matching text)
def substituteMacro(s, l, t):
    if t[0] in macros:
        return macros[t[0]]


ident.setParseAction(substituteMacro)
ident.ignore(macroDef)

print("(simulate #define pre-processor)")
print(ident.transformString(testData))

#################
print("Example of a stripper")
print("----------------------")

from pyparsing import dblQuotedString, LineStart

# remove all string macro definitions (after extracting to a string resource table?)
stringMacroDef = Literal(
    "#define") + ident + "=" + dblQuotedString + LineStart()
stringMacroDef.setParseAction(replaceWith(""))

print(stringMacroDef.transformString(testData))
Exemple #3
0
scopedIdent = ident + OneOrMore( Literal("::").suppress() + ident )
scopedIdent.setParseAction(lambda t: "_".join(t))

print "(replace namespace-scoped names with C-compatible names)"
print scopedIdent.transformString( testData )
    
    
# or a crude pre-processor (use parse actions to replace matching text)
def substituteMacro(s,l,t):
    if t[0] in macros:
        return macros[t[0]]
ident.setParseAction( substituteMacro )
ident.ignore(macroDef)

print "(simulate #define pre-processor)"
print ident.transformString( testData )



#################
print "Example of a stripper"
print "----------------------"

from pyparsing import dblQuotedString, LineStart

# remove all string macro definitions (after extracting to a string resource table?)
stringMacroDef = Literal("#define") + ident + "=" + dblQuotedString + LineStart()
stringMacroDef.setParseAction( replaceWith("") )

print stringMacroDef.transformString( testData )
Exemple #4
0
def main(argv):
    global verbose, keywords
    language = None

    opts, args = getopt(argv, 's:l:hvV', ['source=', 'language=', 'help', 'verbose', 'version'])
    for opt, arg in opts:
        if opt in ['-s', '--source']:
            source = arg
        elif opt in ['-l', '--language']:
            language = arg
        elif opt in ['-h', '--help']:
            # show usage
            usage()
            exit()
        elif opt in ['-v', '--verbose']:
            verbose = True
        elif opt in ['-V', '--version']:
            version()
            exit()

    try:
        if source:
            pass
    except:
        print('The source is not defined')
        usage()
        if verbose:
            print('exit')
        exit()

    try:
        if language:
            lang = importlib.import_module(language)
        else:
            lang = importlib.import_module('es')
    except:
        print('Error loading the language plugin')
        if verbose:
            print('exit')
        exit()

    keywords = lang.Keywords()

    # translation settings
    w = Word(alphas)
    w.ignore(quotedString)
    w.setParseAction(translate)

    # get all pi files inside root directory
    for root, dirs, files in os.walk(os.getcwd()):
        piFiles = fnmatch.filter(files, lang.FILE_PATTERN)

        if piFiles:
            for piFile in piFiles:
                # read pi file
                piCode = open(root + '/' + piFile, 'r').read()

                # translate pi file
                pyCode = w.transformString(piCode)

                # create python file
                fpy = open(root + '/' + splitext(piFile)[0] + '.py', 'w')
                fpy.write(pyCode)
                fpy.close()

    # read python file
    pythonCode = open(splitext(source)[0] + '.py', 'r').read()

    # exec python code
    exec pythonCode in {'__name__': '__main__', '__doc__': None}, {}
scopedIdent.setParseAction(lambda t: "_".join(t))

print "(replace namespace-scoped names with C-compatible names)"
print scopedIdent.transformString(testData)


# or a crude pre-processor (use parse actions to replace matching text)
def substituteMacro(s, l, t):
    if t[0] in macros:
        return macros[t[0]]


ident.setParseAction(substituteMacro)
ident.ignore(macroDef)

print "(simulate #define pre-processor)"
print ident.transformString(testData)

#################
print "Example of a stripper"
print "----------------------"

from pyparsing import dblQuotedString, LineStart

# remove all string macro definitions (after extracting to a string resource table?)
stringMacroDef = Literal(
    "#define") + ident + "=" + dblQuotedString + LineStart()
stringMacroDef.setParseAction(replaceWith(""))

print stringMacroDef.transformString(testData)