Exemplo n.º 1
0
    def _get_infos_from_file(self, info):
        # Object file line: '{object}:  file format elf32-xtensa-le'
        object = Fragment.ENTITY.setResultsName("object") + Literal(
            ":").suppress() + Literal(
                "file format elf32-xtensa-le").suppress()

        # Sections table
        header = Suppress(
            Literal("Sections:") + Literal("Idx") + Literal("Name") +
            Literal("Size") + Literal("VMA") + Literal("LMA") +
            Literal("File off") + Literal("Algn"))
        entry = Word(nums).suppress() + Fragment.ENTITY + Suppress(
            OneOrMore(Word(alphanums, exact=8)) + Word(nums + "*") +
            ZeroOrMore(Word(alphas.upper()) + Optional(Literal(","))))

        # Content is object file line + sections table
        content = Group(object + header +
                        Group(ZeroOrMore(entry)).setResultsName("sections"))

        parser = Group(ZeroOrMore(content)).setResultsName("contents")

        sections_info_text = info.content
        results = None

        try:
            results = parser.parseString(sections_info_text)
        except ParseException as p:
            raise ParseException("Unable to parse section info file " +
                                 info.filename + ". " + p.message)

        return results
Exemplo n.º 2
0
Arquivo: ras.py Projeto: CSPUT/pyras
    def grammar(self):
        prefix_op = Literal('.')
        parallel = Literal('|')
        comm_op = Literal('||')
        choice_op = Literal('+')
        colon = Literal(':')
        loca_sym = Literal('@').suppress()
        lpar = Literal('(').suppress()
        rpar = Literal(')').suppress()
        pound = Literal('#').suppress()
        slash = Literal('/')
        # 8
        lbra = Literal('{').suppress()
        rbra = Literal('}').suppress()
        define = Literal('=')
        Ident = Word(alphas.upper(), alphanums + "_")
        ident = Word(alphas.lower(), alphanums + "_")
        pragma = Optional(pound + Literal('language') + colon + 'ras')
        # pseudo uri: /a/s/s/s/s/ or /a/ or /a/s/s/s
        puri = Combine(slash + ident + ZeroOrMore(slash + ident) + Optional(slash))
        loca = loca_sym + puri 

        comm = Forward()
        choice = Forward()
        prefix = Forward()

        process = ident | loca + lbra + choice + rbra | lpar + choice + rpar
        prefix << Group(process + ZeroOrMore(prefix_op + prefix) )
        choice << Group(prefix + ZeroOrMore(choice_op + prefix))
        comm << Group(choice + ZeroOrMore(comm_op + comm ))
        rmdef = Ident + define + loca+ lbra + comm + rbra
        ras = pragma + rmdef

        return ras
Exemplo n.º 3
0
def parse_weak_order(s: str) -> list:
    """
    Convert a string representing a weak order to a list of sets.

    :param s: a string.
    :return: a list of sets, where each set is an indifference class. The first set of the list contains the top
        (= most liked) candidates, while the last set of the list contains the bottom (= most disliked) candidates.

    >>> s = 'Alice ~ Bob ~ Catherine32 > me > you ~ us > them'
    >>> parse_weak_order(s) == [{'Alice', 'Bob', 'Catherine32'}, {'me'}, {'you', 'us'}, {'them'}]
    True
    """

    # Build the parser
    candidate = Word(alphas.upper() + alphas.lower() + nums + '_')
    equiv_class = Group(candidate +
                        ZeroOrMore(Word('~').suppress() + candidate))
    weak_preference = equiv_class + ZeroOrMore(
        Word('>').suppress() + equiv_class)
    empty_preference = ZeroOrMore(' ')

    # if s = 'Jean ~ Titi ~ tata32 > me > you ~ us > them', then
    # parsed = [['Jean', 'Titi', 'tata32'], ['me'], ['you', 'us'], ['them']]
    try:
        parsed = empty_preference.parseString(s, parseAll=True).asList()
    except ParseException:
        parsed = weak_preference.parseString(s, parseAll=True).asList()

    # Final conversion to format [{'Jean', 'tata32', 'Titi'}, {'me'}, {'us', 'you'}, {'them'}]
    return [NiceSet(s) for s in parsed]
Exemplo n.º 4
0
def gramma():
    ## Tokens
    point = Literal('.')
    prefix_op = Literal('.')
    choice_op = Literal('+')
    parallel = Literal("||") | Literal("<>")
    #ident = Word(alphas, alphanums+'_')
    ratename = Word(alphas.lower(), alphanums + "_")
    lpar = Literal('(').suppress()
    rpar = Literal(')').suppress()
    lsqpar = Literal('[').suppress()
    rsqpar = Literal(']').suppress()

    define = Literal('=')
    semicol = Literal(';').suppress()
    col = Literal(',').suppress()
    number = Word(nums)
    integer = number
    floatnumber = Combine(integer + Optional(point + Optional(number)))
    passiverate = Word('infty') | Word('T')
    internalrate = Word('tau')
    pound = Literal('#').suppress()
    percent = Literal('%').suppress()
    peparate = (ratename | floatnumber | internalrate
                | passiverate).setParseAction(_check_var)
    peparate_indef = floatnumber | internalrate | passiverate
    sync = Word('<').suppress() + ratename + ZeroOrMore(col + ratename) + Word(
        '>').suppress()
    coop_op = (parallel | sync).setParseAction(_create_sync_set)
    activity = (ratename + col + peparate).setParseAction(_create_activity)
    procdef = (Word(alphas.upper(), alphanums + "_") +
               Optional(lsqpar + peparate_indef +
                        rsqpar)).setParseAction(_create_procdef)
    ## RATES Definitions
    ratedef = (Optional(percent) + ratename + define +
               peparate_indef).setParseAction(assign_var) + semicol

    prefix = Forward()
    choice = Forward()
    coop = Forward()

    process = (activity
               | procdef
               | lpar + coop + rpar).setParseAction(_create_process)
    prefix << (process +
               ZeroOrMore(prefix_op + prefix)).setParseAction(_create_prefix)
    choice << (prefix +
               ZeroOrMore(choice_op + choice)).setParseAction(_create_choice)
    coop << (choice + ZeroOrMore(coop_op + coop)).setParseAction(_create_coop)
    rmdef = (Optional(pound) + procdef + define + coop +
             semicol).setParseAction(_create_definition)
    system_eq = Optional(pound) + coop
    pepa = ZeroOrMore(ratedef) + ZeroOrMore(rmdef) + system_eq.setParseAction(
        _create_system_equation)
    pepacomment = '//' + restOfLine
    pepa.ignore(pepacomment)
    return pepa
Exemplo n.º 5
0
def parser(filename=None):
    """
    Generate a callable parser for each file. The filename is necessary to get better Error messages.

    :param filename: Name of the file, on which the parser will be applied
    :type filename: str
    :return: A parser for the file on filename
    """
    line_comment = Group(Suppress("--") + restOfLine + SkipTo(LineEnd()))("SingleLineComment")
    block_comment = nestedExpr("{-", "-}")("MultiLineComment")
    comment = Group(line_comment | block_comment)
    module_name = Group(delimitedList(Word(alphas.upper(),alphas),"."))("Module Name")
    module_exports = Group(Keyword("..") | Group(delimitedList(Word(printables, excludeChars=',)'))))("Module Exports")
    module_declaration = Group(
        Suppress("module").setFailAction(e.ModuleDeclarationMissingException(filename)) +
        module_name +
        Suppress("exposing") +
        Suppress("(") +
        module_exports+Suppress(")"))("Module Declaration")

    LPAR, RPAR = map(Suppress, '()')
    integer = Word(nums)
    real = Optional(Word(nums)) + '.' + Word(nums)
    expr = Forward()
    operand = Optional("-") + (real | integer)
    factor = operand | Group(LPAR + expr + RPAR)
    term = factor + ZeroOrMore(oneOf('* /') + factor)
    expr <<= term + ZeroOrMore(oneOf('+ -') + term)

    function_name = Word(alphas.lower())
    function_parameter = Word(alphas.upper()+alphas.lower())
    function_parameters = ZeroOrMore(function_parameter)("Function Parameter")
    function_body = expr
    function_declaration = Group(function_name + function_parameters + Suppress("=") + function_body)\
        ("Function Declaration")

    return module_declaration + ZeroOrMore(function_declaration | comment)("Body")
Exemplo n.º 6
0
def gramma():
## Tokens
    point = Literal('.')
    prefix_op = Literal('.')
    choice_op = Literal('+')
    parallel = Literal("||") | Literal("<>")
