Skip to content

Instantly share code, notes, and snippets.

@niklaskorz
Last active December 21, 2015 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklaskorz/6333446 to your computer and use it in GitHub Desktop.
Save niklaskorz/6333446 to your computer and use it in GitHub Desktop.
from plex import *
from washr.parser.types import Variable, BlockStart, BlockEnd
from cStringIO import StringIO
left_edge = Str("{")
right_edge = Str("}")
block_token = NoCase(Str("block:"))
end_token = Str("/")
identifier = Rep1(Range("azAZ09") | Str("-"))
variable = left_edge + identifier + right_edge
block_ident = block_token + identifier
block_begin = left_edge + block_ident + right_edge
block_end = left_edge + end_token + block_ident + right_edge
lex = Lexicon([
(block_begin, lambda s, t: BlockStart(t[1:-1])),
(block_end, lambda s, t: BlockEnd(t[2:-1])),
(variable, lambda s, t: Variable(t[1:-1], '')),
(AnyChar, TEXT),
])
def parse(stream):
if isinstance(stream, str) or isinstance(stream, unicode):
stream = StringIO(stream)
scanner = Scanner(lex, stream)
while 1:
token = scanner.read()
if token[0] is None:
break
yield token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment