本文整理汇总了Python中simpleparse.parser.Parser.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Parser.parse方法的具体用法?Python Parser.parse怎么用?Python Parser.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类simpleparse.parser.Parser
的用法示例。
在下文中一共展示了Parser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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]
示例2: testBasic
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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),
)
示例3: testBasic
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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
)
示例4: testBasic
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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))
示例5: _testSet
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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:] )
示例6: typographify
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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
示例7: debugparser
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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)
示例8: main
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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
示例9: testTZ
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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)
示例10: __init__
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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)
示例11: parse
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
def parse(self, txt, *args, **kwargs):
# Easter egg.
if txt == "2+2": return (True, 5)
try:
success, children, next = Parser.parse(self, txt, *args, **kwargs)
except ParserSyntaxError:
return (False, 0.0)
if not (success and next == len(txt)):
return (False, 0.0)
else:
return (True, children[0])
示例12: __init__
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
class Compiler:
def __init__(self):
self.parser = Parser(grammar)
self.translator = SyntaxTreeProcessor()
def compile(self, command):
cmd = re.sub('\s', '', command)
(success, children, nextchar) = self.parser.parse(cmd)
result = self.translator((success, children, nextchar), cmd)
python_src = result[1][0]
return compile(python_src, '', 'exec')
示例13: main
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
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()
示例14: testISODate
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
def testISODate( self ):
"""Test the parsing of ISO date and time formats"""
values = [
("2002-02-03", DateTime.DateTime( 2002, 2,3)),
("2002-02",DateTime.DateTime( 2002, 2)),
("2002",DateTime.DateTime( 2002)),
("2002-02-03T04:15", DateTime.DateTime( 2002, 2,3, 4,15)),
("2002-02-03T04:15:16", DateTime.DateTime( 2002, 2,3, 4,15, 16)),
("2002-02-03T04:15:16+00:00", DateTime.DateTime( 2002, 2,3, 4,15, 16)-tzOffset),
]
p = Parser ("d:= ISO_date_time", "d")
proc = iso_date.MxInterpreter()
for to_parse, date in values:
success, children, next = p.parse( to_parse, processor=proc)
assert success, """Unable to parse any of the string %s with the ISO date-time parser"""% (to_parse)
assert next == len(to_parse),"""Did not finish parsing string %s with the ISO date-time parser, remainder was %s, found was %s"""%( to_parse, to_parse [next:],children)
assert children [0] == date,"""Returned different date for string %s than expected, got %s, expected %s"""% (to_parse,children [0], date)
示例15: convert
# 需要导入模块: from simpleparse.parser import Parser [as 别名]
# 或者: from simpleparse.parser.Parser import parse [as 别名]
def convert(input, definition = 'compilation_unit'):
"""
Example of converting syntax from ActionScript to C#.
>>> print(convert('import com.finegamedesign.anagram.Model;', 'import_definition'))
using /*<com>*/Finegamedesign.Anagram/*<Model>*/;
Related to grammar unit testing specification (gUnit)
https://theantlrguy.atlassian.net/wiki/display/ANTLR3/gUnit+-+Grammar+Unit+Testing
"""
source = cfg['source']
to = cfg['to']
parser = Parser(grammars[source], definition)
input = may_import(None, input, definition, to)
taglist = parser.parse(input)
taglist = [(definition, 0, taglist[-1], taglist[1])]
text = _recurse_tags(taglist, input, source, to)
text = may_import(taglist, text, definition, to)
text = may_format(definition, text)
return text