示例#1
0
class SYMBOL(CharToken):
    chars = '@;{}'

def many(rule):
    rule | ('{', plus(at), '}')

def at(rule):
    rule | (no_ignore('@', ID), _or(STRING, SSTRING, star(_not(_or(';','}')))), ';')
    rule | star(_not(_or(';','}')))

g = pgm.Grammar(start=many, tokens=[SYMBOL, ID, STRING, SSTRING, WHITE], ignore=[WHITE])

from codetalker import testing

parse_rule = testing.parse_rule(__name__, g)

parse_rule(many, (
    '{@one "hi";}',
    '{@two "ho" ;}',
    '{@three lots of stuff;}',
    '{@four many" m"ore;}',
    '{random junk}',
    '{@I know you can}',
    '{@do "it" yes}',
    ))

if __name__ == '__main__':
    for name, fn in sorted(globals().items()):
        if name.startswith('test_'):
            print 'testing', name
示例#2
0
from codetalker import testing
import codetalker.contrib.configparser as configparser

parse_rule = testing.parse_rule(__name__, configparser.grammar)

parse_rule(configparser.name, (
    'hi',
    'eggs and spam',
    'mult1ple 23',
    'spam-eggs',
    ), (
    '',
    '.',
    'sandom ch@rs',
    ))

parse_rule(configparser.value, (
    'basically anything',
    'with\n  and indent',
    'and\n  some more\n  lines',
    ), (
    'no\nindent',
    'multiple\n  indent\n    levels',
    'invalid\n    dedents\n  are bad',
    ))

parse_rule(configparser.define, (
    'a=b',
    'party=23 54',
    'but:we already\n  know this',
示例#3
0
__author__ = 'megabytephreak'

import rdlcompiler.systemrdl.grammar as grammar
from codetalker import testing
import os


def readall(path):
    path = os.path.join(os.path.dirname(__file__), path)
    with open(path, 'rb') as f:
        return f.read()


parse_rule = testing.parse_rule(__name__,grammar.grammar)

parse_rule(grammar.enum_def, (
    'enum TEST_ENUM {};',
    'enum TEST_ENUM { test_entry = 0; };',
    'enum TEST_ENUM { test_entry = 1\'b0 { desc="Some Description"; }; };',
    'enum a_123 { '
    'test_entry = 1\'b0 { desc="Some Description"; }; '
    'another_entry =  1\'b2; '
    '};',
), (
    'enum TEST_ENUM { test_entry = 1\'b0 { desc="Some Description" }; };',
))

parse_rule(grammar.value, (
    '0',
    '1234',
    '10\'b1',
示例#4
0
#!/usr/bin/env python

import os
import glob
HERE = os.path.dirname(__file__)

files = glob.glob(os.path.join(HERE, '../data/json/*.json'))

from codetalker import testing
import codetalker.contrib.json as json

parse_rule = testing.parse_rule(__name__, json.grammar)

def make_parse(fname):
    text = open(fname).read()
    def meta():
        if os.path.basename(fname).startswith('fail'):
            try:
                res = json.loads(text)
            except:
                pass
            else:
                raise Exception('JSON parsing of %s should have failed: %s' % (fname, text))
        else:
            res = json.loads(text)
    return meta

for fname in files:
    globals()['test_json "%s"' % fname] = make_parse(fname)

parse_rule(json.dict_, (
示例#5
0
文件: grammar.py 项目: yishh/css
#!/usr/bin/env python

from codetalker import testing

import css.grammar as grammar
import css.values

parse_rule = testing.parse_rule(__name__, grammar.grammar)

parse_rule(grammar.class_, (
    '.one',
    '.green',
    '.GReEn',
    '.div',
    ), (
    'one',
    ))

parse_rule(grammar.simple_selector, (
    'div',
    'div#some',
    'div#one.green',
    'div.frown',
    'ul.cheese:first-child',
    'li.one.two.three',
    'a#b.c.d:last-child',
    ), (
    'one',
    'div# and',
    'div. one',
    ))
示例#6
0
#!/usr/bin/env python

from codetalker import testing
import codetalker.contrib.configparser as configparser

parse_rule = testing.parse_rule(__name__, configparser.grammar)

parse_rule(configparser.name, (
    'hi',
    'eggs and spam',
    'mult1ple 23',
    'spam-eggs',
), (
    '',
    '.',
    'sandom ch@rs',
))

parse_rule(configparser.value, (
    'basically anything',
    'with\n  and indent',
    'and\n  some more\n  lines',
), (
    'no\nindent',
    'multiple\n  indent\n    levels',
    'invalid\n    dedents\n  are bad',
))

parse_rule(configparser.define, (
    'a=b',
    'party=23 54',
示例#7
0
#!/usr/bin/env python

import os
import glob
HERE = os.path.dirname(__file__)

files = glob.glob(os.path.join(HERE, '../data/json/*.json'))

from codetalker import testing
import codetalker.contrib.json as json

parse_rule = testing.parse_rule(__name__, json.grammar)


def make_parse(fname):
    text = open(fname).read()

    def meta():
        if os.path.basename(fname).startswith('fail'):
            try:
                res = json.loads(text)
            except:
                pass
            else:
                raise Exception('JSON parsing of %s should have failed: %s' %
                                (fname, text))
        else:
            res = json.loads(text)

    return meta