本文整理汇总了Python中pyparsing.ParserElement类的典型用法代码示例。如果您正苦于以下问题:Python ParserElement类的具体用法?Python ParserElement怎么用?Python ParserElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ParserElement类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self):
from pyparsing import (ParserElement, StringEnd, LineEnd, Literal,
pythonStyleComment, ZeroOrMore, Suppress,
Optional, Combine, OneOrMore, Regex, oneOf,
QuotedString, Group, ParseException)
ParserElement.setDefaultWhitespaceChars("\t ")
EOF = StringEnd()
EOL = ~EOF + LineEnd() # EOL must not match on EOF
escape = Literal("\\")
comment = pythonStyleComment
junk = ZeroOrMore(comment | EOL).suppress()
## word (i.e: single argument string)
word = Suppress(escape + EOL + Optional(comment)) \
| Combine(OneOrMore( escape.suppress() + Regex(".") |
QuotedString("'", escChar='\\', multiline=True) |
QuotedString('"', escChar='\\', multiline=True) |
Regex("[^ \t\r\n\f\v\\\\$&<>();\|\'\"`]+") |
Suppress(escape + EOL) ))
## redirector (aka bash file redirectors, such as "2>&1" sequences)
fd_src = Regex("[0-2]").setParseAction(lambda t: int(t[0]))
fd_dst = Suppress("&") + fd_src
# "[n]<word" || "[n]<&word" || "[n]<&digit-"
fd_redir = (Optional(fd_src, 0) + Literal("<")
|Optional(fd_src, 1) + Literal(">"))\
+(word | (fd_dst + Optional("-")))
# "&>word" || ">&word"
full_redir = (oneOf("&> >&") + word)\
.setParseAction(lambda t:("&" ,">", t[-1]))
# "<<<word" || "<<[-]word"
here_doc = Regex("<<(<|-?)") + word
# "[n]>>word"
add_to_file = Optional(fd_src | Literal("&"), 1) + \
Literal(">>") + word
# "[n]<>word"
fd_bind = Optional(fd_src, 0) + Literal("<>") + word
redirector = (fd_redir | full_redir | here_doc
| add_to_file | fd_bind)\
.setParseAction(lambda token: tuple(token))
## single command (args/redir list)
command = Group(OneOrMore(redirector | word))
## logical operators (section splits)
semicolon = Suppress(";") + junk
connector = (oneOf("&& || |") + junk) | semicolon
## pipeline, aka logical block of interconnected commands
pipeline = junk + Group(command +
ZeroOrMore(connector + command) +
Optional(semicolon))
# define object attributes
self.LEXER = pipeline.ignore(comment) + EOF
self.parseException = ParseException
开发者ID:0x0mar,项目名称:phpsploit,代码行数:60,代码来源:lexer.py
示例2: read_sets_java
def read_sets_java(string):
from pyparsing import nestedExpr, alphas, Word, nums, ParserElement, delimitedList
ParserElement.setDefaultWhitespaceChars(" ,")
element = Word(alphas + nums).setParseAction(parse_elem_java)
elements = delimitedList(element)
setofsets = nestedExpr("[", "]", content=elements).setParseAction(lambda x: frozenset(x[0]))
return setofsets.parseString(string).asList()[0]
开发者ID:VeraLiconaResearchGroup,项目名称:MHSGenerationAlgorithms,代码行数:7,代码来源:sethelper.py
示例3: hwdb_grammar
def hwdb_grammar():
ParserElement.setDefaultWhitespaceChars('')
prefix = Or(category + ':' + Or(conn) + ':'
for category, conn in TYPES.items())
matchline = Combine(prefix + Word(printables + ' ' + '®')) + EOL
propertyline = (
White(' ', exact=1).suppress()
+ Combine(UDEV_TAG
- '='
- Word(alphanums + '_=:@*.! ')
- Optional(pythonStyleComment))
+ EOL
)
propertycomment = White(' ', exact=1) + pythonStyleComment + EOL
group = (
OneOrMore(matchline('MATCHES*') ^ COMMENTLINE.suppress())
- OneOrMore(propertyline('PROPERTIES*') ^ propertycomment.suppress())
- (EMPTYLINE ^ stringEnd()).suppress()
)
commentgroup = OneOrMore(COMMENTLINE).suppress() - EMPTYLINE.suppress()
grammar = OneOrMore(group('GROUPS*') ^ commentgroup) + stringEnd()
return grammar
开发者ID:ybakos,项目名称:libinput,代码行数:26,代码来源:parse_hwdb.py
示例4: property_grammar
def property_grammar():
ParserElement.setDefaultWhitespaceChars(' ')
dpi_setting = (Optional('*')('DEFAULT') + INTEGER('DPI') + Suppress('@') + INTEGER('HZ'))('SETTINGS*')
mount_matrix_row = SIGNED_REAL + ',' + SIGNED_REAL + ',' + SIGNED_REAL
mount_matrix = (mount_matrix_row + ';' + mount_matrix_row + ';' + mount_matrix_row)('MOUNT_MATRIX')
props = (('MOUSE_DPI', Group(OneOrMore(dpi_setting))),
('MOUSE_WHEEL_CLICK_ANGLE', INTEGER),
('MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL', INTEGER),
('MOUSE_WHEEL_CLICK_COUNT', INTEGER),
('MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL', INTEGER),
('ID_INPUT_TRACKBALL', Literal('1')),
('POINTINGSTICK_SENSITIVITY', INTEGER),
('POINTINGSTICK_CONST_ACCEL', REAL),
('ID_INPUT_TOUCHPAD_INTEGRATION', Or(('internal', 'external'))),
('XKB_FIXED_LAYOUT', STRING),
('XKB_FIXED_VARIANT', STRING),
('ACCEL_MOUNT_MATRIX', mount_matrix),
)
fixed_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE')
for name, val in props]
kbd_props = [Regex(r'KEYBOARD_KEY_[0-9a-f]+')('NAME')
- Suppress('=') -
('!' ^ (Optional('!') - Word(alphanums + '_')))('VALUE')
]
abs_props = [Regex(r'EVDEV_ABS_[0-9a-f]{2}')('NAME')
- Suppress('=') -
Word(nums + ':')('VALUE')
]
grammar = Or(fixed_props + kbd_props + abs_props) + EOL
return grammar
开发者ID:tsmock,项目名称:systemd,代码行数:34,代码来源:parse_hwdb.py
示例5: defineParsers
def defineParsers():
#Enable a fast parsing mode with caching.
ParserElement.enablePackrat()
#end of line terminates statements, so it is not regular whitespace
ParserElement.setDefaultWhitespaceChars('\t ')
func_call = Forward() #forward declaration because this is a recursive rule
#The "terminal" rules
symbol = Word(alphas+'_-', alphanums+'_-') .setParseAction(action_symbol)
q_symbol = quotedString .setParseAction(action_q_symbol)
bracket_term = Literal("(").suppress() - func_call \
+ Literal(")").suppress()
word = symbol | q_symbol | bracket_term
#The function call
#Parse: "foo | bar | baz" or "foo"
pipeline = (word + ZeroOrMore("|" - word)) .setParseAction(action_pipeline)
#Parse "foo|bar op1 op2 op3"
func_call << (pipeline - ZeroOrMore(word)) .setParseAction(action_func_call)
#High level structure of program
line = LineEnd() | func_call - LineEnd() #empty line or function call
program = ZeroOrMore(line) + StringEnd() #multiple lines are a program
#define the comments
program.ignore('%' + restOfLine)
#no tab expansion
program.parseWithTabs()
#return additional func_call parser to make testing more easy
return program, func_call
开发者ID:eike-welk,项目名称:freeode,代码行数:31,代码来源:simple_config_lang.py
示例6: parser
def parser():
global _parser
if _parser is None:
ParserElement.setDefaultWhitespaceChars("")
lbrack, rbrack, lbrace, rbrace, lparen, rparen = map(Literal, "[]{}()")
reMacro = Combine("\\" + oneOf(list("dws")))
escapedChar = ~ reMacro + Combine("\\" + oneOf(list(printables)))
reLiteralChar = "".join(c for c in printables if c not in r"\[]{}().*?+|") + " \t"
reRange = Combine(lbrack + SkipTo(rbrack, ignore=escapedChar) + rbrack)
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("*+?"))
)
reRange.setParseAction(handle_range)
reLiteral.setParseAction(handle_literal)
reMacro.setParseAction(handle_macro)
reDot.setParseAction(handle_dot)
reTerm = (reLiteral | reRange | reMacro | reDot)
reExpr = operatorPrecedence(reTerm, [
(repetition, 1, opAssoc.LEFT, handle_repetition),
(None, 2, opAssoc.LEFT, handle_sequence),
(Suppress('|'), 2, opAssoc.LEFT, handle_alternative),
])
_parser = reExpr
return _parser
开发者ID:Bernie,项目名称:spyne,代码行数:34,代码来源:invregexp.py
示例7: property_grammar
def property_grammar():
ParserElement.setDefaultWhitespaceChars(' ')
model_props = [
Regex(r'LIBINPUT_MODEL_[_0-9A-Z]+')('NAME') -
Suppress('=') - (Literal('1'))('VALUE')
]
dimension = INTEGER('X') + Suppress('x') + INTEGER('Y')
crange = INTEGER('X') + Suppress(':') + INTEGER('Y')
vprops = (
('LIBINPUT_ATTR_SIZE_HINT', Group(dimension('SETTINGS*'))),
('LIBINPUT_ATTR_RESOLUTION_HINT', Group(dimension('SETTINGS*'))),
('LIBINPUT_ATTR_PRESSURE_RANGE', Group(crange('SETTINGS*'))),
('LIBINPUT_ATTR_TOUCH_SIZE_RANGE', Group(crange('SETTINGS*'))),
('LIBINPUT_ATTR_TPKBCOMBO_LAYOUT', Or(('below'))),
('LIBINPUT_ATTR_LID_SWITCH_RELIABILITY',
Or(('reliable', 'write_open'))),
('LIBINPUT_ATTR_KEYBOARD_INTEGRATION', Or(('internal', 'external'))),
)
value_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE') for
name, val in vprops]
tprops = (
('LIBINPUT_ATTR_PALM_PRESSURE_THRESHOLD', INTEGER('X')),
('LIBINPUT_ATTR_PALM_SIZE_THRESHOLD', INTEGER('X')),
)
typed_props = [Literal(name)('NAME') - Suppress('=') - val
for name, val in tprops]
grammar = Or(model_props + value_props + typed_props)
return grammar
开发者ID:ybakos,项目名称:libinput,代码行数:34,代码来源:parse_hwdb.py
示例8: property_grammar
def property_grammar():
ParserElement.setDefaultWhitespaceChars(' ')
setting = Optional('*')('DEFAULT') + INTEGER('DPI') + Suppress('@') + INTEGER('HZ')
props = (('MOUSE_DPI', Group(OneOrMore(setting('SETTINGS*')))),
('MOUSE_WHEEL_CLICK_ANGLE', INTEGER),
('MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL', INTEGER),
('ID_INPUT_TRACKBALL', Literal('1')),
('POINTINGSTICK_SENSITIVITY', INTEGER),
('POINTINGSTICK_CONST_ACCEL', REAL),
('ID_INPUT_TOUCHPAD_INTEGRATION', Or(('internal', 'external'))),
)
fixed_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE')
for name, val in props]
kbd_props = [Regex(r'KEYBOARD_KEY_[0-9a-f]+')('NAME')
- Suppress('=') -
('!' ^ (Optional('!') - Word(alphanums + '_')))('VALUE')
]
abs_props = [Regex(r'EVDEV_ABS_[0-9a-f]{2}')('NAME')
- Suppress('=') -
Word(nums + ':')('VALUE')
]
grammar = Or(fixed_props + kbd_props + abs_props)
return grammar
开发者ID:bkylerussell,项目名称:systemd,代码行数:26,代码来源:parse_hwdb.py
示例9: __init__
def __init__(self, alphabet):
self.operators = alphabet.getOperators()
self.constants = alphabet.getConstants()
self.notNeedSpace = alphabet.notNeedSpace()
self.ffactory = FormulaFactory()
self.__createGram()
ParserElement.enablePackrat()
开发者ID:pszynk,项目名称:LIProjekt,代码行数:8,代码来源:parsers.py
示例10: _parse_items
def _parse_items(self, source):
ParserElement.setDefaultWhitespaceChars(' \t\r')
EOL = LineEnd().suppress()
comment = Literal('#') + Optional( restOfLine ) + EOL
string = CharsNotIn("\n")
line = Group(
Word(alphanums + '-')('key') + Literal(':').suppress() + Optional(Combine(string + ZeroOrMore(EOL + Literal(' ') + string)))("value") + EOL
)
group = ZeroOrMore(line)
group.ignore(comment)
return group.parseString(source, True)
开发者ID:centrumholdings,项目名称:citools,代码行数:11,代码来源:control.py
示例11: set_delimiters
def set_delimiters(self, delimiter):
"""Lets you change the delimiter that is used to identify field
boundaries.
delimiter: str
A string containing characters to be used as delimiters. The
default value is ' \t'. which means that spaces and tabs are not
taken as data but instead mark the boundaries. Note that the
parser is smart enough to recognize characters within quotes as
non-delimiters."""
self.delimiter = delimiter
if delimiter != "columns":
ParserElement.setDefaultWhitespaceChars(str(delimiter))
开发者ID:akhi28,项目名称:OpenMDAO-Framework,代码行数:14,代码来源:filewrap.py
示例12: pythonVar
def pythonVar(self):
if not self._pythonVar:
from pyparsing import (ParserElement, Word, alphas, alphanums,
Literal, Suppress, FollowedBy)
_ws = ' \t'
ParserElement.setDefaultWhitespaceChars(_ws)
ident = Word(alphas+"_", alphanums+"_")
lparen = Literal("(")
dot = Literal(".")
dollar = Literal("$")
self._pythonVar = Suppress(dollar) + ident + ~FollowedBy((dot+ident) | lparen)
self._pythonVar.setParseAction(self.onPythonVar)
return self._pythonVar
开发者ID:David-Bess,项目名称:medicare-demo,代码行数:14,代码来源:pysoc.py
示例13: 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
开发者ID:GunioRobot,项目名称:pycucumber,代码行数:48,代码来源:regex_parser.py
示例14: pn_from_sis
def pn_from_sis(filename):
"""Loads a PN in SIS format."""
# definition of PN grammar
ParserElement.setDefaultWhitespaceChars(" \t")
id = Word(alphanums+"_\"':-")
#place = Literal("p") + Word(nums)
number = Word(nums).setParseAction(lambda tokens: int(tokens[0]))
newlines = Suppress(OneOrMore(LineEnd()))
modelName = ".model" + id("modelName") + newlines
signalNames = ZeroOrMore( Suppress(oneOf(".inputs .outputs .dummy")) + OneOrMore( id ) + newlines)("signals")
arc = id + ZeroOrMore(Group(id + Optional(Suppress("(")+number+Suppress(")"), default=1))) + newlines
graph = Literal(".graph") + Suppress(OneOrMore(LineEnd())) + OneOrMore(Group(arc))("arcs")
capacity_list = ZeroOrMore(Group(id+Suppress("=")+number))
capacity = ".capacity" + capacity_list("capacities") + newlines
marking_list = ZeroOrMore(Group(id+Optional(Suppress("=")+number,default=1)))
marking = ".marking"+Suppress("{") + marking_list("marking") + Suppress("}") + newlines
pn = Optional(newlines) + Optional(modelName) + signalNames + graph + Optional(capacity) + marking + ".end"
pn.ignore(pythonStyleComment)
net = PetriNet(filename=filename, format='sis')
ast = pn.parseFile( filename )
for t in ast.signals:
net.add_transition( t )
#net.name = ast.modelName
net.set_name(ast.modelName)
#net.signals.update( ast.signals )
# tuplelist = [ (m[0],m[1]) for m in ast.capacities ]
# net.capacities = dict( tuplelist )
# net.initial_marking = dict( [ (m[0],m[1]) for m in ast.marking ] )
#print ast.arcs
transitions = set(net.get_transitions())
for a in ast.arcs:
#print a[0]
if a[0] not in transitions:
# it's a place
p = net.add_place(a[0])
for t in a[1:]:
net.add_edge(p,t[0],t[1])
else:
for t in a[1:]:
p = net.add_place(t[0])
net.add_edge(a[0],p,t[1])
for m in ast.marking:
net.set_initial_marking(m[0],m[1])
for m in ast.capacities:
net.set_capacity(m[0],m[1])
net.to_initial_marking()
return net
开发者ID:andycsoto,项目名称:pmlab,代码行数:47,代码来源:utils.py
示例15: rfc2822
def rfc2822():
global _rfc2822
if _rfc2822 is None:
ParserElement.setDefaultWhitespaceChars("")
CRLF = Literal("\r\n")
ATEXT = Regex("[a-zA-Z0-9!#$%&'*+\-/=\?^_`{|}~]")
TEXT = Regex("[\x01-\x09\x0b\x0c\x0e-\x7f]")
QTEXT = Regex("[\x01-\x08\x0b\x0c\x0d-\x1f\x21\x23-\x5b\x5d-\x7f]")
LOWASCII = Regex("[\x00-\x7f]")
DTEXT = Regex("[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x5e-\x7f]")
WSP = Regex("[\x20\x09]")
CTEXT = Regex("[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x27\x2a-\x5b\x5d-\x7f]")
obsQp = r"\\" + LOWASCII
quotedPair = (r"\\" + TEXT) | obsQp
obsFWS = OneOrMore(WSP) + ZeroOrMore(CRLF + OneOrMore(WSP))
FWS = (Optional(ZeroOrMore(WSP) + CRLF) + OneOrMore(WSP)) | obsFWS
comment = Forward()
ccontent = CTEXT | quotedPair | comment
comment << "(" + ZeroOrMore(Optional(FWS) + ccontent) + Optional(FWS) + ")"
CFWS = ZeroOrMore(Optional(FWS) + comment) + ((Optional(FWS) + comment) | FWS)
atom = Optional(CFWS) + OneOrMore(ATEXT) + Optional(CFWS)
dotAtomText = OneOrMore(ATEXT) + ZeroOrMore("." + OneOrMore(ATEXT))
dotAtom = Optional(CFWS) + dotAtomText + Optional(CFWS)
qcontent = QTEXT | quotedPair
quotedString = Optional(CFWS) + '"' + ZeroOrMore(Optional(FWS) + qcontent) + Optional(FWS) + '"'
word = atom | quotedString
obsLocalPart = word + ZeroOrMore("." + word)
localPart = dotAtom | quotedString | obsLocalPart
dcontent = DTEXT | quotedPair
domainLiteral = Optional(CFWS) + "[" + ZeroOrMore(Optional(FWS) + dcontent) + Optional(FWS) + "]" + Optional(CFWS)
obsDomain = atom + ZeroOrMore("." + atom)
domain = dotAtom | domainLiteral | obsDomain
addrSpec = localPart + "@" + domain
_rfc2822 = addrSpec
return _rfc2822
开发者ID:green-span,项目名称:green-mail,代码行数:46,代码来源:util.py
示例16: property_grammar
def property_grammar():
ParserElement.setDefaultWhitespaceChars(' ')
model_props = [Regex(r'LIBINPUT_MODEL_[_0-9A-Z]+')('NAME')
- Suppress('=') -
(Literal('1'))('VALUE')
]
dimension = INTEGER('X') + Suppress('x') + INTEGER('Y')
sz_props = (
('LIBINPUT_ATTR_SIZE_HINT', Group(dimension('SETTINGS*'))),
('LIBINPUT_ATTR_RESOLUTION_HINT', Group(dimension('SETTINGS*'))),
)
size_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE')
for name, val in sz_props]
grammar = Or(model_props + size_props);
return grammar
开发者ID:jiixyj,项目名称:libinput,代码行数:19,代码来源:parse_hwdb.py
示例17: read_sets
def read_sets(string):
"""
>>> read_sets("{}")
frozenset([])
>>> read_sets("{1}")
frozenset([1])
>>> read_sets("{{}, {}}") # invalid, outer set contains two equal sets
frozenset([frozenset([])])
>>> read_sets("{{{1}, {2}}, {3}}")
frozenset([frozenset([frozenset([2]), frozenset([1])]), frozenset([3])])
>>> read_sets("{1, 2,3}")
frozenset([1, 2, 3])
>>> read_sets("{{1, 2}, {3, 4}}")
frozenset([frozenset([1, 2]), frozenset([3, 4])])
>>> read_sets("{a,b,c}")
frozenset(['a', 'c', 'b'])
>>> read_sets('[{1,2,3},{a,c,b}]')
[frozenset([1, 2, 3]), frozenset(['a', 'c', 'b'])]
>>> read_sets('{a}')
frozenset(['a'])
>>> read_sets('{{x1,x2},{x3}}')
frozenset([frozenset(['x2', 'x1']), frozenset(['x3'])])
>>> read_sets('{{23gat,24gat}}')
frozenset([frozenset(['23gat', '24gat'])])
"""
from pyparsing import nestedExpr, alphas, Word, nums, ParserElement, delimitedList
ParserElement.setDefaultWhitespaceChars(" ,")
element = Word(alphas + nums).setParseAction(parse_elem)
elements = delimitedList(element)
setofsets = nestedExpr("{", "}", content=elements).setParseAction(lambda x: frozenset(x[0]))
listofsets = nestedExpr("[", "]", content=setofsets)
expr = setofsets | listofsets
return expr.parseString(string).asList()[0]
开发者ID:VeraLiconaResearchGroup,项目名称:MHSGenerationAlgorithms,代码行数:43,代码来源:sethelper.py
示例18: _int_expression
def _int_expression():
from pyparsing import Word, alphanums, nums, Forward, ZeroOrMore, Combine, CaselessLiteral, srange, ParserElement, Optional
ParserElement.enablePackrat()
entry_name = Word(alphanums + ' _+:.-/')
integer = Combine(Optional('-') + Word(nums)).addParseAction(lambda s,l,t: [Constant(int(t[0]))])
hex = Combine(CaselessLiteral("0x") + Word(srange("[0-9a-fA-F]"))).addParseAction(lambda s,l,t:[Constant(int(t[0][2:], 16))])
named_reference = ('${' + entry_name + '}').addParseAction(lambda s,l,t:ValueResult(t[1]))
length_reference = ('len{' + entry_name + '}').addParseAction(lambda s,l,t:LengthResult(t[1]))
expression = Forward()
factor = hex | integer | named_reference | length_reference | ('(' + expression + ')').addParseAction(lambda s,l,t:t[1])
entry = factor
for ops in _operators:
op_parse = reduce(operator.or_,
[(character + entry).addParseAction(_half(op)) for character, op in ops])
entry = (entry + ZeroOrMore(op_parse)).addParseAction(_collapse)
expression << entry
return expression
开发者ID:birdming22,项目名称:bdec,代码行数:20,代码来源:expression.py
示例19: ts_from_file
def ts_from_file(filename):
"""Loads a TS (possibly extended with state frequencies) in SIS format."""
# definition of TS grammar
ParserElement.setDefaultWhitespaceChars(" \t")
id = Word(alphanums+"_\"':-")
#place = Literal("p") + Word(nums)
number = Word(nums).setParseAction(lambda tokens: int(tokens[0]))
newlines = Suppress(OneOrMore(LineEnd()))
modelName = ".model" + id("modelName") + newlines
signalNames = ZeroOrMore( Suppress(oneOf(".inputs .outputs .dummy")) + OneOrMore( id ) + newlines)("signals")
arc = id + id + id + newlines
graph = Literal(".state graph") + Suppress(OneOrMore(LineEnd())) + OneOrMore(Group(arc))("arcs")
frequency_list = ZeroOrMore(Group(id+number)+newlines)
frequency = ".frequencies" + Suppress(OneOrMore(LineEnd())) + frequency_list("frequencies")
marking_list = ZeroOrMore(id)
marking = ".marking"+Suppress("{") + marking_list("marking") + Suppress("}") + newlines
ts_grammar = Optional(newlines) + Optional(modelName) + signalNames + graph + marking + Optional(frequency) + ".end"
ts_grammar.ignore(pythonStyleComment)
try:
ast = ts_grammar.parseFile( filename )
except ParseException, pe:
print pe
raise pe
开发者ID:andycsoto,项目名称:pmlab,代码行数:23,代码来源:utils.py
示例20: property_grammar
def property_grammar():
ParserElement.setDefaultWhitespaceChars(' ')
model_props = [Regex(r'LIBINPUT_MODEL_[_0-9A-Z]+')('NAME')
- Suppress('=') -
(Literal('1'))('VALUE')
]
dimension = INTEGER('X') + Suppress('x') + INTEGER('Y')
sz_props = (
('LIBINPUT_ATTR_SIZE_HINT', Group(dimension('SETTINGS*'))),
('LIBINPUT_ATTR_RESOLUTION_HINT', Group(dimension('SETTINGS*'))),
)
size_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE')
for name, val in sz_props]
reliability_tags = Or(('reliable', 'write_open'))
reliability = [Literal('LIBINPUT_ATTR_LID_SWITCH_RELIABILITY')('NAME') -
Suppress('=') -
reliability_tags('VALUE')]
tpkbcombo_tags = Or(('below'))
tpkbcombo = [Literal('LIBINPUT_ATTR_TPKBCOMBO_LAYOUT')('NAME') -
Suppress('=') -
tpkbcombo_tags('VALUE')]
pressure_range = INTEGER('X') + Suppress(':') + INTEGER('Y')
pressure_prop = [ Literal('LIBINPUT_ATTR_PRESSURE_RANGE')('NAME') -
Suppress('=') -
Group(pressure_range('SETTINGS*')) ]
grammar = Or(model_props + size_props + reliability + tpkbcombo +
pressure_prop)
return grammar
开发者ID:bentiss,项目名称:libinput,代码行数:36,代码来源:parse_hwdb.py
注:本文中的pyparsing.ParserElement类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论