当前位置: 首页>>代码示例>>Python>>正文


Python pyparsing.Keyword类代码示例

本文整理汇总了Python中pyparsing.Keyword的典型用法代码示例。如果您正苦于以下问题:Python Keyword类的具体用法?Python Keyword怎么用?Python Keyword使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Keyword类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: build_parser

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
开发者ID:udoprog,项目名称:bsa,代码行数:60,代码来源:named.py

示例2: np

 def np(words, fn = gr_opt_quoted_string, action=None):
     p = Keyword(words[0], caseless=True)
     for w in words[1:]:
         p = p | Keyword(w, caseless=True)
     p = p + gr_eq + fn
     p.setParseAction(action)
     return p
开发者ID:amon-ra,项目名称:bacula_configuration,代码行数:7,代码来源:client.py

示例3: np

 def np(words, fn = gr_opt_quoted_string, action=nullDebugAction):
     p = Keyword(words[0], caseless=True).setDebug(bacula_tools.DEBUG)
     for w in words[1:]:
         p = p | Keyword(w, caseless=True).setDebug(bacula_tools.DEBUG)
     p = p + gr_eq + fn
     p.setParseAction(action)
     return p
开发者ID:amon-ra,项目名称:bacula_configuration,代码行数:7,代码来源:job.py

示例4: contains_keyword

def contains_keyword(text, query_keyword, atStart=False):
    """test presence of the keyword query_keyword with regard to surrounding unicode characters
    if atStart=True, this function success only if text starts with query_keyword
    """
    keyword = Keyword(query_keyword)
    for token in keyword.scanString(text):
        if atStart and token[1]:
            return False
        if not text[token[1] - 1:token[1]].isalnum() and not text[token[2]:token[2] + 1].isalnum():
            return True
    return False
开发者ID:zvolsky,项目名称:jsforhuman,代码行数:11,代码来源:tojs.py

示例5: parse

def parse (input):
    # parse a string into an element of the abstract representation

    # Grammar:
    #
    # <expr> ::= <integer>
    #            true
    #            false
    #            <identifier>
    #            ( if <expr> <expr> <expr> )
    #            ( let ( ( <name> <expr> ) ) <expr )
    #            ( + <expr> <expr> )
    #            ( * <expr> <expr> )
    #


    idChars = alphas+"_+*-?!=<>"

    pIDENTIFIER = Word(idChars, idChars+"0123456789")
    pIDENTIFIER.setParseAction(lambda result: EId(result[0]))

    # A name is like an identifier but it does not return an EId...
    pNAME = Word(idChars,idChars+"0123456789")

    pINTEGER = Word("-0123456789","0123456789")
    pINTEGER.setParseAction(lambda result: EInteger(int(result[0])))

    pBOOLEAN = Keyword("true") | Keyword("false")
    pBOOLEAN.setParseAction(lambda result: EBoolean(result[0]=="true"))

    pEXPR = Forward()

    pIF = "(" + Keyword("if") + pEXPR + pEXPR + pEXPR + ")"
    pIF.setParseAction(lambda result: EIf(result[2],result[3],result[4]))

    pBINDING = "(" + pNAME + pEXPR + ")"
    pBINDING.setParseAction(lambda result: (result[1],result[2]))

    pLET = "(" + Keyword("let") + "(" + pBINDING + ")" + pEXPR + ")"
    pLET.setParseAction(lambda result: ELet([result[3]],result[5]))

    pPLUS = "(" + Keyword("+") + pEXPR + pEXPR + ")"
    pPLUS.setParseAction(lambda result: ECall("+",[result[2],result[3]]))

    pTIMES = "(" + Keyword("*") + pEXPR + pEXPR + ")"
    pTIMES.setParseAction(lambda result: ECall("*",[result[2],result[3]]))

    pEXPR << (pINTEGER | pBOOLEAN | pIDENTIFIER | pIF | pLET | pPLUS | pTIMES)

    result = pEXPR.parseString(input)[0]
    return result    # the first element of the result is the expression
开发者ID:rpucella,项目名称:rpucella.github.io,代码行数:51,代码来源:homework3.py

示例6: create_grammar

def create_grammar():
	global arrows
	global stereotypes
	assert len(arrows) > 0
	assert len(stereotypes) > 0
	
	linechars = ''.join((c for c in printables if c not in '}\n')) + ' \t'
	norbracket = ''.join((c for c in printables if c != ']')) + ' \t'
	nogt = ''.join((c for c in printables if c != '>')) + ' \t'
	norparen = ''.join((c for c in printables if c != ')')) + ' \t'
	
	line = Word(linechars)
	cls_body = Group(ZeroOrMore(line))
	classkeyword = Keyword('class').setResultsName('type')
	
	st_names = stereotypes.keys()
	st = Literal(st_names[0])
	for s in st_names[1:]:
		st = st | Literal(s)
	stereotype = Group(Optional(Literal('<<').suppress() + st + Literal('>>').suppress()))
	
	identifier_list = Word(alphas) + ZeroOrMore(Literal(',').suppress() + Word(alphas))
	baseclasses = Group(Optional(Literal(':').suppress() + identifier_list))
	
	cls = Group(stereotype + classkeyword + Word(alphas) + baseclasses + \
		Literal('{').suppress() + cls_body + Literal('}').suppress())
	
	arrow_names = arrows.keys()
	arrow_names.sort(lambda x,y: -cmp(len(x), len(y)))
	arrow = Keyword(arrow_names[0])
	for ar in arrow_names[1:]:
		arrow = arrow | Keyword(ar)
	
	relation_caption = Literal('(').suppress() + Word(norparen) + \
		Literal(')').suppress()
	
	quantifier = Literal('[').suppress() + Word(norbracket) + Literal(']').suppress()
	relation = Group(
		Word(alphas) + Group(Optional(quantifier)) + \
		arrow.setResultsName('type') + \
		Word(alphas) + Group(Optional(quantifier)) + \
		Group(Optional(relation_caption))
		)
	
	grammar = ZeroOrMore(cls | relation)
	
	grammar.ignore(cStyleComment)
	grammar.ignore("//" + restOfLine)
	
	return grammar
开发者ID:Droggelbecher,项目名称:Grail,代码行数:50,代码来源:generate_clsgraph.py

示例7: __createGram

    def __createGram(self):

        if self.notNeedSpace:
            lNot = Keyword(self.operators['not'])
        else:
            lNot = Literal(self.operators['not'])
        
        lAnd = Literal(self.operators['and'])
        lOr = Literal(self.operators['or'])
        lImp = Literal(self.operators['impL'])
        lEqu = Literal(self.operators['equ'])
        
        lTrue = Keyword(self.constants['true'])
        lFalse = Keyword(self.constants['false'])
        
        lVar = Word(alphas, alphanums+'_')
        
        lVar.setParseAction(self.ffactory.createLogicVariable)
        lTrue.setParseAction(self.ffactory.createLogicTruth)
        lFalse.setParseAction(self.ffactory.createLogicFalse)

        factor = lTrue | lFalse | lVar
        
        expression = myparsing.operatorPrecedence(factor,
        [
         (lNot, 1, opAssoc.RIGHT, self.ffactory.createNotOperation),
         (lAnd, 2, opAssoc.LEFT, self.ffactory.createAndOperation),
         (lOr,  2, opAssoc.LEFT, self.ffactory.createOrOperation),
         (lImp, 2, opAssoc.LEFT, self.ffactory.createImpicationOperation),
         (lEqu, 2, opAssoc.LEFT, self.ffactory.createEquvalenceOperation)
        ],
        [('(', ')'), ('[', ']'), ('{', '}')])
    

        self.final = expression + StringEnd()
开发者ID:pszynk,项目名称:LIProjekt,代码行数:35,代码来源:parsers.py

