scyther/scripts/if2spdl/parser.py

92 lines
2.8 KiB
Python
Raw Normal View History

2005-11-16 16:24:18 +00:00
#!/usr/bin/python
# requires python-pyparsing module
# http://pyparsing.sourceforge.net/
from pyparsing import Word, alphanums, alphas, nums, oneOf, restOfLine, OneOrMore, \
ParseResults, Forward, Combine, Or, Optional,MatchFirst, \
ZeroOrMore, StringEnd, LineEnd, delimitedList, Group, Literal
2005-11-16 19:07:19 +00:00
import Term
2005-11-16 16:24:18 +00:00
def ifParse (str):
2005-11-16 16:24:18 +00:00
# Tokens
lbr = Literal("(").suppress()
rbr = Literal(")").suppress()
com = Literal(",").suppress()
hash = Literal("#").suppress()
equ = Literal("=").suppress()
implies = Literal("=>").suppress()
2005-11-16 16:24:18 +00:00
# Functions to construct tuples etc
def bracket(x):
return lbr + x + rbr
def ntup(n):
x = Message
while n > 1:
x = x + com + Message
n = n - 1
return x
def btup(n):
return bracket(ntup(n))
def funcy(x,y):
return x + bracket(y)
def ftup(x,n):
return funcy(x, ntup(n))
# Message section
Alfabet= alphas+nums+"_$"
2005-11-16 19:07:19 +00:00
Variable = Word("x",Alfabet).setParseAction(lambda s,l,t: [ Term.TermVariable(t[0],None) ])
Constant = Word(alphas,Alfabet).setParseAction(lambda s,l,t: [ Term.TermConstant(t[0]) ])
Number = Word(nums).setParseAction(lambda s,l,t: [ Term.TermConstant(t[0]) ])
2005-11-16 16:24:18 +00:00
Basic = MatchFirst([ Variable, Constant, Number ])
Message = Forward()
2005-11-16 19:07:19 +00:00
TypeInfo = oneOf ("mr nonce pk sk fu table").setParseAction(lambda s,l,t: [ Term.TermConstant(t[0]) ])
TypeMsg = Group(TypeInfo + lbr + Message + rbr).setParseAction(lambda s,l,t: [ t[3].setType(t[1]) ])
2005-11-16 16:24:18 +00:00
CryptOp = oneOf ("crypt scrypt c funct rcrypt tb")
CryptMsg = Group(CryptOp + lbr + Message + com + Message + rbr).setName("crypt")
SMsg = Group(Literal("s") + lbr + Message + rbr)
2005-11-16 19:07:19 +00:00
Message << Group(Or ([TypeMsg, CryptMsg, SMsg, Basic]) +
Optional(Literal("'")) )
2005-11-16 16:24:18 +00:00
# Fact section
Request = Group("request" + btup(4))
Witness = Group("witness" + btup(4))
Give = Group("give" + lbr + Message + com + ftup(Literal("f"),
1) + rbr)
Secret = Group("secret" + lbr + Message + com +
ftup(Literal("f"),1) + rbr)
TimeFact = Group(ftup (Literal("h"), 1))
IntruderKnowledge = Group(ftup (Literal("i"), 1))
MessageFact = Group(ftup(Literal("m"),6))
Principal = Group(ftup(Literal("w"), 7))
2005-11-16 16:24:18 +00:00
Fact = Principal | MessageFact | IntruderKnowledge | TimeFact | Secret | Give | Witness | Request
#State = Fact + OptioZeroOrMore ("." + Fact)
2005-11-16 18:19:50 +00:00
State = Group(delimitedList (Fact, "."))
2005-11-16 16:24:18 +00:00
# Rules and labels
rulename = Word (alphanums + "_")
rulecategory = oneOf("Protocol_Rules Invariant_Rules Decomposition_Rules Intruder_Rules Init Goal")
label = hash + "lb" + equ + rulename + com + "type" + equ + rulecategory
2005-11-16 18:19:50 +00:00
rule = Group(State + Optional(implies + State))
2005-11-16 16:24:18 +00:00
labeledrule = Group(label + rule)
typeflag = hash + "option" + equ + oneOf ("untyped","typed")
2005-11-16 16:24:18 +00:00
# A complete file
2005-11-16 18:19:50 +00:00
iffile = typeflag + Group(OneOrMore(labeledrule))
2005-11-16 16:24:18 +00:00
parser = iffile
parser.ignore("##" + restOfLine)
return parser.parseString(str)