示例#1
0
def get_grammar() -> ArithmeticGrammar:
    grammar = _raw_grammar()
    if get_config_value('resume_notices'):
        resume_notices_on(grammar)
    elif get_config_value('history_tracking'):
        set_tracer(grammar, trace_history)
    return grammar
示例#2
0
    def test_trace_resume_message(self):
        lang = """
        @literalws = right
        document = alpha [beta] gamma "."
          alpha = "ALPHA" abc
            abc = "    " | §"a" "b" "c"
          beta = "BETA" (bac | bca)
            bac = "b" "a" §"c"
            bca = "b" "c" §"a"
          gamma = "GAMMA" §(cab | cba)
            cab = "c" "a" §"b"
            cba = "c" "b" §"a"
        """
        gr = grammar_provider(lang)()
        gr.resume_rules = dict()
        gr.resume_rules__['alpha'] = [re.compile(r'(?=BETA)')]
        content = 'ALPHA acb BETA bac GAMMA cab .'

        # test resume notice
        resume_notices_on(gr)
        cst = gr(content)
        # there should be one error message and one resume notice
        assert len(cst.errors_sorted) == 2
        notices = [e for e in cst.errors_sorted if e.code < WARNING]
        assert len(notices) == 1, str(notices)
        notice = str(notices[0])
        assert notice.find('Skip') < 0, 'Not a resume-notice: ' + notice
        assert notice.find('"abc') >= 0, "Wrong parser reported: " + notice
        set_tracer(gr, None)
        assert not gr.history_tracking__
示例#3
0
    def test_trace_resume_complex_case(self):
        lang = r"""
            @ literalws = right
            @ comment =  /(?:\/\/.*)|(?:\/\*(?:.|\n)*?\*\/)/  # Kommentare im C++-Stil
            document = block_A block_B
            @ block_A_resume = /(?=x)/
            block_A = "a" §"b" "c"
            block_B = "x" "y" "z"
        """

        def mini_suite(grammar):
            tree = grammar('abc/*x*/xyz')
            assert not tree.errors

            tree = grammar('abDxyz')
            mandatory_cont = (MANDATORY_CONTINUATION,
                              MANDATORY_CONTINUATION_AT_EOF)
            assert len(
                tree.errors) > 1 and tree.errors[0].code in mandatory_cont
            reveal(grammar, 'trace_resume_complex_1')

            tree = grammar('abD/*x*/xyz')
            assert len(
                tree.errors) > 1 and tree.errors[0].code in mandatory_cont
            reveal(grammar, 'trace_resume_complex_2')

            tree = grammar('aD /*x*/ c /* a */ /*x*/xyz')
            assert len(
                tree.errors) > 1 and tree.errors[0].code in mandatory_cont
            reveal(grammar, 'trace_resume_complex_3')

        # test regex-defined resume rule
        grammar = grammar_provider(lang)()
        resume_notices_on(grammar)
        mini_suite(grammar)
示例#4
0
    def test_trace_resume(self):
        lang = """
        @literalws = right
        document = alpha [beta] gamma "."
          alpha = "ALPHA" abc
            abc = §"a" "b" "c"
          beta = "BETA" (bac | bca)
            bac = "b" "a" §"c"
            bca = "b" "c" §"a"
          gamma = "GAMMA" §(cab | cba)
            cab = "c" "a" §"b"
            cba = "c" "b" §"a"
        """
        gr = grammar_provider(lang)()
        gr.resume_rules = dict()
        gr.resume_rules__['alpha'] = [re.compile(r'(?=BETA)')]
        content = 'ALPHA acb BETA bac GAMMA cab .'
        cst = gr(content)
        assert cst.error_flag
        assert cst.content == content, cst.as_sxpr()
        assert cst.pick('alpha').content.startswith('ALPHA')
        # because of resuming, there should be only one error message
        assert len(cst.errors_sorted) == 1

        # test resume notice
        resume_notices_on(gr)
        cst = gr(content)
        # there should be one error message and one resume notice
        set_tracer(gr, None)
        assert not gr.history_tracking__
def get_grammar() -> json_fail_tolerantGrammar:
    grammar = _raw_grammar()
    if get_config_value('resume_notices'):
        resume_notices_on(grammar)
    elif get_config_value('history_tracking'):
        set_tracer(grammar, trace_history)
    return grammar
示例#6
0
 def test_trace_resume(self):
     gr = self.gr
     gr.resume_rules = dict()
     gr.resume_rules__['alpha'] = [re.compile(r'(?=BETA)')]
     resume_notices_on(gr)
     content = 'ALPHA acb BETA bac GAMMA cab .'
     cst = gr(content)
     assert cst.error_flag
     assert cst.content == content
     assert cst.pick('alpha').content.startswith('ALPHA')
     # because of resuming, there should be only one error message
     assert len([err for err in cst.errors_sorted if err.code >= 1000]) == 1
     reveal(gr, 'trace_resume')
示例#7
0
def get_grammar() -> LyrikGrammar:
    """Returns a thread/process-exclusive LyrikGrammar-singleton."""
    THREAD_LOCALS = access_thread_locals()
    try:
        grammar = THREAD_LOCALS.Lyrik_00000001_grammar_singleton
    except AttributeError:
        THREAD_LOCALS.Lyrik_00000001_grammar_singleton = LyrikGrammar()
        if hasattr(get_grammar, 'python_src__'):
            THREAD_LOCALS.Lyrik_00000001_grammar_singleton.python_src__ = get_grammar.python_src__
        grammar = THREAD_LOCALS.Lyrik_00000001_grammar_singleton
    if get_config_value('resume_notices'):
        resume_notices_on(grammar)
    elif get_config_value('history_tracking'):
        set_tracer(grammar, trace_history)
    return grammar
示例#8
0
 def test_trace_skip_clause(self):
     lang = """
     document = series | /.*/
     @series_skip = /(?=[A-Z])/
     series = "A" "B" §"C" "D"
     """
     gr = grammar_provider(lang)()
     resume_notices_on(gr)
     st = gr('AB_D')
     assert 'Skipping' in str(st.errors_sorted[1])
     for record in gr.history__:
         if record.status.startswith(record.ERROR):
             assert record.excerpt == '_D'
             break
     else:
         assert False, "Missing Error!"
     reveal(gr, 'trace_skip_clause')