def grammar(self):
        """ Define the parser grammar.
        """
        # Ignore TeX commands between delimiters $$, \(,  \)
        tex_eq = (Literal(r'\(') | Literal(r'$$') | Literal(r'\[')) + ... + (
            Literal(r'\)') | Literal(r'$$') | Literal(r'\]'))

        # Define elemtary stuff
        leftAc = Literal('{').suppress()
        rightAc = Literal('}').suppress()
        lpar = Literal('(')
        rpar = Literal(')')
        integer = Word(nums)  # simple unsigned integer
        real = Regex(r"[+-]?\d+(:?\.\d*)?(:?[eE][+-]?\d+)?")
        real.setParseAction(self.real_hook)
        number = real | integer

        # Define function
        fnname = Word(alphas, alphanums + "_")('name')
        # Require expr to finalize the def
        function = Forward()
        function.setParseAction(self.function_hook)

        # What are the namming rule for the jocker? Need to start by a letter,
        # may contain almost everything
        variable = Combine(leftAc + Word(alphas, alphanums + "_") +
                           rightAc)('name')
        variable.setParseAction(self.variable_hook)
        variable.ignore(tex_eq)
        # arithmetic operators
        minus = Literal('-')
        arithOp = oneOf("+ * /") | minus
        equal = Literal('{=').suppress()
        # Require atom to finalize the def
        expr = Forward()
        # Define atom
        atom = number | (0, None) * minus + (Group(lpar + expr + rpar)
                                             | variable | function)
        atom.setParseAction(self.atom_hook)
        # Finalize postponed elements...
        expr << atom + ZeroOrMore(arithOp + atom)
        # Need to group arguments for swapping them
        function << fnname + Group(lpar + Group(ZeroOrMore(expr)) +
                                   Optional(Literal(',') + Group(expr)) + rpar)
        # Define equation
        equation = equal + expr + rightAc
        equation.setParseAction(self.equation_hook)
        return equation, variable
Beispiel #2
0
char_arr_deck = (
    name_of_deck("key")
    + Literal("C")("type")
    + Literal("N=").suppress()
    + integer("size")
    + LineEnd().suppress()
    + Group(SkipTo(LineEnd() + name_of_deck + data_type) | SkipTo(StringEnd()))("value")
)

real_arr_deck = (
    name_of_deck("key")
    + Literal("R")("type")
    + Literal("N=").suppress()
    + integer("size")
    + LineEnd().suppress()
    + Group(real.ignore("\n")[...])("value")
)

int_arr_deck = (
    name_of_deck("key")
    + Literal("I")("type")
    + Literal("N=").suppress()
    + integer("size")
    + LineEnd().suppress()
    + Group(integer.ignore("\n")[...])("value")
)
# fchck file header
# ------------------------------------------------------------------------------------------
title = (LineStart() + SkipTo(LineEnd())).setParseAction(lambda x: "".join(x).strip())
details = LineStart() + OneOrMore(Word(printables))
header = Group(