Last active
December 21, 2015 16:28
-
-
Save niklaskorz/6333446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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