Esempio n. 1
0
 def loc(m):
     a = m[1]
     if isnamedtuplewhere(a):
         if a.where is None:
             print('warning: no where found for %s' % str(a))
             return 0
         return a.where.character
     return 0
Esempio n. 2
0
 def loc(m):
     a = m[1]
     if isnamedtuplewhere(a):
         if a.where is None:
             print('warning: no where found for %s' % str(a))
             return 0
         return a.where.character
     return 0
Esempio n. 3
0
def print_html_inner(x):
    assert isnamedtuplewhere(x), x

    CDP = CDPLanguage
    if isinstance(x, CDP.Placeholder):
        orig0 = x.where.string[x.where.character:x.where.character_end]
        check_isinstance(x.label, str)
        if x.label == '...':  # special case
            transformed = '…'
        else:
            transformed = '<span class="PlaceholderLabel">⟨%s⟩</span>' % x.label

        yield Snippet(op=x,
                      orig=orig0,
                      a=x.where.character,
                      b=x.where.character_end,
                      transformed=transformed)
        return

    subs = list(iterate_check_order(x, order_contributions(iterate2(x))))

    if is_a_special_list(x):
        for _ in subs:
            yield _
        return

    cur = x.where.character
    out = ""
    for _op, _orig, a, b, transformed in subs:
        if a > cur:
            out += sanitize(x.where.string[cur:a])
        out += transformed
        cur = b

    if cur != x.where.character_end:
        out += sanitize(x.where.string[cur:x.where.character_end])

    orig0 = x.where.string[x.where.character:x.where.character_end]

    klass = type(x).__name__

    transformed0 = ("<span class='%s' %s='%d' %s='%s'>%s</span>" %
                    (klass, ATTR_WHERE_CHAR, x.where.character,
                     ATTR_WHERE_CHAR_END, x.where.character_end, out))
    yield Snippet(op=x,
                  orig=orig0,
                  a=x.where.character,
                  b=x.where.character_end,
                  transformed=transformed0)
Esempio n. 4
0
    def collect_dependencies(self, s, x):
        assert isnamedtuplewhere(x), x
        default_library = s.libname
        #print recursive_print(x)
        deps = set()
        CDP = CDPLanguage

        def visit(x):
            if isinstance(x, CDP.LoadNDP):
                if isinstance(x.load_arg, CDP.NDPName):
                    name = x.load_arg.value
                    libname = default_library
                if isinstance(x.load_arg, CDP.NDPNameWithLibrary):
                    libname = x.load_arg.library.value
                    name = x.load_arg.name.value
                d = EntryNDP(libname=libname, name=name)
                deps.add(d)

            if isinstance(x, CDP.LoadPoset):
                if isinstance(x.load_arg, CDP.PosetName):
                    name = x.load_arg.value
                    libname = default_library
                if isinstance(x.load_arg, CDP.PosetNameWithLibrary):
                    libname = x.load_arg.library.value
                    name = x.load_arg.name.value
                d = EntryPoset(libname=libname, name=name)
                deps.add(d)

            if isinstance(x, CDP.LoadTemplate):
                if isinstance(x.load_arg, CDP.TemplateName):
                    name = x.load_arg.value
                    libname = default_library
                if isinstance(x.load_arg, CDP.TemplateNameWithLibrary):
                    libname = x.load_arg.library.value
                    name = x.load_arg.name.value
                d = EntryTemplate(libname=libname, name=name)
                deps.add(d)

        def traverse(x):
            if not isnamedtupleinstance(x):
                return
            else:
                visit(x)
                for _k, v in x._asdict().items():
                    traverse(v)

        traverse(x)

        return deps
Esempio n. 5
0
    def collect_dependencies(self, s, x):
        assert isnamedtuplewhere(x), x
        default_library = s.libname
        #print recursive_print(x)
        deps = set()
        CDP = CDPLanguage
        def visit(x):
            if isinstance(x, CDP.LoadNDP):
                if isinstance(x.load_arg, CDP.NDPName):
                    name = x.load_arg.value
                    libname = default_library
                if isinstance(x.load_arg, CDP.NDPNameWithLibrary):
                    libname = x.load_arg.library.value
                    name = x.load_arg.name.value
                d = EntryNDP(libname=libname, name=name)
                deps.add(d)

            if isinstance(x, CDP.LoadPoset):
                if isinstance(x.load_arg, CDP.PosetName):
                    name = x.load_arg.value
                    libname = default_library
                if isinstance(x.load_arg, CDP.PosetNameWithLibrary):
                    libname = x.load_arg.library.value
                    name = x.load_arg.name.value
                d = EntryPoset(libname=libname, name=name)
                deps.add(d)

            if isinstance(x, CDP.LoadTemplate):
                if isinstance(x.load_arg, CDP.TemplateName):
                    name = x.load_arg.value
                    libname = default_library
                if isinstance(x.load_arg, CDP.TemplateNameWithLibrary):
                    libname = x.load_arg.library.value
                    name = x.load_arg.name.value
                d = EntryTemplate(libname=libname, name=name)
                deps.add(d)

        def traverse(x):
            if not isnamedtupleinstance(x):
                return
            else:
                visit(x)
                for _k, v in x._asdict().items():
                    traverse(v)

        traverse(x)

        return deps
Esempio n. 6
0
def print_ast(x):
    try:
        if isnamedtuplewhere(x):
            s = '%s' % type(x).__name__
            s += '  %r' % x.where
            for k, v in iterate_sub(x):
                first = ' %s: ' % k
                s += '\n' + indent(print_ast(v), ' ' * len(first), first=first)

            if x.where is None:
                raise ValueError(x)
            return s
        else:
            return x.__repr__()
    except ValueError as e:
        raise_wrapped(ValueError, e, 'wrong', x=x)
Esempio n. 7
0
def print_ast(x):
    try:
        if isnamedtuplewhere(x):
            s = '%s' % type(x).__name__
            s += '  %r' % x.where
            for k, v in iterate_sub(x):
                first = ' %s: ' % k
                s += '\n' + indent(print_ast(v), ' ' * len(first), first=first)

            if x.where is None:
                raise ValueError(x)
            return s
        else:
            return x.__repr__()
    except ValueError as e:
        raise_wrapped(ValueError, e, 'wrong', x=x)
Esempio n. 8
0
def iterate3(transform, x):
    from mcdp_report.html import iterate_notwhere
    from mcdp_lang.namedtuple_tricks import isnamedtuplewhere
    from mcdp_report.html import Snippet

    nsubs = 0
    for  _, op in iterate_notwhere(x):
        if isnamedtuplewhere(op):
            for m in transform(op):
                nsubs += 1
                yield m

    d = x._asdict()
    if len(d) == 1 + 1 and nsubs==0:
        orig0 = x.where.string[x.where.character:x.where.character_end]
        k0 = list(d)[0]
        v0 = d[k0]
    
        wsbefore, _, wsafter = extract_ws(orig0)
         
        out = wsbefore + str(v0) + wsafter
        yield Snippet(op=x, orig=orig0, a=x.where.character, b=x.where.character_end,
              transformed=out)
Esempio n. 9
0
def ast_to_html(s,
                parse_expr,
                ignore_line=None,
                add_line_gutter=True,
                encapsulate_in_precode=True,
                postprocess=None):
    """
        postprocess = function applied to parse tree
    """
    check_isinstance(s, str)
    #     if parse_expr is None:
    #         raise Exception('Please add specific parse_expr (default=Syntax.ndpt_dp_rvalue)')

    if ignore_line is None:
        ignore_line = lambda _lineno: False

    original_lines = s.split('\n')

    s_lines, s_comments = isolate_comments(s)
    assert len(s_lines) == len(s_comments)

    num_empty_lines_start = 0
    for line in s_lines:
        if line.strip() == '':
            num_empty_lines_start += 1
        else:
            break

    num_empty_lines_end = 0
    for line in reversed(s_lines):
        if line.strip() == '':
            num_empty_lines_end += 1
        else:
            break

    full_lines = s_lines[num_empty_lines_start:len(s_lines) -
                         num_empty_lines_end]

    from mcdp_report.out_mcdpl import extract_ws
    for_pyparsing0 = "\n".join(full_lines)
    # remove also initial and final whitespace
    extra_before, for_pyparsing, extra_after = extract_ws(for_pyparsing0)
    # parse the string 'for_pyparsing'
    block0 = parse_wrap(parse_expr, for_pyparsing)[0]

    #     print indent(recursive_print(block0), ' block0 |')
    assert isnamedtuplewhere(block0)
    # now transform everything so that it refers to s
    transform_original_s = s

    def transform(x, parents):  # @UnusedVariable
        w0 = x.where
        s0 = w0.string

        def translate(line, col):
            # add initial white space on first line
            if line == 0:
                col += len(extra_before)
            # add the initial empty lines
            line += num_empty_lines_start
            return line, col

        # these are now in the original string transform_original_s
        line, col = translate(*line_and_col(w0.character, s0))
        character = location(line, col, transform_original_s)

        if w0.character_end is None:
            character_end = None
        else:
            line, col = translate(*line_and_col(w0.character_end, s0))
            character_end = location(line, col, transform_original_s)

        where = Where(string=transform_original_s,
                      character=character,
                      character_end=character_end)

        return get_copy_with_where(x, where)

    block = namedtuple_visitor_ext(block0, transform)
    assert isnamedtuplewhere(block)

    if postprocess is not None:
        block = postprocess(block)
        if not isnamedtuplewhere(block):
            raise ValueError(block)

    snippets = list(print_html_inner(block))
    # the len is > 1 for mcdp_statements
    assert len(snippets) == 1, snippets
    snippet = snippets[0]
    transformed_p = snippet.transformed

    #     if block.where.character != 0:
    #         assert False
    #         transformed_p = for_pyparsing[:block.where.character] + transformed_p

    # re-add the initial and final space here
    transformed_p = extra_before + transformed_p + extra_after

    def sanitize_comment(x):
        x = x.replace('>', '&gt;')
        x = x.replace('<', '&lt;')
        return x

    # re-add the initial and final lines
    transformed = ''
    transformed += "\n".join(s_lines[:num_empty_lines_start])
    if num_empty_lines_start:
        transformed += '\n'
    transformed += transformed_p
    if num_empty_lines_end:
        transformed += '\n'
    transformed += "\n".join(s_lines[len(original_lines) -
                                     num_empty_lines_end:])

    lines = transformed.split('\n')
    if len(lines) != len(s_comments):
        msg = 'Lost some lines while pretty printing: %s, %s' % (
            len(lines), len(s_comments))
        raise DPInternalError(msg)

#     print('transformed', transformed)

    out = ""

    for i, (line, comment) in enumerate(zip(lines, s_comments)):
        lineno = i + 1
        if ignore_line(lineno):
            continue
        else:
            #             print('line %d' % i)
            #             print(' oiginal line: %r' % original_lines[i])
            #             print('         line: %r' % line)
            #             print('      comment: %r' % comment)
            original_line = original_lines[i]
            if comment is not None:
                assert '#' in original_line

            if '#' in original_line:
                if '#' in line:
                    w = line.index('#')
                    before = line[:
                                  w]  # (already transformed) #original_line[:w]
                    comment = line[w:]
                else:
                    before = line
                    comment = comment

#                 print('       before: %r' % comment)
#                 print('      comment: %r' % comment)

                if comment.startswith(unparsable_marker):
                    unparsable = comment[len(unparsable_marker):]
                    linec = before + '<span class="unparsable">%s</span>' % sanitize_comment(
                        unparsable)
                else:
                    linec = before + '<span class="comment">%s</span>' % sanitize_comment(
                        comment)

            else:
                linec = line

#             print('        linec: %r' % linec)

            if add_line_gutter:
                out += "<span class='line-gutter'>%2d</span>" % lineno
                out += "<span class='line-content'>" + linec + "</span>"
            else:
                out += linec

            if i != len(lines) - 1:
                out += '\n'

    if MCDPConstants.test_insist_correct_html_from_ast_to_html:
        from xml.etree import ElementTree as ET
        ET.fromstring(out)


#     print 'ast_to_html, out', out

    frag = ""

    if encapsulate_in_precode:
        frag += '<pre><code>'
        frag += out
        frag += '</code></pre>'
    else:
        frag += out

    assert isinstance(frag, str)

    return frag
Esempio n. 10
0
def iterate_(transform, x):
    for _, op in iterate_notwhere(x):
        if isnamedtuplewhere(op):
            for m in transform(op):
                yield m
Esempio n. 11
0
def ast_to_html(s, 
                parse_expr=None,
                
                ignore_line=None,
                add_line_gutter=True, 
                encapsulate_in_precode=True, 
                postprocess=None,
                
                # deprecated
                complete_document=None,
                extra_css=None, 
                add_css=None,
                add_line_spans=None,):
    """
        postprocess = function applied to parse tree
    """
    
    if add_line_spans is not None and add_line_spans != False:
        warnings.warn('deprecated param add_line_spans')
    
    if parse_expr is None:
        raise Exception('Please add specific parse_expr (default=Syntax.ndpt_dp_rvalue)')
        
    if add_css is not None:
        warnings.warn('please do not use add_css', stacklevel=2)

    if complete_document is not None:
        warnings.warn('please do not use complete_document', stacklevel=2)

    if ignore_line is None:
        ignore_line = lambda _lineno: False
        
    if extra_css is not None: 
        warnings.warn('please do not use extra_css', stacklevel=2)
        
    extra_css = ''

    s_lines, s_comments = isolate_comments(s)
    assert len(s_lines) == len(s_comments) 

    num_empty_lines_start = 0
    for line in s_lines:
        if line.strip() == '':
            num_empty_lines_start += 1
        else:
            break
    
    num_empty_lines_end = 0
    for line in reversed(s_lines):
        if line.strip() == '':
            num_empty_lines_end += 1
        else:
            break

    # use = s_lines
    use = s.split('\n')
    full_lines = use[num_empty_lines_start: len(s_lines)- num_empty_lines_end]
    for_pyparsing = "\n".join(full_lines)
    
    block = parse_wrap(parse_expr, for_pyparsing)[0]

    if not isnamedtuplewhere(block): # pragma: no cover
        raise DPInternalError('unexpected', block=block)

    if postprocess is not None:
        block = postprocess(block)
        if not isnamedtuplewhere(block):
            raise ValueError(block)

    snippets = list(print_html_inner(block))
    # the len is > 1 for mcdp_statements
    assert len(snippets) == 1, snippets
    snippet = snippets[0]
    transformed_p = snippet.transformed
    
    if block.where.character != 0:
        transformed_p = for_pyparsing[:block.where.character] + transformed_p

    def sanitize_comment(x):
        x = x.replace('>', '&gt;')
        x = x.replace('<', '&lt;')
        return x

    transformed = '\n' * num_empty_lines_start + transformed_p
    transformed = transformed +  '\n' * num_empty_lines_end
    
