Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from lark import Lark
- tree_grammar = r"""
- IDENTIFIER: ( LETTER | DIGIT | ".") +
- string : ( WORD | expr | logic ) *
- expr : "${" TARGETS ":" IDENTIFIER "}"
- simple : expr OPERATIONS SIGNED_NUMBER
- composed : "(" (expr | OPERATIONS | simple | composed)+ ")" //logical operations which can be composed from 'expr', 'OPERATIONS' ,'simple' ,'composed' expressions, which should occure at least once
- logic : "$(" (expr | OPERATIONS | simple | composed)+ ")" //logical operations which can be composed from 'expr', 'OPERATIONS' ,'simple' ,'composed' expressions, which should occure at least once
- CHAR : /[^$"\n\\]/
- WORD : CHAR +
- OPERATIONS : ("GT" | "LT" | "EQ" | "LTE" | "GTE" | "AND" | "OR" | "NOT") //types of operations used, instead of standart '<',= etc
- TARGETS : ("context" | "query" | "user" ) //predefined list of prefixes
- %import common.SIGNED_NUMBER
- %import common.CNAME -> NAME
- %import common.WS
- %import common.LETTER
- %import common.DIGIT
- %ignore WS
- """
- parser_lark = Lark(tree_grammar, start='string')
- test_tree = ' My name is $(${context:name} EQ 0) and ${context:test} or $( ${context:testthree3.sample} EQ ${context:testfour} EQ (${user:nestedone} LT ${context:mail}) GT ${context:test} LT ${context:testtwo} EQ (${context:subscriber} GT ${context:user} LT (${context:subscriber} GT ${context:user} AND -1.4 OR ${context:subscribertestwo} )) GT ${user:.world} ) maybe ${context:mail}'
- def test():
- parsed_tree = parser_lark.parse(test_tree)
- print(parsed_tree)
- print(parsed_tree.pretty())
- if __name__ == '__main__':
- test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement