Search code examples
pythonpython-3.xparsingpyparsing

Make python consider a quoted string as a block


I'm making a grammar using Pyparsing and want to make it parse all the inserts, even those having a date. I don't get how to make the quotedStrings an entire block.

ident          = Word( alphas, alphanums + "_$" ).setName("identifier")
number         = Word(nums)

quotedString = "'" + delimitedList(number | ident , ".", combine=True) + "'"
quotedStringList = Group(delimitedList(quotedString))

insert_statement <<= (INSERT + INTO + tableNameList + VALUES + delimitedList(openingparenthese + quotedStringList + closingparenthese) + Optional(endLine))

data = "INSERT INTO test VALUES('2008-07-28 00:00:05', 'a');"
print (data, "\n", insert_statement.parseString( data ))

I expect to get something like :

['insert', 'into', ['test'], 'values', '(', ["'", '2008-07-28 00:00:05', "'", "'", 'a', "'"], ')', ';']

But I only get :

Expected "'"

Solution

  • I don't think this mistake will happen to many people, but the easiest way to have quoted Strings...is to use the one given by pyparser from pyparser import quotedString, ... as pp instead of trying to create your own

    Thanks for the replies anyway, helped me for other problems