Ein einfacher Makroprozessor in Python (Ein einfacher Makroprozessor in Python), article, Seite 724101
https://www.purl.org/stefan_ram/pub/makroprozessor_python (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
Python-Kurs

Ein einfacher Makroprozessor in Python 

main.py
techyle = \
{ 'uae': { 'type': 'markup', 'meaning': 'emphasized' }}

techyle_html = \
{ 'uae': { 'pre': '<em>', 'post': '</em>' }}

source = """\
This is `uae:text with `uae:markup``. Is this `uae:cool` or what?
"""

position = 0;

def at( n ):
if n >= len( source ): return chr( 0 )
else: return source[ n ]

def getnextch():
global position
if position >= len( source ): return chr( 0 )
else: position += 1; return source[ position - 1 ]

def emit( text ):
print( end=text )

def check_open():
"""is this an opening of markup?"""
global position
# print( '[', at( position ), at( position + 3 ), ']' )
if( at( position )== 'u' and at( position + 3 )== ':' ):
result = source[ position:position+3 ]
position += 4
return result
else: return None

def check_text():
"""is this a text reference?"""
# not yet implemented!
return None

def pretext( code ):
return techyle_html[ code ][ 'pre' ];

def posttext( code ):
return techyle_html[ code ][ 'post' ];

stack = []

def push( text ):
stack.append( text )

def pop():
return stack.pop()

looping = True
while looping:
ch = getnextch()
if ch == chr( 0 ): break
if ch != '`': emit( ch )
else:
type = check_open()
if type:
text = check_text()
if text:
print( "TEXT REFERENCE NOT IMPLEMENTED." )
else:
emit( pretext( type ))
push( posttext( type ))
else:
emit( pop() )
Protokoll
  File "main.py", line 2

^
SyntaxError: invalid syntax
Notizen

This is text with `uae:markup`.

The tag »uae« means "emphasized". It is applied to the word

"markup" using the pattern: `tag:...`. All tags have three

letters and start with »u«.

Now, I will answer a few questions:

Q: How do you include a literal backquote in the text?

A: Probably there will be an extension later with

something like `utx:gravis` for this. This will create

one »`« in the output. So if you like to manually markup text

with a lot of backquotes, this language is not for you.

Q: Are the patterns intended to be nestable?

A: Yes, but this was not tested yet.

Q: How do you diagnose errors?

A: The first version does not emit diagnostics or

recovery for errors in the source code. If there are

mark-up errors in the source, it has undefined behavior.

Q: What do you mean by "implementation" of a markup

language?

A: A translator that translates the language to at

least one other markup language. The first implementation

translates to HTML. So, for the input:

This is text with `uae:markup`. Is this cool or what?

it outputs:

This is text with <em>markup</em>. Is this cool or what?

And here is the source code (written in 35 minutes):

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724101 stefan_ram:724101 Ein einfacher Makroprozessor in Python Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724101, slrprddef724101, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/makroprozessor_python