#     out = transformed
    
    lines = transformed.split('\n')
    if len(lines) != len(s_comments): 
        msg = 'Lost some lines while pretty printing: %s, %s' % (len(lines), len(s_comments))
        raise DPInternalError(msg) 
 
    out = ""
    for i, (line, comment) in enumerate(zip(lines, s_comments)):
        lineno = i + 1
        if ignore_line(lineno):
            continue
        else:
            
            if '#' in line:
                w = line.index('#')
                before = line[:w]
                comment = line[w:]
                
                if comment.startswith(unparsable_marker):
                    unparsable = comment[len(unparsable_marker):]
                    linec = before + '<span class="unparsable">%s</span>' % sanitize_comment(unparsable)
                else:
                    linec = before + '<span class="comment">%s</span>' % sanitize_comment(comment)
            else:
                
                linec = line   
                
            
            if add_line_gutter:
                out += "<span class='line-gutter'>%2d</span>" % lineno
                out += "<span class='line-content'>" + linec + "</span>"
            else:
                out += linec

        
 
        
            if i != len(lines) - 1:
                out += '\n'
    
    if MCDPConstants.test_insist_correct_html_from_ast_to_html:
        from xml.etree import ElementTree as ET
        ET.fromstring(out)
    
    frag = ""

    if encapsulate_in_precode:
        frag += '<pre><code>'
        frag += out
        frag += '</code></pre>'
    else:
        frag += out

    return frag 
Esempio n. 12
0
def print_html_inner(x):
    assert isnamedtuplewhere(x), x
    
    CDP = CDPLanguage 
    if isinstance(x, CDP.Placeholder):
        orig0 = x.where.string[x.where.character:x.where.character_end]
        check_isinstance(x.label, str)
        if x.label == '...': # special case
            transformed = '…' 
        else:
            transformed = '<span class="PlaceholderLabel">⟨%s⟩</span>' % x.label
        
        yield Snippet(op=x, orig=orig0, a=x.where.character, b=x.where.character_end,
                  transformed=transformed)
        return

    def iterate_check_order(it):
        last = 0
        cur = []
        for i in it:
            op, o, a, b, _ = i
            cur.append('%s from %d -> %d: %s -> %r' % (type(x).__name__,
                                                       a, b, type(op).__name__, o))

            if not a >= last: # pragma: no cover
                raise_desc(ValueError, 'bad order', cur="\n".join(cur))
            if not b >= a: # pragma: no cover
                raise_desc(ValueError, 'bad ordering', cur="\n".join(cur))
            last = b
            yield i

    subs = list(iterate_check_order(order_contributions(iterate2(x))))

    if is_a_special_list(x):
        for _ in subs:
            yield _
        return

    cur = x.where.character
    out = ""
    for _op, _orig, a, b, transformed in subs:
        if a > cur:
            out += sanitize(x.where.string[cur:a])
        out += transformed
        cur = b

    if cur != x.where.character_end:
        out += sanitize(x.where.string[cur:x.where.character_end])

    orig0 = x.where.string[x.where.character:x.where.character_end]

    klass = type(x).__name__
    # special case: OpenBraceKeyword
    if out == '<':
        out = '&lt;'
    if out == '>':
        out = '&gt;'
    transformed0 = ("<span class='%s' where_character='%d' where_character_end='%s'>%s</span>" 
                    % (klass, x.where.character, x.where.character_end, out))
    yield Snippet(op=x, orig=orig0, a=x.where.character, b=x.where.character_end,
                  transformed=transformed0)
Esempio n. 13
0
def iterate2(x):
    for  _, op in iterate_notwhere(x):
        if isnamedtuplewhere(op):
            for m in print_html_inner(op):
                yield m