QPL Parsing, uses a custom grammar implemented with LARK, a modern general-purpose parsing library for Python, with it we can parse the query using the grammar and a LALR(1) parser.
Parsing a Query
Parsing a query with QPL is as simple as it gets, we start by importing the QPLOptions and the QPLParser
Code Block |
---|
|
from pyqpl.qpl import QPLOptions
from pyqpl.parser import QPLParser |
QPLOptions is the one which will said the grammar to use and the rules to apply,
Code Block |
---|
|
options = QPLOptions(fields='content', implicit_operator='or', wildcard=False) |
- The
implicit_operator
option, accepts either 'or' or 'and', this option select grammars with a slight variation between them, to set the operator (or / and) as the implicit operation. - The
wildcard
options, accepts a boolean, in case of True, a new rule to identify wildcards will be added
One the options are set, create a parser and pass the options as argument
Code Block |
---|
|
parser = QPLParser(options=options) |
With the parser created, parse the query with the line bellow
Code Block |
---|
|
qpl_query = parser.parse_query('This is PyQPL') |
The qpl_query will be an object of type Operator