#ident = Word(alphas, alphanums+'_')
    ratename = Word(alphas.lower(),alphanums+"_")
    lpar = Literal('(').suppress()
    rpar = Literal(')').suppress()
    lsqpar = Literal('[').suppress()
    rsqpar = Literal(']').suppress()

    define = Literal('=')
    semicol = Literal(';').suppress()
    col = Literal(',').suppress()
    number = Word(nums)
    integer = number
    floatnumber = Combine( integer + Optional( point + Optional(number)))
    passiverate = Word('infty') | Word('T')
    internalrate = Word('tau')
    pound = Literal('#').suppress()
    percent = Literal('%').suppress()
    peparate = (ratename | floatnumber | internalrate | passiverate).setParseAction(_check_var)
    peparate_indef = floatnumber | internalrate | passiverate
    sync = Word('<').suppress() + ratename + ZeroOrMore(col + ratename) + Word('>').suppress()
    coop_op = (parallel | sync).setParseAction(_create_sync_set)
    activity = (ratename + col + peparate).setParseAction(_create_activity)
    procdef = (Word(alphas.upper(), alphanums+"_") + Optional(lsqpar + peparate_indef + rsqpar)).setParseAction(_create_procdef)
## RATES Definitions
    ratedef = (Optional(percent)+ratename + define + peparate_indef).setParseAction(assign_var) + semicol

    prefix = Forward()
    choice = Forward()
    coop = Forward()

    process = ( activity
             | procdef
             | lpar + coop + rpar
            ).setParseAction(_create_process)
    prefix  << (process + ZeroOrMore(prefix_op + prefix)).setParseAction( _create_prefix)
    choice << (prefix + ZeroOrMore(choice_op + choice)).setParseAction(_create_choice)
    coop << (choice + ZeroOrMore(coop_op + coop)).setParseAction(_create_coop)
    rmdef = (Optional(pound) + procdef + define + coop + semicol).setParseAction(_create_definition)
    system_eq =  Optional(pound) + coop
    pepa = ZeroOrMore(ratedef)  + ZeroOrMore(rmdef) + system_eq.setParseAction(_create_system_equation)
    pepacomment = '//' + restOfLine
    pepa.ignore(pepacomment)
    return pepa
singleTextString = originalTextFor(
    ZeroOrMore(~EOL + (White(" \t") | Word(printables)))).leaveWhitespace()
XDIGIT = hexnums
INT = Word(nums)
ESC = Literal('\\') + (oneOf(list(r'nrtbf\">' + "'")) |
                       ('u' + Word(hexnums, exact=4))
                       | Word(printables, exact=1))
LITERAL_CHAR = ESC | ~(Literal("'") | Literal('\\')) + Word(printables,
                                                            exact=1)
CHAR_LITERAL = Suppress("'") + LITERAL_CHAR + Suppress("'")
STRING_LITERAL = Suppress("'") + Combine(
    OneOrMore(LITERAL_CHAR)) + Suppress("'")
DOUBLE_QUOTE_STRING_LITERAL = '"' + ZeroOrMore(LITERAL_CHAR) + '"'
DOUBLE_ANGLE_STRING_LITERAL = '<<' + ZeroOrMore(Word(printables,
                                                     exact=1)) + '>>'
TOKEN_REF = Word(alphas.upper(), alphanums + '_')
RULE_REF = Word(alphas.lower(), alphanums + '_')
ACTION_ESC = (Suppress("\\") + Suppress("'")) | Suppress('\\"') | Suppress(
    '\\') + (~(Literal("'") | Literal('"')) + Word(printables, exact=1))
ACTION_CHAR_LITERAL = Suppress("'") + (
    ACTION_ESC | ~(Literal('\\') | Literal("'")) +
    Word(printables, exact=1)) + Suppress("'")
ACTION_STRING_LITERAL = Suppress(
    '"') + ZeroOrMore(ACTION_ESC | ~(Literal('\\') | Literal('"')) +
                      Word(printables, exact=1)) + Suppress('"')
SRC = Suppress('src') + ACTION_STRING_LITERAL("file") + INT("line")
id = TOKEN_REF | RULE_REF
SL_COMMENT = Suppress('//') + Suppress('$ANTLR') + SRC | ZeroOrMore(
    ~EOL + Word(printables)) + EOL
