Skip to content

Instantly share code, notes, and snippets.

@RemyPorter
Last active August 29, 2015 14:05
Show Gist options
  • Save RemyPorter/5f3df5a905b3ddcd578c to your computer and use it in GitHub Desktop.
Save RemyPorter/5f3df5a905b3ddcd578c to your computer and use it in GitHub Desktop.
Text Bundler DSL - parsing this file never terminates
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)
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
#Main
one.txt
two.txt,"Caption"
#Second
three.txt
@RemyPorter
Copy link
Author

The issue I'm having here is that this file, which does fit the grammar, doesn't terminate when I parse it. I can parse the individual token types just fine, but parsing ZeroOrMore(LINE) breaks. I don't understand why.

@RemyPorter
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment