Skip to content

Instantly share code, notes, and snippets.

@leminhtr
Created February 11, 2019 15:25
Show Gist options
  • Save leminhtr/835525e0b18aa3b2458bbd69f7460b35 to your computer and use it in GitHub Desktop.
Save leminhtr/835525e0b18aa3b2458bbd69f7460b35 to your computer and use it in GitHub Desktop.
topic_map_xml_grammar = r"""
start: topic_map_xml
topic_map_xml: XML_DEF topic_map
XML_DEF: "<?xml version="1.0" encoding="UTF-8"?>"
topic_map: OPENTOPICMAPTAG PREDEFINEDTOPIC topic* PREDEFINEDASSOCIATION association* CLOSETOPICMAPTAG
OPENTOPICMAPTAG: "{{opentopicmaptag}}"
PREDEFINEDTOPIC: "{{predefinedtopic}}"
topic: open_topic_tag subject_locator_tag subject_identifier_tag instance_of_tag name_tag occurrence_tag* CLOSETOPICTAG
open_topic_tag: TOPICTAG id_attrib ">"
TOPICTAG: "<topic "
id_attrib: "id=" "\"" topic_id_value "\""
topic_id_value: "topic" TOPIC_ID
TOPIC_ID: "{{topic_id}}"
subject_locator_tag: "<subjectLocator " href_locator_attrib CLOSETAG
CLOSETAG: "/>"
href_locator_attrib: "href=" "\"" HREFLOCATORVALUE "\""
HREFLOCATORVALUE: "{{hreflocatorvalue}}"
subject_identifier_tag: "<subjectIdentifier " href_identifier_attrib CLOSETAG
href_identifier_attrib: HREFATTRIB "\"" HREFIDENTIFIERVALUE "\""
HREFATTRIB: "href=""
HREFIDENTIFIERVALUE: "{{hrefidentifiervalue}}"
instance_of_tag: OPENINSTANCEOFTAG topicRef_instance_of_tag CLOSEINSTANCEOFTAG
OPENINSTANCEOFTAG: "<instanceOf>"
topicRef_instance_of_tag: TOPICREFTAG href_instance_of_tag_attrib CLOSETAG
TOPICREFTAG: "<topicRef "
href_instance_of_tag_attrib: HREFATTRIB "#" TOPICINSTANCEID
TOPICINSTANCEID: "{{topicinstanceid}}"
CLOSEINSTANCEOFTAG: "</instanceOf>"
name_tag: OPENNAMETAG name_value_tag name_variant_tag? CLOSENAMETAG
OPENNAMETAG: "<name>"
name_value_tag: OPENVALUETAG NAMEVALUE CLOSEVALUETAG
OPENVALUETAG: "<value>"
NAMEVALUE: "{{namevalue}}"
name_variant_tag: OPENVARIANTTAG scope_variant_tag resourceData_variant_tag CLOSEVARIANTTAG
OPENVARIANTTAG: "<variant>"
scope_variant_tag: OPENSCOPETAG topicRef_variant_tag+ CLOSESCOPETAG
OPENSCOPETAG: "<scope>"
topicRef_variant_tag: TOPICREFTAG HREFATTRIB "#" TOPICVARIANTIDDEFAULT TOPICVARIANTID CLOSETAG
TOPICVARIANTIDDEFAULT: "{{topicvariantiddefault}}"
TOPICVARIANTID: "{{topicvariantid}}""
CLOSESCOPETAG: "</scope>"
resourceData_variant_tag: OPENRESOURCEDATATAG RESOURCEDATAVARIANT CLOSERESOURCEDATATAG
OPENRESOURCEDATATAG: "<resourceData>"
resOURCEDATAVARIANT: "{{resourcedatavariant}}""
CLOSERESOURCEDATATAG: "</resourceData>"
CLOSEVARIANTTAG: "</variant>"
CLOSEVALUETAG: "</value>"
CLOSENAMETAG: "</name>"
occurrence_tag: OPENOCCURRENCETAG type_occurrence_tag scope_tag resourceData_tag CLOSEOCCURRENCETAG
OPENOCCURRENCETAG: "<occurrence>"
type_occurrence_tag: OPENTYPETAG topicRef_type_occurrence_tag CLOSETYPETAG
OPENTYPETAG: "<type>"
topicRef_type_occurrence_tag: TOPICREFTAG HREFATTRIB "#" TOPICTYPEOCCURRENCEID CLOSETAG
TOPICTYPEOCCURRENCEID: "{{topictypeoccurrenceid}}""
CLOSETYPETAG: "</type>"
scope_tag: OPENSCOPETAG topicRef_scope_tag CLOSESCOPETAG
topicRef_scope_tag: TOPICREFTAG HREFATTRIB "#" TOPICSCOPEID CLOSETAG
TOPICSCOPEID: "{{topicscopeid}}""
resourceData_tag: OPENRESOURCEDATATAG RESOURCEDATA CLOSERESOURCEDATATAG
RESOURCEDATA: "{{resourcedata}}",
CLOSEOCCURRENCETAG: "</occurrence>"
CLOSETOPICTAG: "</topic>"
PREDEFINEDASSOCIATION: "{{predefinedassociation}}"
association: OPENASSOCIATIONTAG type_assoc_tag role_assoc_prereq_tag role_assoc_subsid_tag CLOSEASSOCIATIONTAG
OPENASSOCIATIONTAG: "<association>"
type_assoc_tag: OPENTYPETAG topicRef_type_assoc_tag CLOSETYPETAG
topicRef_type_assoc_tag: TOPICREFTAG HREFATTRIB "#" TOPICREFTYPEASSOCID CLOSETAG
TOPICREFTYPEASSOCID: "{{topicreftypeassocid}}""
role_assoc_prereq_tag: OPENROLETAG role_assoc_prereq_type_tag topicRef_prereq_tag CLOSEROLETAG
OPENROLETAG: "<role>"
role_assoc_prereq_type_tag: OPENTYPETAG topicRef_role_prereq_tag CLOSETYPETAG
topicRef_role_prereq_tag: TOPICREFTAG HREFATTRIB "#" TOPICREFROLEPREREQID CLOSETAG
TOPICREFROLEPREREQID: "{{topicrefroleprereqid}}""
topicRef_prereq_tag: TOPICREFTAG HREFATTRIB "#" TOPICREFPREREQID CLOSETAG
TOPICREFPREREQID: "{{topicrefprereqid}}""
CLOSEROLETAG: "</role>"
role_assoc_subsid_tag: OPENROLETAG role_assoc_subsid_type_tag topicRef_subsid_tag CLOSEROLETAG
role_assoc_subsid_type_tag: OPENTYPETAG topicRef_role_subsid_tag CLOSETYPETAG
topicRef_role_subsid_tag: TOPICREFTAG HREFATTRIB "#" TOPICREFROLESUBSIDID CLOSETAG
TOPICREFROLESUBSIDID: "{{topicrefrolesubsidid}}""
topicRef_subsid_tag: TOPICREFTAG HREFATTRIB "#" TOPICREFSUBSIDID CLOSETAG
TOPICREFSUBSIDID: "{{topicrefsubsidid}}""
CLOSEASSOCIATIONTAG: "</association>"
CLOSETOPICMAPTAG: "</topicMap>"
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS
%import common.NEWLINE
%ignore WS
""".format(opentopicmaptag = '"<topicMap xmlns="http://www.topicmaps.org/xtm/" version="2.0">"',
predefinedtopic = XML_FIXED_PART,
topic_id=get_topic_id(math.random()),
hreflocatorvalue=get_topic_uri(topic_name),
hrefidentifiervalue=get_local_href_identifier_uri(list_of_id_already_used),
topicinstanceid=get_topic_instance_id(topic_name),
namevalue=get_topic_name_value(),
topicvariantiddefault=get_topic_variant_id_default(),
topicvariantid=get_topic_variant_id(isEnglish),
resourcedatavariant=get_resourceData_variant(topic_name),
topictypeoccurrenceid=get_topic_type_occurrence_id(isDescription),
topicscopeid=get_topic_scope_id(isEnglish),
resourcedata=get_topic_description(topic_name),
predefinedassociation= XML_ASSOCIATION_FIXED_PART,
topicreftypeassocid=get_topicRef_type_assoc(isReq),
topicrefroleprereqid=get_topicRef_role_prereq_id(prereq_id='topic74'),
topicrefprereqid=get_topicRef_id(topic1),
topicrefrolesubsidid=get_topicRef_role_subsid_id(prereq_id='topic64'),
topicrefsubsidid=get_topicRef_id(topic2)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment