本文整理汇总了Python中pyparsing.Forward.ignore方法的典型用法代码示例。如果您正苦于以下问题:Python Forward.ignore方法的具体用法?Python Forward.ignore怎么用?Python Forward.ignore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing.Forward
的用法示例。
在下文中一共展示了Forward.ignore方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_parser
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
def build_parser(root_directory, path, fake_root=os.getcwd(), file_reader=None):
from pyparsing import nestedExpr
from pyparsing import QuotedString
from pyparsing import Group
from pyparsing import restOfLine
from pyparsing import Word
from pyparsing import alphanums
from pyparsing import cStyleComment
from pyparsing import OneOrMore
from pyparsing import ZeroOrMore
from pyparsing import Optional
from pyparsing import Forward
from pyparsing import Literal
from pyparsing import Keyword
root = Forward()
include_handler = IncludeHandler(
root_directory,
path,
root,
fake_root=fake_root,
file_reader=file_reader)
# relaxed grammar
identifier = Word(alphanums + "-_.:/")
comment = ("//" + restOfLine).suppress() \
| ("#" + restOfLine).suppress() \
| cStyleComment
endstmt = Literal(";").suppress()
argument = QuotedString('"') \
| identifier
arguments = ZeroOrMore(argument)
statements = Forward()
section = nestedExpr("{", "}", statements)
include = Keyword("include").suppress() + QuotedString('"')
regular = identifier + Group(arguments) + Optional(section, default=[])
statement = include.setParseAction(include_handler.pyparsing_call) \
| regular.setParseAction(include_handler.pyparsing_mark)
statements << OneOrMore(statement + endstmt)
root << Optional(statements)
root.ignore(comment)
setattr(
root, 'parse_file',
lambda f, root=root: root.parseFile(f, parseAll=True))
return root
示例2: Optional
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
_index_stmt << (
Optional(create_kw)
+ index_kw
+ index_source.setResultsName('source')
+ '(' + column_name_list.setResultsName('columns') + ')')
# Examples:
# index = index_stmt.parseString('INDEX partition1 (col1, col2, col3);')
# print(index.source)
# 'partition1'
# print(index.columns)
# ['col1', 'col2', 'col3']
# define Oracle comment format, and ignore them
oracle_sql_comment = '--' + restOfLine
_view_stmt.ignore(oracle_sql_comment)
_index_stmt.ignore(oracle_sql_comment)
def substitute_vids(library, statement):
""" Replace all of the references to tables and partitions with their vids.
This is a bit of a hack -- it ought to work with the parser, but instead it just looks for
common SQL tokens that indicate an identifier.
:param statement: an sqlstatement. String.
:return: tuple: new_statement, set of table vids, set of partition vids.
"""
from ambry.identity import ObjectNumber, TableNumber, NotObjectNumberError
from ambry.orm.exc import NotFoundError
示例3: operatorPrecedence
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
term << operatorPrecedence(
number | predicate | variable,
[
(oneOf("+ -"), 1, opAssoc.RIGHT, FOLUnOp),
(oneOf("^"), 2, opAssoc.LEFT, FOLBinOp),
(oneOf("* /"), 2, opAssoc.LEFT, FOLBinOp),
(oneOf("+ -"), 2, opAssoc.LEFT, FOLBinOp),
(oneOf("< <= > >= "), 2, opAssoc.LEFT, FOLBinOp),
],
)
# main parser for FOL formula
formula = Forward()
formula.ignore(comment)
forall_expression = Group(
forall.setResultsName("quantifier")
+ delimitedList(variable).setResultsName("vars")
+ colon
+ formula.setResultsName("args")
).setParseAction(FOLQuant)
exists_expression = Group(
exists.setResultsName("quantifier")
+ delimitedList(variable).setResultsName("vars")
+ colon
+ formula.setResultsName("args")
).setParseAction(FOLQuant)
operand = forall_expression | exists_expression | boolean | term
示例4: performIPOperatorSanityCheck
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
def performIPOperatorSanityCheck(componentName, propagationDimension, operatorCodeSlice, codeBlock):
"""
Check that the user hasn't tried to use an IP operator where an IP operator cannot be used.
IP operators must be diagonal, so one cannot have expressions of the form ``dy_dt = L[x];`` for IP operators.
This is valid for EX operators, but not for IP. This is a common mistake for users to make, and so we should
do our best to spot it and report the error. Another mistake users make is trying to multiply the operator,
for example ``dy_dt = i*L[y];``. This code does a sophisticated validation by constructing a parse tree for
each statement in the code taking into account operator precedence. This sanity checking is even able to pick
up problems such as ``dphi_dt = i*(V*phi + U*mod2(phi)*phi + T[phi]);``.
If the user's code passes this test, then it is a reasonable assumption that they are using IP operators safely.
"""
operatorString = codeBlock.codeString[operatorCodeSlice]
expr = Forward()
operatorKeyword = Keyword(operatorString).setResultsName('targetOperator')
operand = operatorKeyword \
| (identifier + Group('(' + delimitedList(expr) + ')')) \
| (identifier + Group(OneOrMore('[' + expr + ']'))) \
| quotedString.copy() \
| identifier \
| numericConstant
operand.ignore(cppStyleComment.copy())
expr << operatorPrecedence(
operand,
[
(oneOf('++ --'), 1, opAssoc.LEFT),
(oneOf('. ->'), 2, opAssoc.LEFT),
(~oneOf('-> -= += *= &= |=') + oneOf('+ - ! ~ * & ++ --'), 1, opAssoc.RIGHT),
(~oneOf('*= /= %=') + oneOf('* / %'), 2, opAssoc.LEFT),
(~oneOf('++ -- -> -= +=') + oneOf('+ -'), 2, opAssoc.LEFT),
# Although the operators below don't all have the same precedence, as we don't actually
# care about them as they are all invalid uses of the IP operator, we can cheat and lump
# them together
(~oneOf('<<= >>= &= |=') + oneOf('<< >> < <= > >= == != & ^ | && ||'), 2, opAssoc.LEFT),
# Correct ordering
# (~oneOf('<<= >>=') + oneOf('<< >>'), 2, opAssoc.LEFT),
# (~oneOf('<< >> <<= >>=') + oneOf('< <= > >='), 2, opAssoc.LEFT),
# (oneOf('== !='), 2, opAssoc.LEFT),
# (~oneOf('&& &=') + '&', 2, opAssoc.LEFT),
# ('^', 2, opAssoc.LEFT),
# (~oneOf('|| |=') + '|', 2, opAssoc.LEFT),
# ('&&', 2, opAssoc.LEFT),
# ('||', 2, opAssoc.LEFT),
(('?',':'), 3, opAssoc.RIGHT),
(~Literal('==') + oneOf('= += -= *= /= %= <<= >>= &= ^= |= =>'), 2, opAssoc.RIGHT),
(',', 2, opAssoc.LEFT),
]
)
expr.ignore(cppStyleComment.copy())
statement = expr + Suppress(';')
stack = []
expectedAssignmentVariable = 'd%(componentName)s_d%(propagationDimension)s' % locals()
def validateStack():
"""
It is the job of this function to validate the operations that the located operator is involved in.
The stack describes the part of the parse tree in which the operator was found. The first element in the stack
is the outermost operation, and the last the innermost. The last element is guaranteed to be the operator itself.
"""
# Reverse the stack as we want to search the parse tree from inner-most expression to outer-most.
stack.reverse()
assignmentHit = False
errorMessageCommon = "Due to the way IP operators work, they can only contribute to the derivative of the variable " \
"they act on, i.e. dx_dt = L[x]; not dy_dt = L[x];\n\n"
# We don't need to check the first element of the stack
# as we are guaranteed that it is the operator itself. This will be useful for determining
# which part of the parse tree we should be looking at.
for idx, node in enumerate(stack[1:]):
if len(node) == 1: continue
# idx is the index in the stack of the next element *deeper* in the parse tree.
previousStackEntry = stack[idx]
if not isinstance(stack[idx], basestring):
previousStackEntry = previousStackEntry.asList()
binaryOpIdx = node.asList().index(previousStackEntry) - 1
if binaryOpIdx < 0: binaryOpIdx = 1
# Unary '+' is safe.
if node[0] == '+': continue
# Binary '+' is safe.
if node[binaryOpIdx] == '+': continue
# Binary '-' is safe if the operator is the first argument.
if node[binaryOpIdx] == '-' and node.asList().index(previousStackEntry) == 0: continue
# Assignment is safe if it there is only one, and if it's to the right variable
if node[binaryOpIdx] in ['=', '+=']:
if node[0] == expectedAssignmentVariable:
assignmentHit = True
continue
else:
return errorMessageCommon + "In this case, you should probably use an EX operator instead of an "\
"IP operator."
else:
return errorMessageCommon + "You appear to be using the IP operator in an unsafe operation. " \
"The most likely cause is trying to multiply it by something, e.g. dphi_dt = 0.5*L[phi]; "\
#.........这里部分代码省略.........
示例5: Word
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
identifier = Word(alphas + '_', alphanums + '_')
numericConstant = Regex(r'\b((0(x|X)[0-9a-fA-F]*)|(([0-9]+\.?[0-9]*)|(\.[0-9]+))((e|E)(\+|-)?[0-9]+)?)(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\b')
ignoreExpr = cppStyleComment.copy() | quotedString.copy()
baseExpr = Forward()
arrayAccess = originalTextFor(nestedExpr('[', ']', baseExpr, ignoreExpr))
parenthisedExpression = originalTextFor(nestedExpr('(', ')', baseExpr, ignoreExpr))
functionCall = nestedExpr('(', ')', delimitedList(baseExpr), ignoreExpr)
alphaNumPlusSafePunctuation = alphanums + '!#$%&\\*+-./:;<=>@^_`{|}~'
baseExpr << OneOrMore(originalTextFor(identifier + functionCall) | quotedString.copy() \
| identifier | numericConstant | arrayAccess | parenthisedExpression \
| Word(alphaNumPlusSafePunctuation))
baseExpr.ignore(cppStyleComment.copy())
def targetComponentsForOperatorsInString(operatorNames, codeBlock):
"""
Return a list of pairs of operator names and their targets that are in `codeString`.
The valid operator names searched for are `operatorNames`. For example, if 'L' is in `operatorNames`,
then in the code ``L[phi]`` the return value would be ``('L', 'phi', slice(firstCharacterIndex, lastCharacterIndex))``.
"""
parser = MatchFirst(Keyword(operatorName) for operatorName in operatorNames).setResultsName('name') \
+ Optional(nestedExpr('[', ']', baseExpr, ignoreExpr).setResultsName('target'))
parser.ignore(cppStyleComment.copy())
parser.ignore(quotedString.copy())
results = []
for tokens, start, end in parser.scanString(codeBlock.codeString):
if 'target' in tokens:
示例6: parser
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
def parser(text):
cvtTuple = lambda toks: tuple(toks.asList())
cvtRaw = lambda toks: RawString(' '.join(map(str, toks.asList())))
#cvtDict = lambda toks: dict(toks.asList())
cvtGlobDict = lambda toks: GlobDict(toks.asList())
cvtDict = cvtGlobDict
extractText = lambda s, l, t: RawString(s[t._original_start:t._original_end])
def pythonize(toks):
s = toks[0]
if s == 'true':
return True
elif s == 'false':
return False
elif s == 'none':
return [None]
elif s.isdigit():
return int(s)
elif re.match('(?i)^-?(\d+\.?e\d+|\d+\.\d*|\.\d+)$', s):
return float(s)
return toks[0]
def noneDefault(s, loc, t):
return t if len(t) else [RawEOL]
# define punctuation as suppressed literals
lbrace, rbrace = map(Suppress, "{}")
identifier = Word(printables, excludeChars='{}"\'')
quotedStr = QuotedString('"', escChar='\\', multiline=True) | \
QuotedString('\'', escChar='\\', multiline=True)
quotedIdentifier = QuotedString('"', escChar='\\', unquoteResults=False) | \
QuotedString('\'', escChar='\\', unquoteResults=False)
dictStr = Forward()
setStr = Forward()
objStr = Forward()
#anyIdentifier = identifier | quotedIdentifier
oddIdentifier = identifier + quotedIdentifier
dictKey = dictStr | quotedStr | \
Combine(oddIdentifier).setParseAction(cvtRaw)
dictKey.setParseAction(cvtRaw)
dictValue = quotedStr | dictStr | setStr | \
Combine(oddIdentifier).setParseAction(cvtRaw)
if OLD_STYLE_KEYS:
dictKey |= Combine(identifier + ZeroOrMore(White(' ') + (identifier + ~FollowedBy(Optional(White(' ')) + LineEnd()))))
dictValue |= identifier.setParseAction(pythonize)
else:
dictKey |= identifier
dictValue |= delimitedList(identifier | quotedIdentifier, delim=White(' '), combine=True).setParseAction(pythonize)
ParserElement.setDefaultWhitespaceChars(' \t')
#dictEntry = Group(Combine(OneOrMore(identifier | quotedIdentifier)).setParseAction(cvtRaw) +
dictEntry = Group(dictKey +
Optional(White(' ').suppress() + dictValue).setParseAction(noneDefault) +
Optional(White(' ').suppress()) +
LineEnd().suppress())
#dictEntry = Group(SkipTo(dictKey + LineEnd() + dictKey))
dictStr << (lbrace + ZeroOrMore(dictEntry) + rbrace)
dictStr.setParseAction(cvtDict)
ParserElement.setDefaultWhitespaceChars(' \t\r\n')
setEntry = identifier.setParseAction(pythonize) | quotedString.setParseAction(removeQuotes)
setStr << (lbrace + delimitedList(setEntry, delim=White()) + rbrace)
setStr.setParseAction(cvtTuple)
# TODO: take other literals as arguments
blobObj = Group(((Literal('ltm') + Literal('rule') + identifier) | \
(Literal('rule') + identifier)).setParseAction(cvtRaw) +
originalTextFor(nestedExpr('{', '}')).setParseAction(extractText))
objEntry = Group(OneOrMore(identifier | quotedIdentifier).setParseAction(cvtRaw) +
Optional(dictStr).setParseAction(noneDefault))
objStr << (Optional(delimitedList(blobObj | objEntry, delim=LineEnd())))
objStr.setParseAction(cvtGlobDict)
#objStr.setParseAction(cvtTuple)
objStr.ignore(pythonStyleComment)
return objStr.parseString(text)[0]
示例7: import
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
from pyparsing import (Regex, OneOrMore, Forward, delimitedList, restOfLine, Group as Grp,
Suppress)
from musicobject import Tone, Group, Transformed
musicobject = Forward()
comment = '#' + restOfLine
musicobject.ignore(comment)
#fraction = Regex(r'(\d*[./]?\d*)')
number = Regex(r'[\d./]+')
number.setParseAction(lambda s, l, t: [float(eval(t[0]))])
frequency_symbol = Regex(r'[abcdefg_]\d?[#-]?')
frequency_number = number
frequency = frequency_number ^ frequency_symbol
duration = number
tone = frequency ^ (Suppress('(') + frequency + Suppress(',') + duration + Suppress(')'))
tone.setParseAction(lambda s, l, t: Tone(*t))
group = Suppress('{') + delimitedList(Grp(OneOrMore(musicobject)), ',') + Suppress('}')
group.setParseAction(lambda s, l, t: Group(t))
transformed = tone + '*' + musicobject
transformed.setParseAction(lambda s, l, t: Transformed(t[0], t[2]))
musicobject << (tone ^ group ^ transformed)
示例8: Regex
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import ignore [as 别名]
ID = ~MatchFirst([Keyword(w) for w in _keywords]) + Regex(r"[a-zA-Z_][a-zA-Z0-9_$]*")("id")
LP, RP, LB, RB, LC, RC, COLON, SEMICOLON, CAMMA, PERIOD, SHARP, EQUAL, AT, ASTA, Q, PLUS, MINUS, USC, APS = map(
Suppress, ("()[]{}:;,.#[email protected]*?+-_'")
)
DSLASH = Suppress(Literal("//"))
for k in _keywords:
setattr(this_mod, k.swapcase(), Keyword(k)("keyword"))
# setattr(sys.modules[__name__],k,Literal(k))
with open(_non_terminal_symbols_file, "r") as f:
for name in (line.strip() for line in f):
sym = Forward()(name)
sym.enablePackrat()
sym.ignore(cStyleComment)
# print("sym={0}".format(name))
setattr(this_mod, name, sym)
def alias(grammar, name):
if name:
return Group(grammar)(name)
else:
return Group(grammar)
class ErrorReportException(ParseException):
pass