Beispiel #1
0
def parse(text, parameters):

    try:
        # create XML object from xml string
        parsed_xml = etree.fromstring(text)

    except etree.XMLSyntaxError as error:
        Utilities.logErrors(Utilities.formatLxmlErrorMessage(error, text=text),
                            parameters)
        sys.exit(1)

    # If the node has parameters, then add them to the global parameters dictionary
    parameters = Utilities.mergeDictionaries(
        nodeParametersToDictionary(parsed_xml), parameters)

    return parsed_xml, parameters
Beispiel #2
0
def parse(text, parameters):

    # @BUG If the command line flag --language is used may need to recompute dictionary
    # load cached language and grammar or create from scratch if needed
    grammar, dictionary = Utilities.cache(
        'RoL-language-grammar', lambda: prepareGrammarEngine(parameters))

    if parameters['Inputs']['RoL']['debug']['grammar']:
        print(grammar)

    # @NOTE could not pickle the language itself. Is there a way to solve this?
    # create the grammar
    language = parsley.makeGrammar(
        grammar, {
            'xml': xml,
            'xmlInfix': xmlInfix,
            'dictionary': dictionary,
            'miniLanguage': lambda x, y, z: miniLanguage(x, y, z, parameters)
        })

    try:
        # parse the text against the grammar
        parsed_xml_text = ''.join(language(text).main())
    except parsley.ParseError as error:
        Utilities.logErrors(Utilities.formatParsleyErrorMessage(error),
                            parameters)
        sys.exit(1)

    try:
        # create XML object from xml string
        parsed_xml = etree.fromstring(parsed_xml_text)
    except etree.XMLSyntaxError as error:
        Utilities.logErrors(Utilities.formatLxmlErrorMessage(error),
                            parameters)
        sys.exit(1)

    # If the node has parameters, then add them to the global parameters dictionary
    parameters = Utilities.mergeDictionaries(
        nodeParametersToDictionary(parsed_xml), parameters)

    # apply semantic changes
    parsed_xml = semanticTransformations(parsed_xml)

    return parsed_xml, parameters