本文整理汇总了Python中pyparsing.Regex.setParseAction方法的典型用法代码示例。如果您正苦于以下问题:Python Regex.setParseAction方法的具体用法?Python Regex.setParseAction怎么用?Python Regex.setParseAction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing.Regex
的用法示例。
在下文中一共展示了Regex.setParseAction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _make_grammar
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
def _make_grammar(self):
from pyparsing import (QuotedString, ZeroOrMore, Combine,
Literal, Optional, OneOrMore,
Regex, CaselessKeyword)
def escape_handler(s, loc, toks):
if toks[0] == '\\\\':
return "\\"
elif toks[0] == '\\\'':
return "'"
elif toks[0] == '\\"':
return '"'
elif toks[0] == '\\f':
return "\f"
elif toks[0] == '\\n':
return "\n"
elif toks[0] == '\\r':
return "\r"
elif toks[0] == '\\t':
return "\t"
elif toks[0] == '\\ ':
return " "
else:
return toks[0][1:]
escape = Combine(Regex(r'\\.')).setParseAction(escape_handler)
word = Combine(OneOrMore(escape | Regex(r'[^\s\\]+')))
whitespace = Regex(r'\s+').suppress()
quotedstring = Combine(OneOrMore(QuotedString('"', escChar='\\') | QuotedString("'", escChar='\\')))
command = Regex(r'[^\s:]+') + Literal(":").suppress() + (quotedstring | word)
include = quotedstring | command | word
exclude = (Literal("-") | Literal("^")).suppress() + (quotedstring | command | word)
or_keyword = CaselessKeyword("or")
and_keyword = CaselessKeyword("and")
keyword = or_keyword | and_keyword
argument = (keyword | exclude | include)
expr = ZeroOrMore(Optional(whitespace) + argument)
# arguments.leaveWhitespace()
command.setParseAction(CommandExpr)
include.setParseAction(IncludeExpr)
exclude.setParseAction(ExcludeExpr)
or_keyword.setParseAction(OrKeywordExpr)
and_keyword.setParseAction(AndKeywordExpr)
# or_expr.setParseAction(lambda s, loc, toks: OrOperator(toks[0], toks[2]))
# and_expr.setParseAction(lambda s, loc, toks: AndOperator(toks[0], toks[2]))
# no_expr.setParseAction(lambda s, loc, toks: AndOperator(toks[0], toks[1]))
# expr.setParseAction(Operator)
return expr
示例2: __init__
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
def __init__(self, EvaluateVariableChild=None, EvaluateNumberChild=None):
EvaluateVariableChild = EvaluateVariableChild or EvaluateVariable
EvaluateNumberChild = EvaluateNumberChild or EvaluateNumber
# what is a float number
floatNumber = Regex(r'[-]?\d+(\.\d*)?([eE][-+]?\d+)?')
# a variable is a combination of letters, numbers, and underscor
variable = Word(alphanums + "_")
# a sign is plus or minus
signOp = oneOf('+ -')
# an operand is a variable or a floating point number
operand = floatNumber ^ variable
# when a floatNumber is found, parse it with evaluate number
floatNumber.setParseAction(EvaluateNumberChild)
# when a variable is found, parse it with the EvaluateVariableChild
# or EvaluateVariable
variable.setParseAction(EvaluateVariableChild)
# comparisons include lt,le,gt,ge,eq,ne
comparisonOp = oneOf("< <= > >= == !=")
# negation of the boolean is !
notOp = oneOf("!")
# an expression is a either a comparison or
# a NOT operation (where NOT a is essentially (a == False))
comparisonExpression = operatorPrecedence(operand,
[
(comparisonOp,
2,
opAssoc.LEFT,
EvaluateComparison
),
(notOp,
1,
opAssoc.RIGHT,
EvaluateNot
),
])
# boolean logic of AND or OR
boolOp = oneOf("& |")
# a bool expression contains a nested bool expression or a comparison,
# joined with a boolean operation
boolExpression = Forward()
boolPossible = boolExpression | comparisonExpression
self.boolExpression = operatorPrecedence(boolPossible,
[
(boolOp,
2,
opAssoc.RIGHT,
EvaluateOrAnd
),
])
return
示例3: ListParser
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
def ListParser():
"""
A parser for list columns, where each list is composed of pairs of values.
"""
value = Regex(r'[-+]?[0-9]+(?:\.[0-9]*)?(?:e[-+]?[0-9]+)?', IGNORECASE)
value.setParseAction(lambda toks: float(toks[0]))
item = Suppress('(') + value + Suppress(',') + value + Suppress(')')
item.setParseAction(tuple)
lst = Suppress('[') + delimitedList(item) + Suppress(']')
lst.setParseAction(list)
def parse(s):
try:
return lst.parseString(s).asList()
except ParseBaseException as e:
raise ValueError(e)
return parse
示例4: translate
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
def translate(self, text, filename):
self.source = text
self.super = None
self.inheritance = 0
self.declaration_lines = ['inheritance = 0']
self.block_lines = []
self.body_lines = ['def body():']
self.target_lines = self.body_lines
self.indent = 1
template_close = Literal('%>')
white = White()
attribute = Word(alphanums + '_') + Literal('=') + QuotedString('"') + Optional(white)
directive = "<%@" + Optional(white) + Word(alphanums + '_') + white + ZeroOrMore(attribute) + template_close
declaration = "<%!" + SkipTo(template_close) + template_close
expression = "<%=" + SkipTo(template_close) + template_close
scriptlet = '<%' + SkipTo(template_close) + template_close
template_text = directive | declaration | expression | scriptlet
plain_text = Regex(r'((?!<%).|\s)+', re.MULTILINE)
body = template_text | plain_text
lit = OneOrMore(body)
directive.setParseAction(self.compile_directive)
declaration.setParseAction(self.compile_declaration)
expression.setParseAction(self.compile_expression)
scriptlet.setParseAction(self.compile_scriptlet)
plain_text.setParseAction(self.compile_plain_text)
lit.leaveWhitespace()
lit.parseString(self.source)
translated = '\n' + '\n'.join(self.declaration_lines + ['\n'] + self.block_lines + ['\n'] + self.body_lines)
if self.super:
translated = self.super.module_source + translated
return translated
示例5: make_sexp_parser
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
def make_sexp_parser ():
"""
Returns a simple parser for nested lists of real numbers. Round
parens () are assumed as customary in lisps.
"""
# Punctuation literals (note round parens):
LPAR, RPAR = map (Suppress, "()")
# Real numbers:
real_string = Regex (r"[+-]?\d+\.\d*([eE][+-]?\d+)?")
real = real_string.setParseAction (lambda tokens: float (tokens[0]))
# Voodoo:
sexp = Forward ()
sexp_list = Group (LPAR + ZeroOrMore (sexp) + RPAR)
sexp << (real | sexp_list)
return lambda s: sexp.parseString (s)[0]
示例6: getkw_bnf
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
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
示例7: Regex
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
delimitedList,
operatorPrecedence,
opAssoc,
ParseException,
)
# Variables
variable = Regex(r"(?P<table>[ai|di|sv]{2})\.(?P<tag>[\w\d]+)\.(?P<attr>\w+)")
def var_parse_action(text, index, context):
return context[0]
variable.setParseAction(var_parse_action)
# Numbers
numeric_literal = Regex(r"\-?\d+(\.\d+)?")
def number_prase_action(text, index, data):
number = data[0]
if "." in number:
return float(number)
else:
return int(number)
numeric_literal.setParseAction(number_prase_action)
示例8: import
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
import re
from pyparsing import (
Word, Keyword, NotAny, alphanums, nums, alphas, OneOrMore, srange,
ZeroOrMore, Regex
)
from whispy_lispy import ast
int_literal = Word(nums) + NotAny('.')
int_literal.setParseAction(ast.Int.from_parsed_result)
float_literal = Word(nums) + Word('.') + Word(nums)
float_literal.setParseAction(ast.Float.from_parsed_result)
bool_literal = Keyword('#t') | Keyword('#f')
bool_literal.setParseAction(ast.Bool.from_parsed_result)
string_literal = Regex(r'\".*?(?<!\\)\"', re.DOTALL)
string_literal.setParseAction(ast.String.from_parse_result)
grammar = OneOrMore(float_literal | int_literal | bool_literal | string_literal)
示例9: QuotedString
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
string = QuotedString('"', escChar="\\") | QuotedString('\'', escChar="\\")
operand = model_reference | real | integer | constant | string | variable
plusop = oneOf('+ -')
multop = oneOf('* / // %')
groupop = Literal(',')
expr = Forward()
modifier = Combine(Word(alphas + nums) + ':')
integer.setParseAction(EvalInteger)
real.setParseAction(EvalReal)
string.setParseAction(EvalString)
constant.setParseAction(EvalConstant)
variable.setParseAction(EvalVariable)
model_reference.setParseAction(EvalModelReference)
comparisonop = (oneOf("< <= > >= != == ~= ^= $=") |
(Literal('not in') + WordEnd()) |
(oneOf("in lt lte gt gte matches contains icontains like") + WordEnd()))
logicopOR = Literal('or') + WordEnd()
logicopAND = Literal('and') + WordEnd()
expr << operatorPrecedence(operand, [
(modifier, 1, opAssoc.RIGHT, EvalModifierOp),
(multop, 2, opAssoc.LEFT, EvalMultOp),
(plusop, 2, opAssoc.LEFT, EvalAddOp),
(comparisonop, 2, opAssoc.LEFT, EvalComparisonOp),
示例10: map
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
Optional, Group, FollowedBy, operatorPrecedence, opAssoc, ParseException, ParserElement)
ParserElement.enablePackrat()
COLON,LBRACK,RBRACK,LBRACE,RBRACE,TILDE,CARAT = map(Literal,":[]{}~^")
LPAR,RPAR = map(Suppress,"()")
and_ = CaselessKeyword("AND")
or_ = CaselessKeyword("OR")
not_ = CaselessKeyword("NOT")
to_ = CaselessKeyword("TO")
keyword = and_ | or_ | not_
expression = Forward()
valid_word = Regex(r'([a-zA-Z0-9*_+.-]|\\[!(){}\[\]^"~*?\\:])+').setName("word")
valid_word.setParseAction(
lambda t : t[0].replace('\\\\',chr(127)).replace('\\','').replace(chr(127),'\\')
)
string = QuotedString('"')
required_modifier = Literal("+")("required")
prohibit_modifier = Literal("-")("prohibit")
integer = Regex(r"\d+").setParseAction(lambda t:int(t[0]))
proximity_modifier = Group(TILDE + integer("proximity"))
number = Regex(r'\d+(\.\d+)?').setParseAction(lambda t:float(t[0]))
fuzzy_modifier = TILDE + Optional(number, default=0.5)("fuzzy")
term = Forward()
field_name = valid_word.copy().setName("fieldname")
incl_range_search = Group(LBRACK + term("lower") + to_ + term("upper") + RBRACK)
excl_range_search = Group(LBRACE + term("lower") + to_ + term("upper") + RBRACE)
示例11: Literal
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
required_modifier = Literal('+')('required')
prohibit_modifier = Literal('-')('prohibit')
special_characters = '=><(){}[]^"~*?:\\/'
valid_word = Word(printables, excludeChars=special_characters).setName('word')
valid_word.setParseAction(
lambda t: t[0].replace('\\\\', chr(127)).replace('\\', '').replace(chr(127), '\\')
)
clause = Forward()
field_name = valid_word()('fieldname')
single_term = valid_word()('singleterm')
phrase = QuotedString('"', unquoteResults=True)('phrase')
wildcard = Regex('[a-z0-9]*[\?\*][a-z0-9]*')('wildcard')
wildcard.setParseAction(
lambda t: t[0].replace('?', '.?').replace('*', '.*')
)
regex = QuotedString('/', unquoteResults=True)('regex')
_all = Literal('*')
lower_range = Group((LBRACK('inclusive') | LBRACE('exclusive')) + (valid_word | _all)('lowerbound'))
upper_range = Group((valid_word | _all)('upperbound') + (RBRACK('inclusive') | RBRACE('esclusive')))
_range = (lower_range + to_ + upper_range)('range')
GT = Literal('>')
GTE = Literal('>=')
LT = Literal('<')
LTE = Literal('<=')
mongo_op = (GTE | GT | LTE | LT)
mongo_op.setParseAction(
示例12: Word
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
# Operands
integer = Word(nums).setParseAction(operators.process_int)
float_ = Regex(r'''[0-9]+ # integer part
(?:
(?: # optional decimal part followed by e-part
(?: \.[0-9]* )?
[eE]
[-\u2212+]? # U+2212 is unicode minus
[0-9]+
)
|
(?: \.[0-9]* ) # mandatory decimal part without e-part
)''',
re.VERBOSE)
float_.setParseAction(operators.process_float)
unicode_fraction = oneOf("½ ⅓ ¼ ⅕ ⅙ ⅐ ⅛ ⅑ ⅒ ⅔ ¾ ⅖ ⅗ ⅘ ⅚ ⅜ ⅝ ⅞")
unicode_fraction.setParseAction(operators.process_unicode_fraction)
hexint = Group( Literal("0x") + Regex(r'[0-9a-fA-F]+') )
hexint.setParseAction(operators.process_intbase)
octint = Group( Literal("0o") + Regex(r'[0-7]+') )
octint.setParseAction(operators.process_intbase)
binint = Group( Literal("0b") + Regex(r'[01]+') )
binint.setParseAction(operators.process_intbase)
romanint = Group(
Literal("0r")
+ FollowedBy(
# do not accept empty string!
示例13: define_dot_parser
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
def define_dot_parser(self):
"""Define dot grammar
Based on the grammar http://www.graphviz.org/doc/info/lang.html
"""
# punctuation
colon = Literal(":")
lbrace = Suppress("{")
rbrace = Suppress("}")
lbrack = Suppress("[")
rbrack = Suppress("]")
lparen = Literal("(")
rparen = Literal(")")
equals = Suppress("=")
comma = Literal(",")
dot = Literal(".")
slash = Literal("/")
bslash = Literal("\\")
star = Literal("*")
semi = Suppress(";")
at = Literal("@")
minus = Literal("-")
pluss = Suppress("+")
# keywords
strict_ = CaselessLiteral("strict")
graph_ = CaselessLiteral("graph")
digraph_ = CaselessLiteral("digraph")
subgraph_ = CaselessLiteral("subgraph")
node_ = CaselessLiteral("node")
edge_ = CaselessLiteral("edge")
punctuation_ = "".join( [ c for c in string.punctuation if c not in '_' ] ) +string.whitespace
# token definitions
identifier = Word(alphanums + "_" ).setName("identifier")
#double_quoted_string = QuotedString('"', multiline=True,escChar='\\',
# unquoteResults=True) # dblQuotedString
double_quoted_string = Regex(r'\"(?:\\\"|\\\\|[^"])*\"', re.MULTILINE)
double_quoted_string.setParseAction(removeQuotes)
quoted_string = Combine(double_quoted_string+
Optional(OneOrMore(pluss+double_quoted_string)),adjacent=False)
alphastring_ = OneOrMore(CharsNotIn(punctuation_))
def parse_html(s, loc, toks):
return '<<%s>>' % ''.join(toks[0])
opener = '<'
closer = '>'
try:
html_text = pyparsing.nestedExpr( opener, closer,
(( CharsNotIn(
opener + closer ).setParseAction( lambda t:t[0] ))
)).setParseAction(parse_html)
except:
log.debug('nestedExpr not available.')
log.warning('Old version of pyparsing detected. Version 1.4.8 or '
'later is recommended. Parsing of html labels may not '
'work properly.')
html_text = Combine(Literal("<<") + OneOrMore(CharsNotIn(",]")))
ID = ( alphastring_ | html_text |
quoted_string | #.setParseAction(strip_quotes) |
identifier ).setName("ID")
float_number = Combine(Optional(minus) +
OneOrMore(Word(nums + "."))).setName("float_number")
righthand_id = (float_number | ID ).setName("righthand_id")
port_angle = (at + ID).setName("port_angle")
port_location = ((OneOrMore(Group(colon + ID)) |
Group(colon + lparen + ID + comma + ID + rparen))).setName("port_location")
port = Combine((Group(port_location + Optional(port_angle)) |
Group(port_angle + Optional(port_location)))).setName("port")
node_id = (ID + Optional(port))
a_list = OneOrMore(ID + Optional(equals + righthand_id) +
Optional(comma.suppress())).setName("a_list")
attr_list = OneOrMore(lbrack + Optional(a_list) +
rbrack).setName("attr_list").setResultsName('attrlist')
attr_stmt = ((graph_ | node_ | edge_) + attr_list).setName("attr_stmt")
edgeop = (Literal("--") | Literal("->")).setName("edgeop")
stmt_list = Forward()
graph_stmt = (lbrace + Optional(stmt_list) +
rbrace + Optional(semi) ).setName("graph_stmt")
edge_point = Forward()
#.........这里部分代码省略.........
示例14: proto_integer_fn
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
def proto_integer_fn(s,l,t):
return ProtoInteger(int(t[0]))
def proto_string_fn(s,l,t):
return String(t[0])
def proto_data_fn(s,l,t):
return ProtoData(t[0])
def top_level_proto_definition_fn(s,l,t):
return TopLevelProtoDefinition(t[0], t[1])
def nested_proto_fn(s,l,t):
return NestedProto(t[0], t[1])
def proto_parser_fn(s,l,t):
return ProtoParser(t)
proto_integer = Regex(r"[+-]?\d+")
proto_integer.setParseAction(proto_integer_fn)
LBRACE = Suppress('{')
RBRACE = Suppress('}')
COLON = Suppress(':')
proto_string = copy.copy(dblQuotedString)
proto_string.setParseAction(proto_string_fn)
proto_data = proto_integer | proto_string
proto_data.setParseAction(proto_data_fn)
top_level_proto_definition = identifier + COLON + proto_data
top_level_proto_definition.setParseAction(top_level_proto_definition_fn)
nested_proto = Forward()
示例15: Regex
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import setParseAction [as 别名]
# Spanish
4.294.967.295,000
# Swedish
4 294 967 295,000
# GB-English
4,294,967,295.000
# US-English
4,294,967,295.000
# Thai
4,294,967,295.000
"""
from pyparsing import Regex
comma_decimal = Regex(r'\d{1,2}(([ .])\d\d\d(\2\d\d\d)*)?,\d*')
comma_decimal.setParseAction(lambda t: float(t[0].replace(' ','').replace('.','').replace(',','.')))
dot_decimal = Regex(r'\d{1,2}(([ ,])\d\d\d(\2\d\d\d)*)?\.\d*')
dot_decimal.setParseAction(lambda t: float(t[0].replace(' ','').replace(',','')))
decimal = comma_decimal ^ dot_decimal
decimal.runTests(tests, parseAll=True)
grouped_integer = Regex(r'\d{1,2}(([ .,])\d\d\d(\2\d\d\d)*)?')
grouped_integer.setParseAction(lambda t: int(t[0].replace(' ','').replace(',','').replace('.','')))
grouped_integer.runTests(tests, parseAll=False)