ML_COMMENT = cStyleComment
WS = OneOrMore(
Exemplo n.º 8
0
    def get_parser(self):
        declaration = Forward()
        keyword = (Keyword("enum") | Keyword("case") | Keyword("struct")
                   | Keyword("default") | Keyword("switch") | Keyword("union")
                   | Keyword("const") | Keyword("unsigned") | Keyword("int")
                   | Keyword("hyper") | Keyword("float") | Keyword("double")
                   | Keyword("bool") | Keyword("typedef") | Keyword("opaque")
                   | Keyword("string") | Keyword("void") | Keyword("program")
                   | Keyword("version"))
        identifier = NotAny(keyword) + Word(
            alphas + alphas.upper(),
            alphanums + alphanums.upper() + "_",
            asKeyword=True)

        constant = Combine(Optional("-") + Word(nums))
        constant.setParseAction(lambda s, l, t: [int(t[0])])

        value = constant | identifier

        enum_body = Literal("{").suppress() + identifier + Literal(
            "=").suppress() + value + ZeroOrMore(
                Literal(",").suppress() + identifier +
                Literal("=").suppress() + value) + Literal("}").suppress()
        enum_type_spec = Literal("enum").suppress() + enum_body
        enum_body.setParseAction(self.parse_enum)

        struct_body = Literal("{").suppress() + OneOrMore(
            declaration + Literal(";").suppress()) + Literal("}").suppress()
        struct_type_spec = Literal("struct").suppress() + struct_body
        struct_body.setParseAction(self.parse_struct)

        case_stmt = Literal("case").suppress() + value + Literal(
            ":").suppress() + declaration + Literal(";").suppress()
        default_stmt = Literal("default") + Literal(
            ":").suppress() + declaration + Literal(";").suppress()
        union_body = Literal("switch").suppress() + Literal("(").suppress(
        ) + declaration + Literal(")").suppress() + Literal("{").suppress(
        ) + Group(OneOrMore(Group(case_stmt)) +
                  Optional(Group(default_stmt))) + Literal("}").suppress()
        union_type_spec = Literal("union").suppress() + union_body
        union_body.setParseAction(self.parse_union)

        constant_def = Literal("const").suppress() + identifier + Literal(
            "=").suppress() + constant + Literal(";").suppress()
        constant_def.setParseAction(self.parse_const)

        type_spec = ((Optional(Literal("unsigned")) +
                      Literal("int")).setParseAction(self.parse_builtin) |
                     (Optional(Literal("unsigned")) +
                      Literal("hyper")).setParseAction(self.parse_builtin)
                     | Literal("float").setParseAction(self.parse_builtin)
                     | Literal("double").setParseAction(self.parse_builtin)
                     | Literal("bool").setParseAction(self.parse_builtin)
                     | enum_type_spec | struct_type_spec | union_type_spec
                     | identifier)
        proc_return = Literal("void") | type_spec
        procedure_def = proc_return + identifier + Literal("(").suppress() + (
            Literal("void") | type_spec) + ZeroOrMore(
                Literal(",").suppress() +
                type_spec) + Literal(")").suppress() + Literal(
                    "=").suppress() + constant + Literal(";").suppress()
        procedure_def.setParseAction(self.parse_procedure_def)
        version_def = Literal("version").suppress() + identifier + Literal(
            "{").suppress() + OneOrMore(procedure_def) + Literal("}").suppress(
            ) + Literal("=").suppress() + constant + Literal(";").suppress()
        version_def.setParseAction(self.parse_version_def)
        program_body = Literal("{").suppress() + Group(
            OneOrMore(version_def)) + Literal("}").suppress()

        type_def = (
            (Literal("typedef") + declaration + Literal(";")) |
            (Literal("enum") + identifier + enum_body + Literal(";")) |
            (Literal("struct") + identifier + struct_body + Literal(";")) |
            (Literal("union") + identifier + union_body + Literal(";")) |
            (Literal("program") + identifier + program_body +
             Literal("=").suppress() + constant + Literal(";")))
        type_def.setParseAction(self.parse_type_def)

        declaration << (
            (type_spec + identifier + Literal("[") + value + Literal("]")) |
            (type_spec + identifier + Literal("<") + value + Literal(">")) |
            (type_spec + identifier) | (Literal("opaque") + identifier +
                                        Literal("[") + value + Literal("]")) |
            (Literal("opaque") + identifier + Literal("<") + value +
             Literal(">")) | (Literal("string") + identifier + Literal("<") +
                              value + Literal(">")) |
            (type_spec + Literal("*") + identifier) | Literal("void"))
        declaration.setParseAction(self.parse_decl)

        definition = type_def | constant_def
        specification = ZeroOrMore(definition)
        comment = (Literal("#") + restOfLine).suppress()
        specification.ignore(comment)

        return specification
Exemplo n.º 9
0
#! /usr/bin/env python3

from collections import namedtuple

from pyparsing import (
    alphanums, alphas, Combine, delimitedList, Forward, Group, infixNotation,
    Keyword, oneOf, opAssoc, Optional, Suppress, Word, ParserElement)
from pyparsing import pyparsing_common as ppc

ParserElement.enablePackrat()

LPAR, RPAR, LBRAK, RBRAK, COMMA = map(Suppress, "()[],")
kws = oneOf("and or not id true false forall exists")
BUILTIN = oneOf("abs max min not")
VARNAME = Word(alphas.lower(), alphanums + "_").ignore(kws).ignore(BUILTIN)
TYPENAME = Word(alphas.upper(), alphanums + "_").ignore(kws).ignore(BUILTIN)
EXTERN = Combine(Suppress("_") + VARNAME).setParseAction(lambda toks: toks[0])

# AST nodes for ATLAS properties
EnvNode = namedtuple("Env", ["var", "offset"])
OfNode = namedtuple("Of", ["var", "offset", "agent"])
BinOp = namedtuple("BinOp", ["e1", "op", "e2"])
BuiltIn = namedtuple("BuiltIn", ["fn", "args"])
Nary = namedtuple("Nary", ["fn", "args"])
Quant = namedtuple("Quant", ["quantifier", "typename", "varname", "inner"])
Prop = namedtuple("Prop", ["modality", "quant"])


def pprint(node):
    if isinstance(node, EnvNode):
        return f"{node.var}"
Exemplo n.º 10
0
    def get_parser(self):
        declaration = Forward()
        keyword = (
            Keyword("enum") |
            Keyword("case") |
            Keyword("struct") |
            Keyword("default") |
            Keyword("switch") |
            Keyword("union") |
            Keyword("const") |
            Keyword("unsigned") |
            Keyword("int") |
            Keyword("hyper") |
            Keyword("float") |
            Keyword("double") |
            Keyword("bool") |
            Keyword("typedef") |
            Keyword("opaque") |
            Keyword("string") |
            Keyword("void") |
            Keyword("program") | 
            Keyword("version")
        )
        identifier = NotAny(keyword) + Word(alphas + alphas.upper(), alphanums + alphanums.upper() + "_", asKeyword=True)

        constant = Combine(Optional("-") + Word(nums))
        constant.setParseAction(lambda s,l,t: [int(t[0])])

        value = constant | identifier

        enum_body = Literal("{").suppress() + identifier + Literal("=").suppress() + value + ZeroOrMore(Literal(",").suppress() + identifier + Literal("=").suppress() + value) + Literal("}").suppress()
        enum_type_spec = Literal("enum").suppress() + enum_body
        enum_body.setParseAction(self.parse_enum)

        struct_body = Literal("{").suppress() + OneOrMore(declaration + Literal(";").suppress()) + Literal("}").suppress()
        struct_type_spec = Literal("struct").suppress() + struct_body
        struct_body.setParseAction(self.parse_struct)
        
        case_stmt = Literal("case").suppress() + value + Literal(":").suppress() + declaration + Literal(";").suppress()
        default_stmt = Literal("default") + Literal(":").suppress() + declaration + Literal(";").suppress()
        union_body = Literal("switch").suppress() + Literal("(").suppress() + declaration + Literal(")").suppress() + Literal("{").suppress() + Group(OneOrMore(Group(case_stmt)) + Optional(Group(default_stmt))) + Literal("}").suppress()
        union_type_spec = Literal("union").suppress() + union_body
        union_body.setParseAction(self.parse_union)
        
        constant_def = Literal("const").suppress() + identifier + Literal("=").suppress() + constant + Literal(";").suppress()
        constant_def.setParseAction(self.parse_const)
        
        type_spec = (
            (Optional(Literal("unsigned")) + Literal("int")).setParseAction(self.parse_builtin) |
            (Optional(Literal("unsigned")) + Literal("hyper")).setParseAction(self.parse_builtin) |
            Literal("float").setParseAction(self.parse_builtin) |
            Literal("double").setParseAction(self.parse_builtin) |
            Literal("bool").setParseAction(self.parse_builtin) |
            enum_type_spec |
            struct_type_spec |
            union_type_spec |
            identifier
        )
        proc_return = Literal("void") | type_spec
        procedure_def = proc_return + identifier + Literal("(").suppress() + (Literal("void") | type_spec) + ZeroOrMore(Literal(",").suppress() + type_spec) + Literal(")").suppress() + Literal("=").suppress() + constant + Literal(";").suppress()
        procedure_def.setParseAction(self.parse_procedure_def)
        version_def = Literal("version").suppress() + identifier + Literal("{").suppress() + OneOrMore(procedure_def) + Literal("}").suppress() + Literal("=").suppress() + constant + Literal(";").suppress()
        version_def.setParseAction(self.parse_version_def)
        program_body = Literal("{").suppress() + Group(OneOrMore(version_def)) + Literal("}").suppress()
        
        type_def = (
            (Literal("typedef") + declaration + Literal(";")) |
            (Literal("enum") + identifier + enum_body + Literal(";")) |
            (Literal("struct") + identifier + struct_body + Literal(";")) |
            (Literal("union") + identifier + union_body + Literal(";")) |
            (Literal("program") + identifier + program_body + Literal("=").suppress() + constant + Literal(";"))
        )
        type_def.setParseAction(self.parse_type_def)

        declaration << (
            (type_spec + identifier + Literal("[") + value + Literal("]")) |
            (type_spec + identifier + Literal("<") + value + Literal(">")) |
            (type_spec + identifier) |
            (Literal("opaque") + identifier + Literal("[") + value + Literal("]")) |
            (Literal("opaque") + identifier + Literal("<") + value + Literal(">")) |
            (Literal("string") + identifier + Literal("<") + value + Literal(">")) |
            (type_spec + Literal("*") + identifier) |
            Literal("void")
        )
        declaration.setParseAction(self.parse_decl)

        definition = type_def | constant_def
        specification = ZeroOrMore(definition)
        comment = (Literal("#") + restOfLine).suppress()
        specification.ignore(comment)

        return specification
Exemplo n.º 11
0
    def __init__(self,
                 stock_users,
                 currency_name,
                 index_name,
                 timezone,
                 min_count=100,
                 max_count=500,
                 points_per_death=15,
                 payout_hours=6,
                 min_payoff_period_days=4,
                 max_payoff_period_days=21,
                 slack=None):
        current = set(stock.target_user for stock in StockDoc.objects())
        now = datetime.now()
        last_dividend_time = now.replace(minute=0,
                                         second=0,
                                         microsecond=0,
                                         hour=now.hour // payout_hours *
                                         payout_hours).timestamp()
        valid_letters = {c for c in alphas.upper() if c not in 'AEIOU'}
        for user in stock_users:
            if user not in current:
                total = int(np.random.uniform(low=min_count, high=max_count))
                name = ''.join(c for c in user.upper()
                               if c in valid_letters)[:4]
                StockDoc(target_user=user,
                         name=name,
                         quantity=total,
                         total=total,
                         last_dividend_time=last_dividend_time).save()

        self.payout_hours = payout_hours
        self.next_dividend_time = last_dividend_time + payout_hours * 3600
        self.timezone = timezone
        self.payoff_scale_factor = max_payoff_period_days - min_payoff_period_days
        self.payoff_offset = min_payoff_period_days
        self.points_per_death = points_per_death

        self.check_name = 'Check Stocks'
        self.check_expr = CaselessLiteral('portfolio') + StringEnd()
        self.check_doc = 'Check your stock inventory:\n\tportfolio'

        self.info_name = 'Stock Info'
        self.info_expr = CaselessLiteral(
            'info') + symbols.user_name.setResultsName('stock') + StringEnd()
        self.info_doc = ('Check prices, inventory and dividends for a stock:\n'
                         '\tinfo <stock>')

        self.available_name = 'Available Stocks'
        self.available_expr = CaselessLiteral('ticker') + StringEnd()
        self.available_doc = ('See what stocks are available:\n' '\tticker')

        self.index_name = index_name
        self.index_expr = CaselessLiteral('index') + StringEnd()
        self.index_doc = ('Check the {}:\n' '\tindex').format(index_name)

        self.buy_name = 'Buy Stocks'
        self.buy_expr = (
            CaselessLiteral('buy') +
            Optional(symbols.flag_with_arg('amount', Word(nums))) +
            symbols.user_name.setResultsName('stock'))
        self.buy_doc = 'Buy stocks for {}:\n\tbuy <stock>'.format(
            currency_name)

        self.sell_name = 'Sell Stocks'
        self.sell_expr = (
            CaselessLiteral('sell') +
            Optional(symbols.flag_with_arg('amount', Word(nums))) +
            symbols.user_name.setResultsName('stock'))
        self.sell_doc = 'Sell stocks for {}:\n\tsell <stock>'.format(
            currency_name)

        self.dividend_name = 'Give dividend on demand'
        self.dividend_expr = CaselessLiteral('dividend') + StringEnd()
        self.dividend_doc = (
            'Pay dividend immediately for all stocks. Does not delay next dividend:\n'
            '\tdividend')

        self.currency_name = currency_name
        self.name_to_sname = stock_users
Exemplo n.º 12
0
        print(pe)
    else:
        if fn != None:
            print(fn( bnf.parseString( strg ) ))
        else:
            print()

digits = "0123456789"

# Version 1
element = Regex("A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|"
                "E[rsu]|F[emr]?|G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|"
                "M[dgnot]|N[abdeiop]?|Os?|P[abdmortu]?|R[abefghnu]|"
                "S[bcegimnr]?|T[abcehilm]|U(u[bhopqst])?|V|W|Xe|Yb?|Z[nr]")

element = Word( alphas.upper(), alphas.lower(), max=2)
elementRef = Group( element + Optional( Word( digits ), default="1" ) )
formula = OneOrMore( elementRef )

fn = lambda elemList : sum( [ atomicWeight[elem]*int(qty) for elem,qty in elemList ] )
test( formula, "H2O", fn )
test( formula, "C6H5OH", fn )
test( formula, "NaCl", fn )
print()

# Version 2 - access parsed items by field name
elementRef = Group( element("symbol") + Optional( Word( digits ), default="1" )("qty") )
formula = OneOrMore( elementRef )

fn = lambda elemList : sum( [ atomicWeight[elem.symbol]*int(elem.qty) for elem in elemList ] )
test( formula, "H2O", fn )
Exemplo n.º 13
0
                       oneOf, FollowedBy, pythonStyleComment, ungroup,
                       ParserElement, ParseResults, printables)