示例8: parse_string

    def parse_string(self, string):
        '''Populate a new object from a string.
        
        Parsing is hard, so we're going to call out to the pyparsing
        library here.  I hope you installed it!
        '''
        from pyparsing import Suppress, Regex, quotedString, restOfLine, Keyword, nestedExpr, Group, OneOrMore, Word, Literal, alphanums, removeQuotes, replaceWith
        gr_eq = Literal('=')
        gr_stripped_string = quotedString.copy().setParseAction( removeQuotes )
        gr_opt_quoted_string = gr_stripped_string | restOfLine
        gr_name = Keyword('name', caseless=True) + gr_eq + gr_opt_quoted_string
        gr_name.setParseAction(lambda x, y=self: y._set_name(x[2]))
        gr_yn = Keyword('yes', caseless=True).setParseAction(replaceWith('1')) | Keyword('no', caseless=True).setParseAction(replaceWith('0'))
        gr_phrase = Group(OneOrMore(gr_stripped_string | Word(alphanums)) + gr_eq + gr_opt_quoted_string)

        def np(words, fn = gr_opt_quoted_string, action=print):
            p = Keyword(words[0], caseless=True)
            for w in words[1:]:
                p = p | Keyword(w, caseless=True)
            p = p + gr_eq + fn
            p.setParseAction(action)
            return p
        
        gr_ifsc = np(PList('Ignore File Set Changes'), gr_yn, action=self._parse_setter(IGNORECHANGES))
        gr_evss = np(PList('Enable VSS'), gr_yn, action=self._parse_setter(VSSENABLED))

        gr_i_option = Group(Keyword(OPTIONS, caseless=True) + nestedExpr('{','}', Regex('[^\}]+', re.MULTILINE)))
        gr_e_option = gr_i_option.copy()
        gr_i_file = gr_phrase.copy()
        gr_e_file = gr_phrase.copy()

        gr_inc = Keyword('include', caseless=True) + nestedExpr('{','}', OneOrMore(gr_i_option | gr_i_file))
        gr_inc.addParseAction(self._parse_add_entry)
        gr_exc = Keyword('exclude', caseless=True) + nestedExpr('{','}', OneOrMore(gr_e_option | gr_e_file))
        gr_exc.addParseAction(self._parse_add_entry)

        gr_res = OneOrMore(gr_name | gr_inc | gr_exc | gr_ifsc | gr_evss)
        result = gr_res.parseString(string, parseAll=True)
        return 'Fileset: ' + self[NAME]
开发者ID:amon-ra,项目名称:bacula_configuration,代码行数:39,代码来源:fileset.py

示例9: _create_filter_parser

def _create_filter_parser():
    and_kw = Keyword('AND')
    or_kw = Keyword('OR')
    variable = Literal('?') + Word(alphanums + '_').leaveWhitespace()
    uri_term = NotAny(Literal('"')) + Word(printables, excludeChars='>*')
    uri_part = Keyword('*') ^ uri_term ^ variable
    literal_term = QuotedString(quoteChar='"', escChar='\\')
    triple = Group(Literal('<').suppress() + uri_part.setResultsName('subj')
                   + uri_part.setResultsName('pred')
                   + (Group(uri_part).setResultsName('obj')
                      ^ Group(literal_term).setResultsName('objlit'))
                   + Literal('>').suppress())
    expr = Forward()
    atom = (triple.setResultsName('triple')
            | Literal('(').suppress() + expr + Literal(')').suppress())
    and_group = Group(atom + ZeroOrMore(and_kw.suppress() + atom))
    or_group = Group(atom + ZeroOrMore(or_kw.suppress() + atom))
    expr << (and_group.setResultsName('and') ^ or_group.setResultsName('or'))
    return expr
开发者ID:jfisteus,项目名称:ztreamy,代码行数:19,代码来源:filters.py

示例10: import

from pyparsing import (Word, Group, Suppress, Combine, Optional,
                       Forward, Empty, quotedString, oneOf, removeQuotes,
                       delimitedList, nums, alphas, alphanums,
                       Keyword, CaselessLiteral)

word_free = Word(alphas + '][@_-/.+**' + alphanums)
word_strict = Word(alphas, alphas + alphanums + '_' )

(lparen, rparen, lbrack, rbrack,
 lbrace, rbrace, colon, equal_sign) = map(Suppress, '()[]{}:=')

integer = Combine(Optional(oneOf('+ -')) + Word(nums)).setName('integer')
cvt_int = lambda toks: int(toks[0])
integer.setParseAction(cvt_int)

boolean_true = Keyword('True', caseless=True)
boolean_true.setParseAction(lambda x: True)
boolean_false = Keyword('False', caseless=True)
boolean_false.setParseAction(lambda x: False)

boolean = boolean_true | boolean_false

none = Keyword('None', caseless=True)

cvt_none = lambda toks: [None]
none.setParseAction(cvt_none)

