Added TokenToMatch SemanticType to avoid defaulting to matched_elements

This commit is contained in:
vicky 2022-06-07 19:15:30 +02:00
parent 388c92679d
commit 8e15a30b5d
No known key found for this signature in database
GPG Key ID: AA04A65592395BB8
2 changed files with 20 additions and 15 deletions

View File

@ -361,8 +361,8 @@ class ContextManager:
self.dict_lookahead = {
"list:ulist": ["list:ulist"],
"list:olist": ["list:olist"],
"table:row": ["table:separator", "table:row"], # TODO: Implement tables
"blockquotes": [], # TODO: Implement blockquotes
"table:row": ["table:separator", "table:row"], # future: Implement tables
"blockquotes": [], # future: Implement blockquotes
"linebreak": ["linebreak"],
}
self.contextualised = False
@ -470,7 +470,7 @@ class ContextManager:
index += lookahead_return[0]
line_number += lookahead_return[1]
# TODO: advanced lookahead for * logic
# future: advanced lookahead for * logic
# elif token.label in self.dict_advanced_lookahead:
elif isinstance(token, syntax.FinalSemanticType): # one-liners
@ -485,8 +485,12 @@ class ContextManager:
raise MismatchedContainerError(token)
# Starting token by default (can cause unintended behaviours on bad implementations)
else: # TODO: remove this default behaviour, error instead
elif isinstance(token, syntax.TokensToMatch):
self._add_matched(token.label, index)
else:
raise MismatchedContainerError(token)
except MismatchedContainerError as e:
error_mngr.log_exception(e, level="CRITICAL") # FUTURE: Be more specific.
index += 1

View File

@ -96,6 +96,12 @@ class SemanticType:
raise MismatchedContainerError(self)
class TokensToMatch:
"""Class used for verification in the context manager.
Classes inheriting this should be added to matched_elements."""
pass
class AddFirstElementToLabel(SemanticType):
def __init__(self, content):
super().__init__(content)
@ -183,32 +189,27 @@ class TextToken(EmptySemanticType):
# FUTURE: all one-line elements should to inherit FinalSemanticType
class EnhancedToken(ExplicitSemanticType):
"""Mother of all EtTokens"""
label = "text:enhanced"
class EtEmToken(ExplicitSemanticType):
class EtEmToken(ExplicitSemanticType, TokensToMatch):
"""*"""
label = "text:em"
class EtStrongToken(ExplicitSemanticType):
class EtStrongToken(ExplicitSemanticType, TokensToMatch):
"""**"""
label = "text:strong"
class EtUnderlineToken(ExplicitSemanticType):
class EtUnderlineToken(ExplicitSemanticType, TokensToMatch):
"""__"""
label = "text:underline"
class EtStrikethroughToken(ExplicitSemanticType):
class EtStrikethroughToken(ExplicitSemanticType, TokensToMatch):
"""~~"""
label = "text:strikethrough"
class EtCustomSpanToken(AddFirstElementToLabel, ExplicitSemanticType):
class EtCustomSpanToken(AddFirstElementToLabel, ExplicitSemanticType, TokensToMatch):
"""(#int)"""
label = "text:custom_span"
@ -239,7 +240,7 @@ class DisplayToken(FinalSemanticType):
label = "display"
class StructuralElementStartToken(AddFirstElementToLabel, OpenedSemanticType):
class StructuralElementStartToken(AddFirstElementToLabel, OpenedSemanticType, TokensToMatch):
"""<<div|article|section|aside"""
label = 'se:start'