DSL и многострочная документация с отступом от lark-parser - PullRequest
0 голосов
/ 13 февраля 2019

Я пытаюсь реализовать определение записи DSL, используя lark .Это основано на отступе, который делает вещи немного более сложными.

Lark - отличный инструмент, но я сталкиваюсь с некоторыми дификулами.

Вот фрагмент кода DSL, который я реализую:

record Order :
    """Order record documentation
    should have arbitrary size"""

    field1 Int
    field2 Datetime:
        """Attributes should also have
        multiline documentation"""

    field3 String "inline documentation also works"

и вотиспользуемая грамматика:

?start: (_NEWLINE | redorddef)*

simple_type: NAME

multiline_doc:  MULTILINE_STRING _NEWLINE
inline_doc: INLINE_STRING

?element_doc:  ":" _NEWLINE _INDENT multiline_doc _DEDENT | inline_doc

attribute_name: NAME
attribute_simple_type: attribute_name simple_type [element_doc] _NEWLINE
attributes: attribute_simple_type+
_recordbody: _NEWLINE _INDENT [multiline_doc] attributes _DEDENT
redorddef: "record" NAME ":" _recordbody



MULTILINE_STRING: /"""([^"\\]*(\\.[^"\\]*)*)"""/
INLINE_STRING: /"([^"\\]*(\\.[^"\\]*)*)"/

_WS_INLINE: (" "|/\t/)+
COMMENT: /#[^\n]*/
_NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+

%import common.CNAME -> NAME
%import common.INT

%ignore /[\t \f]+/  // WS
%ignore /\\[\t \f]*\r?\n/   // LINE_CONT
%ignore COMMENT
%declare _INDENT _DEDENT

Отлично работает для многострочных документов для определения записи, отлично подходит для определения встроенного атрибута, но не работает для многострочного документа атрибута.

код, который я использую для выполнения, выглядит так:

import sys
import pprint

from pathlib import Path

from lark import Lark, UnexpectedInput
from lark.indenter import Indenter

scheman_data_works = '''
record Order :
        """Order record documentation
        should have arbitrary size"""

        field1 Int
        # field2 Datetime:
        #   """Attributes should also have
        #   multiline documentation"""

        field3 String "inline documentation also works"
'''

scheman_data_wrong = '''
record Order :
        """Order record documentation
        should have arbitrary size"""

        field1 Int
        field2 Datetime:
                """Attributes should also have
                multiline documentation"""

        field3 String "inline documentation also works"
'''
grammar = r'''

?start: (_NEWLINE | redorddef)*

simple_type: NAME

multiline_doc:  MULTILINE_STRING _NEWLINE
inline_doc: INLINE_STRING

?element_doc:  ":" _NEWLINE _INDENT multiline_doc _DEDENT | inline_doc

attribute_name: NAME
attribute_simple_type: attribute_name simple_type [element_doc] _NEWLINE
attributes: attribute_simple_type+
_recordbody: _NEWLINE _INDENT [multiline_doc] attributes _DEDENT
redorddef: "record" NAME ":" _recordbody



MULTILINE_STRING: /"""([^"\\]*(\\.[^"\\]*)*)"""/
INLINE_STRING: /"([^"\\]*(\\.[^"\\]*)*)"/

_WS_INLINE: (" "|/\t/)+
COMMENT: /#[^\n]*/
_NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+

%import common.CNAME -> NAME
%import common.INT

%ignore /[\t \f]+/  // WS
%ignore /\\[\t \f]*\r?\n/   // LINE_CONT
%ignore COMMENT
%declare _INDENT _DEDENT

'''

class SchemanIndenter(Indenter):
    NL_type = '_NEWLINE'
    OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE']
    CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE']
    INDENT_type = '_INDENT'
    DEDENT_type = '_DEDENT'
    tab_len = 4

scheman_parser = Lark(grammar, parser='lalr', postlex=SchemanIndenter())
print(scheman_parser.parse(scheman_data_works).pretty())
print("\n\n")
print(scheman_parser.parse(scheman_data_wrong).pretty())

и результат:

redorddef
Order
multiline_doc """Order record documentation
        should have arbitrary size"""
attributes
    attribute_simple_type
    attribute_name    field1
    simple_type       Int
    attribute_simple_type
    attribute_name    field3
    simple_type       String
    inline_doc        "inline documentation also works"




Traceback (most recent call last):
File "schema_parser.py", line 83, in <module>
    print(scheman_parser.parse(scheman_data_wrong).pretty())
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/lark.py", line 228, in parse
    return self.parser.parse(text)
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/parser_frontends.py", line 38, in parse
    return self.parser.parse(token_stream, *[sps] if sps is not NotImplemented else [])
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/parsers/lalr_parser.py", line 68, in parse
    for token in stream:
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/indenter.py", line 31, in process
    for token in stream:
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/lexer.py", line 319, in lex
    for x in l.lex(stream, self.root_lexer.newline_types, self.root_lexer.ignore_types):
File "/Users/branquif/Dropbox/swf_projects/schema-manager/.venv/lib/python3.7/site-packages/lark/lexer.py", line 167, in lex
    raise UnexpectedCharacters(stream, line_ctr.char_pos, line_ctr.line, line_ctr.column, state=self.state)
lark.exceptions.UnexpectedCharacters: No terminal defined for 'f' at line 11 col 2

        field3 String "inline documentation also
^

Я понимаю, что отступы грамматики являются более сложными, и, похоже, жаворонок облегчает их, но не может найтиошибка здесь.

PS: я также пытался выполнить pyparsing, но безуспешно с этим же сценарием, и мне было бы трудно перейти на PLY, учитывая количество кода, которое, вероятно, понадобится.

Ответы [ 2 ]

0 голосов
/ 13 февраля 2019

Вы упомянули попытку pyparsing, в противном случае я бы оставил ваш вопрос в покое.

Синтаксический анализ не очень удобен для pyparsing, но он действительно прилагает усилия в этом случае, используя pyparsing.indentedBlock.В написании этого есть некоторый уровень бедствия, но это можно сделать.

import pyparsing as pp

COLON = pp.Suppress(':')
tpl_quoted_string = pp.QuotedString('"""', multiline=True) | pp.QuotedString("'''", multiline=True)
quoted_string = pp.ungroup(tpl_quoted_string | pp.quotedString().addParseAction(pp.removeQuotes))
RECORD = pp.Keyword("record")
ident = pp.pyparsing_common.identifier()

field_expr = (ident("name")
              + ident("type") + pp.Optional(COLON)
              + pp.Optional(quoted_string)("docstring"))

indent_stack = []
STACK_RESET = pp.Empty()
def reset_indent_stack(s, l, t):
    indent_stack[:] = [pp.col(l, s)]
STACK_RESET.addParseAction(reset_indent_stack)

record_expr = pp.Group(STACK_RESET
                       + RECORD - ident("name") + COLON + pp.Optional(quoted_string)("docstring")
                       + (pp.indentedBlock(field_expr, indent_stack))("fields"))

record_expr.ignore(pp.pythonStyleComment)

Если ваш пример записан в переменную 'sample', выполните:

print(record_expr.parseString(sample).dump())

и получите:

[['record', 'Order', 'Order record documentation\n    should have arbitrary size', [['field1', 'Int'], ['field2', 'Datetime', 'Attributes should also have\n        multiline documentation'], ['field3', 'String', 'inline documentation also works']]]]
[0]:
  ['record', 'Order', 'Order record documentation\n    should have arbitrary size', [['field1', 'Int'], ['field2', 'Datetime', 'Attributes should also have\n        multiline documentation'], ['field3', 'String', 'inline documentation also works']]]
  - docstring: 'Order record documentation\n    should have arbitrary size'
  - fields: [['field1', 'Int'], ['field2', 'Datetime', 'Attributes should also have\n        multiline documentation'], ['field3', 'String', 'inline documentation also works']]
    [0]:
      ['field1', 'Int']
      - name: 'field1'
      - type: 'Int'
    [1]:
      ['field2', 'Datetime', 'Attributes should also have\n        multiline documentation']
      - docstring: 'Attributes should also have\n        multiline documentation'
      - name: 'field2'
      - type: 'Datetime'
    [2]:
      ['field3', 'String', 'inline documentation also works']
      - docstring: 'inline documentation also works'
      - name: 'field3'
      - type: 'String'
  - name: 'Order'
0 голосов
/ 13 февраля 2019

Ошибка связана с неуместными терминалами _NEWLINE.Как правило, рекомендуется убедиться, что правила сбалансированы с точки зрения их роли в грамматике.Итак, вот как вы должны были определить element_doc:

?element_doc:  ":" _NEWLINE _INDENT multiline_doc _DEDENT
            | inline_doc _NEWLINE

Обратите внимание на добавленную новую строку, что означает, что независимо от того, какой из двух вариантов использует синтаксический анализатор, он заканчивается в аналогичном состоянии синтаксически (_DEDENT также соответствует символу новой строки).

Второе изменение, как следствие первого,:

attribute_simple_type: attribute_name simple_type (element_doc|_NEWLINE)

Поскольку element_doc уже обрабатывает символы новой строки, мы не должныпопробуйте сопоставить его дважды.

...