Skip to content

Commit

Permalink
Update serialization logic & fix tests (WiP)
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Nov 7, 2024
1 parent 9f75e7f commit 1973bef
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ COMPOUND_OPERATOR: "×" | "%" | "&" | "+" | "."

grapheme: grapheme_name sub_index modifiers flags
grapheme_name: grapheme_name_part
grapheme_name_part: VALUE_CHARACTER+ | LOGOGRAM_CHARACTER+
grapheme_name_part: VALUE_CHARACTER+
| LOGOGRAM_CHARACTER+
| number_name_head number_name_part*

sub_index: [SUB_INDEX]
SUB_INDEX: NUMERIC_SUB_INDEX | ""
Expand Down
3 changes: 3 additions & 0 deletions ebl/transliteration/domain/sign_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ def of_name(
class Grapheme(AbstractSign):
name: SignName

def __str__(self) -> str:
return self.value

@property
def value(self) -> str:
modifiers = "".join(self.modifiers)
Expand Down
34 changes: 30 additions & 4 deletions ebl/transliteration/domain/signs_transformer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from lark import Token, Tree
from lark.visitors import Transformer, v_args

from ebl.transliteration.domain import atf
from ebl.transliteration.domain.atf import sub_index_to_int
from ebl.transliteration.domain.atf import sub_index_to_int, to_sub_index
from ebl.transliteration.domain.egyptian_metrical_feet_separator_token import (
EgyptianMetricalFeetSeparator,
)
Expand All @@ -17,6 +18,18 @@
from ebl.transliteration.domain.unknown_sign_tokens import UnclearSign, UnidentifiedSign


def tree_to_string(tree: Tree) -> str:
_children = []
for part in tree.scan_values(lambda x: x):
if hasattr(part, "value") :
_children.append(part.value)
elif isinstance(part, Tree):
_children.append(tree_to_string(part))
else:
_children.append(str(part))
return "".join(_children)


class SignTransformer(Transformer):
@v_args(inline=True)
def ebl_atf_text_line__unidentified_sign(self, flags):
Expand Down Expand Up @@ -85,8 +98,21 @@ def ebl_atf_text_line__flags(self, tokens):
return tuple(map(atf.Flag, tokens))

@v_args(inline=True)
def ebl_atf_text_line__grapheme(self, name, modifiers, flags):
return Grapheme.of(name.value, modifiers, flags)
def ebl_atf_text_line__grapheme(self, name, sub_index, modifiers, flags):
_name = tree_to_string(name)
_sub_index = to_sub_index(sub_index) if sub_index and sub_index != 1 else ""
return Grapheme.of(_name + _sub_index, modifiers, flags)

def ebl_atf_text_line__sub_compound(self, children):
return Tree("ebl_atf_text_line__sub_compound", ["(", *children, ")"])

def ebl_atf_text_line__compound_grapheme(self, children):
return CompoundGrapheme.of([part.value for part in children])
_children = []
for part in children:
if isinstance(part, Token):
_children.append(part.value)
elif isinstance(part, Tree):
_children.append(tree_to_string(part))
else:
_children.append(str(part))
return CompoundGrapheme.of(_children)

0 comments on commit 1973bef

Please sign in to comment.