Beispiel #1
0
# Import from the Standard Library
from unittest import TestCase, main

# Import from itools
from itools.abnf import build_grammar, get_parser, BaseContext

###########################################################################
# Grammar / LR(1)
###########################################################################
lr1_grammar = build_grammar('S = A\r\n'
                            'S = "xb"\r\n'
                            'A = "a" A "b"\r\n'
                            'A = B\r\n'
                            'B = "x"\r\n')

lr1_parser = get_parser(lr1_grammar, 'S')

###########################################################################
# Grammar / Expressions
###########################################################################
expression_grammar = ('E = I\r\n'
                      'E = E "+" E\r\n'
                      'E = E "*" E\r\n'
                      'I = 1*DIGIT\r\n')


class ExpressionContext(BaseContext):
    def I(self, start, end, *args):
        return int(self.data[start:end])

    def E_1(self, start, end, value):
Beispiel #2
0
        fragment = fragment and fragment[0] or ''
        return scheme, hier_part[0], hier_part[1], query, fragment


    def relative_ref(self, start, end, relative_part, query, fragment):
        query = query and query[0] or ''
        fragment = fragment and fragment[0] or ''
        return ('', relative_part[0], relative_part[1], query, fragment)


    def URI_reference(self, start, end, uri):
        return uri


uri_grammar = build_grammar(uri_grammar, URIContext)
uri_parser = get_parser(uri_grammar, 'URI-reference')


def parse_uri(data):
    context = URIContext(data)
    return uri_parser.run(data, context)



class GenericDataType2(object):

    @staticmethod
    def decode(data):
        if isinstance(data, Path):
            return Reference('', '', data, {}, None)
Beispiel #3
0
    def URI(self, start, end, scheme, hier_part, query, fragment):
        query = query and query[0] or ''
        fragment = fragment and fragment[0] or ''
        return scheme, hier_part[0], hier_part[1], query, fragment

    def relative_ref(self, start, end, relative_part, query, fragment):
        query = query and query[0] or ''
        fragment = fragment and fragment[0] or ''
        return ('', relative_part[0], relative_part[1], query, fragment)

    def URI_reference(self, start, end, uri):
        return uri


uri_grammar = build_grammar(uri_grammar, URIContext)
uri_parser = get_parser(uri_grammar, 'URI-reference')


def parse_uri(data):
    context = URIContext(data)
    return uri_parser.run(data, context)


class GenericDataType2(object):
    @staticmethod
    def decode(data):
        if isinstance(data, Path):
            return Reference('', '', data, {}, None)

        if not isinstance(data, (str, unicode)):
            raise TypeError, 'unexpected %s' % type(data)
Beispiel #4
0
# Import from itools
from itools.abnf import build_grammar, get_parser, BaseContext


###########################################################################
# Grammar / LR(1)
###########################################################################
lr1_grammar = build_grammar(
    'S = A\r\n'
    'S = "xb"\r\n'
    'A = "a" A "b"\r\n'
    'A = B\r\n'
    'B = "x"\r\n')

lr1_parser = get_parser(lr1_grammar, 'S')


###########################################################################
# Grammar / Expressions
###########################################################################
expression_grammar = (
    'E = I\r\n'
    'E = E "+" E\r\n'
    'E = E "*" E\r\n'
    'I = 1*DIGIT\r\n')


class ExpressionContext(BaseContext):

    def I(self, start, end, *args):