Last active
August 29, 2015 14:05
-
-
Save RemyPorter/5f3df5a905b3ddcd578c to your computer and use it in GitHub Desktop.
Text Bundler DSL - parsing this file never terminates
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 pyparsing import * | |
from collections import namedtuple | |
Entry = namedtuple("Entry", "description path section") | |
__currentsection = None | |
def __section(section): | |
global __currentsection | |
__currentsection = section.Section | |
SECTION = Combine(Suppress("#") + restOfLine("Section")).setParseAction(__section) | |
ENTRY = Combine(restOfLine("Path") + Optional(Suppress(",") + quotedString("Description")))("entry").setParseAction( | |
lambda x: Entry(x.entry.Description.replace('"',""), x.entry.Path, __currentsection)) | |
LINE = SECTION | ENTRY | |
BUNDLE = ZeroOrMore(LINE) |
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
import grammar | |
sectiontest = grammar.BUNDLE.parseString("#Main") #works | |
entrytest = grammar.BUNDLE.parseString("foo.txt,'Caption'") #works | |
linetest = grammar.BUNDLE.parseString("#Main") #works | |
otherlinetest = grammar.BUNDLE.parseString("foo.txt,'Caption'") #also works | |
data = grammar.BUNDLE.parseFile("sample_file") #this line never terminates |
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
#Main | |
one.txt | |
two.txt,"Caption" | |
#Second | |
three.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, look at that, I'm trying to consume "restOfLine" to capture the Path (an earlier revision had path at the end of the line, not the start). Oops! This is a wonderful example of a PEBKAC.