本文整理汇总了Python中simpleparse.parser.Parser类的典型用法代码示例。如果您正苦于以下问题:Python Parser类的具体用法?Python Parser怎么用?Python Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: typographify
def typographify(input):
"""
Run from parent directory.
>>> import os
>>> os.chdir('typographify')
>>> print typographify(open('typographify.txt').read())
<strong>strong</strong> <em>words</em>
parsed 17 chars of 17
https://pypi.python.org/pypi/SimpleParse/
Version 2.2
http://www.ibm.com/developerworks/linux/library/l-simple/index.html
https://books.google.com/books?id=GxKWdn7u4w8C&pg=PA319&lpg=PA319&dq=simpleparse+standard+input&source=bl&ots=M8x58SCzpT&sig=5DOLvoC5-TZyxxlq3_LHD68gbXY&hl=en&sa=X&ved=0ahUKEwjFjOCurKjMAhVMuYMKHaM4ATUQ6AEIMTAD#v=onepage&q=simpleparse%20standard%20input&f=false
"""
parser = Parser(open('typographify.def').read(), 'para')
taglist = parser.parse(input)
text = ''
for tag, beg, end, parts in taglist[1]:
if tag == 'plain':
text += (input[beg:end])
elif tag == 'markup':
markup = parts[0]
mtag, mbeg, mend = markup[:3]
start, stop = codes.get(mtag, ('<!-- unknown -->','<!-- / -->'))
text += start + input[mbeg+1:mend-1] + stop
text += 'parsed %s chars of %s' % (taglist[-1], len(input))
return text
示例2: testTermSharing
def testTermSharing( self ):
"""Test that shared terminal productions are using the same parser"""
first =""" a := b,b >b<:= d d:= 'this'"""
pFirst = Parser( first, "a")
tFirst = pFirst.buildTagger()
b,c = tFirst
assert b is c, """Not sharing the same tuple for b and c instances"""
示例3: testBasic
def testBasic(self):
proc = dispatchprocessor.DispatchProcessor()
setattr(proc, "string", strings.StringInterpreter())
for production, yestable, notable in parseTests:
p = Parser("x := %s" % production, "x")
for data in yestable:
if production == "string":
success, results, next = p.parse(data, processor=proc)
else:
success, results, next = p.parse(data)
assert success and (next == len(data)), """Did not parse string %s as a %s result=%s""" % (
repr(data),
production,
(success, results, next),
)
assert results, """Didn't get any results for string %s as a %s result=%s""" % (
repr(data),
production,
(success, results, next),
)
if production == "string":
expected = eval(data, {}, {})
assert results[0] == expected, (
"""Got different interpreted value for data %s, we got %s, expected %s"""
% (repr(data), repr(results[0]), repr(expected))
)
for data in notable:
success, results, next = p.parse(data)
assert not success, """Parsed %s of %s as a %s result=%s""" % (
repr(data),
production,
(success, results, next),
)
示例4: parse
def parse(self, *args, **kwargs):
res = Parser.parse(self, *args, **kwargs)
l = [r for r in res[1] if isinstance(r, Node)]
count = 0
while count < len(l):
l += l[count].children
count += 1
for e in l:
if e.__class__.__name__ == "Inline":
url = os.path.join(self.root_path, e.url)
if e.children[:]:
continue
logger.info("Parse inlined vrml {0}".format(url))
e.children = Parser.parse(self, open(url).read())[1]
for child in e.children:
child._parent = e
code = "from parser import Node\n\n"
for name, prototype in self.prototypes.items():
obj = prototype()
attrs = [(key, getattr(obj, key)) for key in dir(obj)
if not ( key.startswith("_") or callable(getattr(obj, key))
or key == "children")]
code += "class {0}({1}):\n".format(name, "object")#prototype.__bases__[0].__name__)
#print obj, dir(obj), "\n---\n", obj._ftypes, "\n---\n",attrs
code += " def __init__(self):\n"
for key, value in attrs:
code += " self.{0} = {1} #{2}\n".format(key, repr(value), prototype.ftype(key))
code += "\n"
f = open("/tmp/robotviewer_protos.py",'w')
f.write(code)
f.close()
logger.debug("internally generated foloowing classes:\n{0}".format(code))
return res[0], res[1], res[2]
示例5: debugparser
def debugparser(self, filename):
file = open(filename).read()
debugparser = Parser (self.declaration)
import pprint
info("started debug parsing")
pprint.pprint(debugparser.parse(file))
info("completed debug parsing")
exit(0)
示例6: main
def main():
oparser = get_parser()
opts, args = oparser.parse_args()
parser = Parser(open(opts.grammar).read(),
opts.root)
success, tags, next = parser.parse( open(opts.input).read() )
print tags
示例7: testTZ
def testTZ( self ):
names = list(timezone_names.timezone_mapping.keys())
names.sort() # tests that the items don't match shorter versions...
decl = Parser("""this := (timezone_name, ' '?)+""", 'this')
proc = dispatchprocessor.DispatchProcessor()
proc.timezone_name = timezone_names.TimeZoneNameInterpreter()
text = ' '.join(names)
success, result, next = decl.parse( text, processor = proc )
assert success, """Unable to complete parsing the timezone names, stopped parsing at char %s %s"""%(next, text[next:])
assert result == list(map( timezone_names.timezone_mapping.get, names)), """Got different results for interpretation than expected (expected first, recieved second)\n%s\n%s"""%(list(map( timezone_names.timezone_mapping.get, names)), result)
示例8: __init__
def __init__(self, filename):
with open(filename) as f:
content = f.read()
parser = Parser(declaration)
success, tree, nextChar = parser.parse(content, processor=ConfigProcessor(self))
if not success:
raise Exception
for k, v in tree[0].iteritems():
setattr(self, k, v)
示例9: testBasic
def testBasic( self ):
for production, yestable, notable in parseTests:
p = Parser( "x := %s"%production, 'x')
for data in yestable:
success, results, next = p.parse( data)
assert success and (next == len(data)), """Did not parse comment %s as a %s result=%s"""%( repr(data), production, (success, results, next))
assert results, """Didn't get any results for comment %s as a %s result=%s"""%( repr(data), production, (success, results, next))
for data in notable:
success, results, next = p.parse( data)
assert not success, """Parsed %s of %s as a %s result=%s"""%(
next, repr(data), production, results
)
示例10: testTermCompression
def testTermCompression( self ):
"""Test that unreported productions are compressed
Term compression is basically an inlining of terminal
expressions into the calling table. At the moment
the terminal expressions are all duplicated, which may
balloon the size of the grammar, not sure if this will
be an actual problem. As written, this optimization
should provide a significant speed up, but there may
the even more of a speed up if we allow for sharing
the terminal tuples as well.
This:
a:=b <b>:= -c* c:='this'
Should eventually compress to this:
a := -'this'*
"""
failures = []
for first, second in [
("""a:=b <b>:= -c* c:='this'""", """a := -'this'*"""),
("""a:=b >b<:= c c:= 'this'""", """a := c c:= 'this'"""),
("""a:=b >b<:= c <c>:= 'this'""", """a := 'this'"""),
("""a:=b >b<:= c+ <c>:= 'this'""", """a := 'this'+"""),
# The following will never work, so eventually may raise
# an error or at least give a warning!
("""a:=b,c >b<:= c+ <c>:= 'this'""", """a := 'this'+,'this'"""),
("""a:=b/c >b<:= c+ <c>:= 'this'""", """a := 'this'+/'this'"""),
# This is requiring group-compression, which isn't yet written
("""a:=-b/c >b<:= c+ <c>:= 'this'""", """a := -'this'+/'this'"""),
("""a := (table1 / table2 / any_line)*
<any_line> := ANY*, EOL
<ANY> := -EOL
<EOL> := '\n'
table1 := 'a'
table2 := 'b'
""", """a := (table1 / table2 / (-'\n'*, '\n'))*
table1 := 'a'
table2 := 'b'
"""),
("""a:= b,c <b>:= -c* <c>:= '\n'""", """a := -'\n'*,'\n'"""),
]:
pFirst = Parser( first, "a")
pSecond = Parser( second, "a")
tFirst = pFirst.buildTagger()
tSecond = pSecond.buildTagger()
if not rcmp( tFirst , tSecond):
tFirstRepr = pprint.pformat(tFirst)
tSecondRepr = pprint.pformat(tSecond)
failures.append( """%(first)r did not produce the same parser as %(second)r\n\t%(tFirstRepr)s\n\t%(tSecondRepr)s"""%locals())
if failures:
raise ValueError( "\n".join(failures))
示例11: testBasic
def testBasic( self ):
for production, processor, yestable, notable in _data:
p = Parser( "x := %s"%production, 'x')
proc = dispatchprocessor.DispatchProcessor()
setattr(proc, production, processor())
for data, length, value in yestable:
success, results, next = p.parse( data, processor = proc)
assert next == length, """Did not parse string %s of %s as a %s result=%s"""%( repr(data[:length]), repr(data), production, (success, results, next))
assert results[0] == value, """Didn't get expected value from processing value %s, expected %s, got %s"""%( data[:length], value, results[0])
for data in notable:
success, results, next = p.parse( data)
assert not success, """Parsed %s of %s as a %s result=%s"""%( repr(data[:length]), repr(data), production, (success, results, next))
示例12: __init__
def __init__(self):
declaration = r'''
fun := fun_name,'(',exp_list,')'
fun_name := [a-zA-Z0-9_-]+
exp_list := exp,(',',exp)*
exp := pm_exp
pm_exp := md_exp,('+'/'-',md_exp)*
md_exp := bracket_exp,('*'/'/',bracket_exp)*
bracket_exp := ('(',exp,')')/str_var/number
str_var := [a-zA-Z],[a-zA-Z0-9_-\\.]*
'''
self.fun_parser = Parser( declaration, "fun" )
self.exp_parser = Parser( declaration, "exp" )
示例13: __init__
def __init__(self, grammar = None, root_node = "vrmlScene"):
path = os.path.abspath(os.path.dirname(__file__))
if not grammar:
grammar_file = os.path.join(path,"vrml.sbnf" )
# print("Using grammar {0}".format(grammar_file))
grammar = open(grammar_file).read()
#logger.info("Grammar: {0}".format(grammar))
Parser.__init__(self, grammar, root_node)
logging.debug("created parser instance")
self.root_path = ""
self.prototypes = {}
spec_data = open(os.path.join(path, 'standard_nodes.wrl')).read()
self.parse(spec_data)
logging.debug("Parsed vrml2.0 specs")
示例14: main
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'hn')
except getopt.GetoptError:
usage()
sys.exit(2)
# Get any options
navFlag = False
for o, a in opts:
if o == '-h':
usage()
sys.exit()
if o == '-n':
navFlag = True
# Get the input filename
if len(args) != 1:
usage()
sys.exit(2)
else:
filename = args[0]
# Initialise data base
db = freenav.freedb.Freedb()
db.delete_airspace()
# Initialise parser
parser = Parser(tnp.TNP_DECL, 'tnp_file')
p = db.get_projection()
proj = freenav.projection.Lambert(p['parallel1'], p['parallel2'],
p['latitude'], p['longitude'])
output_processor = AirProcessor(db, proj)
tnp_processor = tnp.TnpProcessor(output_processor)
# Read data and parse
airdata = open(filename).read()
success, parse_result, next_char = parser.parse(airdata,
processor=tnp_processor)
# Report any syntax errors
if not (success and next_char==len(airdata)):
print "%s: Syntax error at (or near) line %d" % \
(filename, len(airdata[:next_char].splitlines())+1)
sys.exit(1)
# Create indices and tidy up
db.commit()
db.vacuum()
示例15: _testSet
def _testSet( self, set, singleName, multiName ):
"""Test multi-line definitions"""
decl = """single := %s multiple := %s"""%( singleName, multiName )
p = Parser(decl)
notset = string.translate( fulltrans, fulltrans, set )
for char in set:
success, children, next = p.parse( char, singleName)
assert success and (next == 1), """Parser for %s couldn't parse %s"""%( singleName, char )
for char in notset:
success, children, next = p.parse( char, singleName)
assert (not success) and (next == 0), """Parser for %s parsed %s"""%( singleName, char )
success, children, next = p.parse( char, multiName)
assert (not success) and (next == 0), """Parser for %s parsed %s"""%( multiName, char )
success, children, next = p.parse( set, multiName)
assert success and (next == len(set)), """Parser for %s couldn't parse full set of chars, failed at %s"""%( multiName, set[next:] )