e = CaselessLiteral("e")
real = (Combine(Optional(oneOf('+ -')) + Word(nums)
               + '.' + Optional(Word(nums))
               + Optional(e + Optional(oneOf('+ -')) + Word(nums)))
开发者ID:clazaro,项目名称:sfepy,代码行数:31,代码来源:parse_conf.py

示例11: Word

    alphas,
    alphas8bit,
    alphanums,
    Keyword,
)

word_free = Word(alphas8bit + "_-/.+**" + alphanums)
word_strict = Word(alphas8bit + alphas, alphas8bit + alphanums + "_")

(lparen, rparen, lbrack, rbrack, lbrace, rbrace, colon) = map(Suppress, "()[]{}:")

integer = Combine(Optional(oneOf("+ -")) + Word(nums)).setName("integer")
cvt_int = lambda toks: int(toks[0])
integer.setParseAction(cvt_int)

boolean = Keyword("False", caseless=True)
cvt_bool = lambda toks: toks[0].lower == "true"
boolean.setParseAction(cvt_bool)

none = Keyword("None", caseless=True)

cvt_none = lambda toks: [None]
none.setParseAction(cvt_none)

real = Combine(
    Optional(oneOf("+ -"))
    + Word(nums)
    + "."
    + Optional(Word(nums))
    + Optional("e" + Optional(oneOf("+ -")) + Word(nums))
).setName("real")
开发者ID:brbr520,项目名称:sfepy,代码行数:31,代码来源:parse_conf.py

示例12: parse

