def validation_hgt_cm(s, loc, tokens): if 150 <= int(tokens[0]) <= 193: return (True) return (False) def validation_hgt_in(s, loc, tokens): if 59 <= int(tokens[0]) <= 76: return (True) return (False) hgt_cm = Word(nums) + "cm" hgt_cm.addCondition(validation_hgt_cm) hgt_inch = Word(nums) + "in" hgt_inch.addCondition(validation_hgt_in) hgt = "hgt:" + (hgt_cm ^ hgt_inch) # brackets are required! hgt_test = """hgt: 160cm hgt: 50cm hgt: 60in hgt: 40in """ # hgt.runTests(hgt_test) # careful, in this case the error messages are NOT right?! hcl = "hcl:" + Word("#") + Word(srange("[a-f0-9]"), exact=6)
class InvalidTransitionException(Exception): pass ident = Word(alphas + "_", alphanums + "_$") def no_keywords_allowed(s, l, t): wd = t[0] return not keyword.iskeyword(wd) ident.addCondition( no_keywords_allowed, message="cannot use a Python keyword for state or transition identifier") stateTransition = ident("from_state") + "->" + ident("to_state") stateMachine = (Keyword("statemachine") + ident("name") + ":" + OneOrMore(Group(stateTransition))("transitions")) namedStateTransition = (ident("from_state") + "-(" + ident("transition") + ")->" + ident("to_state")) namedStateMachine = (Keyword("statemachine") + ident("name") + ":" + OneOrMore(Group(namedStateTransition))("transitions")) def expand_state_definition(source, loc, tokens): indent = " " * (col(loc, source) - 1) statedef = []