For a personal project, i need to write a simple lexer in order to treat user input commands. They can contain four patterns and brackets :
- operators :
.,+,-or% - names ([a-zA-Z0-9]) : for instance
foobar,fooZorbar2 - filters ([a-z]{1}"[some text]") : for instance
a"hello world",p"escaped \"string\"" - other filters ([a-z]{1}~"[some text]") : for instance
a~"hello world",p~"escaped \"string\""
I would like to split the command in order to have a hierarchical list (based on the brackets) that contains an entry for each operator, an entry for each name (a single string), and an entry for each filter (a dictionary, for instance {'a': 'hello world'} or {'p~': 'escaped "string"'}).
So here's an example of what i want to get with this input (spaces around operators are optional):
foo . bar - (a"hello \"world\"" % (b~"foo" + bar) . (fooZ.(bar2+(c"foo bar".d"bar foo"))))
[
'foo',
'.',
'bar',
'-',
[
{'a': 'hello "world"'},
'%',
[
{'b~': 'foo'},
'+',
'bar'
]
'.',
[
'fooZ',
'.',
[
'bar2',
'+',
[
{'c': 'foo bar'},
'.',
{'d': 'bar foo'}
]
]
]
]
]
I had a look at pyparsing, PLY, pyPEG, Yapps, etc. but they all seem hard to use and i don't know if i need this kind of tool since the grammar i need is not very complex. Thanks in advance for your suggestions!
pyparsingis dead easy to use. It would be my first port of call for a simple grammar like yours.