def parse (input):
    # parse a string into an element of the abstract representation

    # Grammar:
    #
    # <expr> ::= <integer>
    #            true
    #            false
    #            <identifier>
    #            ( if <expr> <expr> <expr> )
    #            ( let ( ( <name> <expr> ) ... ) <expr )
    #            ( function ( <name> ... ) <expr> )
    #            ( <expr> <expr> ... )
    #            ( call/cc <expr>)
    #


    idChars = alphas+"_+*-?!=<>"

    pIDENTIFIER = Word(idChars, idChars+"0123456789")
    pIDENTIFIER.setParseAction(lambda result: EId(result[0]))

    # A name is like an identifier but it does not return an EId...
    pNAME = Word(idChars,idChars+"0123456789")

    pNAMES = ZeroOrMore(pNAME)
    pNAMES.setParseAction(lambda result: [result])

    pINTEGER = Word("0123456789")
    pINTEGER.setParseAction(lambda result: EValue(VInteger(int(result[0]))))

    pBOOLEAN = Keyword("true") | Keyword("false")
    pBOOLEAN.setParseAction(lambda result: EValue(VBoolean(result[0]=="true")))

    pEXPR = Forward()

    pEXPRS = ZeroOrMore(pEXPR)
    pEXPRS.setParseAction(lambda result: [result])

    pIF = "(" + Keyword("if") + pEXPR + pEXPR + pEXPR + ")"
    pIF.setParseAction(lambda result: EIf(result[2],result[3],result[4]))

    pBINDING = "(" + pNAME + pEXPR + ")"
    pBINDING.setParseAction(lambda result: (result[1],result[2]))

    pBINDINGS = ZeroOrMore(pBINDING)
    pBINDINGS.setParseAction(lambda result: [ result ])

    def makeLet (bindings,body):
        params = [ param for (param,exp) in bindings ]
        args = [ exp for (param,exp) in bindings ]
        return ECall(EFunction(params,body),args)

    pLET = "(" + Keyword("let") + "(" + pBINDINGS + ")" + pEXPR + ")"
    pLET.setParseAction(lambda result: makeLet(result[3],result[5]))

    pCALL = "(" + pEXPR + pEXPRS + ")"
    pCALL.setParseAction(lambda result: ECall(result[1],result[2]))

    pFUN = "(" + Keyword("function") + "(" + pNAMES + ")" + pEXPR + ")"
    pFUN.setParseAction(lambda result: EFunction(result[3],result[5]))

    pFUNrec = "(" + Keyword("function") + pNAME + "(" + pNAMES + ")" + pEXPR + ")"
    pFUNrec.setParseAction(lambda result: EFunction(result[4],result[6],name=result[2]))

    def makeDo (exprs):
        result = exprs[-1]
        for e in reversed(exprs[:-1]):
            # space is not an allowed identifier in the syntax!
            result = makeLet([(" ",e)],result)
        return result

    pDO = "(" + Keyword("do") + pEXPRS + ")"
    pDO.setParseAction(lambda result: makeDo(result[2]))

    def makeWhile (cond,body):
        return makeLet([(" while",
                         EFunction([],EIf(cond,makeLet([(" ",body)],ECall(EId(" while"),[])),EValue(VNone())),name=" while"))],
                       ECall(EId(" while"),[]))

    pWHILE = "(" + Keyword("while") + pEXPR + pEXPR + ")"
    pWHILE.setParseAction(lambda result: makeWhile(result[2],result[3]))

    pCALLCC = "(" + Keyword("call/cc") + pEXPR + ")"
    pCALLCC.setParseAction(lambda result: ECallCC(result[2]))

    pEXPR << (pINTEGER | pBOOLEAN | pIDENTIFIER | pIF | pLET | pFUN | pFUNrec| pDO | pWHILE | pCALLCC | pCALL)

    # can't attach a parse action to pEXPR because of recursion, so let's duplicate the parser
    pTOPEXPR = pEXPR.copy()
    pTOPEXPR.setParseAction(lambda result: {"result":"expression","expr":result[0]})

    pDEFINE = "(" + Keyword("define") + pNAME + pEXPR + ")"
    pDEFINE.setParseAction(lambda result: {"result":"value",
                                           "name":result[2],
                                           "expr":result[3]})

    pDEFUN = "(" + Keyword("defun") + pNAME + "(" + pNAMES + ")" + pEXPR + ")"
    pDEFUN.setParseAction(lambda result: {"result":"function",
                                          "name":result[2],
#.........这里部分代码省略.........
开发者ID:rpucella,项目名称:rpucella.github.io,代码行数:101,代码来源:code-lect-07-callcc.py

示例13: parse_imp

def parse_imp (input):
    # parse a string into an element of the abstract representation

    # Grammar:
    #
    # <expr> ::= <integer>
    #            true
    #            false
    #            <identifier>
    #            ( if <expr> <expr> <expr> )
    #            ( function ( <name ... ) <expr> )    
    #            ( <expr> <expr> ... )
    #
    # <decl> ::= var name = expr ; 
    #
    # <stmt> ::= if <expr> <stmt> else <stmt>
    #            while <expr> <stmt>
    #            name <- <expr> ;
    #            print <expr> ;
    #            <block>
    #
    # <block> ::= { <decl> ... <stmt> ... }
    #
    # <toplevel> ::= <decl>
    #                <stmt>
    #


    idChars = alphas+"_+*-?!=<>"

    pIDENTIFIER = Word(idChars, idChars+"0123456789")
    #### NOTE THE DIFFERENCE
    pIDENTIFIER.setParseAction(lambda result: EPrimCall(oper_deref,[EId(result[0])]))

    # A name is like an identifier but it does not return an EId...
    pNAME = Word(idChars,idChars+"0123456789")

    pNAMES = ZeroOrMore(pNAME)
    pNAMES.setParseAction(lambda result: [result])

    pINTEGER = Word("0123456789")
    pINTEGER.setParseAction(lambda result: EValue(VInteger(int(result[0]))))

    pBOOLEAN = Keyword("true") | Keyword("false")
    pBOOLEAN.setParseAction(lambda result: EValue(VBoolean(result[0]=="true")))

    pEXPR = Forward()

    pEXPRS = ZeroOrMore(pEXPR)
    pEXPRS.setParseAction(lambda result: [result])

    pIF = "(" + Keyword("if") + pEXPR + pEXPR + pEXPR + ")"
    pIF.setParseAction(lambda result: EIf(result[2],result[3],result[4]))

    def mkFunBody (params,body):
        bindings = [ (p,ERefCell(EId(p))) for p in params ]
        return ELet(bindings,body)

    pFUN = "(" + Keyword("function") + "(" + pNAMES + ")" + pEXPR + ")"
    pFUN.setParseAction(lambda result: EFunction(result[3],mkFunBody(result[3],result[5])))

    pCALL = "(" + pEXPR + pEXPRS + ")"
    pCALL.setParseAction(lambda result: ECall(result[1],result[2]))

    pEXPR << (pINTEGER | pBOOLEAN | pIDENTIFIER | pIF | pFUN | pCALL)

    pDECL_VAR = "var" + pNAME + "=" + pEXPR + ";"
    pDECL_VAR.setParseAction(lambda result: (result[1],result[3]))

    # hack to get pDECL to match only PDECL_VAR (but still leave room
    # to add to pDECL later)
    pDECL = ( pDECL_VAR | NoMatch() )

    pDECLS = ZeroOrMore(pDECL)
    pDECLS.setParseAction(lambda result: [result])

    pSTMT = Forward()

    pSTMT_IF_1 = "if" + pEXPR + pSTMT + "else" + pSTMT
    pSTMT_IF_1.setParseAction(lambda result: EIf(result[1],result[2],result[4]))

    pSTMT_IF_2 = "if" + pEXPR + pSTMT
    pSTMT_IF_2.setParseAction(lambda result: EIf(result[1],result[2],EValue(VBoolean(True))))
   
    pSTMT_WHILE = "while" + pEXPR + pSTMT
    pSTMT_WHILE.setParseAction(lambda result: EWhile(result[1],result[2]))

    pSTMT_PRINT = "print" + pEXPR + ";"
    pSTMT_PRINT.setParseAction(lambda result: EPrimCall(oper_print,[result[1]]));

    pSTMT_UPDATE = pNAME + "<-" + pEXPR + ";"
    pSTMT_UPDATE.setParseAction(lambda result: EPrimCall(oper_update,[EId(result[0]),result[2]]))

    pSTMTS = ZeroOrMore(pSTMT)
    pSTMTS.setParseAction(lambda result: [result])

    def mkBlock (decls,stmts):
        bindings = [ (n,ERefCell(expr)) for (n,expr) in decls ]
        return ELet(bindings,EDo(stmts))
        
#.........这里部分代码省略.........
开发者ID:rpucella,项目名称:rpucella.github.io,代码行数:101,代码来源:homework6.py

示例14: parse

def parse (input):
    # parse a string into an element of the abstract representation

    # Grammar:
    #
    # <expr> ::= <integer>
    #            true
    #            false
    #            <identifier>
    #            ( if <expr> <expr> <expr> )
    #            ( let ( ( <name> <expr> ) ) <expr> )
    #            ( <name> <expr> ... )
    #


    idChars = alphas+"_+*-?!=<>"

    pIDENTIFIER = Word(idChars, idChars+"0123456789")
    pIDENTIFIER.setParseAction(lambda result: EId(result[0]))

    # A name is like an identifier but it does not return an EId...
    pNAME = Word(idChars,idChars+"0123456789")

    pNAMES = ZeroOrMore(pNAME)
    pNAMES.setParseAction(lambda result: [result])

    pINTEGER = Word("-0123456789","0123456789")
    pINTEGER.setParseAction(lambda result: EInteger(int(result[0])))

    pBOOLEAN = Keyword("true") | Keyword("false")
    pBOOLEAN.setParseAction(lambda result: EBoolean(result[0]=="true"))

    pEXPR = Forward()

    pIF = "(" + Keyword("if") + pEXPR + pEXPR + pEXPR + ")"
    pIF.setParseAction(lambda result: EIf(result[2],result[3],result[4]))

    pBINDING = "(" + pNAME + pEXPR + ")"
    pBINDING.setParseAction(lambda result: (result[1],result[2]))

    pBINDINGS = OneOrMore(pBINDING)
    pBINDINGS.setParseAction(lambda result: [ result ])

    pLET = "(" + Keyword("let") + "(" + pBINDINGS + ")" + pEXPR + ")"
    pLET.setParseAction(lambda result: ELet(result[3],result[5]))

    pEXPRS = ZeroOrMore(pEXPR)
    pEXPRS.setParseAction(lambda result: [result])

    pCALL = "(" + pNAME + pEXPRS + ")"
    pCALL.setParseAction(lambda result: ECall(result[1],result[2]))

    pEXPR << (pINTEGER | pBOOLEAN | pIDENTIFIER | pIF | pLET | pCALL)

    # can't attach a parse action to pEXPR because of recursion, so let's duplicate the parser
    pTOPEXPR = pEXPR.copy()
    pTOPEXPR.setParseAction(lambda result: {"result":"expression","expr":result[0]})
    
    pDEFUN = "(" + Keyword("defun") + pNAME + "(" + pNAMES + ")" + pEXPR + ")"
    pDEFUN.setParseAction(lambda result: {"result":"function",
                                          "name":result[2],
                                          "params":result[4],
                                          "body":result[6]})

    pTOP = (pDEFUN | pTOPEXPR)

    result = pTOP.parseString(input)[0]
    return result    # the first element of the result is the expression
开发者ID:rpucella,项目名称:rpucella.github.io,代码行数:68,代码来源:homework4.py

示例15: parse_morphology

def parse_morphology(filename, filename_toparse):
    global current_section_name
    current_section_name = ''
    converted_file = open(filename, 'w')

    put_string = 'from neuron import h\ndef shape_3D(self):\n'
    converted_file.write(put_string)
    ntabs = 1 # from here on, add a tab to all lines
    # define lists of characters for a..z and 1..9
    uppercase = lowercase.upper()
    lowercaseplus = lowercase+('_')
    lowercaseplus = lowercaseplus+(uppercase)
    nonzero = ''.join([str(i) for i in range(1, 10)])

    COMMA   = Literal(',')
    EQUALS  = Literal('=')
    MINUS   = Literal('-')
    PERIOD  = Literal('.')

    LCURL   = Literal('{')
    RCURL   = Literal('}')

    LBRACK  = Literal('(')
    RBRACK  = Literal(')')

    LSQUARE = Literal('[')
    RSQUARE = Literal(']')
    PTSCLEAR = Literal('{pt3dclear()').suppress()
    PTSCLEARNL = Literal('{\npt3dclear()\n').suppress()

    integer = Word(nums)
    single_section = Word(lowercaseplus, min = 2)
    single_section.setResultsName('SINGLE')

    integer_var = Word(lowercase, exact = 1)

    double = Group(Optional(MINUS) + integer + Optional(PERIOD + integer))

    operand = integer ^ integer_var
    operator = Word('+-*/', exact=1)

    unaryoperation = operand
    binaryoperation = operand + operator + operand
    operation = unaryoperation ^ binaryoperation

    array_section = Group(single_section + LSQUARE.suppress() + operation + RSQUARE.suppress())
    array_section.setResultsName('ARRAY')

    section = single_section ^ array_section

    section_location = Group(section + LBRACK.suppress() + double + RBRACK.suppress())

    create   = Keyword('create').suppress()  + section          + ZeroOrMore(COMMA.suppress() + section)
    create.setParseAction(print_create(converted_file, ntabs))

    connect  = Keyword('connect').suppress() + section_location + COMMA.suppress()  + section_location
    connect.setParseAction(print_connect(converted_file, ntabs))

    for_loop = Keyword('for').suppress()     + integer_var      + EQUALS.suppress()  + integer + COMMA.suppress() + integer
    # NOTE TO FUTURE SELF: for loops can only have one line of code in this implementation
    for_loop.setParseAction(print_for_loop(converted_file, ntabs))

    point_add = Literal('pt3dadd(').suppress() + double + COMMA.suppress() + double + COMMA.suppress() + double + COMMA.suppress() + double + RBRACK.suppress()
    point_add.setParseAction(print_point_add(converted_file, ntabs))

    point_style = Literal('pt3dstyle(').suppress() + double + COMMA.suppress() + double + COMMA.suppress()  + double + COMMA.suppress()  + double + RBRACK.suppress()
    point_style.setParseAction(print_point_style(converted_file, ntabs))

    geom_define_pre = section + (PTSCLEAR ^ PTSCLEARNL)
    geom_define_body = OneOrMore(point_add ^ point_style) + RCURL.suppress()
    geom_define_pre.setParseAction(update_current_section(converted_file, ntabs))

    geom_define = geom_define_pre + geom_define_body

    expression = (connect ^ for_loop ^ geom_define ^ create)
    codeblock = OneOrMore(expression)

    test_str = 'Ia_node[0] {\npt3dclear()\n pt3dadd( 47, 76, 92.5, 3.6) }'
    #file_to_parse = open('../../tempdata/Ia_geometry')
    file_to_parse = open(filename_toparse)
    tokens = codeblock.parseString(file_to_parse.read())
开发者ID:rdarie,项目名称:Spinal-Cord-Modeling,代码行数:81,代码来源:morphology_parser.py


注:本文中的pyparsing.Keyword类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。