Пример #1
0
def init_parser():
    """Set up the parser with the grammar to be recognized.
    """
    # CORE
    digit = h.ch_range(0x30, 0x39)
    alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
    space = h.in_(b" \t\n\r\f\v")

    # AUX.
    plus = h.ch(b'+')
    slash = h.ch(b'/')
    equals = h.ch(b'=')

    bsfdig = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_(b"AEIMQUYcgkosw048")
    bsfdig_2bit = h.in_(b"AQgw")
    base64_3 = h.repeat_n(bsfdig, 4)
    base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64 = h.action(
        h.sequence(h.many(base64_3), h.optional(h.choice(base64_2, base64_1))),
        act_base64)

    # TODO This is not quite the same as the C example, with uses act_ignore.
    #      But I can't get hammer to filter any value returned by act_ignore.
    ws = h.ignore(h.many(space))
    document = h.action(h.sequence(ws, base64, ws, h.end_p()), act_document)

    # BUG sometimes inputs that should just don't parse.
    # It *seemed* to happen mostly with things like "bbbbaaaaBA==".
    # Using less actions seemed to make it less likely.

    return document
Пример #2
0
def init_parser():
    """Set up the parser with the grammar to be recognized.
    """
    # CORE
    digit   = h.ch_range(0x30, 0x39)
    alpha   = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
    space   = h.in_(" \t\n\r\f\v")

    # AUX.
    plus    = h.ch('+')
    slash   = h.ch('/')
    equals  = h.ch('=')

    bsfdig      = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_("AEIMQUYcgkosw048")
    bsfdig_2bit = h.in_("AQgw")
    base64_3    = h.repeat_n(bsfdig, 4)
    base64_2    = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1    = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64      = h.action(h.sequence(h.many(base64_3),
                                      h.optional(h.choice(base64_2,
                                                          base64_1))),
                           act_base64)

    # TODO This is not quite the same as the C example, with uses act_ignore.
    #      But I can't get hammer to filter any value returned by act_ignore.
    ws          = h.ignore(h.many(space))
    document    = h.action(h.sequence(ws, base64, ws, h.end_p()),
                           act_document)

    # BUG sometimes inputs that should just don't parse.
    # It *seemed* to happen mostly with things like "bbbbaaaaBA==".
    # Using less actions seemed to make it less likely.

    return document
Пример #3
0
def init_parser():
    return h.sequence(
        h.many1(
            h.choice(relay_definition_message(),
                     fast_meter_configuration_block(), fast_meter_message(),
                     demand_fast_meter_configuration_block(),
                     peak_fast_meter_configuration_block(),
                     fast_message_block(), relay_operate_configuration())),
        h.end_p())
Пример #4
0
def init_parser():
    """Return a parser with the grammar to be recognized.
    """
    # CORE

    # This is a direct translation of the  C example. In C the literal 0x30
    # is interchangable with the char literal '0' (note the single quotes).
    # This is not the case in Python.
    
    # TODO In the interests of being more Pythonic settle on either string
    #      literals, or integers
    digit   = h.ch_range(0x30, 0x39)
    alpha   = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
    space   = h.in_(" \t\n\r\f\v")

    # AUX.
    plus    = h.ch('+')
    slash   = h.ch('/')
    equals  = h.action(h.ch('='), act_equals)

    bsfdig      = h.action(h.choice(alpha, digit, plus, slash), act_bsfdig)
    bsfdig_4bit = h.action(h.in_("AEIMQUYcgkosw048"), act_bsfdig_4bit)
    bsfdig_2bit = h.action(h.in_("AQgw"), act_bsfdig_2bit)
    base64_3    = h.action(h.repeat_n(bsfdig, 4), act_base64_3)
    base64_2    = h.action(h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals),
                           act_base64_2)
    base64_1    = h.action(h.sequence(bsfdig, bsfdig_2bit, equals, equals),
                           act_base64_1)
    base64      = h.action(h.sequence(h.many(base64_3),
                                      h.optional(h.choice(base64_2,
                                                          base64_1))),
                           act_base64)

    # TODO This is not quite the same as the C example, with uses act_ignore.
    #      But I can't get hammer to filter any value returned by act_ignore.
    ws          = h.ignore(h.many(space))
    document    = h.action(h.sequence(ws, base64, ws, h.end_p()),
                           act_document)

    # BUG sometimes inputs that should just don't parse.
    # It *seemed* to happen mostly with things like "bbbbaaaaBA==".
    # Using less actions seemed to make it less likely.

    return document
Пример #5
0
def init_parser():
    return h.sequence(
        h.many1(
            h.choice(
                #sampleEvents_parser,
                #pollingEngine_parser,
                #envelope_parser,
                #measurementDevice_parser,
                #sensors_parser,
                schemaVersion_parser())))
Пример #6
0
def init_parser():
    # CORE
    digit = h.ch_range(0x30, 0x39)
    alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))

    # AUX.
    plus = h.ch(b'+')
    slash = h.ch(b'/')
    equals = h.ch(b'=')

    bsfdig = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_(b'AEIMQUYcgkosw048')
    bsfdig_2bit = h.in_(b'AQgw')
    base64_3 = h.repeat_n(bsfdig, 4)
    base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64 = h.sequence(h.many(base64_3),
                        h.optional(h.choice(base64_2, base64_1)))

    return h.sequence(h.whitespace(base64), h.whitespace(h.end_p()))