from pyparsing import pyparsing_common as ppc

ParserElement.enablePackrat()

LBRACE, \
    RBRACE, LBRACK, RBRACK, EQ, COLON,\
    SEMICOLON, COMMA, LPAR, RPAR, RAWPREFIX = map(Suppress, "{}[]=:;,()$")
NoWhite = pp.NotAny(pp.White())

kws = oneOf("and or not id true false")

VARNAME = Word(alphas.lower(), alphanums + "_").ignore(kws)
# TODO reserve keywords in IDENTIFIER
IDENTIFIER = Word(alphas.upper(), alphanums).ignore(Keyword("Skip"))
EXTERN = Combine(Literal("_") + VARNAME)


def konst(val):
    def f(*args, **kwargs):
        return val

    return f


def to_sexpr(name, add=None):
    def fn(toks):
        return [name, *toks, *add] if add else [name, *toks]

    return fn
Exemplo n.º 14
0
    def _get_infos_from_file(self, info):
        # Object file line: '{object}:  file format elf32-xtensa-le'
        object = Fragment.ENTITY.setResultsName("object") + Literal(":").suppress() + Literal("file format elf32-xtensa-le").suppress()

        # Sections table
        header = Suppress(Literal("Sections:") + Literal("Idx") + Literal("Name") + Literal("Size") + Literal("VMA") +
                          Literal("LMA") + Literal("File off") + Literal("Algn"))
        entry = Word(nums).suppress() + Fragment.ENTITY + Suppress(OneOrMore(Word(alphanums, exact=8)) +
                                                                   Word(nums + "*") + ZeroOrMore(Word(alphas.upper()) +
                                                                   Optional(Literal(","))))

        # Content is object file line + sections table
        content = Group(object + header + Group(ZeroOrMore(entry)).setResultsName("sections"))

        parser = Group(ZeroOrMore(content)).setResultsName("contents")

        sections_info_text = info.content
        results = None

        try:
            results = parser.parseString(sections_info_text)
        except ParseException as p:
            raise ParseException("Unable to parse section info file " + info.filename + ". " + p.message)

        return results
