コード例 #1
0
ファイル: parser.py プロジェクト: stx-lang/python-stx
def parse_list(ctx: CTX, mark: str, location: Location,
               content: Content) -> int:
    if mark == ordered_item_block_mark:
        ordered = True
    elif mark == unordered_item_block_mark:
        ordered = False
    else:
        return PASS

    list_block = ctx.composer.get_last_component()

    if not isinstance(list_block, ListBlock):
        list_block = ListBlock(location, ordered)

        ctx.composer.add(list_block)

    # TODO is this ok?
    skip_void(content)

    indentation = content.column

    list_item = capture_component(ctx, indentation, True)

    list_block.items.append(list_item)

    return CONSUMED
コード例 #2
0
ファイル: parser.py プロジェクト: stx-lang/python-stx
def parse_section(ctx: CTX, mark: str, location: Location, content: Content,
                  before_mark_indentation: int) -> int:
    if mark not in heading_block_marks:
        return PASS

    parent_section = ctx.get_parent_section()
    section_level = section_levels[mark]

    if (parent_section is not None and parent_section.level >= section_level):
        content.go_back(location)
        return EXIT

    # TODO is this ok?
    skip_void(content)

    after_mark_indentation = content.column

    section = Section(location, section_level)

    ctx.composer.add(section)

    ctx.section_stack.append(section)

    section.heading = capture_component(ctx, after_mark_indentation, True)
    section.content = capture_component(ctx, before_mark_indentation, True)

    ctx.section_stack.pop()

    return CONSUMED
コード例 #3
0
ファイル: parser.py プロジェクト: stx-lang/python-stx
def parse_caption(ctx: CTX, mark: str, content: Content) -> int:
    if mark == pre_caption_block_mark:
        pre_mode = True
    elif mark == post_caption_block_mark:
        pre_mode = False
    else:
        return PASS

    # TODO is this ok?
    skip_void(content)

    caption = capture_component(ctx, content.column, True)

    if pre_mode:
        ctx.composer.push_pre_caption(caption)
    else:
        ctx.composer.push_post_caption(caption)

    return CONSUMED
コード例 #4
0
ファイル: parser.py プロジェクト: stx-lang/python-stx
def parse_inline_function(ctx: CTX, mark: str, location: Location,
                          content: Content) -> int:
    if mark != function_begin_mark:
        return PASS

    skip_void(content)

    function_location = content.get_location()

    function = parse_entry(content)

    skip_void(content)

    if not content.pull(function_end_mark):
        raise StxError(f'Expected mark: {function_end_mark}')

    ctx.composer.add(
        FunctionCall(function_location,
                     inline=True,
                     key=function.name,
                     options=function.value))

    return CONSUMED
コード例 #5
0
ファイル: parser.py プロジェクト: stx-lang/python-stx
def parse_inline_container(ctx: CTX, mark: str, location: Location,
                           content: Content, indentation: int) -> int:
    if mark != container_begin_mark:
        return PASS

    with ctx.using_stop_mark(container_end_mark):
        # It uses the original indentation
        #   so the paragraph can be continued.
        contents = parse_inline(ctx, content.get_location(), content,
                                indentation)

    if not content.pull(container_end_mark):
        raise StxError(f'Expected mark: {container_end_mark}')

    if not content.pull(function_begin_mark):
        raise StxError(f'Expected mark: {function_begin_mark}')

    skip_void(content)

    function_location = content.get_location()

    function = parse_entry(content)

    skip_void(content)

    if not content.pull(function_end_mark):
        raise StxError(f'Expected mark: {function_end_mark}')

    ctx.composer.add(
        FunctionCall(function_location,
                     inline=True,
                     key=function.name,
                     options=function.value,
                     argument=Composite(location, contents)))

    return CONSUMED
コード例 #6
0
ファイル: parser.py プロジェクト: stx-lang/python-stx
def parse_table(ctx: CTX, mark: str, location: Location,
                content: Content) -> int:
    if mark == header_row_block_mark:
        header = True
        reuse_row = False
    elif mark == normal_row_block_mark:
        header = False
        reuse_row = False
    elif mark == cell_block_mark:
        header = False
        reuse_row = True
    else:
        return PASS

    table = ctx.composer.get_last_component()

    if not isinstance(table, Table):
        table = Table(location)

        ctx.composer.add(table)

    row = table.get_last_row() if reuse_row else None

    if row is None:
        row = TableRow(location, header)

        table.rows.append(row)

    # TODO is this ok?
    skip_void(content)

    indentation0 = content.column
    indentation = indentation0

    while True:
        with ctx.using_stop_mark(cell_block_mark):
            cell = capture_component(ctx, indentation, True)

            row.cells.append(cell)

        if not ctx.reader.active():
            break

        content = ctx.reader.get_content()

        loc0 = content.get_location()

        # Consume indentation when it is the beginning of the line
        if content.column == 0:
            if content.read_spaces(indentation0) < indentation0:
                content.go_back(loc0)
                break

        if content.peek() == cell_block_mark:
            content.move_next()
            content.read_spaces()

            indentation = content.column
        else:
            break

    return CONSUMED