Skip to content

Commit

Permalink
Fix #323: allow identifiers on EDS native codec
Browse files Browse the repository at this point in the history
  • Loading branch information
goodmami committed Dec 17, 2020
1 parent 4bb8571 commit 933077b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

* `delphin.eds.EDSWarning`
* `delphin.codecs.eds` now reads and writes identifiers ([#323])

### Fixed

Expand Down Expand Up @@ -1462,3 +1463,4 @@ information about changes, except for
[#306]: https://github.com/delph-in/pydelphin/issues/306
[#316]: https://github.com/delph-in/pydelphin/issues/316
[#319]: https://github.com/delph-in/pydelphin/issues/319
[#323]: https://github.com/delph-in/pydelphin/issues/323
14 changes: 11 additions & 3 deletions delphin/codecs/eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def encode(e, properties=True, lnk=True, show_status=False, indent=False):

_EDSLexer = Lexer(
tokens=[
(r'\#([^\s\{]+)\s*(?=\{|$)', 'IDENTIFIER'),
(r'\{', 'LBRACE:{'),
(r'\}', 'RBRACE:}'),
(r'\((?:cyclic *)?(?:fragmented)?\)', 'GRAPHSTATUS'),
Expand All @@ -153,6 +154,7 @@ def encode(e, properties=True, lnk=True, show_status=False, indent=False):
],
error_class=EDSSyntaxError)

IDENTIFIER = _EDSLexer.tokentypes.IDENTIFIER
LBRACE = _EDSLexer.tokentypes.LBRACE
RBRACE = _EDSLexer.tokentypes.RBRACE
GRAPHSTATUS = _EDSLexer.tokentypes.GRAPHSTATUS
Expand All @@ -176,6 +178,7 @@ def _decode(lineiter):


def _decode_eds(lexer):
identifier = lexer.accept_type(IDENTIFIER)
lexer.expect_type(LBRACE)

# after the LBRACE, the following patterns determine the top:
Expand Down Expand Up @@ -204,7 +207,7 @@ def _decode_eds(lexer):
start, _ = lexer.expect_type(SYMBOL, COLON)
nodes.append(_decode_node(start, lexer))
lexer.expect_type(RBRACE)
return EDS(top=top, nodes=nodes)
return EDS(top=top, nodes=nodes, identifier=identifier)


def _decode_node(start, lexer):
Expand Down Expand Up @@ -249,9 +252,14 @@ def _decode_edges(start, lexer):
# Encoding

def _encode_eds(e, properties, lnk, show_status, indent):
start = '{'
if e.identifier:
start = f'#{e.identifier}' + ('\n' if indent else ' ') + '{'
end = '\n}' if indent else '}'

# do something predictable for empty EDS
if len(e.nodes) == 0:
return '{\n}' if indent else '{}'
return start + end

delim = '\n' if indent else ' '
connected = ' ' if indent else ''
Expand Down Expand Up @@ -279,7 +287,7 @@ def _encode_eds(e, properties, lnk, show_status, indent):
membership = connected if node.id in nidgrp else disconnected
parts.append(membership + _encode_node(node, properties, lnk))

return '{' + delim.join(parts) + ('\n}' if indent else '}')
return start + delim.join(parts) + end


def _encode_node(node, properties, lnk):
Expand Down
31 changes: 31 additions & 0 deletions tests/codecs/edsnative_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,23 @@ def test_decode_no_top():
assert len(e.nodes) == 3


def test_decode_identifier():
e = edsnative.decode(
'#123\n'
'{e2:\n'
' e2:_rain_v_1<3:9>{e SF prop, TENSE pres}[]\n'
'}'
)
assert e.identifier == '123'
e = edsnative.decode(
'#123 {e2: e2:_rain_v_1<3:9>{e SF prop, TENSE pres}[] }'
)
assert e.identifier == '123'


def test_encode(dogs_bark_from_mrs):
assert edsnative.encode(EDS()) == '{}'
assert edsnative.encode(EDS(), indent=True) == '{\n}'
d = EDS(**dogs_bark_from_mrs)
assert edsnative.encode(d) == (
'{e2: e2:_bark_v_1{e}[ARG1 x4] _1:udef_q[BV x4] x4:_dog_n_1{x}[]}')
Expand All @@ -91,3 +107,18 @@ def test_encode_no_top(dogs_bark_from_mrs):
' _1:udef_q[BV x4]\n'
' x4:_dog_n_1{x}[]\n'
'}')


def test_encode_identifier(dogs_bark_from_mrs):
assert edsnative.encode(EDS(identifier='123')) == '#123 {}'
d = EDS(**dogs_bark_from_mrs)
d.identifier = '123'
assert edsnative.encode(d) == (
'#123 {e2: e2:_bark_v_1{e}[ARG1 x4] _1:udef_q[BV x4] x4:_dog_n_1{x}[]}')
assert edsnative.encode(d, indent=True) == (
'#123\n'
'{e2:\n'
' e2:_bark_v_1{e}[ARG1 x4]\n'
' _1:udef_q[BV x4]\n'
' x4:_dog_n_1{x}[]\n'
'}')

0 comments on commit 933077b

Please sign in to comment.