本文整理汇总了Python中pyparsing.Combine类的典型用法代码示例。如果您正苦于以下问题:Python Combine类的具体用法?Python Combine怎么用?Python Combine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Combine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: define_number
def define_number(self):
"""
Return the syntax definition for a number in Arabic Numerals.
Override this method to support numeral systems other than Arabic
Numerals (0-9).
Do not override this method just to change the character used to
separate thousands and decimals: Use :attr:`T_THOUSANDS_SEPARATOR`
and :attr:`T_DECIMAL_SEPARATOR`, respectively.
"""
# Defining the basic tokens:
to_dot = lambda t: "."
to_plus = lambda t: "+"
to_minus = lambda t: "-"
positive_sign = Literal(self._grammar.get_token("positive_sign"))
positive_sign.setParseAction(to_plus)
negative_sign = Literal(self._grammar.get_token("negative_sign"))
negative_sign.setParseAction(to_minus)
decimal_sep = Literal(self._grammar.get_token("decimal_separator"))
decimal_sep.setParseAction(to_dot)
thousands_sep = Suppress(self._grammar.get_token("thousands_separator"))
digits = Word(nums)
# Building the integers and decimals:
sign = positive_sign | negative_sign
thousands = Word(nums, max=3) + \
OneOrMore(thousands_sep + Word(nums, exact=3))
integers = thousands | digits
decimals = decimal_sep + digits
number = Combine(Optional(sign) + integers + Optional(decimals))
number.setParseAction(self.make_number)
number.setName("number")
return number
示例2: func_tokens
def func_tokens(dictionary, parse_action):
func_name = Word(alphas+'_', alphanums+'_')
func_ident = Combine('$' + func_name.copy()('funcname'))
func_tok = func_ident + originalTextFor(nestedExpr())('args')
func_tok.leaveWhitespace()
func_tok.setParseAction(parse_action)
func_tok.enablePackrat()
rx_tok = Combine(Literal('$').suppress() + Word(nums)('num'))
def replace_token(tokens):
index = int(tokens.num)
return dictionary.get(index, u'')
rx_tok.setParseAction(replace_token)
strip = lambda s, l, tok: tok[0].strip()
text_tok = CharsNotIn(u',').setParseAction(strip)
quote_tok = QuotedString('"')
if dictionary:
arglist = Optional(delimitedList(quote_tok | rx_tok | text_tok))
else:
arglist = Optional(delimitedList(quote_tok | text_tok))
return func_tok, arglist, rx_tok
示例3: define_identifier
def define_identifier(self):
"""
Return the syntax definition for an identifier.
"""
# --- Defining the individual identifiers:
# Getting all the Unicode numbers in a single string:
unicode_numbers = "".join([unichr(n) for n in xrange(0x10000)
if unichr(n).isdigit()])
unicode_number_expr = Regex("[%s]" % unicode_numbers, re.UNICODE)
space_char = re.escape(self._grammar.get_token("identifier_spacing"))
identifier0 = Regex("[\w%s]+" % space_char, re.UNICODE)
# Identifiers cannot start with a number:
identifier0 = Combine(~unicode_number_expr + identifier0)
identifier0.setName("individual_identifier")
# --- Defining the namespaces:
namespace_sep = Suppress(self._grammar.get_token("namespace_separator"))
namespace = Group(ZeroOrMore(identifier0 + namespace_sep))
namespace.setName("namespace")
# --- The full identifier, which could have a namespace:
identifier = Combine(namespace.setResultsName("namespace_parts") +
identifier0.setResultsName("identifier"))
identifier.setName("full_identifier")
return identifier
示例4: make_enewick_parser
def make_enewick_parser():
# atoms
lparen = Literal("(").suppress()
rparen = Literal(")").suppress()
colon = Literal(":").suppress()
# semicolon = Literal(";").suppress()
comma = Literal(",").suppress()
point = Literal(".")
e = CaselessLiteral("E")
sharp = Literal("#").suppress()
# terminal
name = Word(
alphanums + alphas8bit + "_" + "-" + "." + "+" + "&" + "/" + "~" + "{" + "}" + "*" + "'" + '"' + "\\" + "?"
)
string = Word(alphas)
fnumber = Combine(
Word("+-" + nums, nums) + Optional(point + Optional(Word(nums))) + Optional(e + Word("+-" + nums, nums))
).setParseAction(cvtReal)
number = Combine(Word(nums)).setParseAction(cvtInt)
label = (
Optional(name).setResultsName("label")
+ Optional(sharp + Optional(string).setResultsName("type") + number.setResultsName("tag"))
+ Optional(colon + fnumber).setResultsName("length")
)
subtree = Forward()
subtreelist = Forward()
subtree << Group(((lparen + subtreelist + rparen).setResultsName("subtree") | label) + Optional(label))
subtreelist << subtree + Optional(comma + subtreelist)
tree = subtree + Word(";").suppress()
return tree.parseString
示例5: expression
def expression(self):
from pyparsing import Suppress,Combine,Optional,oneOf,OneOrMore,Word,nums,Group,alphas,alphanums,Literal,SkipTo,empty,lineEnd
cvtInt = lambda toks: int(toks[0])
cvtReal = lambda toks: float(toks[0])
cvtTuple = lambda toks : tuple(toks.asList())
nameJoin = lambda toks : "".join([tok.replace("#","") for tok in toks[0]])
#lambda toks: " ".join([str(t) for t in toks[0]])
# define punctuation as suppressed literals
lparen,rparen,lbrack,rbrack,lbrace,rbrace,colon = map(Suppress,"()[]{}:")
integer = Combine(Optional(oneOf("+ -")) + Word(nums))\
.setName("integer")\
.setParseAction( cvtInt )
real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
Optional(Word(nums)) +
Optional(oneOf("e E")+Optional(oneOf("+ -")) +Word(nums))).setName("real").setParseAction( cvtReal )
# TREE DEFINITION
# ((seq2: 0.537243, seq1: 0.000004): 0.255741, seq3: 0.281503);
tree_w_branches = (
OneOrMore(Word("():,."+alphas+nums))+Literal(";")
).setParseAction(lambda tokens: " ".join(tokens[:-1])+";")
# SITE PROBABILITIES
# site Freq Data:
# 1 1 AAA: A(0.978) A(1.000)
site_prob = (
integer.setResultsName("site",listAllMatches=True) +
integer.setResultsName("freq",listAllMatches=True) +
Word(alphas+"-").setResultsName("extant",listAllMatches=True) + colon +
Group(OneOrMore(Group(Word(alphas,exact=1)+lparen+real+rparen))).setResultsName("probability",listAllMatches=True) +
lineEnd
)
# ANCESTRAL SEQUENCES
# seq1 ACC
# node #4 ACC
# Optional # character with node # needs to be joined into a single name
sequence = (
Group(Word(alphanums)+
Optional(Combine(Literal("#")+Word(nums)))).setParseAction(nameJoin).setResultsName("name",listAllMatches=True)+
Word(alphas+"- ").setResultsName("sequence", listAllMatches=True)+lineEnd
)
return (SkipTo(Literal("Ancestral reconstruction by AAML."),include=True).suppress() +
tree_w_branches.setResultsName("tree") +
SkipTo(Literal("site")+Literal("Freq")+Literal("Data:"), include=True,).suppress()+
Group(OneOrMore(site_prob)).setResultsName("sites")+
SkipTo(Literal("List of extant and reconstructed sequences")+Word(nums)+Word(nums), include=True).suppress()+
Group(OneOrMore(sequence)).setResultsName("sequences")+
SkipTo(Literal("for a site."),include=True).suppress()+
Group(OneOrMore(real)).setResultsName("probability")+
empty
)
示例6: is_ipv4_addr
def is_ipv4_addr(inputstr):
from pyparsing import Combine, Word, nums
ipAddress = Combine(Word(nums) + ('.' + Word(nums)) * 3)
try:
ipAddress.parseString(inputstr)
return True
except:
return False
示例7: _BNF
def _BNF(self):
base16 = Literal("$")
hex = Combine(base16 + Word(hexnums + "_"))
base4 = Literal("%%")
quaternary = Combine(base4 + Word("0123_"))
base2 = Literal("%")
binary = Combine(base2 + Word("01_"))
plusminus = Literal("+") | Literal("-")
integer = Combine(Optional(plusminus) + Word(nums+"_"))
name_token = Combine(Optional(Literal(":") | Literal("@")) + Word("_" + alphas, "_" + alphanums))
name_token.setParseAction(self._mark_name_token)
lparens = Literal("(").suppress()
rparens = Literal(")").suppress()
# op0 = Literal("@")
op1 = (Literal("^^") | Literal("||") | Literal("|<") | Literal(">|") | Literal("!")).setParseAction(self._mark_unary)
op2 = Literal("->") | Literal("<-") | Literal(">>") | Literal("<<") | Literal("~>") | Literal("><")
op3 = Literal("&")
op4 = Literal("|") | Literal("^")
op5 = Literal("**") | Literal("*") | Literal("//") | Literal("/")
op6 = Literal("+") | Literal("-")
op7 = Literal("#>") | Literal("<#")
op8 = Literal("<") | Literal(">") | Literal("<>") | Literal("==") | Literal("=<") | Literal("=>")
op9 = Literal("NOT").setParseAction(self._mark_unary)
op10 = Literal("AND")
op11 = Literal("OR")
op12 = Literal(",")
expr = Forward()
atom = name_token | hex | quaternary | binary | integer | quotedString
atom.setParseAction(self._push)
atom = atom | (lparens + expr.suppress() + rparens)
# term0 = atom + ZeroOrMore((op0 + atom) .setParseAction(self._push))
# term1 = term0 + ZeroOrMore((op1 + term0) .setParseAction(self._push))
term1 = atom + ZeroOrMore((op1 + atom) .setParseAction(self._push))
term2 = term1 + ZeroOrMore((op2 + term1) .setParseAction(self._push))
term3 = term2 + ZeroOrMore((op3 + term2) .setParseAction(self._push))
term4 = term3 + ZeroOrMore((op4 + term3) .setParseAction(self._push))
term5 = term4 + ZeroOrMore((op5 + term4) .setParseAction(self._push))
term6 = term5 + ZeroOrMore((op6 + term5) .setParseAction(self._push))
term7 = term6 + ZeroOrMore((op7 + term6) .setParseAction(self._push))
term8 = term7 + ZeroOrMore((op8 + term7) .setParseAction(self._push))
term9 = term8 + ZeroOrMore((op9 + term8) .setParseAction(self._push))
term10 = term9 + ZeroOrMore((op10 + term9) .setParseAction(self._push))
term11 = term10 + ZeroOrMore((op11 + term10).setParseAction(self._push))
expr << term11 + ZeroOrMore((op12 + term11).setParseAction(self._push))
return expr
示例8: getkw_bnf
def getkw_bnf(self):
sect_begin = Literal("{").suppress()
sect_end = Literal("}").suppress()
array_begin = Literal("[").suppress()
array_end = Literal("]").suppress()
tag_begin = Literal("<").suppress()
tag_end = Literal(">").suppress()
eql = Literal("=").suppress()
dmark = Literal('$').suppress()
end_data=Literal('$end').suppress()
prtable = alphanums+r'!$%&*+-./<>[email protected]^_|~'
ival=Regex('[-]?\d+')
dval=Regex('-?\d+\.\d*([eE]?[+-]?\d+)?')
lval=Regex('([Yy]es|[Nn]o|[Tt]rue|[Ff]alse|[Oo]n|[Oo]ff)')
# Helper definitions
kstr= quotedString.setParseAction(removeQuotes) ^ \
dval ^ ival ^ lval ^ Word(prtable)
name = Word(alphas+"_",alphanums+"_")
vec=array_begin+delimitedList(dval ^ ival ^ lval ^ Word(prtable) ^ \
Literal("\n").suppress() ^ \
quotedString.setParseAction(removeQuotes))+array_end
sect=name+sect_begin
tag_sect=name+Group(tag_begin+name+tag_end)+sect_begin
# Grammar
keyword = name + eql + kstr
vector = name + eql + vec
data=Combine(dmark+name)+SkipTo(end_data)+end_data
section=Forward()
sect_def=(sect | tag_sect ) #| vec_sect)
input=section | data | vector | keyword
section << sect_def+ZeroOrMore(input) + sect_end
# Parsing actions
ival.setParseAction(self.conv_ival)
dval.setParseAction(self.conv_dval)
lval.setParseAction(self.conv_lval)
keyword.setParseAction(self.store_key)
vector.setParseAction(self.store_vector)
data.setParseAction(self.store_data)
sect.setParseAction(self.add_sect)
tag_sect.setParseAction(self.add_sect)
sect_end.setParseAction(self.pop_sect)
bnf=ZeroOrMore(input) + StringEnd().setFailAction(parse_error)
bnf.ignore(pythonStyleComment)
return bnf
示例9: receiver_input_rule
def receiver_input_rule():
path = Combine(ZeroOrMore(word + ".") + word)
input = path.setResultsName("input")
operator = oneOf(operators.keys()).setResultsName("operator")
value = path.setResultsName("value")
comparison = operator + value
is_or_was = Word("is") | Word("was")
condition = Group(input + is_or_was.setResultsName("temporal") + comparison)
res = ZeroOrMore(condition + _and) + condition
conditions = Group(res).setResultsName("conditions")
return Optional("always").setResultsName("always_fire_rule") + when + conditions + then + actions
示例10: parser
def parser():
global _parser
if _parser is None:
ParserElement.setDefaultWhitespaceChars("")
lbrack = Literal("[")
rbrack = Literal("]")
lbrace = Literal("{")
rbrace = Literal("}")
lparen = Literal("(")
rparen = Literal(")")
reMacro = Suppress("\\") + oneOf(list("dwsZ"))
escapedChar = ~reMacro + Combine("\\" + oneOf(list(printables)))
reLiteralChar = "".join(c for c in string.printable if c not in r"\[]{}().*?+|")
reRange = Combine(lbrack.suppress() + SkipTo(rbrack,ignore=escapedChar) + rbrack.suppress())
reLiteral = ( escapedChar | oneOf(list(reLiteralChar)) )
reDot = Literal(".")
repetition = (
( lbrace + Word(nums).setResultsName("count") + rbrace ) |
( lbrace + Word(nums).setResultsName("minCount")+","+ Word(nums).setResultsName("maxCount") + rbrace ) |
oneOf(list("*+?"))
)
reExpr = Forward()
reGroup = (lparen.suppress() +
Optional(Literal("?").suppress() + oneOf(list(":P"))).setResultsName("option") +
reExpr.setResultsName("expr") +
rparen.suppress())
reTerm = ( reLiteral | reRange | reMacro | reDot | reGroup )
reExpr << operatorPrecedence( reTerm,
[
(repetition, 1, opAssoc.LEFT, create(Repetition)),
(None, 2, opAssoc.LEFT, create(Sequence)),
(Suppress('|'), 2, opAssoc.LEFT, create(Alternation)),
]
)
reGroup.setParseAction(create(Group))
reRange.setParseAction(create(Range))
reLiteral.setParseAction(create(Character))
reMacro.setParseAction(create(Macro))
reDot.setParseAction(create(Dot))
_parser = reExpr
return _parser
示例11: getkw_bnf
def getkw_bnf(self):
sect_begin = Literal("{").suppress()
sect_end = Literal("}").suppress()
array_begin = Literal("[").suppress()
array_end = Literal("]").suppress()
arg_begin = Literal("(").suppress()
arg_end = Literal(")").suppress()
eql = Literal("=").suppress()
dmark = Literal('$').suppress()
end_data=Literal('$end').suppress()
prtable = alphanums+r'!$%&*+-./<>[email protected]^_|~'
# Helper definitions
kstr=Word(prtable) ^ quotedString.setParseAction(removeQuotes)
name = Word(alphas+"_",alphanums+"_")
vec=array_begin+delimitedList(Word(prtable) ^ \
Literal("\n").suppress() ^ \
quotedString.setParseAction(removeQuotes))+array_end
sect=name+sect_begin
key_sect=name+Group(arg_begin+kstr+arg_end)+sect_begin
vec_sect=name+Group(arg_begin+vec+ arg_end)+sect_begin
# Grammar
keyword = name + eql + kstr
vector = name + eql + vec
data=Combine(dmark+name)+SkipTo(end_data)+end_data
section=Forward()
sect_def=(sect | key_sect | vec_sect)
input=section | data | vector | keyword
section << sect_def+ZeroOrMore(input) + sect_end
# Parsing actions
keyword.setParseAction(self.store_key)
vector.setParseAction(self.store_vector)
data.setParseAction(self.store_data)
sect.setParseAction(self.add_sect)
key_sect.setParseAction(self.add_sect)
vec_sect.setParseAction(self.add_vecsect)
sect_end.setParseAction(self.pop_sect)
bnf=ZeroOrMore(input) + StringEnd().setFailAction(parse_error)
bnf.ignore(pythonStyleComment)
return bnf
示例12: instance
def instance():
lit_e = CaselessLiteral('E')
plusorminus = Literal('+') | Literal('-')
number = Word(nums)
integer = Combine(Optional(plusorminus) +
number).setParseAction(lambda t:int(t[0]))
index = integer.copy().addParseAction(index_check(0))
floatnumber = Combine( integer +
Optional( Literal('.') + Optional(number) ) +
Optional( lit_e + integer )
).setParseAction(lambda t:float(t[0]))
#comment = Suppress("%") + Word(alphanums + " ")
comment = Regex(r"%.*").setName("comment").suppress()
linend = Or( [comment , LineEnd()] ).suppress()
section_end = (Literal('#') + LineEnd()).suppress()
vertex = (Group( OneOrMore( floatnumber('point') +
OneOrMore( White() ).suppress() ) ) + linend)('vertex')
vertex_header = (Keyword('VERTEX') + linend).suppress()
vertex_section = (vertex_header + Group(OneOrMore(vertex))('vertices') +
section_end)
simplex = (Group( OneOrMore( index('index')
+ OneOrMore( White() ).suppress() ) ) + linend)('simplex')
simplex_header = (Keyword('SIMPLEX') + linend).suppress()
simplex_section = (simplex_header + Group(OneOrMore(simplex))('simplices') +
section_end)
boundarysegment = (Group( index('id') +
OneOrMore( index('index') +
OneOrMore( White() ).suppress() ) ) +
linend)('boundarysegment')
boundarysegment_header = (Keyword('BOUNDARYSEGMENTS') + linend).suppress()
boundarysegment_section = (boundarysegment_header +
Dict(OneOrMore(
boundarysegment ))('boundarysegments') +
section_end)
sections = Each([vertex_section, simplex_section, boundarysegment_section])
dgf_header = (Keyword('DGF') + linend).suppress()
dgf = (dgf_header + Dict(sections) + OneOrMore( section_end ))('dgf')
return dgf
示例13: _build_grammar
def _build_grammar(self):
expr = Forward()
float_lit = Combine(Word(nums) + '.' + Word(nums))
float_lit.setName('float')
float_lit.setParseAction(lambda x: \
self.to_literal(float(x[0])))
int_lit = Word(nums)
int_lit.setName('int')
int_lit.setParseAction(lambda x: \
self.to_literal(int(x[0])))
num = (float_lit | int_lit)
num.setParseAction(lambda x: x[0])
tag_name = Word(alphas + "_", alphanums + "_")
tag_name.setName('tag_name')
tag_name.setParseAction(lambda t: tag_reference.TagReference(t[0]))
quoted_string = QuotedString("'")
quoted_string.setParseAction(lambda s: self.to_literal(s[0]))
oper = oneOf('+ * / -')
oper.setParseAction(lambda o: o[0])
lpar = Literal("(").suppress()
rpar = Literal(")").suppress()
arith = Group(lpar + expr + oper + expr + rpar)
arith.setParseAction(lambda t: \
self.to_arith(t[0][0], t[0][1], t[0][2]))
assign = tag_name + '=' + expr
assign.setName('assign')
assign.setParseAction(lambda x: self.to_assign(x[0],x[2]))
print_tags = Literal('?')
print_tags.setParseAction(lambda x: self.to_print_tags())
expr <<(arith|assign|tag_name|num|quoted_string|print_tags)
expr.setParseAction(lambda x: x[0])
return expr
示例14: _get_handbrake_title_pattern
def _get_handbrake_title_pattern(self):
title = Literal("+ title").suppress()
integer = Word("0123456789")
time = Combine(integer + ":" + integer + ":" + integer)
duration = Literal("+ duration:").suppress()
subtitle = Literal("+ subtitle tracks:")
iso = Literal('(iso639-2:').suppress() + Word(alphas)
subtitle_track = Literal("+").suppress() + Group(integer + SkipTo(iso).suppress() + iso) + restOfLine.suppress()
title_num = integer.setResultsName("title")
duration_num = time.setResultsName("duration")
subtitles = Group(ZeroOrMore(subtitle_track)).setResultsName("subtitles")
pattern = title + title_num + \
SkipTo(duration).suppress() + \
duration + duration_num + \
SkipTo(subtitle).suppress() + subtitle.suppress() + subtitles
return pattern
示例15: pattern
def pattern():
"""pyparsing pattern
"""
def attachLocation(s, loc, tocs):
"""pyparsing callback. Saves path position in the original string
"""
return [(loc, tocs[0])]
path = CharsNotIn(" \t")("path")
path.setParseAction(attachLocation)
longPath = CharsNotIn(" \t", min=2)("path")
longPath.setParseAction(attachLocation)
slashPath = Combine(Literal('/') + Optional(CharsNotIn(" \t")))("path")
slashPath.setParseAction(attachLocation)
pat = ((Literal('f ') + Optional(White()) + Optional(path)) ^ longPath ^ slashPath) + \
Optional(White() + Word(nums)("line"))
pat.leaveWhitespace()
pat.setParseAction(CommandOpen.create)
return pat