本文整理汇总了Python中pyparsing.Forward.parseFile方法的典型用法代码示例。如果您正苦于以下问题:Python Forward.parseFile方法的具体用法?Python Forward.parseFile怎么用?Python Forward.parseFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing.Forward
的用法示例。
在下文中一共展示了Forward.parseFile方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_parser
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import parseFile [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: Word
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import parseFile [as 别名]
attribute = Word(alphanums + '_-')
value = (Word(alphanums+ '._-') ^ QuotedString('"'))
command = oneOf('Resource Attr Replace Apply SetCache Set Rule Insert Compose Copy Debug Literal Defmac Fetch Script Deliver Prerequisite Modify')
named_command = command + Optional(value)
command_property = Group(attribute + colon + value)
def printCommandName(parse_string, location, matched_tokens_list):
import time
print 'COMMAND (%s):' % str(time.clock()), matched_tokens_list
# This recursive statement form taken from http://w3facility.info/question/simple-demonstration-of-using-pyparsings-indentedblock-recursively/
statement = ((command + newline) ^ (command + arrow) ^ (named_command + arrow)) + ZeroOrMore(command_property)
statement.setParseAction(printCommandName)
statementBlock = Forward()
statementBlock << statement + Optional(indentedBlock(statementBlock, [1]))
if __name__ == '__main__':
from pyparsing import *
import pprint
import sys
#statement.setDebug(True)
FILE = sys.argv[1]
for i, l in enumerate(file(FILE).read().split('\n')):
print i+1, l
parseTree = statementBlock.parseFile(FILE)
print '\n'
pprint.pprint(parseTree.asList())
示例3: Regex
# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import parseFile [as 别名]
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)
result = musicobject.parseFile('example.music')
print(result[0])
from to_music21 import construct_music21
construct_music21(result[0]).write('musicxml', 'foo.xml')
construct_music21(result[0]).show('text')