def __init__(self) -> None: BoogieInt: ParserElement["BoogieVal"] = O("-") + Word(nums) def parseInt(s: str, loc: int, toks: "ParseResults[Any]") -> List["BoogieVal"]: return [int("".join(toks))] BoogieInt.setParseAction(parseInt) BoogieBool: ParserElement["BoogieVal"] = L("True") | L("False") def parseBool(s: str, loc: int, toks: "ParseResults[Any]") -> List["BoogieVal"]: return [toks[0]=="True"] BoogieBool.setParseAction(parseBool) BoogieMapRule: Forward = Forward() BoogieValRule: ParserElement["BoogieVal"] = BoogieInt | BoogieBool | BoogieMapRule BoogieMapRule << (S(L("{")) + G(csl(G(BoogieValRule + S(L(":")) + BoogieValRule))) +\ O(S(L("|")) + BoogieValRule) + S(L("}"))) def parseMap(s: str, loc: int, toks: "ParseResults[Any]") -> List[BoogieVal]: print(toks) explicitVals : Dict["BoogieVal", "BoogieVal"] = {key: val for (key,val) in toks[0]} defaultVal: Optional["BoogieVal"] = None if (len(toks) == 2): defaultVal = toks[1] return [BoogieMap(explicitVals, defaultVal)] BoogieMapRule.setParseAction(parseMap) self._root = BoogieValRule
def __init__(s): s.LT = L("<") s.GT = L(">") s.EQ = L("=") s.LPARN = S("(") s.RPARN = S(")") s.LBRAC = S("{") s.RBRAC = S("}") s.EquivOp = L("<==>") s.ImplOp = L("==>") s.OrOp = L("||") s.AndOp = L("&&") s.RelOp = (L("!=") | L ("<=") | L(">=") | L("<:")| L("==") | \ L("<") | L(">") ) s.AddOp = (L("+") | L("-")) s.MulOp = (L("*") | L("/") | L("%")) s.BWAnd = L('&') s.PowOp = L("**") s.UnOp = (L("!") | L("-")) s.FALSE = K("false") s.FALSE.setParseAction( lambda st, loc, toks: s.onAtom(s.FALSE, st, loc, toks)) s.TRUE = K("true") s.TRUE.setParseAction( lambda st, loc, toks: s.onAtom(s.TRUE, st, loc, toks)) s.Id = R("[a-zA-Z_][a-zA-Z0-9_#]*") s.Id.setParseAction( lambda st, loc, toks: s.onAtom(s.Id, st, loc, toks)) s.Number = W(nums) s.Number.setParseAction( lambda st, loc, toks: s.onAtom(s.Number, st, loc, toks)) s.Atom = s.FALSE | s.TRUE | s.Number | s.Id s.AndOrOp = s.AndOp | s.OrOp s.ArithExpr = operatorPrecedence(s.Atom, [ (s.PowOp, 2, opAssoc.RIGHT, lambda st, loc, toks: s.onRABinOp(s.PowOp, st, loc, toks[0])), (s.UnOp, 1, opAssoc.RIGHT, lambda st, loc, toks: s.onUnaryOp(s.UnOp, st, loc, toks[0])), (s.MulOp, 2, opAssoc.LEFT, lambda st, loc, toks: s.onLABinOp(s.MulOp, st, loc, toks[0])), (s.AddOp, 2, opAssoc.LEFT, lambda st, loc, toks: s.onLABinOp(s.MulOp, st, loc, toks[0])), ]) s.RelExpr = s.ArithExpr + s.RelOp + s.ArithExpr s.RelExpr.setParseAction( lambda st, loc, toks: s.onNABinOp(s.RelOp, st, loc, toks)) s.BoolExpr = operatorPrecedence(s.RelExpr, [ (s.AndOrOp, 2, opAssoc.LEFT, lambda st, loc, toks: s.onLABinOp(s.AndOrOp, st, loc, toks[0])), (s.ImplOp, 2, opAssoc.LEFT, lambda st, loc, toks: s.onLABinOp(s.ImplOp, st, loc, toks[0])), (s.EquivOp, 2, opAssoc.LEFT, lambda st, loc, toks: s.onLABinOp(s.EquivOp, st, loc, toks[0])), ]) s.Expr = s.BoolExpr | s.RelExpr | s.ArithExpr s.IsPow2 = s.Id + S("is a power of 2") s.IsPow2.setParseAction( lambda st, loc, toks: s.onUnaryOp(s.IsPow2, st, loc, toks)) s.IsOneOf = s.Id + S("one of") + S(s.LBRAC) + csl(s.Expr) + S(s.RBRAC) s.IsOneOf.setParseAction(lambda st, loc, toks: s.onNABinOp( s.IsOneOf, st, loc, [toks[0], toks[1:]])) s.IsInRange = s.Number + S(L("<=")) + s.Id + S(L("<=")) + s.Number s.IsInRange.setParseAction( lambda st, loc, toks: s.onNABinOp(s.IsInRange, st, loc, toks)) s.IsBoolean = s.Id + S(L("is boolean")) s.IsBoolean.setParseAction( lambda st, loc, toks: s.onUnaryOp(s.IsBoolean, st, loc, toks)) s.IsEven = s.Id + S(L("is even")) s.IsEven.setParseAction( lambda st, loc, toks: s.onUnaryOp(s.IsEven, st, loc, toks)) s.IsConstMod = s.Id + S(L("==")) + s.Number + \ S(L("(mod")) + s.Number + S(L(")")) s.IsConstMod.setParseAction( lambda st, loc, toks: s.onTernaryOp(s.IsConstMod, st, loc, toks)) ValFreqPair = s.ArithExpr + S(L("[") + s.Number + L("]")) s.HasValues = s.Id + S(L("has values:")) + OneOrMore(ValFreqPair) s.HasValues.setParseAction( lambda st, loc, toks: s.onVariaryOp(s.HasValues, st, loc, toks)) s.JustInv = s.IsPow2 |\ s.IsOneOf |\ s.IsInRange |\ s.IsBoolean |\ s.IsEven |\ s.IsConstMod |\ s.HasValues |\ s.Expr s.WarnInv = S(R("warning: too few samples for [a-zA-Z\._]* invariant:")) + \ s.JustInv | s.JustInv s.OneLine = s.WarnInv + StringEnd()
def serialize(self): return str(self) class Value(Node): def serialize(self): return '"{0}"'.format(self) class Op(Node): def serialize(self): return str(self) VARIABLE = ( L("implementation_version") | L("platform_python_implementation") | L("implementation_name") | L("python_full_version") | L("platform_release") | L("platform_version") | L("platform_machine") | L("platform_system") | L("python_version") | L("sys_platform") | L("os_name") | L("os.name") | L("sys.platform") # PEP-345 | L("platform.version") # PEP-345 | L("platform.machine") # PEP-345 | L("platform.python_implementation") # PEP-345
from pyparsing import Literal as L # noqa from six.moves.urllib import parse as urlparse from .markers import MARKER_EXPR, Marker from .specifiers import LegacySpecifier, Specifier, SpecifierSet class InvalidRequirement(ValueError): """ An invalid requirement was found, users should refer to PEP 508. """ ALPHANUM = Word(string.ascii_letters + string.digits) LBRACKET = L("[").suppress() RBRACKET = L("]").suppress() LPAREN = L("(").suppress() RPAREN = L(")").suppress() COMMA = L(",").suppress() SEMICOLON = L(";").suppress() AT = L("@").suppress() PUNCTUATION = Word("-_.") IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM) IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END)) NAME = IDENTIFIER("name") EXTRA = IDENTIFIER URI = Regex(r'[^ ]+')("url")
def __init__(s) -> None: s.LT = L("<") s.GT = L(">") s.EQ = L("=") # Braces/Brackets s.LSQBR = S("[") s.RSQBR = S("]") s.LPARN = S("(") s.RPARN = S(")") s.LBRAC = S("{") s.RBRAC = S("}") # Various Delimiters s.SEMI = S(";") s.COLN = S(":") s.ASSGN = S(":=") s.STAR = S("*") ####### Keywords s.INT = K("int") s.BOOL = K("bool") s.TYPE = K("type") s.FINITE = K("finite") s.CONST = K("const") s.UNIQUE = K("unique") s.RETURNS = K("returns") s.FUNCTION = K("function") s.FALSE = K("false") # type: ParserElement[T] s.FALSE.setParseAction(\ lambda st, loc, toks: s.onAtom(s.FALSE, st, loc, toks)) s.TRUE = K("true") # type: ParserElement[T] s.TRUE.setParseAction(\ lambda st, loc, toks: s.onAtom(s.TRUE, st, loc, toks)) s.OLD = K("old") s.AXIOM = K("axiom") s.FORALL = K("forall") s.EXISTS = K("exists") s.VAR = K("var") s.PROCEDURE = K("procedure") s.FREE = K("free") s.RETURNS = K("returns") s.REQUIRES = K("requires") s.MODIFIES = K("modifies") s.ENSURES = K("ensures") s.ASSERT = K("assert") s.COMPLETE = K("complete") s.UNIQUE = K("unique") s.IF = K("if") s.ELSE = K("else") s.FREE = K("free") s.INVARIANT = K("invariant") s.ASSUME = K("assume") s.ASSERT = K("assert") s.HAVOC = K("havoc") s.CALL = K("call") s.WHILE = K("while") s.BREAK = K("break") s.GOTO = K("goto") s.RETURN = K("return") s.IMPLEMENTATION = K("implementation") s.Id = R("[a-zA-Z_#.$'`~^?\\\\][a-zA-Z0-9_#.$'`~^?\\\\]*" ) # type: ParserElement[T] s.Id.setParseAction( lambda st, loc, toks: s.onAtom(s.Id, st, loc, toks)) s.ParentEdge = O(s.UNIQUE) + s.Id s.ParentInfo = S("<:") + csl(s.ParentEdge) s.OrderSpec = O(s.ParentInfo) + O(s.COMPLETE) s.StringLiteral = S(L("\"")) + R("[^\"]*") + S(L("\"")) s.Trigger = F() # TODO ####### Expressions s.EquivOp = L("<==>") s.ImplOp = L("==>") s.OrOp = L("||") s.AndOp = L("&&") s.AndOrOp = s.AndOp | s.OrOp s.RelOp = (L("!=") | L ("<=") | L(">=") | L("<:") \ | L("==") | L("<") | L(">") ) s.ConcatOp = L("++") s.AddOp = (L("+") | L("-")) s.MulOp = (L("*") | L("/") | L("div") | L("mod")) s.UnOp = (L("!") | L("-")) s.ArithUnOp = L("-") s.BoolUnOp = L("!") s.QOp = (s.FORALL | s.EXISTS) s.QSep = L("::") s.Expr = F() # type: ParserElement[T] ####### Attributes s.AttrArg = s.Expr | s.StringLiteral s.Attribute = S(s.LBRAC) + S(s.COLN) + s.Id + G(O(csl(s.AttrArg))) + S( s.RBRAC) s.Attribute.setParseAction( lambda st, loc, toks: s.onAttribute(s.Attribute, st, loc, toks)) s.AttrList = ZoM(s.Attribute) ####### Types s.Type = F() # type: ParserElement[T] s.BVType = R("bv[0-9][0-9]*") s.INT.setParseAction( lambda st, loc, toks: s.onPrimitiveType(s.INT, st, loc, toks)) s.BOOL.setParseAction( lambda st, loc, toks: s.onPrimitiveType(s.BOOL, st, loc, toks)) s.BVType.setParseAction( lambda st, loc, toks: s.onPrimitiveType(s.BVType, st, loc, toks)) s.TypeAtom = s.INT | s.BOOL | s.BVType | S(s.LPARN) + s.Type + S( s.RPARN) s.TypeArgs = S(s.LT) + csl(s.Id) + S(s.GT) s.MapType = G(O(s.TypeArgs)) + S(s.LSQBR) + G(csl(s.Type)) + S( s.RSQBR) + s.Type s.MapType.setParseAction( lambda st, loc, toks: s.onMapType(s.Type, st, loc, toks)) s.TypeCtorArgs = F() s.TypeCtorArgs << (s.TypeAtom + O(s.TypeCtorArgs)\ | s.Id + O(s.TypeCtorArgs)\ | s.MapType) s.CompoundType = s.Id + O(s.TypeCtorArgs) s.CompoundType.setParseAction(lambda st, loc, toks: s.onCompoundType( s.CompoundType, st, loc, toks)) s.Type << (s.TypeAtom | s.MapType | s.CompoundType) #pylint: disable=expression-not-assigned s.Type.setParseAction( lambda st, loc, toks: s.onType(s.Type, st, loc, toks)) s.IdsType = csl(s.Id) + s.COLN + s.Type s.IdsType.setParseAction( lambda st, loc, toks: s.onBinding(s.Type, st, loc, toks)) ####### Type Declarations s.TypeConstructor = S(s.TYPE) + G(s.AttrList) + G(O( s.FINITE)) + s.Id + G(ZoM(s.Id)) + S(s.SEMI) s.TypeConstructor.setParseAction( lambda st, loc, toks: s.onTypeConstructorDecl( s.TypeConstructor, st, loc, toks)) s.TypeSynonym = s.TYPE + s.AttrList + OoM( s.Id) + s.EQ + s.Type + s.SEMI s.TypeDecl = s.TypeConstructor | s.TypeSynonym ####### Constant Declarations s.ConstantDecl = s.CONST + O(s.Attribute) + O(s.UNIQUE) + \ s.IdsType + s.OrderSpec s.Number = W(nums) # type: ParserElement[T] s.Number.setParseAction( lambda st, loc, toks: s.onAtom(s.Number, st, loc, toks)) s.BitVector = R("[0-9][0-9]*bv[0-9][0-9]*") # type: ParserElement[T] s.BitVector.setParseAction( lambda st, loc, toks: s.onAtom(s.BitVector, st, loc, toks)) # TODO # TrigAttr = Trigger | Attribute s.Old = s.OLD + s.LPARN + s.Expr + s.RPARN # type: ParserElement[T] s.Old.setParseAction( lambda st, loc, toks: s.onAtom(s.Old, st, loc, toks)) s.Primitive = s.FALSE | s.TRUE | s.Number | s.BitVector # type: ParserElement[T] # TODO: Handle TriggerAttrs in Quantified expressions #E9_Quantified = LPARN + QOp + O(TypeArgs) + csl(IdsType) \ # + QSep + ZoM(TrigAttr) + Expr + RPARN s.Quantified = S(s.LPARN) + s.QOp + O(s.TypeArgs) + \ G(csl(s.IdsType)) + S(s.QSep) + s.Expr + S(s.RPARN) s.Quantified.setParseAction( lambda st, loc, toks: s.onQuantified(s.Old, st, loc, toks)) s.AtomIdCont = F() s.AtomId = s.Id + O(s.AtomIdCont) s.AtomId.setParseAction( lambda st, loc, toks: s.onAtom(s.Id, st, loc, toks)) s.Atom = (s.Primitive | s.Old | s.AtomId) # type: ParserElement[T] s.MapUpdateArgs = S(s.LSQBR) + s.Expr + s.ASSGN + s.Expr + S(s.RSQBR) s.MapUpdateArgs.setParseAction( lambda st, loc, toks: s.onMapUpdateArgs(s.Old, st, loc, toks)) s.MapIndexArgs = S(s.LSQBR) + s.Expr + S(s.RSQBR) s.MapIndexArgs.setParseAction( lambda st, loc, toks: s.onMapIndexArgs(s.Old, st, loc, toks)) s.FunAppArgs = s.LPARN + csl( s.Expr) + s.RPARN # type: ParserElement[T] s.FunAppArgs.setParseAction( lambda st, loc, toks: s.onFunAppArgs(s.FunAppArgs, st, loc, toks)) s.AtomIdCont << (s.MapUpdateArgs | s.MapIndexArgs | s.FunAppArgs) + O( s.AtomIdCont) s.ArithExpr = operatorPrecedence(s.Atom, [ (s.ArithUnOp, 1, opAssoc.RIGHT, \ lambda st, loc, toks: s.onUnaryOp(s.ArithUnOp, st, loc, toks[0])), (s.MulOp, 2, opAssoc.LEFT, \ lambda st, loc, toks: s.onLABinOp(s.MulOp, st, loc, toks[0])), (s.AddOp, 2, opAssoc.LEFT, \ lambda st, loc, toks: s.onLABinOp(s.AddOp, st, loc, toks[0])), ]) # type: ParserElement[T] # TODO: Add support for M[x,y,z:=foo], M[a:b], concat ops s.RelExpr = s.ArithExpr + s.RelOp + s.ArithExpr # type: ParserElement[T] s.RelExpr.setParseAction( lambda st, loc, toks: s.onNABinOp(s.RelExpr, st, loc, toks)) s.BoolExpr = operatorPrecedence((s.RelExpr | s.Atom | s.Quantified), [ (s.BoolUnOp, 1, opAssoc.RIGHT, \ lambda st, loc, toks: s.onUnaryOp(s.BoolUnOp, st, loc, toks[0])), (s.AndOrOp, 2, opAssoc.LEFT, \ lambda st, loc, toks: s.onLABinOp(s.AndOrOp, st, loc, toks[0])), (s.ImplOp, 2, opAssoc.LEFT, \ lambda st, loc, toks: s.onLABinOp(s.ImplOp, st, loc, toks[0])), (s.EquivOp, 2, opAssoc.LEFT, \ lambda st, loc, toks: s.onLABinOp(s.EquivOp, st, loc, toks[0])), ]) # type: ParserElement[T] s.Expr << (s.BoolExpr ^ s.RelExpr ^ s.ArithExpr) #pylint: disable=pointless-statement ####### Function Declarations s.FArgName = s.Id + s.COLN s.FArg = s.FArgName + s.Type s.FSig = O(s.TypeArgs) + s.LPARN + csl(s.FArg) + \ s.RPARN + s.RETURNS + s.LPARN + s.FArg + s.RPARN s.FunctionDecl = s.FUNCTION + s.AttrList + s.Id + s.FSig + s.SEMI |\ s.FUNCTION + s.AttrList + s.Id + s.FSig + s.SEMI +\ s.LBRAC + s.Expr + s.RBRAC ####### Axiom Declarations s.AxiomDecl = s.AXIOM + s.AttrList + s.Expr s.WhereClause = F() # TODO s.IdsTypeWhere = s.IdsType + O(s.WhereClause) s.VarDecl = s.VAR + s.AttrList + s.IdsTypeWhere ####### Procedure Declarations s.Spec = O(s.FREE) + s.REQUIRES + s.Expr + s.SEMI \ | O(s.FREE) + s.MODIFIES + csl(s.Id) + s.SEMI \ | O(s.FREE) + s.ENSURES + s.Expr + s.SEMI s.OutParameters = s.RETURNS + s.LPARN + csl(s.IdsTypeWhere) + s.RPARN s.PSig = O(s.TypeArgs) + s.LPARN + csl(s.IdsTypeWhere) + \ s.RPARN + O(s.OutParameters) s.LocalVarDecl = S(s.VAR) + s.AttrList + csl(s.IdsTypeWhere) + \ S(s.SEMI) # type: ParserElement[T] s.LocalVarDecl.setParseAction(lambda st, loc, toks: s.onLocalVarDecl( s.LocalVarDecl, st, loc, toks)) s.StmtList = F() s.WildcardExpr = s.Expr | s.STAR s.BlockStmt = s.LBRAC + s.StmtList + s.RBRAC s.LoopInv = O(s.FREE) + s.INVARIANT + s.Expr + s.SEMI s.IfStmt = F() # type: Union[F, ParserElement[T]] s.Else = s.ELSE + s.BlockStmt | s.ELSE + s.IfStmt s.IfStmt << s.IF + s.LBRAC + s.WildcardExpr + s.RBRAC + s.BlockStmt + \ O(s.Else) s.CallLhs = csl(s.Id) + s.ASSGN s.Lhs = s.Id + ZoM(s.MapIndexArgs) s.Lhs.setParseAction( lambda st, loc, toks: s.onAtom(s.Id, st, loc, toks)) s.Label = s.Id | s.Number s.AssertStmt = S(s.ASSERT) + s.Expr + S( s.SEMI) # type: ParserElement[T] s.AssertStmt.setParseAction( lambda st, loc, toks: s.onAssert(s.AssertStmt, st, loc, toks)) s.AssumeStmt = S(s.ASSUME) + G(s.AttrList) + s.Expr + S( s.SEMI) # type: ParserElement[T] s.AssumeStmt.setParseAction( lambda st, loc, toks: s.onAssume(s.AssumeStmt, st, loc, toks)) s.ReturnStmt = S(s.RETURN) + S(s.SEMI) # type: ParserElement[T] s.ReturnStmt.setParseAction( lambda st, loc, toks: s.onReturn(s.ReturnStmt, st, loc, toks)) s.GotoStmt = S(s.GOTO) + csl(s.Label) + S( s.SEMI) # type: ParserElement[T] s.GotoStmt.setParseAction( lambda st, loc, toks: s.onGoto(s.GotoStmt, st, loc, toks)) s.AssignmentStmt = G(csl(s.Lhs)) + S(s.ASSGN) + G(csl(s.Expr)) + S( s.SEMI) # type: ParserElement[T] s.AssignmentStmt.setParseAction(lambda st, loc, toks: s.onAssignment( s.AssignmentStmt, st, loc, toks)) s.HavocStmt = S(s.HAVOC) + csl(s.Id) + S( s.SEMI) # type: ParserElement[T] s.HavocStmt.setParseAction( lambda st, loc, toks: s.onHavoc(s.HavocStmt, st, loc, toks)) s.CallAssignStmt = s.CALL + O(s.CallLhs) + s.Id + s.LPARN + csl(s.Expr) +\ s.RPARN + S(s.SEMI) s.CallForallStmt = s.CALL + s.FORALL + s.Id + s.LPARN + \ csl(s.WildcardExpr) + s.RPARN + S(s.SEMI) s.WhileStmt = s.WHILE + s.LPARN + s.WildcardExpr + s.RPARN + \ ZoM(s.LoopInv) + s.BlockStmt s.BreakStmt = s.BREAK + O(s.Id) + S(s.SEMI) s.Stmt = s.AssertStmt \ | s.AssumeStmt \ | s.HavocStmt \ | s.AssignmentStmt \ | s.CallAssignStmt \ | s.CallForallStmt \ | s.IfStmt \ | s.WhileStmt \ | s.BreakStmt \ | s.ReturnStmt \ | s.GotoStmt # type: ParserElement[T] s.LStmt = F() s.LabeledStatement = s.Label + S( s.COLN) + s.LStmt # type: ParserElement[T] s.LEmpty = (s.Label + S(s.COLN)) s.LStmt << (s.Stmt | s.LabeledStatement | s.LEmpty) #pylint: disable=pointless-statement s.LabeledStatement.setParseAction( lambda st, loc, toks: s.onLabeledStatement(s.LabeledStatement, st, loc, toks)) s.LEmpty.setParseAction(lambda st, loc, toks: s.onLabeledStatement( s.LEmpty, st, loc, toks)) s.StmtList << (ZoM(s.LStmt)) #pylint: disable=expression-not-assigned s.Body = S(s.LBRAC) + G(ZoM(s.LocalVarDecl)) + G(s.StmtList) + S( s.RBRAC) # type: ParserElement[T] s.Body.setParseAction( lambda st, loc, toks: s.onBody(s.Body, st, loc, toks)) s.ProcedureDecl = \ s.PROCEDURE + s.AttrList + s.Id + s.PSig + s.SEMI + ZoM(s.Spec) |\ s.PROCEDURE + s.AttrList + s.Id + s.PSig + ZoM(s.Spec) + s.Body s.IOutParameters = S(s.RETURNS) + S(s.LPARN) + csl(s.IdsType) + S( s.RPARN) s.ISig = G(O(s.TypeArgs)) + S(s.LPARN) + G(O(csl(s.IdsType))) + S(s.RPARN) +\ G(O(s.IOutParameters)) s.ImplementationDecl = S(s.IMPLEMENTATION) + G(s.AttrList) + s.Id +\ G(s.ISig) + G(ZoM(s.Body)) # type: ParserElement[T] s.ImplementationDecl.setParseAction( lambda st, loc, toks: s.onImplementationDecl( s.ImplementationDecl, st, loc, toks)) s.Decl : ParserElement[T] = s.TypeDecl |\ s.ConstantDecl |\ s.FunctionDecl |\ s.AxiomDecl |\ s.VarDecl |\ s.ProcedureDecl |\ s.ImplementationDecl s.Program = ZoM(s.Decl) # type: ParserElement[T] s.Program.setParseAction( lambda st, loc, toks: s.onProgram(s.Program, st, loc, toks)) s.Program.ignore("//" + restOfLine)
return str(self.value) def __repr__(self): return "<{0}({1!r})>".format(self.__class__.__name__, str(self)) class Variable(Node): pass class Value(Node): pass VARIABLE = ( L("implementation_version") | L("platform_python_implementation") | L("implementation_name") | L("python_full_version") | L("platform_release") | L("platform_version") | L("platform_machine") | L("platform_system") | L("python_version") | L("sys_platform") | L("os_name") | L("extra") ) VARIABLE.setParseAction(lambda s, l, t: Variable(t[0])) VERSION_CMP = (
oneThru31 = oneOf(['%.2d' % i for i in range(1, 32)]) oneThru59 = oneOf(['%.2d' % i for i in range(1, 60)]) zeroThru59 = oneOf(['%.2d' % i for i in range(0, 60)]) positiveDigit = Word(nums, exact=1, excludeChars='0') digit = Word(nums, exact=1) second = zeroThru59 minute = zeroThru59 hour = zeroThru23 day = oneThru31("day") month = oneThru12("month") monthDay = ((oneOf("01 03 05 07 08 10 12")("month") + "-" + oneThru31("day")) ^ (oneOf("04 06 09 11")("month") + "-" + oneThru30("day")) ^ (L("02")("month") + "-" + oneThru29("day"))) # 4 digits, 0 to 9 positiveYear = Word(nums, exact=4) # Negative version of positive year, but "-0000" is illegal negativeYear = NotAny(L("-0000")) + ("-" + positiveYear) year = Combine(positiveYear ^ negativeYear)("year") yearMonth = year + "-" + month yearMonthDay = year + "-" + monthDay # o hai iso date date = Combine(year ^ yearMonth ^ yearMonthDay)("date") Date.set_parser(date)
from . import Facility, Severity class UnparseableSyslogMessage(Exception): pass class NilValue: pass NilValue = NilValue() SP = L(" ").suppress() LANGLE = L("<").suppress() RANGLE = L(">").suppress() LBRACKET = L("[").suppress() RBRACKET = L("]").suppress() COLON = L(":").suppress() NIL = L("-") NIL.setName("Nil") NIL.setParseAction(lambda s, l, t: NilValue) PRIORITY = LANGLE + Word(srange("[0-9]"), min=1, max=3) + RANGLE # 191 Max PRIORITY = PRIORITY.setResultsName("priority") PRIORITY.setName("Priority") PRIORITY.setParseAction(lambda s, l, t: int(t[0]))
class UnparseableEvent(Exception): pass class NullValue: pass NullValue = NullValue() printables = "".join(set(_printables + " " + "\t") - {"|", "@"}) PIPE = L("|").suppress() AT = L("@").suppress() NULL = L("(null)") NULL.setParseAction(lambda s, l, t: NullValue) TIMESTAMP = Word(printables) TIMESTAMP = TIMESTAMP.setResultsName("timestamp") TIMESTAMP.setName("Timestamp") COUNTRY_CODE = Word(printables) COUNTRY_CODE = COUNTRY_CODE.setResultsName("country_code") COUNTRY_CODE.setName("Country Code") URL = Word(printables)
oneThru59 = oneOf(['%.2d' % i for i in range(1, 60)]) zeroThru59 = oneOf(['%.2d' % i for i in range(0, 60)]) positiveDigit = Word(nums, exact=1, excludeChars='0') digit = Word(nums, exact=1) second = zeroThru59 minute = zeroThru59 hour = zeroThru23 day = oneThru31("day") month = oneThru12("month") monthDay = ( (oneOf("01 03 05 07 08 10 12")("month") + "-" + oneThru31("day")) ^ (oneOf("04 06 09 11")("month") + "-" + oneThru30("day")) ^ (L("02")("month") + "-" + oneThru29("day")) ) # 4 digits, 0 to 9 positiveYear = Word(nums, exact=4) # Negative version of positive year, but "-0000" is illegal negativeYear = NotAny(L("-0000")) + ("-" + positiveYear) year = Combine(positiveYear ^ negativeYear)("year") yearMonth = year + "-" + month yearMonthDay = year + "-" + monthDay # o hai iso date date = Combine(year ^ yearMonth ^ yearMonthDay)("date") Date.set_parser(date)
from pyparsing import printables as _printables, restOfLine from pyparsing import ParseException from . import user_agents class NullValue: pass NullValue = NullValue() printables = "".join(set(_printables + " " + "\t") - {"|", "@"}) PIPE = L("|").suppress() AT = L("@").suppress() NULL = L("(null)") NULL.setParseAction(lambda s, l, t: NullValue) TIMESTAMP = Word(printables) TIMESTAMP = TIMESTAMP.setResultsName("timestamp") TIMESTAMP.setName("Timestamp") COUNTRY_CODE = Word(printables) COUNTRY_CODE = COUNTRY_CODE.setResultsName("country_code") COUNTRY_CODE.setName("Country Code") URL = Word(printables)