diff --git a/xplace/x.py b/xplace/x.py index c33a31e..ad6064a 100644 --- a/xplace/x.py +++ b/xplace/x.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 from lark import Lark, Transformer, ParseError, Tree +from random import shuffle parser = Lark(r''' DIGIT : /[0-9]/ @@ -12,9 +13,9 @@ parser = Lark(r''' | bracketliteral "-" bracketliteral brackets : "[" range+ "]" -> either - char : /[^\\\[\]\{\}\(\)\|\^~\?!]/ + ?achar : /[^\\\[\]\{\}\(\)\|\^~\?!¿]/ | "\\" /\D/ - | // + char : achar? numrange : DIGITS | DIGITS "-" DIGITS [ "*" DIGITS ] [ "+" DIGITS ] @@ -23,6 +24,7 @@ parser = Lark(r''' ?concat_func : unit | concat_func "{" DIGITS "}" -> concat_repeat | concat_func "?" -> zero_or_one + | concat_func "¿" -> random | concat_func "~" -> reverse | concat_func "~" NUMBER -> roll | concat_func "~{" NUMBER ["," DIGITS] "}" -> roll @@ -44,7 +46,7 @@ parser = Lark(r''' | choice ("|" choice_func)+ -> either ?parens : "(" choice ")" - ''', start='choice', ambiguity='resolve__antiscore_sum') + ''', start='choice', priority='invert') class Expand(Transformer): def __init__(self, amp=None): @@ -74,6 +76,10 @@ class Expand(Transformer): def zero_or_one(self, args): return self.either([[''], args[0]]) + def random(self, args): + shuffle(args[0]) + return args[0] + def either(self, args): result = [] for x in args: