Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. from lark import Lark
  2.  
  3. tree_grammar = r"""
  4. IDENTIFIER: ( LETTER | DIGIT | ".") +
  5. string : ( WORD | expr | logic ) *
  6. expr : "${" TARGETS ":" IDENTIFIER "}"
  7. simple : expr OPERATIONS SIGNED_NUMBER
  8.  
  9. composed : "(" (expr | OPERATIONS | simple | composed)+ ")" //logical operations which can be composed from 'expr', 'OPERATIONS' ,'simple' ,'composed' expressions, which should occure at least once
  10. logic : "$(" (expr | OPERATIONS | simple | composed)+ ")" //logical operations which can be composed from 'expr', 'OPERATIONS' ,'simple' ,'composed' expressions, which should occure at least once
  11. CHAR : /[^$"\n\\]/
  12. WORD : CHAR +
  13.  
  14. OPERATIONS : ("GT" | "LT" | "EQ" | "LTE" | "GTE" | "AND" | "OR" | "NOT") //types of operations used, instead of standart '<',= etc
  15. TARGETS : ("context" | "query" | "user" ) //predefined list of prefixes
  16.  
  17. %import common.SIGNED_NUMBER
  18. %import common.CNAME -> NAME
  19. %import common.WS
  20. %import common.LETTER
  21. %import common.DIGIT
  22. %ignore WS
  23.  
  24. """
  25.  
  26. parser_lark = Lark(tree_grammar, start='string')
  27.  
  28. 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}'
  29.  
  30.  
  31.  
  32. def test():
  33. parsed_tree = parser_lark.parse(test_tree)
  34. print(parsed_tree)
  35. print(parsed_tree.pretty())
  36.  
  37.  
  38. if __name__ == '__main__':
  39. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement