Exemplo n.º 1
0
def ParseGrammarDefinitions(gramSpec,
                            gramName,
                            Lists,
                            Dicts,
                            activeRules,
                            All=1,
                            Exclusive=0,
                            exclusiveState=0):
    if type(gramSpec) != type([]): gramSpec = [gramSpec]
    gramparser.splitApartLines(gramSpec)
    ##    Parser = natlinkutils.GramParser(gramSpec)
    ##    Parser.doParse()
    # with gramparserlexyacc:
    Parser = gramparser.GramParser(gramSpec)
    # print '%s, type gramSpec: %s, '% (gramName, type(gramSpec))
    # if type(gramSpec) == list:
    #     gramSpec = [checkForBinary(g) for g in gramSpec]
    Parser.doParse()
    ParserInfo = (InverseDict(Parser.knownWords),
                  InverseDict(Parser.knownRules),
                  InverseDict(Parser.knownLists), Parser.importRules)
    stack = []
    for name in Parser.ruleDefines.keys():
        ParseRuleDefinitions(name, stack, Parser, ParserInfo, Lists, Dicts)
    DefRules = {}
    for x in stack:
        DefRules[x.Name] = x
    UsedRules = []
    for x in stack:
        x.FillInRules(DefRules, UsedRules)
        x.FoldLongAlternatives(0)
    Grammar = GrammarElement()
    Grammar.Init(RuleCode, gramName)
    if Exclusive:
        if not exclusiveState:
            return
        # if asking for exclusive, only show the activerules
        All = 0

    if All:
        Obsolete = GrammarElement()
        Obsolete.Init(RuleCode, 'Obsolete')
        for rule in stack:
            if rule.Name in Parser.exportRules:
                Grammar.Insert(rule)
            elif not rule.Name in UsedRules:
                Obsolete.Insert(rule)
        if len(Obsolete.Included) != 0: Grammar.Append(Obsolete)
    elif activeRules:
        for rule in stack:
            if (rule.Name in activeRules):
                Grammar.Insert(rule)
    else:
        return  # nothing if no active rules QH
    return Grammar
Exemplo n.º 2
0
def test_splitApartLines_first_line_does_not_influence_stripping_of_others():
    actual = splitApartLines([
        "This is line one\n This is line two, one space",
        "  This is line three, one more space"
    ])
    expected = [
        'This is line one', 'This is line two, one space',
        ' This is line three, one more space'
    ]
    assert actual == expected
Exemplo n.º 3
0
def test_splitApartLines_complex_indentation():
    actual = splitApartLines([
        "Initial line\n    This is line two indented\n        This is line three extra indented",
        "Second string no indent\n    But second line indented four"
    ])
    expected = [
        'Initial line', 'This is line two indented',
        '    This is line three extra indented', 'Second string no indent',
        'But second line indented four'
    ]
    assert actual == expected
Exemplo n.º 4
0
def test_splitApartLines_first_line_does_influence_if_it_starts_with_space():
    actual = splitApartLines(
        [" This is line one, one space\n  This is line two, two spaces"])
    expected = ['This is line one, one space', ' This is line two, two spaces']
    assert actual == expected
Exemplo n.º 5
0
def test_splitApartLines_the_first_line_is_ignored():
    actual = splitApartLines(
        "\n     <start> exported = d\xe9mo sample one; \n")
    expected = ['<start> exported = démo sample one;']
    assert actual == expected
Exemplo n.º 6
0
def test_splitApartLines():
    actual = splitApartLines(
        ["This is line one\nThis is line two", "This is line three"])
    expected = ["This is line one", "This is line two", "This is line three"]
    assert actual == expected