Exemplo n.º 15
0
import numpy as np
import matplotlib.pyplot as plt
from pyparsing import Word, alphas, nums

source = open("simpsonsRule_acctest.out", "r")
line = Word(alphas.upper(),
            alphas.lower()) + ':' + Word('-' + nums + '.') + ','
line = line + Word(alphas) + ':' + Word(nums + '.') + ','
line = line + Word(alphas) + ':' + Word(nums) + ','
line = line + Word(alphas) + ':' + Word(nums + '.')

src = [line.strip() for line in source]
acc = list()
N = list()
for _ in src:
    N.append(line.parseString(_)[10])
for _ in src:
    acc.append(line.parseString(_)[2])

print(N)
print(acc)

x = np.array(N, dtype=float)
y = np.array(acc, dtype=float)

fig = plt.figure()
plt.plot(x, y, "o-", color='r')
plt.xlabel("N")
plt.ylabel("acc")
plt.title("f : acc(N)")
fig.savefig("simpsonsRule_acctest.png")
Exemplo n.º 16
0
from pyparsing import Word, Optional, OneOrMore, Group, ParseException, Regex
from pyparsing import alphas

atomicWeight = {
    "O": 15.9994,
    "H": 1.00794,
    "Na": 22.9897,
    "Cl": 35.4527,
    "C": 12.0107,
}