Пример #7
0
def init_parser():
    # CORE
    digit = h.ch_range(0x30, 0x39)
    alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))

    # AUX.
    plus = h.ch('+')
    slash = h.ch('/')
    equals = h.ch('=')

    bsfdig = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_('AEIMQUYcgkosw048')
    bsfdig_2bit = h.in_('AQgw')
    base64_3 = h.repeat_n(bsfdig, 4)
    base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64 = h.sequence(h.many(base64_3),
                        h.optional(h.choice(base64_2, base64_1)))

    return h.sequence(h.whitespace(base64), h.whitespace(h.end_p()))
Пример #8
0
def init_parser():
    return h.sequence(h.many1(h.choice(
        length_block(),
        connection_start_parser(),
        connection_start_ok_parser(),
        connection_tune_parser(),
        connection_tune_ok_parser(),
        connection_open_vhost_parser(),
        channel_open_parser(),
        channel_open_ok_parser(),
        queue_declare_parser(),
        queue_declare_ok_parser(),
        basic_consume_parser(),
        basic_consume_ok_parser(),
        basic_publish_parser(),
        basic_deliver_parser(),
        channel_close_parser(),
        channel_close_ok_parser(),
        connection_close_parser(),
        connection_close_ok_parser()
    )))
Пример #9
0
 def setUpClass(cls):
     cls.parser = h.action(h.sequence(h.choice(h.ch("a"), h.ch("A")),
                                      h.choice(h.ch("b"), h.ch("B"))),
                             lambda x: [y.upper() for y in x])
Пример #10
0
 def setUpClass(cls):
     cls.parser = h.many1(h.choice(h.ch("a"), h.ch("b")))
Пример #11
0
 def setUpClass(cls):
     cls.parser = h.sequence(
         h.ch(b"a"),
         h.choice(h.sequence(h.ch(b"+"), h.not_(h.ch(b"+"))),
                  h.token(b"++")), h.ch(b"b"))
Пример #12
0
 def setUpClass(cls):
     #raise unittest.SkipTest(b"Bind doesn't work right now")
     cls.parser = h.indirect()
     a = h.ch(b"a")
     cls.parser.bind(h.choice(h.sequence(a, cls.parser), h.epsilon_p()))
Пример #13
0
 def setUpClass(cls):
     cls.parser = h.sepBy1(h.choice(h.ch(b"1"), h.ch(b"2"), h.ch(b"3")),
                           h.ch(b","))
Пример #14
0
 def setUpClass(cls):
     cls.parser = h.attr_bool(h.many1(h.choice(h.ch(b"a"), h.ch(b"b"))),
                              lambda x: x[0] == x[1])
Пример #15
0
 def setUpClass(cls):
     cls.parser = h.choice(h.ch("a"), h.ch("b"))
Пример #16
0
import hammer as h
signals = h.choice(h.token("hmi.signal1"), h.token("hmi.signal2"),
                   h.token("hmi.signal3"))
Пример #17
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.choice(h.sequence(h.ch("+"), h.not_(h.ch("+"))),
                                                 h.token("++")),
                             h.ch("b"))
Пример #18
0
 def setUpClass(cls):
     cls.parser = h.sepBy1(h.choice(h.ch("1"), h.ch("2"), h.ch("3")), h.ch(","))
Пример #19
0
 def setUpClass(cls):
     cls.parser = h.repeat_n(h.choice(h.ch("a"), h.ch("b")), 2)
Пример #20
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.choice(h.ch("+"), h.token("++")),
                             h.ch("b"))
Пример #21
0
 def setUpClass(cls):
     cls.parser = h.sepBy(h.choice(h.ch("1"), h.ch("2"), h.ch("3")),
                          h.ch(","))
Пример #22
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.optional(h.choice(h.ch("b"), h.ch("c"))), h.ch("d"))
Пример #23
0
 def setUpClass(cls):
     cls.parser = h.action(
         h.sequence(h.choice(h.ch(b"a"), h.ch(b"A")),
                    h.choice(h.ch(b"b"), h.ch(b"B"))),
         lambda x: [y.upper() for y in x])
Пример #24
0
 def setUpClass(cls):
     cls.parser = h.attr_bool(h.many1(h.choice(h.ch("a"), h.ch("b"))),
                              lambda x: x[0] == x[1])
Пример #25
0
 def setUpClass(cls):
     cls.parser = h.many1(h.choice(h.ch(b"a"), h.ch(b"b")))
Пример #26
0
 def setUpClass(cls):
     #raise unittest.SkipTest("Bind doesn't work right now")
     cls.parser = h.indirect()
     a = h.ch("a")
     cls.parser.bind(h.choice(h.sequence(a, cls.parser),
                              h.epsilon_p()))
Пример #27
0
 def setUpClass(cls):
     cls.parser = h.repeat_n(h.choice(h.ch(b"a"), h.ch(b"b")), 2)
Пример #28
0
import hammer as h


digit   = h.ch_range('0','9')
upper   = h.ch_range('A', 'Z')
lower   = h.ch_range('a', 'z')
chars   = h.choice(digit, upper, lower)

alphanumeric = h.many(chars)

words = h.sepBy1(alphanumeric, h.ch(' '))
version = h.token("Version")
allchars = h.ch_range("!", "~")

printable_chars = h.many(allchars)

token = h.many1(printable_chars)
tokens = h.sepBy1(printable_chars, h.choice(h.ch(' '), h.ch('\n'), h.ch('\r')))

# Action Modifiers

def tuple_string(t):
    new_coll = list()
    for c in t:
        if isinstance(c, tuple):
                new_coll.append("".join(c))
        else:
            return "".join(t)
    return tuple(new_coll)

Пример #29
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch(b"a"),
                             h.optional(h.choice(h.ch(b"b"), h.ch(b"c"))),
                             h.ch(b"d"))