本文整理汇总了Python中pyparsing.Forward方法的典型用法代码示例。如果您正苦于以下问题:Python pyparsing.Forward方法的具体用法?Python pyparsing.Forward怎么用?Python pyparsing.Forward使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing
的用法示例。
在下文中一共展示了pyparsing.Forward方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: expression_parser
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def expression_parser(self):
"""A function returning a (pyparsing) parser for parsing C expressions.
Returns:
a (pyparsing) parser for parsing C expressions.
"""
precedence = (self._build_precedence(_UNARY_MACROS) +
self._build_precedence(_PRECEDENCE))
self.expression = pyparsing.Forward()
# pylint: disable=expression-not-assigned
self.expression << (
pyparsing.infixNotation(
baseExpr=self._base_or_array_expression(),
opList=precedence,
)
)
return self.expression
示例2: _element
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def _element(self):
"""The parser for all elements."""
self.element = pyparsing.Forward()
self.element << (
(~_TYPEDEF) + (
# e.g. int x;
self._type_name_with_fields()
# e.g. struct s {};
| self._struct_definition_possibly_with_fields()
# e.g. enum foo { OPTION = 1 + 2; };
| self._enum_definition()
| pyparsing.OneOrMore(_SEMICOLON)
)
)
return self.element.setName("element")
示例3: expression
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def expression(self):
expression = pyparsing.Forward()
# (1 + (2 + 3))
nested_expression = pyparsing.nestedExpr(
"(", ")", expression).setParseAction(self._combine_lists)
# FOO(2 , 3)
function_call = (
_TOKEN().setResultsName("function")
+ _OPEN_PARENTHESIS()
+ pyparsing.delimitedList(
pyparsing.Combine(expression, adjacent=False, joinString=" "),
delim=",").setResultsName("func_args")
+ _CLOSE_PARENTHESIS()
)
expression << pyparsing.OneOrMore(
function_call.setParseAction(self._is_known_function)
| pyparsing.Group(nested_expression)
| _TOKEN()
| _NOT_TOKEN()
)
return pyparsing.Combine(expression, adjacent=False, joinString=" ")
示例4: anything_beetween
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def anything_beetween(opener_and_closer):
"""Builds a (pyparsing) parser for the content inside delimiters.
Args:
opener_and_closer: a string containing two elements: opener and closer
Returns:
A (pyparsing) parser for the content inside delimiters.
"""
opener = pyparsing.Literal(opener_and_closer[0])
closer = pyparsing.Literal(opener_and_closer[1])
char_removal_mapping = dict.fromkeys(map(ord, opener_and_closer))
other_chars = unicode(string.printable).translate(char_removal_mapping)
word_without_delimiters = pyparsing.Word(other_chars).setName(
"other_chars")
anything = pyparsing.Forward()
delimited_block = opener + anything + closer
# pylint: disable=expression-not-assigned
anything << pyparsing.ZeroOrMore(
word_without_delimiters.setName("word_without_delimiters")
| delimited_block.setName("delimited_block")
)
# Combine all the parts into a single string.
return pyparsing.Combine(anything)
示例5: _parse_filter
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def _parse_filter():
op = pyparsing.oneOf('! & |')
lpar = pyparsing.Literal('(').suppress()
rpar = pyparsing.Literal(')').suppress()
k = pyparsing.Word(pyparsing.alphanums)
# NOTE: We may need to expand on this list, but as this is not a real
# LDAP server we should be OK.
# Value to contain:
# numbers, upper/lower case letters, astrisk, at symbol, minus, full
# stop, backslash or a space
v = pyparsing.Word(pyparsing.alphanums + "-*@.\\ äöü")
rel = pyparsing.oneOf("= ~= >= <=")
expr = pyparsing.Forward()
atom = pyparsing.Group(lpar + op + expr + rpar) \
| pyparsing.Combine(lpar + k + rel + v + rpar)
expr << atom + pyparsing.ZeroOrMore( expr )
return expr
示例6: grammar
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def grammar():
parenthesis = Forward()
parenthesis <<= "(" + ZeroOrMore(CharsNotIn("()") | parenthesis) + ")"
field_def = OneOrMore(Word(alphanums + "_\"'`:-") | parenthesis)
field_def.setParseAction(field_act)
tablename_def = ( Word(alphas + "`_") | QuotedString("\"") )
field_list_def = field_def + ZeroOrMore(Suppress(",") + field_def)
field_list_def.setParseAction(field_list_act)
create_table_def = Literal("CREATE") + "TABLE" + tablename_def.setResultsName("tableName") + "(" + field_list_def.setResultsName("fields") + ")" + ";"
create_table_def.setParseAction(create_table_act)
add_fkey_def = Literal("FOREIGN") + "KEY" + "(" + Word(alphanums).setResultsName("keyName") + ")" + "REFERENCES" + Word(alphanums).setResultsName("fkTable") + "(" + Word(alphanums + "_").setResultsName("fkCol") + ")" + Optional(Literal("DEFERRABLE")) + ";"
add_fkey_def.setParseAction(add_fkey_act)
other_statement_def = OneOrMore(CharsNotIn(";")) + ";"
other_statement_def.setParseAction(other_statement_act)
comment_def = "--" + ZeroOrMore(CharsNotIn("\n"))
comment_def.setParseAction(other_statement_act)
return OneOrMore(comment_def | create_table_def | add_fkey_def | other_statement_def)
示例7: _parse
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def _parse(mystr):
LBRACE, RBRACE, EQUAL = map(pp.Suppress, "{}=")
field = pp.Word(pp.printables + ' ', excludeChars='[]=')
field.addParseAction(pp.tokenMap(str.rstrip))
string = pp.dblQuotedString().setParseAction(pp.removeQuotes)
number = pp.pyparsing_common.number()
date_expr = pp.Regex(r'\d\d\d\d-\d\d-\d\d')
time_expr = pp.Regex(r'\d\d:\d\d:\d\d\.\d\d\d')
nan = pp.Keyword('nan')
scalar_value = (string | date_expr | time_expr | number | nan)
list_marker = pp.Suppress("[]")
value_list = pp.Forward()
jobject = pp.Forward()
memberDef1 = pp.Group(field + EQUAL + scalar_value)
memberDef2 = pp.Group(field + EQUAL + jobject)
memberDef3 = pp.Group(field + list_marker + EQUAL + LBRACE + value_list +
RBRACE)
memberDef = memberDef1 | memberDef2 | memberDef3
value_list <<= (pp.delimitedList(scalar_value, ",") |
pp.ZeroOrMore(pp.Group(pp.Dict(memberDef2))))
value_list.setParseAction(lambda t: [pp.ParseResults(t[:])])
members = pp.OneOrMore(memberDef)
jobject <<= pp.Dict(LBRACE + pp.ZeroOrMore(memberDef) + RBRACE)
# force empty jobject to be a dict
jobject.setParseAction(lambda t: t or {})
parser = members
parser = pp.OneOrMore(pp.Group(pp.Dict(memberDef)))
return parser.parseString(mystr)
示例8: parse
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def parse(self, flags):
_flags = []
for k, v in flags.items():
if v == True:
_flags.append(k)
elif v in [False, None]:
pass
else:
_flags.append(k + "_" + v)
def cb_conditional(s, l, t):
if (t.cond in _flags) != (t.negate == "!"):
return t.expr
else:
return []
word = Word(alphanums + ":<>.[]_-,=~/^~")
conditional = Forward()
conditional << (
Optional("!")("negate")
+ word("cond")
+ Suppress("?")
+ Suppress("(")
+ OneOrMore(conditional ^ word)("expr")
+ Suppress(")")
).setParseAction(cb_conditional)
string = word
string_list = OneOrMore(conditional ^ string)
s = " ".join(string_list.parseString(self.__str__()))
logger.debug(
"Parsing '{}' with flags {} => {}".format(self.__str__(), str(_flags), s)
)
return s
示例9: XXXX_cast_expression
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def XXXX_cast_expression(self):
"""A function returning a parser for parsing cast expressions.
Args:
expression: a pyparsing parser for parsing an expression to be cast.
Returns:
A (pyparsing) parser for parsing cast expressions.
"""
word = pyparsing.Word(pyparsing.alphanums + '_*[]')
nested = pyparsing.Forward().setName("nested")
nested << pyparsing.Combine(
pyparsing.Literal('(').suppress()
+ pyparsing.Combine(
pyparsing.ZeroOrMore(self._integer() | word | nested))
+ pyparsing.Literal(')').suppress()
)
typeof_expression = (
_OPEN_PARENTHESIS
+ pyparsing.Keyword('typeof')
+ nested("typeof_arg")
+ _CLOSE_PARENTHESIS
)
type_expression = (
typeof_expression
| nested("simple_type")
)
return (
type_expression
+ ~(_PLUS | _MINUS)
+ self.expression("expression")
).setParseAction(self._create_cast_expression)
示例10: __init__
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def __init__(self):
# create parsing grammer
sQStringLiteral = pyparsing.QuotedString("'")
sQStringLiteral.setParseAction(
lambda s, loc, toks: StringLiteral(s, loc, toks, False))
dQStringLiteral = pyparsing.QuotedString('"', '\\')
dQStringLiteral.setParseAction(
lambda s, loc, toks: StringLiteral(s, loc, toks, True))
stringLiteral = sQStringLiteral | dQStringLiteral
functionCall = pyparsing.Forward()
functionArg = stringLiteral | functionCall
functionCall << pyparsing.Word(pyparsing.alphas, pyparsing.alphanums+'-') + \
pyparsing.Suppress('(') + \
pyparsing.Optional(functionArg +
pyparsing.ZeroOrMore(pyparsing.Suppress(',') + functionArg)) + \
pyparsing.Suppress(')')
functionCall.setParseAction(
lambda s, loc, toks: FunctionCall(s, loc, toks))
predExpr = pyparsing.infixNotation(
stringLiteral ^ functionCall ,
[
('!', 1, pyparsing.opAssoc.RIGHT, lambda s, loc, toks: NotOperator(s, loc, toks)),
('<', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryStrOperator)),
('<=', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryStrOperator)),
('>', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryStrOperator)),
('>=', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryStrOperator)),
('==', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryStrOperator)),
('!=', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryStrOperator)),
('&&', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryBoolOperator)),
('||', 2, pyparsing.opAssoc.LEFT, infixBinaryOp(BinaryBoolOperator))
])
self.__ifgrammer = predExpr
示例11: _nested_scopes
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def _nested_scopes(opening, closing, body):
"""Constructs a parser for (possibly nested) scopes."""
scope = pp.Forward()
scope << pp.Group( # pylint: disable=expression-not-assigned
opening +
pp.ZeroOrMore(body | scope)("members") +
closing)
return scope
示例12: _nested_if_else
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def _nested_if_else(if_, pred, else_, endif, match_if_true, match_if_false):
"""Constructs a parser for (possibly nested) if...(else)...endif blocks."""
ifelse = pp.Forward()
ifelse << pp.Group( # pylint: disable=expression-not-assigned
if_ +
pred("predicate") +
pp.ZeroOrMore(match_if_true | ifelse)("if_true") +
pp.Optional(else_ +
pp.ZeroOrMore(match_if_false | ifelse)("if_false")) +
endif)
return ifelse
# Some common string patterns to suppress.
# ------------------------------------------------------------------------------
示例13: jsParse
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def jsParse(inStr):
# This disaster is a context-free grammar parser for parsing javascript object literals.
# It needs to be able to handle a lot of the definitional messes you find in in-the-wild
# javascript object literals.
# Unfortunately, Javascript is /way/ more tolerant then JSON when it comes to object literals
# so we can't just parse objects using python's `json` library.
TRUE = pp.Keyword("true").setParseAction( pp.replaceWith(True) )
FALSE = pp.Keyword("false").setParseAction( pp.replaceWith(False) )
NULL = pp.Keyword("null").setParseAction( pp.replaceWith(None) )
jsonString = pp.quotedString.setParseAction( pp.removeQuotes )
jsonNumber = pp.Combine( pp.Optional('-') + ( '0' | pp.Word('123456789',pp.nums) ) +
pp.Optional( '.' + pp.Word(pp.nums) ) +
pp.Optional( pp.Word('eE',exact=1) + pp.Word(pp.nums+'+-',pp.nums) ) )
jsonObject = pp.Forward()
jsonValue = pp.Forward()
jsonDict = pp.Forward()
jsonArray = pp.Forward()
jsonElements = pp.Forward()
rawText = pp.Regex('[a-zA-Z_$][0-9a-zA-Z_$]*')
commaToNull = pp.Word(',,', exact=1).setParseAction(pp.replaceWith(None))
jsonElements << pp.ZeroOrMore(commaToNull) + pp.Optional(jsonObject) + pp.ZeroOrMore((pp.Suppress(',') + jsonObject) | commaToNull)
jsonValue << ( jsonString | jsonNumber | TRUE | FALSE | NULL )
dictMembers = pp.delimitedList( pp.Group( (rawText | jsonString) + pp.Suppress(':') + (jsonValue | jsonDict | jsonArray)))
jsonDict << ( pp.Dict( pp.Suppress('{') + pp.Optional(dictMembers) + pp.ZeroOrMore(pp.Suppress(',')) + pp.Suppress('}') ) )
jsonArray << ( pp.Group(pp.Suppress('[') + pp.Optional(jsonElements) + pp.Suppress(']') ) )
jsonObject << (jsonValue | jsonDict | jsonArray)
jsonComment = pp.cppStyleComment
jsonObject.ignore( jsonComment )
def convertDict(s, l, toks):
return dict(toks.asList())
def convertNumbers(s,l,toks):
n = toks[0]
try:
return int(n)
except ValueError:
return float(n)
jsonNumber.setParseAction(convertNumbers)
jsonDict.setParseAction(convertDict)
# jsonObject.setDebug()
jsonObject.parseString('"inStr"').pop()
return jsonObject.parseString(inStr).pop()
# Stolen from http://stackoverflow.com/a/12017573/268006
示例14: parser
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def parser(self):
"""
This function returns a parser.
The grammar should be like most full text search engines (Google, Tsearch, Lucene).
Grammar:
- a query consists of alphanumeric words, with an optional '*' wildcard
at the end of a word
- a sequence of words between quotes is a literal string
- words can be used together by using operators ('and' or 'or')
- words with operators can be grouped with parenthesis
- a word or group of words can be preceded by a 'not' operator
- the 'and' operator precedes an 'or' operator
- if an operator is missing, use an 'and' operator
"""
operatorOr = Forward()
operatorWord = Group(Combine(Word(alphanums) + Suppress('*'))).setResultsName('wordwildcard') | \
Group(Word(alphanums)).setResultsName('word')
operatorQuotesContent = Forward()
operatorQuotesContent << (
(operatorWord + operatorQuotesContent) | operatorWord
)
operatorQuotes = Group(
Suppress('"') + operatorQuotesContent + Suppress('"')
).setResultsName("quotes") | operatorWord
operatorParenthesis = Group(
(Suppress("(") + operatorOr + Suppress(")"))
).setResultsName("parenthesis") | operatorQuotes
operatorNot = Forward()
operatorNot << (Group(
Suppress(Keyword("not", caseless=True)) + operatorNot
).setResultsName("not") | operatorParenthesis)
operatorAnd = Forward()
operatorAnd << (Group(
operatorNot + Suppress(Keyword("and", caseless=True)) + operatorAnd
).setResultsName("and") | Group(
operatorNot + OneOrMore(~oneOf("and or") + operatorAnd)
).setResultsName("and") | operatorNot)
operatorOr << (Group(
operatorAnd + Suppress(Keyword("or", caseless=True)) + operatorOr
).setResultsName("or") | operatorAnd)
return operatorOr.parseString
示例15: _def_parser
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Forward [as 别名]
def _def_parser():
# Enabling packrat parsing greatly speeds up the parsing.
pyparsing.ParserElement.enablePackrat()
alphas = pyparsing.alphas
Combine = pyparsing.Combine
Forward = pyparsing.Forward
nums = pyparsing.nums
oneOf = pyparsing.oneOf
opAssoc = pyparsing.opAssoc
operatorPrecedence = pyparsing.operatorPrecedence
Word = pyparsing.Word
integer = Word(nums)
real = Combine(Word(nums) + '.' + Word(nums))
variable = Word(alphas + '_' + '.')
number = real | integer
expr = Forward()
fn = Word(alphas + '_' + '.')
operand = number | variable | fn
signop = oneOf('+ -')
addop = oneOf('+ -')
multop = oneOf('* /')
comparisonop = oneOf(' '.join(EvalComparisonOp.operations.keys()))
ternaryop = ('?', ':')
boolandop = oneOf('AND and &&')
boolorop = oneOf('OR or ||')
negateop = oneOf('NOT not !')
operand.setParseAction(EvalConstant)
expr = operatorPrecedence(operand, [
(fn, 1, opAssoc.RIGHT, EvalFunction),
("^", 2, opAssoc.RIGHT, EvalPowerOp),
(signop, 1, opAssoc.RIGHT, EvalSignOp),
(multop, 2, opAssoc.LEFT, EvalMultOp),
(addop, 2, opAssoc.LEFT, EvalAddOp),
(negateop, 1, opAssoc.RIGHT, EvalNegateOp),
(comparisonop, 2, opAssoc.LEFT, EvalComparisonOp),
(ternaryop, 3, opAssoc.LEFT, EvalTernaryOp),
(boolandop, 2, opAssoc.LEFT, EvalBoolAndOp),
(boolorop, 2, opAssoc.LEFT, EvalBoolOrOp),
(',', 2, opAssoc.RIGHT, EvalCommaSeperator), ])
return expr