digits = "0123456789"

# Version 1
element = Word(alphas.upper(), alphas.lower(), max=2)
# for stricter matching, use this Regex instead
# element = Regex("A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|"
#                 "E[rsu]|F[emr]?|G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|"
#                 "M[dgnot]|N[abdeiop]?|Os?|P[abdmortu]?|R[abefghnu]|"
#                 "S[bcegimnr]?|T[abcehilm]|U(u[bhopqst])?|V|W|Xe|Yb?|Z[nr]")
elementRef = Group(element + Optional(Word(digits), default="1"))
formula = OneOrMore(elementRef)

fn = lambda elemList: sum(atomicWeight[elem] * int(qty)
                          for elem, qty in elemList)
formula.runTests(
    """\
    H2O
    C6H5OH
    NaCl
Exemplo n.º 17
0
    delimitedList

# http://www.antlr.org/grammar/ANTLR/ANTLRv3.g

# Tokens
EOL = Suppress(LineEnd()) # $
singleTextString = originalTextFor(ZeroOrMore(~EOL + (White(" \t") | Word(printables)))).leaveWhitespace()
XDIGIT = hexnums
INT = Word(nums)
ESC = Literal('\\') + (oneOf(list(r'nrtbf\">'+"'")) | ('u' + Word(hexnums, exact=4)) | Word(printables, exact=1))
LITERAL_CHAR = ESC | ~(Literal("'") | Literal('\\')) + Word(printables, exact=1)
CHAR_LITERAL = Suppress("'") + LITERAL_CHAR + Suppress("'")
STRING_LITERAL = Suppress("'") + Combine(OneOrMore(LITERAL_CHAR)) + Suppress("'") 
DOUBLE_QUOTE_STRING_LITERAL = '"' + ZeroOrMore(LITERAL_CHAR) + '"'
DOUBLE_ANGLE_STRING_LITERAL = '<<' + ZeroOrMore(Word(printables, exact=1)) + '>>'
TOKEN_REF = Word(alphas.upper(), alphanums+'_')
RULE_REF = Word(alphas.lower(), alphanums+'_')
ACTION_ESC = (Suppress("\\") + Suppress("'")) | Suppress('\\"') | Suppress('\\') + (~(Literal("'") | Literal('"')) + Word(printables, exact=1))
ACTION_CHAR_LITERAL = Suppress("'") + (ACTION_ESC | ~(Literal('\\') | Literal("'")) + Word(printables, exact=1)) + Suppress("'")
ACTION_STRING_LITERAL = Suppress('"') + ZeroOrMore(ACTION_ESC | ~(Literal('\\') | Literal('"')) + Word(printables, exact=1)) + Suppress('"') 
SRC = Suppress('src') + ACTION_STRING_LITERAL("file") + INT("line")
id = TOKEN_REF | RULE_REF
SL_COMMENT = Suppress('//') + Suppress('$ANTLR') + SRC | ZeroOrMore(~EOL + Word(printables)) + EOL
ML_COMMENT = cStyleComment
WS = OneOrMore(Suppress(' ') | Suppress('\t') | (Optional(Suppress('\r')) + Literal('\n')))
WS_LOOP = ZeroOrMore(SL_COMMENT | ML_COMMENT)
NESTED_ARG_ACTION = Forward()
NESTED_ARG_ACTION << Suppress('[') + ZeroOrMore(NESTED_ARG_ACTION | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL) + Suppress(']')
ARG_ACTION = NESTED_ARG_ACTION
NESTED_ACTION = Forward()
NESTED_ACTION << Suppress('{') + ZeroOrMore(NESTED_ACTION | SL_COMMENT | ML_COMMENT | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL) + Suppress('}')
Exemplo n.º 18
0
    Word,
    ZeroOrMore,
)

region_identifier = Word(alphanums + '_')

region_start = (LineStart() + '@region $ ' +
                region_identifier.setResultsName('region_identifier') + '{' +
                LineEnd())
region_end = LineStart() + '}' + LineEnd()

region_param_valid = Word(alphanums + '_.')
region_param = (region_param_valid + ': ' + region_param_valid + ';' +
                LineEnd())

mod_identifier = Word(alphas.upper() + alphas.lower() + '_')
mod_param_name = Word(' ' + alphas.upper() + alphas.lower() + '_')

mods_start_line = '[' + LineEnd()

mod_param = mod_param_name + ':' + Word(nums) + ";"
mod_param_additional_line = Word(nums) + ':' + Word(nums) + ';'

mod_start = '@' + mod_identifier.setResultsName('mod_identifier') + '{'
mod_end = '}' + LineEnd()

mod = (mod_start + ZeroOrMore(mod_param).setResultsName('mod_params') +
       ZeroOrMore(mod_param_additional_line).setResultsName(
           'mod_params_additional') + mod_end)

mods_end_line = ']' + LineEnd()