Beispiel #1
0
    def init_parser(self):
        """
        expop   :: '^'
        multop  :: '*' | '/'
        addop   :: '+' | '-'
        integer :: ['+' | '-'] '0'..'9'+
        atom    :: real | Word(alphas) | array | fn '(' expr ')' | '(' expr ')'
        factor  :: atom [ expop factor ]*
        term    :: factor [ multop factor ]*
        expr    :: term [ addop term ]*
        """

        atoms = self.language.get_parser_atoms()

        variable = atoms["variable"]  # .setParseAction(downcaseTokens)

        func_lpar = atoms["func_lpar"].setParseAction(self._push2stack("("))
        func_delim = atoms["func_delim"]
        func_rpar = atoms["func_rpar"].setParseAction(self._push2stack(")"))

        array_lpar = atoms["array_lpar"].setParseAction(self._push2stack("["))
        array_delim = atoms["array_delim"]
        array_rpar = atoms["array_rpar"].setParseAction(self._push2stack("]"))

        lpar = atoms["lpar"].suppress()
        rpar = atoms["rpar"].suppress()
        expop = atoms["exp"]

        addop = atoms["plus"] | atoms["minus"]
        multop = atoms["mult"] | atoms["div"]
        cmpop = atoms["equal"]

        expr = Forward()  # forward declaration of an entire expression
        # this is necessary for defining the recursive grammar

        # smallest entity of a mathematical expression:
        array = (
            variable
            + array_lpar
            + atoms["int"].addParseAction(self.push_first)
            + ZeroOrMore(array_delim + atoms["int"])
            + array_rpar
        )

        func_call = (
            atoms["function"].setParseAction(downcaseTokens)
            + func_lpar
            + expr
            + ZeroOrMore(func_delim + expr)
            + func_rpar
        )

        obj = atoms["consts"] | atoms["float"] | array | func_call | variable
        atom = (
            Optional("-")
            + (  # optional unary minus
                obj.addParseAction(self.push_first)
                | (lpar + expr.suppress() + rpar)  # subexpression
            )
        ).setParseAction(self.push_unary_minus)

        # by defining exponentiation as "atom [ ^ factor ]..." instead of
        # "atom [ ^ atom ]...", we get right-to-left exponents, instead of
        # left-to-right. That is, 2^3^2 = 2^(3^2), not (2^3)^2.
        factor = Forward()
        factor << atom + ZeroOrMore((expop + factor).setParseAction(self.push_first))

        # sequence of multiplications
        term = factor + ZeroOrMore((multop + factor).setParseAction(self.push_first))

        # sequence of summations
        expr << term + ZeroOrMore((addop + term).setParseAction(self.push_first))

        # comparison operators
        equation = expr + Optional(cmpop + expr).setParseAction(self.push_first)

        # assignment operator
        self.parser = (
            (variable ^ array).setParseAction(self.push_first)
            + atoms["assign"]
            + equation
        ).setParseAction(self._push2stack("=")) | equation

        return self.parser
Beispiel #2
0
# -*- coding: utf-8 -*-

"""Parse the TSV template format with PyParsing then make a new parser."""

from pyparsing import Group, MatchFirst, Optional, Suppress, delimitedList, nestedExpr, pyparsing_common as ppc

from table_validator import parse_tsv

keyword = ppc.identifier + Suppress('=') + ppc.identifier
te_keywords = nestedExpr(content=delimitedList(keyword))
te_content = ppc.identifier + Optional(te_keywords)
template_command = nestedExpr(opener='{', closer='}', content=te_content)

cell = MatchFirst([
    Group(template_command)('command'),
    Group(template_command)('command') + ppc.identifier('text'),
    ppc.identifier('text'),
])

if __name__ == '__main__':
    with open('../../tests/repeat_template.tsv') as file:
        t = [
            [
                cell.parseString(col)
                for col in row
            ]
            for row in parse_tsv(file)
        ]

    for i, row in enumerate(t):
        for j, col in enumerate(row):
Beispiel #3
0
#       Only IDENTIFIER and EQUATIONS are ever used later
###############################################################################
# Basic Elements
###############################################################################

# identifiers like in C: can start with letter or underscore, then a
# combination of letters, numbers and underscores
# Note that the check_identifiers function later performs more checks, e.g.
# names starting with underscore should only be used internally
IDENTIFIER = Word(string.ascii_letters + '_', string.ascii_letters +
                  string.digits + '_').setResultsName('identifier')

# very broad definition here, expression will be analysed by sympy anyway
# allows for multi-line expressions, where each line can have comments
EXPRESSION = Combine(OneOrMore(
    (CharsNotIn(':#\n') + Suppress(Optional(LineEnd()))).ignore('#' +
                                                                restOfLine)),
                     joinString=' ').setResultsName('expression')

# a unit
# very broad definition here, again. Whether this corresponds to a valid unit
# string will be checked later
UNIT = Word(string.ascii_letters + string.digits +
            '*/.- ').setResultsName('unit')

# a single Flag (e.g. "const" or "event-driven")
FLAG = Word(string.ascii_letters, string.ascii_letters + '_- ' + string.digits)

# Flags are comma-separated and enclosed in parantheses: "(flag1, flag2)"
FLAGS = (Suppress('(') + FLAG + ZeroOrMore(Suppress(',') + FLAG) +
         Suppress(')')).setResultsName('flags')
Beispiel #4
0
    def __init__(self):
        """
        Initialize Numeric Parser.

        expop   :: '^'
        multop  :: '*' | '/'
        addop   :: '+' | '-'
        integer :: ['+' | '-'] '0'..'9'+
        atom    :: PI | E | real | fn '(' expr ')' | '(' expr ')'
        factor  :: atom [ expop factor ]*
        term    :: factor [ multop factor ]*
        expr    :: term [ addop term ]*
        """
        point = Literal(".")
        e = CaselessLiteral("E")
        fnumber = Combine(
            Word("+-" + nums, nums) + Optional(point + Optional(Word(nums))) +
            Optional(e + Word("+-" + nums, nums)))
        ident = Word(alphas, alphas + nums + "_$")
        plus = Literal("+")
        minus = Literal("-")
        mult = Literal("*")
        div = Literal("/")
        lpar = Literal("(").suppress()
        rpar = Literal(")").suppress()
        addop = plus | minus
        multop = mult | div
        expop = Literal("^")
        pi = CaselessLiteral("PI")
        expr = Forward()
        atom = ((Optional(oneOf("- +")) +
                 (ident + lpar + expr + rpar | pi | e
                  | fnumber).setParseAction(self.pushFirst))
                | Optional(oneOf("- +")) +
                Group(lpar + expr + rpar)).setParseAction(self.pushUMinus)
        # by defining exponentiation as "atom [ ^ factor ]..." instead of
        # "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-right
        # that is, 2^3^2 = 2^(3^2), not (2^3)^2.
        factor = Forward()
        factor << atom + \
            ZeroOrMore((expop + factor).setParseAction(self.pushFirst))
        term = factor + \
            ZeroOrMore((multop + factor).setParseAction(self.pushFirst))
        expr << term + \
            ZeroOrMore((addop + term).setParseAction(self.pushFirst))
        # addop_term = ( addop + term ).setParseAction( self.pushFirst )
        # general_term = term + ZeroOrMore( addop_term ) | OneOrMore( addop_term)
        # expr <<  general_term
        self.bnf = expr
        # map operator symbols to corresponding arithmetic operations
        epsilon = 1e-12
        self.opn = {
            "+": operator.add,
            "-": operator.sub,
            "*": operator.mul,
            "/": operator.truediv,
            "^": operator.pow
        }
        self.fn = {
            "sin": math.sin,
            "cos": math.cos,
            "tan": math.tan,
            "exp": math.exp,
            "abs": abs,
            "trunc": lambda a: int(a),
            "round": round,
            "sgn": lambda a: abs(a) > epsilon and cmp(a, 0) or 0
        }
          to package service rpc returns true false option import syntax"""
for kw in kwds.split():
    exec("{}_ = Keyword('{}')".format(kw.upper(), kw))

messageBody = Forward()

messageDefn = MESSAGE_ - ident("messageId") + LBRACE + messageBody(
    "body") + RBRACE

typespec = (oneOf("""double float int32 int64 uint32 uint64 sint32 sint64
                    fixed32 fixed64 sfixed32 sfixed64 bool string bytes""")
            | ident)
rvalue = integer | TRUE_ | FALSE_ | ident
fieldDirective = LBRACK + Group(ident + EQ + rvalue) + RBRACK
fieldDefnPrefix = REQUIRED_ | OPTIONAL_ | REPEATED_
fieldDefn = (Optional(fieldDefnPrefix) + typespec("typespec") +
             ident("ident") + EQ + integer("fieldint") +
             ZeroOrMore(fieldDirective) + SEMI)

# enumDefn ::= 'enum' ident '{' { ident '=' integer ';' }* '}'
enumDefn = (ENUM_("typespec") - ident("name") + LBRACE +
            Dict(ZeroOrMore(Group(ident + EQ + integer + SEMI)))("values") +
            RBRACE)

# extensionsDefn ::= 'extensions' integer 'to' integer ';'
extensionsDefn = EXTENSIONS_ - integer + TO_ + integer + SEMI

# messageExtension ::= 'extend' ident '{' messageBody '}'
messageExtension = EXTEND_ - ident + LBRACE + messageBody + RBRACE

# messageBody ::= { fieldDefn | enumDefn | messageDefn | extensionsDefn | messageExtension }*
Beispiel #6
0
def rc_statement():
    """
    Generate a RC statement parser that can be used to parse a RC file

    :rtype: pyparsing.ParserElement
    """

    one_line_comment = "//" + restOfLine

    comments = cStyleComment ^ one_line_comment

    precompiler = Word("#", alphanums) + restOfLine

    language_definition = (
        "LANGUAGE"
        + Word(alphas + "_").setResultsName("language")
        + Optional("," + Word(alphas + "_").setResultsName("sublanguage"))
    )

    block_start = (Keyword("{") | Keyword("BEGIN")).setName("block_start")
    block_end = (Keyword("}") | Keyword("END")).setName("block_end")

    reserved_words = block_start | block_end

    name_id = ~reserved_words + Word(alphas, alphanums + "_").setName("name_id")

    numbers = Word(nums)

    integerconstant = numbers ^ Combine("0x" + numbers)

    constant = Combine(
        Optional(Keyword("NOT")) + (name_id | integerconstant),
        adjacent=False,
        joinString=" ",
    )

    combined_constants = delimitedList(constant, "|")

    concatenated_string = OneOrMore(quotedString)

    block_options = Optional(
        SkipTo(Keyword("CAPTION"), failOn=block_start)("pre_caption")
        + Keyword("CAPTION")
        + quotedString("caption")
    ) + SkipTo(block_start)("post_caption")

    undefined_control = Group(
        name_id.setResultsName("id_control")
        + delimitedList(
            concatenated_string ^ constant ^ numbers ^ Group(combined_constants)
        ).setResultsName("values_")
    )

    block = block_start + ZeroOrMore(undefined_control)("controls") + block_end

    dialog = (
        name_id("block_id")
        + (Keyword("DIALOGEX") | Keyword("DIALOG"))("block_type")
        + block_options
        + block
    )

    string_table = Keyword("STRINGTABLE")("block_type") + block_options + block

    menu_item = Keyword("MENUITEM")("block_type") + (
        commaSeparatedList("values_") | Keyword("SEPARATOR")
    )

    popup_block = Forward()

    popup_block <<= Group(
        Keyword("POPUP")("block_type")
        + Optional(quotedString("caption"))
        + block_start
        + ZeroOrMore(Group(menu_item | popup_block))("elements")
        + block_end
    )("popups*")

    menu = (
        name_id("block_id")
        + Keyword("MENU")("block_type")
        + block_options
        + block_start
        + ZeroOrMore(popup_block)
        + block_end
    )

    statem = comments ^ precompiler ^ language_definition ^ dialog ^ string_table ^ menu

    return statem
Beispiel #7
0
    if string[0] in 'aeiou':
        return 'an ' + string
    return 'a ' + string


def capitalise(s):
    """Capitalise in BrE."""
    #we do it -better- than s.capitalize() - that lowercases the rest.
    if not s:
        #don't blow up on the empty string - otherwise we get IndexError
        return ''
    return s[0].upper() + s[1:]


#it takes prefixes of symbols to be the 'head word'.
_hwspattern = (Word(punctuation) + Optional(Word(alnumspace))) ^ \
              (Optional(Word(nwprintable)) + Optional(Word(printable)))

#XXX: tabs. watch it blow up!


def head_word_split(string):
    """Split off the first word or group of non-whitespace punctuation."""
    res = _hwspattern.parseString(string)
    if len(res) == 0:
        return ('', '')
    elif len(res) == 1:
        return (res[0], '')
    else:
        return tuple(res)
Beispiel #8
0
def usfmTokenValue(key, value):
    return Group(
        Suppress(backslash) + Literal(key) + Suppress(White()) +
        Optional(value))
Beispiel #9
0
    return OneOrMore(token + maybeComma)


digit_sequence = Word(nums)

sign = oneOf("+ -")


def convertToFloat(s, loc, toks):
    try:
        return float(toks[0])
    except:
        raise ParseException(loc, "invalid float format %s" % toks[0])


exponent = CaselessLiteral("e") + Optional(sign) + Word(nums)

#note that almost all these fields are optional,
#and this can match almost anything. We rely on Pythons built-in
#float() function to clear out invalid values - loosely matching like this
#speeds up parsing quite a lot
floatingPointConstant = Combine(
    Optional(sign) + Optional(Word(nums)) +
    Optional(Literal(".") + Optional(Word(nums))) + Optional(exponent))

floatingPointConstant.setParseAction(convertToFloat)

number = floatingPointConstant

#same as FP constant but don't allow a - sign
nonnegativeNumber = Combine(
    def parse_file(self):
        """Parses an existing namelist file and creates a deck of cards to
        hold the data. After this is executed, you need to call the ``load_model()``
        method to extract the variables from this data structure."""
        
        infile = open(self.filename, 'r')
        data = infile.readlines()
        infile.close()
        
        # Lots of numerical tokens for recognizing various kinds of numbers
        digits = Word(nums)
        dot = "."
        sign = oneOf("+ -")
        ee = CaselessLiteral('E') | CaselessLiteral('D')
    
        num_int = ToInteger(Combine( Optional(sign) + digits ))
        
        num_float = ToFloat(Combine( Optional(sign) + 
                            ((digits + dot + Optional(digits)) |
                             (dot + digits)) +
                             Optional(ee + Optional(sign) + digits)
                            ))
        
        # special case for a float written like "3e5"
        mixed_exp = ToFloat(Combine( digits + ee + Optional(sign) + digits ))
        
        # I don't suppose we need these, but just in case (plus it's easy)
        nan = ToFloat(oneOf("NaN Inf -Inf"))
        
        numval = num_float | mixed_exp | num_int | nan
        strval =  QuotedString(quoteChar='"') | QuotedString(quoteChar="'")
        b_list = "T TRUE True true F FALSE False false .TRUE. .FALSE. .T. .F."
        boolval = ToBool(oneOf(b_list))
        fieldval = Word(alphanums)
        
        # Tokens for parsing a line of data
        numstr_token = numval + ZeroOrMore(Suppress(',') + numval) \
                   | strval
        data_token = numstr_token | boolval
        index_token = Suppress('(') + num_int + Suppress(')')
        
        card_token = Group(fieldval("name") + \
                           Optional(index_token("index")) + \
                           Suppress('=') + \
                           data_token("value") +
                           Optional(Suppress('*') + num_int("dimension")))
        multi_card_token = (card_token + ZeroOrMore(Suppress(',') + card_token))
        array_continuation_token = numstr_token.setResultsName("value")
        array2D_token = fieldval("name") + Suppress("(") + \
                        Suppress(num_int) + Suppress(',') + \
                        num_int("index") + Suppress(')') + \
                        Suppress('=') + numval + \
                        ZeroOrMore(Suppress(',') + numval)
        
        # Tokens for parsing the group head and tail
        group_end_token = Literal("/") | Literal("$END") | Literal("$end")
        group_name_token = (Literal("$") | Literal("&")) + \
                           Word(alphanums).setResultsName("name") + \
                           Optional(multi_card_token) + \
                           Optional(group_end_token)
        
        # Comment Token
        comment_token = Literal("!")
        
        # Loop through each line and parse.
        
        current_group = None
        for line in data:
            line_base = line
            line = line.strip()
            
            # blank line: do nothing
            if not line:
                continue
                
            if current_group:
                
                # Skip comment cards
                if comment_token.searchString(line):
                    pass
                
                # Process orindary cards
                elif multi_card_token.searchString(line):
                    cards = multi_card_token.parseString(line)

                    for card in cards:
                        name, value = _process_card_info(card)
                        self.cards[-1].append(Card(name, value))
                        
                # Catch 2D arrays like -> X(1,1) = 3,4,5
                elif array2D_token.searchString(line):
                    card = array2D_token.parseString(line)
                    
                    name = card[0]
                    index = card[1]
                    value = array(card[2:])
                    
                    if index > 1:
                        old_value = self.cards[-1][-1].value
                        new_value = vstack((old_value, value))
                        self.cards[-1][-1].value = new_value
                    else:
                        self.cards[-1].append(Card(name, value))
                    
                # Arrays can be continued on subsequent lines
                # The value of the most recent card must be turned into an
                # array and appended
                elif array_continuation_token.searchString(line):
                    card = array_continuation_token.parseString(line)
                    
                    if len(card) > 1:
                        element = array(card[0:])
                    else:
                        element = card.value
                        
                    if isinstance(self.cards[-1][-1].value, ndarray):
                        new_value = append(self.cards[-1][-1].value, element)
                    else:
                        new_value = array([self.cards[-1][-1].value, element])
                    
                    self.cards[-1][-1].value = new_value
                    
                # Lastly, look for the group footer
                elif group_end_token.searchString(line):
                    current_group = None
                    
                # Everything else must be a pure comment
                else:
                    print "Comment ignored: %s" % line.rstrip('\n')

                # Group ending '/' can also conclude a data line.
                if line[-1] == '/':
                    current_group = None
                    
                #print self.cards[-1][-1].name, self.cards[-1][-1].value
            else:
                group_name = group_name_token.searchString(line)
                
                # Group Header
                if group_name:
                    group_name = group_name_token.parseString(line)
                    current_group = group_name.name
                    self.add_group(current_group)
                    
                    # Sometimes, variable definitions are included on the
                    # same line as the namelist header
                    if len(group_name) > 2:
                        cards = group_name[2:]
                        
                        for card in cards:
                            # Sometimes an end card is on the same line.
                            if group_end_token.searchString(card):
                                current_group = None
                            else:
                                name, value = _process_card_info(card)
                                self.cards[-1].append(Card(name, value))

                # If there is an ungrouped card at the start, take it as the
                # title for the analysis
                elif len(self.cards) == 0 and self.title == '':
                    self.title = line
                    
                # All other ungrouped cards are saved as free-form (card-less)
                # groups.
                # Note that we can't lstrip because column spacing might be
                # important.
                else:
                    self.add_group(line_base.rstrip())
Beispiel #11
0
        Optional(value))


def usfmTokenNumber(key):
    return Group(
        Suppress(backslash) + Literal(key) + Suppress(White()) +
        Word(nums + '-()') + Suppress(White()))


# define grammar
#phrase      = Word( alphas + u"-.,!? —–‘“”’;:()'\"[]/&%=*…{}" + nums )
phrase = CharsNotIn(u"\n\\")
backslash = Literal(u"\\")
plus = Literal(u"+")

textBlock = Group(Optional(NoMatch(), u"text") + phrase)
unknown = Group(
    Optional(NoMatch(), u"unknown") + Suppress(backslash) +
    CharsNotIn(u' \n\t\\'))

id = usfmTokenValue(u"id", phrase)
ide = usfmTokenValue(u"ide", phrase)
h = usfmTokenValue(u"h", phrase)
toc = usfmTokenValue(u"toc", phrase)
toc1 = usfmTokenValue(u"toc1", phrase)
toc2 = usfmTokenValue(u"toc2", phrase)
toc3 = usfmTokenValue(u"toc3", phrase)
mt = usfmTokenValue(u"mt", phrase)
mt1 = usfmTokenValue(u"mt1", phrase)
mt2 = usfmTokenValue(u"mt2", phrase)
mt3 = usfmTokenValue(u"mt3", phrase)
Beispiel #12
0
def build_grammer():

    selectStmt = Forward()
    compoundselectStmt = Forward()
    subgraphselectStmt = Forward()
    sampleselectStmt = Forward()

    selectToken = oneOf("select find get what search list", caseless=True)
    fromToken = Keyword("from", caseless=True)
    whereToken = Keyword("where", caseless=True)
    sampleToken = Keyword("sample", caseless=True)
    subgraphToken = Keyword("subgraph", caseless=True)
    neighborsToken = Keyword("neighbors", caseless=True)
    targetToken = oneOf("edges nodes  node edge", caseless=True)
    ident = Word(alphas, alphanums + "_$").setName("identifier")

    columnName = Combine((oneOf("v u e") + "." + ident)) | Combine(
        neighborsToken + "(" +
        Word(nums).setResultsName("friends", listAllMatches=True) + ")" + "." +
        ident) | ident

    whereExpression = Forward()

    runs_ = Keyword("number", caseless=True)

    and_ = Keyword("and", caseless=True)
    or_ = Keyword("or", caseless=True)
    in_ = Keyword("in", caseless=True)
    size_ = Keyword("size", caseless=True)

    E = CaselessLiteral("E")
    binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True)

    setop = oneOf("union intersect except", caseless=True)
    arithSign = Word("+-", exact=1)
    realNum = Combine(
        Optional(arithSign) +
        (Word(nums) + "." + Optional(Word(nums)) | ("." + Word(nums))) +
        Optional(E + Optional(arithSign) + Word(nums)))

    samplestmt = (sampleToken + "(" + runs_ + "=" +
                  Word(nums).setResultsName("runs") + "," + size_ + "=" + "[" +
                  Word(nums).setResultsName("lb") + "," +
                  Word(nums).setResultsName("sample") + "]" + ")")

    subgraphstmt = (subgraphToken.setResultsName("type") + "(" +
                    Word(nums).setResultsName("startnode") + "," +
                    Word(nums).setResultsName("depth") + ")")

    intNum = Combine(
        Optional(arithSign) + Word(nums) +
        Optional(E + Optional("+") + Word(nums)))

    columnRval = realNum | intNum | quotedString | columnName.setResultsName(
        "column", listAllMatches=True)

    whereCondition = (columnName.setResultsName(
        "column", listAllMatches=True) + binop + columnRval) | (
            columnName.setResultsName("column", listAllMatches=True) + in_ +
            "(" + columnRval + ZeroOrMore("," + columnRval) + ")")

    whereCondition2 = Group(
        (columnName.setResultsName("column", listAllMatches=True) + binop +
         columnRval)
        | (columnName.setResultsName("column", listAllMatches=True) + in_ +
           "(" + columnRval + ZeroOrMore("," + columnRval) + ")")
        | ("(" + whereExpression + ")"))

    whereExpression << whereCondition + ZeroOrMore(
        (and_ | or_) + whereExpression)

    defstmt = ident.setResultsName("graph") + "." + targetToken.setResultsName(
        "type") + "=" + "{" + delimitedList(whereCondition2).setResultsName(
            "compactwhere") + "}"

    selectStmt << (selectToken + targetToken.setResultsName("type") +
                   fromToken + (ident.setResultsName("graph")))

    sampleselectStmt << (
        selectToken + samplestmt + targetToken.setResultsName("type") +
        fromToken + (ident.setResultsName("graph")) + Optional(whereToken + (
            whereExpression).setResultsName("where", listAllMatches=True)))

    subgraphselectStmt << (selectToken + subgraphstmt + fromToken +
                           (ident.setResultsName("graph")))

    compoundselectStmt << selectStmt.setResultsName(
        "select", listAllMatches=True) + ZeroOrMore(
            setop.setResultsName("setop", listAllMatches=True) + selectStmt)

    SQL = sampleselectStmt | compoundselectStmt | subgraphselectStmt

    bSQL = SQL
    SqlComment = "--" + restOfLine
    bSQL.ignore(SqlComment)

    return bSQL
Beispiel #13
0
    def __init__(self, ws=" \t\r\n\f", min=1, max=0, exact=0):
        super(White, self).__init__(ws, min, max, exact)


escaped = (
    Literal("\\").suppress() +
    #chr(20)-chr(126) + chr(128)-unichr(sys.maxunicode)
    Regex("[\u0020-\u007e\u0080-\uffff]", re.IGNORECASE))


def convertToUnicode(t):
    return chr(int(t[0], 16))


hex_unicode = (
    Literal("\\").suppress() + Regex("[0-9a-f]{1,6}", re.IGNORECASE) +
    Optional(White(exact=1)).suppress()).setParseAction(convertToUnicode)

escape = hex_unicode | escaped

#any unicode literal outside the 0-127 ascii range
nonascii = Regex("[^\u0000-\u007f]")

#single character for starting an identifier.
nmstart = Regex("[A-Z]", re.IGNORECASE) | nonascii | escape

nmchar = Regex("[0-9A-Z-]", re.IGNORECASE) | nonascii | escape

identifier = Combine(nmstart + ZeroOrMore(nmchar))
Beispiel #14
0
        def parse_step1( morph ):
            """parse the field morphology of qurany corpus

            """


            string = "$ " + str( morph ).replace( "POS:", "£ POS:" ).replace( "PRON:", "µ PRON:" ).replace( "&lt;", "<" ).replace( "&gt;", ">" ) + " #"
            #regular expressions 
            begin = Keyword( '$' ).suppress()
            center = Keyword( '£' ).suppress()
            last = Keyword( 'µ' ).suppress()
            end = Keyword( '#' ).suppress()
            skip = SkipTo( end ).suppress()

            prefix = Word( alphas + "+" + ":" )
            prefixes = Group( ZeroOrMore( ~center + prefix ) )

            genderK = TagKeywords( ["M", "F"] )
            numberK = TagKeywords( ["S", "D", "P"] )
            personK = TagKeywords( ["1", "2", "3"] )

            genderL = TagLiterals( ["M", "F"] )
            numberL = TagLiterals( ["S", "D", "P"] )
            personL = TagLiterals( ["1", "2", "3"] )

            person_ = personL + Optional( genderL ) + Optional( numberL )
            gender_ = genderL + numberL



            gen = person_ | gender_ | numberK | genderK
            pos = "POS:" + Word( alphas )
            lem = "LEM:" + CharsNotIn( " " )
            root = "ROOT:" + CharsNotIn( " " )
            sp = "SP:" + CharsNotIn( " " )
            mood = "MOOD:" + CharsNotIn( " " )

            aspect = TagKeywords( ["PERF", "IMPF", "IMPV"] )

            voice = TagKeywords( ["ACT", "PASS"] )
            form = TagKeywords( ["(I)", "(II)", "(III)", "(IV)", "(V)", "(VI)", "(VII)", "(VIII)", "(IX)", "(X)", "(XI)", "(XII)"] )
            verb = aspect | voice | form

            voc = Keyword( "+voc" ).suppress()

            deriv = TagKeywords( ["ACT", "PCPL", "PASS", "VN"] )

            state = TagKeywords( ["DEF", "INDEF"] )
            case = TagKeywords( ["NOM", "ACC", "GEN"] )
            nom = case | state


            tag = lem | root | sp | mood | gen | verb | deriv | nom | voc | skip
            part = Group( center + pos + ZeroOrMore( ~center + ~last + ~end + tag ) )

            base = Group( OneOrMore( ~end + ~last + part ) )

            pron = "PRON:" + Group( gen )
            suffixes = Group( ZeroOrMore( ~end + last + pron ) )

            whole = begin + prefixes + base + suffixes + end

            parsed = whole.parseString( string )


            return parsed
Beispiel #15
0
def _create_grammar_6_0():
    """Create the SYM 6.0 grammar.

    """

    word = Word(printables.replace(';', '').replace(':', ''))
    positive_integer = Word(nums)
    number = Word(nums + '.Ee-+')
    lp = Suppress(Literal('('))
    rp = Suppress(Literal(')'))
    lb = Suppress(Literal('['))
    rb = Suppress(Literal(']'))
    name = Word(alphas + nums + '_-').setWhitespaceChars(' ')
    assign = Suppress(Literal('='))
    comma = Suppress(Literal(','))
    type_ = name

    version = Group(Keyword('FormatVersion') - assign - Keyword('6.0'))

    title = Group(Keyword('Title') - assign - QuotedString('"'))

    enum_value = Group(number + assign + QuotedString('"'))

    enum = Group(
        Suppress(Keyword('Enum')) - assign - name - Suppress(lp) +
        Group(delimitedList(enum_value)) - Suppress(rp))

    sig_unit = Group(Literal('/u:') + word)
    sig_factor = Group(Literal('/f:') + word)
    sig_offset = Group(Literal('/o:') + word)
    sig_min = Group(Literal('/min:') + word)
    sig_max = Group(Literal('/max:') + word)
    sig_default = Group(Literal('/d:') + word)
    sig_long_name = Group(Literal('/ln:') + word)
    sig_enum = Group(Literal('/e:') + word)

    signal = Group(
        Suppress(Keyword('Sig')) - Suppress(assign) - name - type_ +
        Group(Optional(positive_integer)) + Group(Optional(Keyword('-m'))) +
        Group(
            Optional(sig_unit) + Optional(sig_factor) + Optional(sig_offset) +
            Optional(sig_min) + Optional(sig_max) + Optional(sig_default) +
            Optional(sig_long_name) + Optional(sig_enum)))

    symbol = Group(
        Suppress(lb) - name - Suppress(rb) -
        Group(Optional(Keyword('ID') + assign + word)) -
        Group(Keyword('Len') + assign + positive_integer) + Group(
            Optional(
                Keyword('Mux') + assign + word + positive_integer + comma +
                positive_integer + positive_integer)) +
        Group(Optional(Keyword('CycleTime') + assign + positive_integer)) +
        Group(Optional(Keyword('Timeout') + assign + positive_integer)) +
        Group(Optional(Keyword('MinInterval') + assign + positive_integer)) +
        Group(
            ZeroOrMore(Group(
                Keyword('Sig') + assign + name + positive_integer))))

    enums = Group(Keyword('{ENUMS}') + Group(ZeroOrMore(enum)))
    signals = Group(Keyword('{SIGNALS}') + Group(ZeroOrMore(signal)))
    send = Group(Keyword('{SEND}') + Group(ZeroOrMore(symbol)))
    receive = Group(Keyword('{RECEIVE}') + Group(ZeroOrMore(symbol)))
    sendreceive = Group(Keyword('{SENDRECEIVE}') + Group(ZeroOrMore(symbol)))

    section = (enums | signals | send | receive | sendreceive)

    grammar = (version - title + Group(OneOrMore(section)) + StringEnd())
    grammar.ignore(dblSlashComment)

    return grammar
Beispiel #16
0
def pyparsing_parse(text):
    """
    >>> import os
    >>> dirname = os.path.join(os.path.dirname(__file__), "data")
    >>> filename = os.path.join(dirname, "error1.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     pyparsing_parse(file.read())
    Traceback (most recent call last):
    ...
    ValueError: Error {0}: syntax error, line 8
    >>> filename = os.path.join(dirname, "error2.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     pyparsing_parse(file.read())
    Traceback (most recent call last):
    ...
    ValueError: Error {0}: syntax error, line 1
    >>> filename = os.path.join(dirname, "error3.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     pyparsing_parse(file.read())
    Traceback (most recent call last):
    ...
    ValueError: Error {0}: syntax error, line 4
    >>> expected = "[white: ]\\n[lightblue: Director]\\n/\\n/\\n[white: ]\\n[lightgreen: Secretary]\\n/\\n/\\n[white: Minion #1]\\n[white: ]\\n[white: Minion #2]"
    >>> filename = os.path.join(dirname, "hierarchy.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     blocks = pyparsing_parse(file.read())
    >>> str(blocks).strip() == expected
    True

    >>> expected = "[#00CCDE: MessageBox Window\\n[lightgray: Frame\\n[white: ]\\n[white: Message text]\\n/\\n/\\n[goldenrod: OK Button]\\n[white: ]\\n[#ff0505: Cancel Button]\\n/\\n[white: ]\\n]\\n]"
    >>> filename = os.path.join(dirname, "messagebox.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     blocks = pyparsing_parse(file.read())
    >>> str(blocks).strip() == expected
    True
    """
    def add_block(tokens):
        return Block.Block(tokens.name,
                           tokens.color if tokens.color else "white")

    left_bracket, right_bracket = map(Suppress, "[]")
    new_rows = Word("/")("new_rows").setParseAction(
        lambda tokens: len(tokens.new_rows))
    name = CharsNotIn("[]/\n")("name").setParseAction(
        lambda tokens: tokens.name.strip())
    color = (Word("#", hexnums, exact=7) | Word(alphas, alphanums))("color")
    empty_node = (left_bracket +
                  right_bracket).setParseAction(lambda: EmptyBlock)
    nodes = Forward()
    node_data = Optional(color + Suppress(":")) + Optional(name)
    node_data.setParseAction(add_block)
    node = left_bracket - node_data + nodes + right_bracket
    nodes << Group(
        ZeroOrMore(Optional(new_rows) + OneOrMore(node | empty_node)))
    stack = [Block.get_root_block()]
    try:
        results = nodes.parseString(text, parseAll=True)
        assert len(results) == 1
        items = results.asList()[0]
        populate_children(items, stack)
    except (ParseException, ParseSyntaxException) as err:
        raise ValueError("Error {{0}}: syntax error, line "
                         "{0}".format(err.lineno))
    return stack[0]
from pyparsing import (LineStart, Literal, OneOrMore, Optional, Regex, SkipTo,
                       srange, Suppress, Word, ZeroOrMore)

from regparser.grammar import atomic, unified
from regparser.grammar.utils import DocLiteral, keep_pos, Marker

smart_quotes = (Suppress(DocLiteral(u'“', "left-smart-quote")) +
                SkipTo(DocLiteral(u'”', "right-smart-quote")).setParseAction(
                    keep_pos).setResultsName("term"))

e_tag = (Suppress(Regex(r"<E[^>]*>")) + OneOrMore(Word(
    srange("[a-zA-Z-]"))).setParseAction(keep_pos).setResultsName("term") +
         Suppress(Literal("</E>")))

xml_term_parser = (
    LineStart() + Optional(Suppress(unified.any_depth_p)) +
    Suppress(ZeroOrMore(Word(srange("[a-zA-Z]") + '.,'))) +
    e_tag.setResultsName("head") + ZeroOrMore(
        (atomic.conj_phrases + e_tag).setResultsName("tail",
                                                     listAllMatches=True)) +
    Suppress(ZeroOrMore(Regex(r",[a-zA-Z ]+,"))) +
    Suppress(ZeroOrMore((Marker("this") | Marker("the")) + Marker("term"))) +
    ((Marker("mean") | Marker("means"))
     | (Marker("refers") + ZeroOrMore(Marker("only")) + Marker("to"))
     | ((Marker("has") | Marker("have")) + Marker("the") + Marker("same") +
        Marker("meaning") + Marker("as"))))

scope_term_type_parser = (
    Marker("purposes") + Marker("of") + Optional(Marker("this")) +
    SkipTo(",").setResultsName("scope") + Literal(",") +
    Optional(Marker("the") + Marker("term")) +
Beispiel #18
0
COMMA = L(",").suppress()
SEMICOLON = L(";").suppress()
AT = L("@").suppress()

PUNCTUATION = Word("-_.")
IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM)
IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END))

NAME = IDENTIFIER("name")
EXTRA = IDENTIFIER

URI = Regex(r"[^ ]+")("url")
URL = AT + URI

EXTRAS_LIST = EXTRA + ZeroOrMore(COMMA + EXTRA)
EXTRAS = (LBRACKET + Optional(EXTRAS_LIST) + RBRACKET)("extras")

VERSION_PEP440 = Regex(Specifier._regex_str, re.VERBOSE | re.IGNORECASE)
VERSION_LEGACY = Regex(LegacySpecifier._regex_str, re.VERBOSE | re.IGNORECASE)

VERSION_ONE = VERSION_PEP440 ^ VERSION_LEGACY
VERSION_MANY = Combine(VERSION_ONE + ZeroOrMore(COMMA + VERSION_ONE),
                       joinString=",",
                       adjacent=False)("_raw_spec")
_VERSION_SPEC = Optional((LPAREN + VERSION_MANY + RPAREN) | VERSION_MANY)
_VERSION_SPEC.setParseAction(lambda s, l, t: t._raw_spec or "")

VERSION_SPEC = originalTextFor(_VERSION_SPEC)("specifier")
VERSION_SPEC.setParseAction(lambda s, l, t: t[1])

MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker")
Beispiel #19
0
    def parse(cls,
              content,
              basedir=None,
              resolve=True,
              unresolved_value=DEFAULT_SUBSTITUTION):
        """parse a HOCON content

        :param content: HOCON content to parse
        :type content: basestring
        :param resolve: if true, resolve substitutions
        :type resolve: boolean
        :param unresolved_value: assigned value value to unresolved substitution.
        If overriden with a default value, it will replace all unresolved value to the default value.
        If it is set to to pyhocon.STR_SUBSTITUTION then it will replace the value by its substitution expression (e.g., ${x})
        :type unresolved_value: boolean
        :return: a ConfigTree or a list
        """

        unescape_pattern = re.compile(r'\\.')

        def replace_escape_sequence(match):
            value = match.group(0)
            return cls.REPLACEMENTS.get(value, value)

        def norm_string(value):
            return unescape_pattern.sub(replace_escape_sequence, value)

        def unescape_string(tokens):
            return ConfigUnquotedString(norm_string(tokens[0]))

        def parse_multi_string(tokens):
            # remove the first and last 3 "
            return tokens[0][3:-3]

        def convert_number(tokens):
            n = tokens[0]
            try:
                return int(n, 10)
            except ValueError:
                return float(n)

        # ${path} or ${?path} for optional substitution
        SUBSTITUTION_PATTERN = "\$\{(?P<optional>\?)?(?P<variable>[^}]+)\}(?P<ws>[ \t]*)"

        def create_substitution(instring, loc, token):
            # remove the ${ and }
            match = re.match(SUBSTITUTION_PATTERN, token[0])
            variable = match.group('variable')
            ws = match.group('ws')
            optional = match.group('optional') == '?'
            substitution = ConfigSubstitution(variable, optional, ws, instring,
                                              loc)
            return substitution

        # ${path} or ${?path} for optional substitution
        STRING_PATTERN = '"(?P<value>(?:[^"\\\\]|\\\\.)*)"(?P<ws>[ \t]*)'

        def create_quoted_string(instring, loc, token):
            # remove the ${ and }
            match = re.match(STRING_PATTERN, token[0])
            value = norm_string(match.group('value'))
            ws = match.group('ws')
            return ConfigQuotedString(value, ws, instring, loc)

        def include_config(instring, loc, token):
            url = None
            file = None
            required = False

            if token[0] == 'required':
                required = True
                final_tokens = token[1:]
            else:
                final_tokens = token

            if len(final_tokens) == 1:  # include "test"
                value = final_tokens[0].value if isinstance(
                    final_tokens[0], ConfigQuotedString) else final_tokens[0]
                if value.startswith("http://") or value.startswith(
                        "https://") or value.startswith("file://"):
                    url = value
                else:
                    file = value
            elif len(final_tokens) == 2:  # include url("test") or file("test")
                value = final_tokens[1].value if isinstance(
                    token[1], ConfigQuotedString) else final_tokens[1]
                if final_tokens[0] == 'url':
                    url = value
                else:
                    file = value

            if url is not None:
                logger.debug('Loading config from url %s', url)
                obj = ConfigFactory.parse_URL(url,
                                              resolve=False,
                                              required=required,
                                              unresolved_value=NO_SUBSTITUTION)
            elif file is not None:
                path = file if basedir is None else os.path.join(basedir, file)
                logger.debug('Loading config from file %s', path)
                obj = ConfigFactory.parse_file(
                    path,
                    resolve=False,
                    required=required,
                    unresolved_value=NO_SUBSTITUTION)
            else:
                raise ConfigException(
                    'No file or URL specified at: {loc}: {instring}',
                    loc=loc,
                    instring=instring)

            return ConfigInclude(obj if isinstance(obj, list) else obj.items())

        ParserElement.setDefaultWhitespaceChars(' \t')

        assign_expr = Forward()
        true_expr = Keyword("true",
                            caseless=True).setParseAction(replaceWith(True))
        false_expr = Keyword("false",
                             caseless=True).setParseAction(replaceWith(False))
        null_expr = Keyword("null", caseless=True).setParseAction(
            replaceWith(NoneValue()))
        key = QuotedString(
            '"', escChar='\\',
            unquoteResults=False) | Word(alphanums + alphas8bit + '._- /')

        eol = Word('\n\r').suppress()
        eol_comma = Word('\n\r,').suppress()
        comment = (Literal('#') | Literal('//')) - SkipTo(eol | StringEnd())
        comment_eol = Suppress(Optional(eol_comma) + comment)
        comment_no_comma_eol = (comment | eol).suppress()
        number_expr = Regex(
            '[+-]?(\d*\.\d+|\d+(\.\d+)?)([eE][+\-]?\d+)?(?=$|[ \t]*([\$\}\],#\n\r]|//))',
            re.DOTALL).setParseAction(convert_number)

        # multi line string using """
        # Using fix described in http://pyparsing.wikispaces.com/share/view/3778969
        multiline_string = Regex(
            '""".*?"*"""',
            re.DOTALL | re.UNICODE).setParseAction(parse_multi_string)
        # single quoted line string
        quoted_string = Regex('"(?:[^"\\\\\n]|\\\\.)*"[ \t]*',
                              re.UNICODE).setParseAction(create_quoted_string)
        # unquoted string that takes the rest of the line until an optional comment
        # we support .properties multiline support which is like this:
        # line1  \
        # line2 \
        # so a backslash precedes the \n
        unquoted_string = Regex(
            '(?:[^^`+?!@*&"\[\{\s\]\}#,=\$\\\\]|\\\\.)+[ \t]*',
            re.UNICODE).setParseAction(unescape_string)
        substitution_expr = Regex('[ \t]*\$\{[^\}]+\}[ \t]*').setParseAction(
            create_substitution)
        string_expr = multiline_string | quoted_string | unquoted_string

        value_expr = number_expr | true_expr | false_expr | null_expr | string_expr

        include_content = (quoted_string | (
            (Keyword('url') | Keyword('file')) - Literal('(').suppress() -
            quoted_string - Literal(')').suppress()))
        include_expr = (
            Keyword("include", caseless=True).suppress() +
            (include_content |
             (Keyword("required") - Literal('(').suppress() - include_content -
              Literal(')').suppress()))).setParseAction(include_config)

        root_dict_expr = Forward()
        dict_expr = Forward()
        list_expr = Forward()
        multi_value_expr = ZeroOrMore(comment_eol | include_expr
                                      | substitution_expr | dict_expr
                                      | list_expr | value_expr
                                      | (Literal('\\') - eol).suppress())
        # for a dictionary : or = is optional
        # last zeroOrMore is because we can have t = {a:4} {b: 6} {c: 7} which is dictionary concatenation
        inside_dict_expr = ConfigTreeParser(
            ZeroOrMore(comment_eol | include_expr | assign_expr | eol_comma))
        inside_root_dict_expr = ConfigTreeParser(
            ZeroOrMore(comment_eol | include_expr | assign_expr | eol_comma),
            root=True)
        dict_expr << Suppress('{') - inside_dict_expr - Suppress('}')
        root_dict_expr << Suppress('{') - inside_root_dict_expr - Suppress('}')
        list_entry = ConcatenatedValueParser(multi_value_expr)
        list_expr << Suppress('[') - ListParser(
            list_entry - ZeroOrMore(eol_comma - list_entry)) - Suppress(']')

        # special case when we have a value assignment where the string can potentially be the remainder of the line
        assign_expr << Group(key - ZeroOrMore(comment_no_comma_eol) -
                             (dict_expr |
                              (Literal('=') | Literal(':') | Literal('+=')) -
                              ZeroOrMore(comment_no_comma_eol) -
                              ConcatenatedValueParser(multi_value_expr)))

        # the file can be { ... } where {} can be omitted or []
        config_expr = ZeroOrMore(comment_eol | eol) + (
            list_expr | root_dict_expr
            | inside_root_dict_expr) + ZeroOrMore(comment_eol | eol_comma)
        config = config_expr.parseString(content, parseAll=True)[0]

        if resolve:
            allow_unresolved = resolve and unresolved_value is not DEFAULT_SUBSTITUTION and unresolved_value is not MANDATORY_SUBSTITUTION
            has_unresolved = cls.resolve_substitutions(config,
                                                       allow_unresolved)
            if has_unresolved and unresolved_value is MANDATORY_SUBSTITUTION:
                raise ConfigSubstitutionException(
                    'resolve cannot be set to True and unresolved_value to MANDATORY_SUBSTITUTION'
                )

        if unresolved_value is not NO_SUBSTITUTION and unresolved_value is not DEFAULT_SUBSTITUTION:
            cls.unresolve_substitutions_to_value(config, unresolved_value)
        return config
Beispiel #20
0
                       | (columnName + IN + Group("(" + selectStmt + ")"))
                       | (columnName + IS + (NULL | NOT_NULL)))

whereExpression = infixNotation(
    whereCondition,
    [
        (NOT, 1, opAssoc.RIGHT),
        (AND, 2, opAssoc.LEFT),
        (OR, 2, opAssoc.LEFT),
    ],
)

# define the grammar
selectStmt <<= (SELECT + ("*" | columnNameList)("columns") + FROM +
                tableNameList("tables") +
                Optional(Group(WHERE + whereExpression), "")("where"))

simpleSQL = selectStmt

# define Oracle comment format, and ignore them
oracleSqlComment = "--" + restOfLine
simpleSQL.ignore(oracleSqlComment)

if __name__ == "__main__":
    simpleSQL.runTests("""\

        # multiple tables
        SELECT * from XYZZY, ABC

        # dotted table name
        select * from SYS.XYZZY
Beispiel #21
0
                context.set(name[0], _var, var_type=name[1])
                context.set(name[0], _var, var_type=name[1], force_global=True)


# see MS-VBAL 4.2 Modules
#
# MS-GRAMMAR: procedural_module_header = CaselessKeyword('Attribute') + CaselessKeyword('VB_Name') + Literal('=') + quoted_string
# MS-GRAMMAR: procedural_module = procedural_module_header + procedural_module_body
# MS-GRAMMAR: class_module = class_module_header + class_module_body
# MS-GRAMMAR: module = procedural_module | class_module

# Module Header:

header_statement = attribute_statement
# TODO: can we have '::' with an empty statement?
header_statements_line = (Optional(header_statement + ZeroOrMore(Suppress(':') + header_statement)) + EOL.suppress()) | \
                         option_statement | \
                         type_declaration | \
                         simple_if_statement_macro
module_header = ZeroOrMore(header_statements_line)

# 5.1 Module Body Structure

# 5.2 Module Declaration Section Structure

# TODO: 5.2.1 Option Directives
# TODO: 5.2.2 Implicit Definition Directives
# TODO: 5.2.3 Module Declarations

loose_lines = Forward()
#declaration_statement = external_function | global_variable_declaration | loose_lines | option_statement | dim_statement | rem_statement
Beispiel #22
0
flag = oneOf(list(VTT_MNEMONIC_FLAGS.keys()))
# convert flag to binary string
flag.setParseAction(tokenMap(lambda t: VTT_MNEMONIC_FLAGS[t]))
flags = Combine(OneOrMore(flag)).setResultsName("flags")

delta_point_index = pyparsing_common.integer.setResultsName("point_index")
delta_rel_ppem = pyparsing_common.integer.setResultsName("rel_ppem")
delta_step_no = signed_integer.setResultsName("step_no")
# the step denominator is only used in VTT's DELTA[CP]* instructions,
# and must always be 8 (sic!), so we can suppress it.
delta_spec = (
    delta_point_index
    + Suppress("@")
    + delta_rel_ppem
    + delta_step_no
    + Optional(Literal("/8")).suppress()
)

delta = nestedExpr("(", ")", delta_spec, ignoreExpr=None)

deltas = Group(OneOrMore(delta)).setResultsName("deltas")

args = deltas | flags

stack_items = OneOrMore(stack_item).setResultsName("stack_items")

instruction = Group(
    mnemonic + Suppress("[") + Optional(args) + Suppress("]") + Optional(stack_items)
)

label = Word("#", alphanums)
Beispiel #23
0

class Delayed:
    pass


effective_date = (utils.Marker("effective") +
                  utils.Marker("date")).setParseAction(lambda: EffectiveDate())

notice_citation = (
    Word(string.digits) + utils.Marker('FR') +
    Word(string.digits)).setParseAction(lambda m: Notice(int(m[0]), int(m[1])))

delayed = utils.Marker("delayed").setParseAction(lambda: Delayed())


def int2Month(m):
    month = date(2000, m, 1)
    month = month.strftime('%B')
    token = utils.Marker(month)
    return token.setParseAction(lambda: m)


months = reduce(lambda l, r: l | r, map(int2Month, range(2, 13)))

date_parser = (months + Word(string.digits) + Suppress(Optional(",")) + Word(
    string.digits)).setParseAction(lambda m: date(int(m[2]), m[0], int(m[1])))

tokenizer = utils.QuickSearchable(effective_date | notice_citation | delayed
                                  | date_parser)
Beispiel #24
0
def cb_deref(tokens):
    if len(tokens) != 4:
        raise NotImplementedError("TODO")
    return AstMem(tokens[2] + tokens[0], 32)


def cb_deref_nooff(tokens):
    if len(tokens) != 3:
        raise NotImplementedError("TODO")
    return AstMem(tokens[1], 32)


base_expr = cpu.base_expr

deref_off = (Optional(base_expr) + LPARENTHESIS + gpregs.parser +
             RPARENTHESIS).setParseAction(cb_deref)
deref_nooff = (LPARENTHESIS + gpregs.parser +
               RPARENTHESIS).setParseAction(cb_deref_nooff)
deref = deref_off | deref_nooff


class additional_info(object):
    def __init__(self):
        self.except_on_instr = False


br_0 = ['B', 'J', 'JR', 'BAL', 'JAL', 'JALR']
br_1 = ['BGEZ', 'BLTZ', 'BGTZ', 'BLEZ', 'BC1T', 'BC1F']
br_2 = ['BEQ', 'BEQL', 'BNE']
Beispiel #25
0
def Verilog_BNF():
    global verilogbnf

    if verilogbnf is None:

        # compiler directives
        compilerDirective = Combine( "`" + \
            oneOf("define undef ifdef else endif default_nettype "
                  "include resetall timescale unconnected_drive "
                  "nounconnected_drive celldefine endcelldefine") + \
            restOfLine ).setName("compilerDirective")

        # primitives
        SEMI, COLON, LPAR, RPAR, LBRACE, RBRACE, LBRACK, RBRACK, DOT, COMMA, EQ = map(
            Literal, ";:(){}[].,=")

        identLead = alphas + "$_"
        identBody = alphanums + "$_"
        identifier1 = Regex(r"\.?[" + identLead + "][" + identBody + "]*(\.[" +
                            identLead + "][" + identBody +
                            "]*)*").setName("baseIdent")
        identifier2 = Regex(r"\\\S+").setParseAction(
            lambda t: t[0][1:]).setName("escapedIdent")  #.setDebug()
        identifier = identifier1 | identifier2
        assert (identifier2 == r'\abc')

        hexnums = nums + "abcdefABCDEF" + "_?"
        base = Regex("'[bBoOdDhH]").setName("base")
        basedNumber = Combine(Optional(Word(nums + "_")) + base +
                              Word(hexnums + "xXzZ"),
                              joinString=" ",
                              adjacent=False).setName("basedNumber")
        #~ number = ( basedNumber | Combine( Word( "+-"+spacedNums, spacedNums ) +
        #~ Optional( DOT + Optional( Word( spacedNums ) ) ) +
        #~ Optional( e + Word( "+-"+spacedNums, spacedNums ) ) ).setName("numeric") )
        number = ( basedNumber | \
                   Regex(r"[+-]?[0-9_]+(\.[0-9_]*)?([Ee][+-]?[0-9_]+)?") \
                  ).setName("numeric")
        #~ decnums = nums + "_"
        #~ octnums = "01234567" + "_"
        expr = Forward().setName("expr")
        concat = Group(LBRACE + delimitedList(expr) + RBRACE)
        multiConcat = Group("{" + expr + concat + "}").setName("multiConcat")
        funcCall = Group(identifier + LPAR + Optional(delimitedList(expr)) +
                         RPAR).setName("funcCall")

        subscrRef = Group(LBRACK + delimitedList(expr, COLON) + RBRACK)
        subscrIdentifier = Group(identifier + Optional(subscrRef))
        #~ scalarConst = "0" | (( FollowedBy('1') + oneOf("1'b0 1'b1 1'bx 1'bX 1'B0 1'B1 1'Bx 1'BX 1") ))
        scalarConst = Regex("0|1('[Bb][01xX])?")
        mintypmaxExpr = Group(expr + COLON + expr + COLON +
                              expr).setName("mintypmax")
        primary = (number | (LPAR + mintypmaxExpr + RPAR) |
                   (LPAR + Group(expr) + RPAR).setName("nestedExpr")
                   | multiConcat | concat | dblQuotedString | funcCall
                   | subscrIdentifier)

        unop = oneOf("+  -  !  ~  &  ~&  |  ^|  ^  ~^").setName("unop")
        binop = oneOf(
            "+  -  *  /  %  ==  !=  ===  !==  &&  "
            "||  <  <=  >  >=  &  |  ^  ^~  >>  << ** <<< >>>").setName(
                "binop")

        expr << ((unop + expr) |  # must be first!
                 (primary + "?" + expr + COLON + expr) |
                 (primary + Optional(binop + expr)))

        lvalue = subscrIdentifier | concat

        # keywords
        if_ = Keyword("if")
        else_ = Keyword("else")
        edge = Keyword("edge")
        posedge = Keyword("posedge")
        negedge = Keyword("negedge")
        specify = Keyword("specify")
        endspecify = Keyword("endspecify")
        fork = Keyword("fork")
        join = Keyword("join")
        begin = Keyword("begin")
        end = Keyword("end")
        default = Keyword("default")
        forever = Keyword("forever")
        repeat = Keyword("repeat")
        while_ = Keyword("while")
        for_ = Keyword("for")
        case = oneOf("case casez casex")
        endcase = Keyword("endcase")
        wait = Keyword("wait")
        disable = Keyword("disable")
        deassign = Keyword("deassign")
        force = Keyword("force")
        release = Keyword("release")
        assign = Keyword("assign")

        eventExpr = Forward()
        eventTerm = (posedge + expr) | (negedge + expr) | expr | (
            LPAR + eventExpr + RPAR)
        eventExpr << (Group(delimitedList(eventTerm, Keyword("or"))))
        eventControl = Group("@" + (
            (LPAR + eventExpr + RPAR) | identifier | "*")).setName("eventCtrl")

        delayArg = (
            number | Word(alphanums + "$_") |  #identifier |
            (LPAR + Group(delimitedList(mintypmaxExpr | expr)) +
             RPAR)).setName("delayArg")  #.setDebug()
        delay = Group("#" + delayArg).setName("delay")  #.setDebug()
        delayOrEventControl = delay | eventControl

        assgnmt = Group(lvalue + EQ + Optional(delayOrEventControl) +
                        expr).setName("assgnmt")
        nbAssgnmt = Group((lvalue + "<=" + Optional(delay) + expr)
                          | (lvalue + "<=" + Optional(eventControl) +
                             expr)).setName("nbassgnmt")

        range = LBRACK + expr + COLON + expr + RBRACK

        paramAssgnmt = Group(identifier + EQ + expr).setName("paramAssgnmt")
        parameterDecl = Group("parameter" + Optional(range) +
                              delimitedList(paramAssgnmt) +
                              SEMI).setName("paramDecl")

        inputDecl = Group("input" + Optional(range) +
                          delimitedList(identifier) + SEMI)
        outputDecl = Group("output" + Optional(range) +
                           delimitedList(identifier) + SEMI)
        inoutDecl = Group("inout" + Optional(range) +
                          delimitedList(identifier) + SEMI)

        regIdentifier = Group(identifier +
                              Optional(LBRACK + expr + COLON + expr + RBRACK))
        regDecl = Group("reg" + Optional("signed") + Optional(range) +
                        delimitedList(regIdentifier) + SEMI).setName("regDecl")
        timeDecl = Group("time" + delimitedList(regIdentifier) + SEMI)
        integerDecl = Group("integer" + delimitedList(regIdentifier) + SEMI)

        strength0 = oneOf("supply0  strong0  pull0  weak0  highz0")
        strength1 = oneOf("supply1  strong1  pull1  weak1  highz1")
        driveStrength = Group(LPAR + ((strength0 + COMMA + strength1)
                                      | (strength1 + COMMA + strength0)) +
                              RPAR).setName("driveStrength")
        nettype = oneOf(
            "wire  tri  tri1  supply0  wand  triand  tri0  supply1  wor  trior  trireg"
        )
        expandRange = Optional(oneOf("scalared vectored")) + range
        realDecl = Group("real" + delimitedList(identifier) + SEMI)

        eventDecl = Group("event" + delimitedList(identifier) + SEMI)

        blockDecl = (parameterDecl | regDecl | integerDecl | realDecl
                     | timeDecl | eventDecl)

        stmt = Forward().setName("stmt")  #.setDebug()
        stmtOrNull = stmt | SEMI
        caseItem = ( delimitedList( expr ) + COLON + stmtOrNull ) | \
                   ( default + Optional(":") + stmtOrNull )
        stmt << Group(
            (begin + Group(ZeroOrMore(stmt)) + end).setName("begin-end")
            | (if_ + Group(LPAR + expr + RPAR) + stmtOrNull +
               Optional(else_ + stmtOrNull)).setName("if")
            | (delayOrEventControl + stmtOrNull)
            | (case + LPAR + expr + RPAR + OneOrMore(caseItem) + endcase)
            | (forever + stmt) | (repeat + LPAR + expr + RPAR + stmt)
            | (while_ + LPAR + expr + RPAR + stmt)
            | (for_ + LPAR + assgnmt + SEMI + Group(expr) + SEMI + assgnmt +
               RPAR + stmt) | (fork + ZeroOrMore(stmt) + join) |
            (fork + COLON + identifier + ZeroOrMore(blockDecl) +
             ZeroOrMore(stmt) + end) | (wait + LPAR + expr + RPAR + stmtOrNull)
            | ("->" + identifier + SEMI) | (disable + identifier + SEMI)
            | (assign + assgnmt + SEMI) | (deassign + lvalue + SEMI)
            | (force + assgnmt + SEMI) | (release + lvalue + SEMI)
            | (begin + COLON + identifier + ZeroOrMore(blockDecl) +
               ZeroOrMore(stmt) + end).setName("begin:label-end") |
            # these  *have* to go at the end of the list!!!
            (assgnmt + SEMI) | (nbAssgnmt + SEMI)
            | (Combine(Optional("$") + identifier) +
               Optional(LPAR + delimitedList(expr | empty) + RPAR) +
               SEMI)).setName("stmtBody")
        """
        x::=<blocking_assignment> ;
        x||= <non_blocking_assignment> ;
        x||= if ( <expression> ) <statement_or_null>
        x||= if ( <expression> ) <statement_or_null> else <statement_or_null>
        x||= case ( <expression> ) <case_item>+ endcase
        x||= casez ( <expression> ) <case_item>+ endcase
        x||= casex ( <expression> ) <case_item>+ endcase
        x||= forever <statement>
        x||= repeat ( <expression> ) <statement>
        x||= while ( <expression> ) <statement>
        x||= for ( <assignment> ; <expression> ; <assignment> ) <statement>
        x||= <delay_or_event_control> <statement_or_null>
        x||= wait ( <expression> ) <statement_or_null>
        x||= -> <name_of_event> ;
        x||= <seq_block>
        x||= <par_block>
        x||= <task_enable>
        x||= <system_task_enable>
        x||= disable <name_of_task> ;
        x||= disable <name_of_block> ;
        x||= assign <assignment> ;
        x||= deassign <lvalue> ;
        x||= force <assignment> ;
        x||= release <lvalue> ;
        """
        alwaysStmt = Group("always" + Optional(eventControl) +
                           stmt).setName("alwaysStmt")
        initialStmt = Group("initial" + stmt).setName("initialStmt")

        chargeStrength = Group(LPAR + oneOf("small medium large") +
                               RPAR).setName("chargeStrength")

        continuousAssign = Group(assign + Optional(driveStrength) +
                                 Optional(delay) + delimitedList(assgnmt) +
                                 SEMI).setName("continuousAssign")

        tfDecl = (parameterDecl | inputDecl | outputDecl | inoutDecl | regDecl
                  | timeDecl | integerDecl | realDecl)

        functionDecl = Group("function" +
                             Optional(range | "integer" | "real") +
                             identifier + SEMI + Group(OneOrMore(tfDecl)) +
                             Group(ZeroOrMore(stmt)) + "endfunction")

        inputOutput = oneOf("input output")
        netDecl1Arg = (nettype + Optional(expandRange) + Optional(delay) +
                       Group(delimitedList(~inputOutput + identifier)))
        netDecl2Arg = ("trireg" + Optional(chargeStrength) +
                       Optional(expandRange) + Optional(delay) +
                       Group(delimitedList(~inputOutput + identifier)))
        netDecl3Arg = (nettype + Optional(driveStrength) +
                       Optional(expandRange) + Optional(delay) +
                       Group(delimitedList(assgnmt)))
        netDecl1 = Group(netDecl1Arg + SEMI).setName("netDecl1")
        netDecl2 = Group(netDecl2Arg + SEMI).setName("netDecl2")
        netDecl3 = Group(netDecl3Arg + SEMI).setName("netDecl3")

        gateType = oneOf("and  nand  or  nor xor  xnor buf  bufif0 bufif1 "
                         "not  notif0 notif1  pulldown pullup nmos  rnmos "
                         "pmos rpmos cmos rcmos   tran rtran  tranif0  "
                         "rtranif0  tranif1 rtranif1")
        gateInstance = Optional( Group( identifier + Optional( range ) ) ) + \
                        LPAR + Group( delimitedList( expr ) ) + RPAR
        gateDecl = Group(gateType + Optional(driveStrength) + Optional(delay) +
                         delimitedList(gateInstance) + SEMI)

        udpInstance = Group(
            Group(identifier + Optional(range | subscrRef)) + LPAR +
            Group(delimitedList(expr)) + RPAR)
        udpInstantiation = Group(identifier - Optional(driveStrength) +
                                 Optional(delay) + delimitedList(udpInstance) +
                                 SEMI).setName("udpInstantiation")

        parameterValueAssignment = Group(
            Literal("#") + LPAR + Group(delimitedList(expr)) + RPAR)
        namedPortConnection = Group(DOT + identifier + LPAR +
                                    expr + RPAR).setName(
                                        "namedPortConnection")  #.setDebug()
        assert (r'.\abc (abc )' == namedPortConnection)
        modulePortConnection = expr | empty
        #~ moduleInstance = Group( Group ( identifier + Optional(range) ) +
        #~ ( delimitedList( modulePortConnection ) |
        #~ delimitedList( namedPortConnection ) ) )
        inst_args = Group(LPAR + (delimitedList(namedPortConnection)
                                  | delimitedList(modulePortConnection)) +
                          RPAR).setName("inst_args")
        moduleInstance = Group(
            Group(identifier + Optional(range)) + inst_args).setName(
                "moduleInstance")  #.setDebug()

        moduleInstantiation = Group(
            identifier + Optional(parameterValueAssignment) +
            delimitedList(moduleInstance).setName("moduleInstanceList") +
            SEMI).setName("moduleInstantiation")

        parameterOverride = Group("defparam" + delimitedList(paramAssgnmt) +
                                  SEMI)
        task = Group("task" + identifier + SEMI + ZeroOrMore(tfDecl) +
                     stmtOrNull + "endtask")

        specparamDecl = Group("specparam" + delimitedList(paramAssgnmt) + SEMI)

        pathDescr1 = Group(LPAR + subscrIdentifier + "=>" + subscrIdentifier +
                           RPAR)
        pathDescr2 = Group(LPAR + Group(delimitedList(subscrIdentifier)) +
                           "*>" + Group(delimitedList(subscrIdentifier)) +
                           RPAR)
        pathDescr3 = Group(LPAR + Group(delimitedList(subscrIdentifier)) +
                           "=>" + Group(delimitedList(subscrIdentifier)) +
                           RPAR)
        pathDelayValue = Group((
            LPAR + Group(delimitedList(mintypmaxExpr | expr)) + RPAR)
                               | mintypmaxExpr | expr)
        pathDecl = Group((pathDescr1 | pathDescr2 | pathDescr3) + EQ +
                         pathDelayValue + SEMI).setName("pathDecl")

        portConditionExpr = Forward()
        portConditionTerm = Optional(unop) + subscrIdentifier
        portConditionExpr << portConditionTerm + Optional(binop +
                                                          portConditionExpr)
        polarityOp = oneOf("+ -")
        levelSensitivePathDecl1 = Group(if_ + Group(LPAR + portConditionExpr +
                                                    RPAR) + subscrIdentifier +
                                        Optional(polarityOp) + "=>" +
                                        subscrIdentifier + EQ +
                                        pathDelayValue + SEMI)
        levelSensitivePathDecl2 = Group(
            if_ + Group(LPAR + portConditionExpr + RPAR) + LPAR +
            Group(delimitedList(subscrIdentifier)) + Optional(polarityOp) +
            "*>" + Group(delimitedList(subscrIdentifier)) + RPAR + EQ +
            pathDelayValue + SEMI)
        levelSensitivePathDecl = levelSensitivePathDecl1 | levelSensitivePathDecl2

        edgeIdentifier = posedge | negedge
        edgeSensitivePathDecl1 = Group(
            Optional(if_ + Group(LPAR + expr + RPAR)) + LPAR +
            Optional(edgeIdentifier) + subscrIdentifier + "=>" + LPAR +
            subscrIdentifier + Optional(polarityOp) + COLON + expr + RPAR +
            RPAR + EQ + pathDelayValue + SEMI)
        edgeSensitivePathDecl2 = Group(
            Optional(if_ + Group(LPAR + expr + RPAR)) + LPAR +
            Optional(edgeIdentifier) + subscrIdentifier + "*>" + LPAR +
            delimitedList(subscrIdentifier) + Optional(polarityOp) + COLON +
            expr + RPAR + RPAR + EQ + pathDelayValue + SEMI)
        edgeSensitivePathDecl = edgeSensitivePathDecl1 | edgeSensitivePathDecl2

        edgeDescr = oneOf("01 10 0x x1 1x x0").setName("edgeDescr")

        timCheckEventControl = Group(posedge | negedge
                                     | (edge + LBRACK +
                                        delimitedList(edgeDescr) + RBRACK))
        timCheckCond = Forward()
        timCondBinop = oneOf("== === != !==")
        timCheckCondTerm = (expr + timCondBinop +
                            scalarConst) | (Optional("~") + expr)
        timCheckCond << ((LPAR + timCheckCond + RPAR) | timCheckCondTerm)
        timCheckEvent = Group(
            Optional(timCheckEventControl) + subscrIdentifier +
            Optional("&&&" + timCheckCond))
        timCheckLimit = expr
        controlledTimingCheckEvent = Group(timCheckEventControl +
                                           subscrIdentifier +
                                           Optional("&&&" + timCheckCond))
        notifyRegister = identifier

        systemTimingCheck1 = Group("$setup" + LPAR + timCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck2 = Group("$hold" + LPAR + timCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck3 = Group("$period" + LPAR +
                                   controlledTimingCheckEvent + COMMA +
                                   timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck4 = Group("$width" + LPAR +
                                   controlledTimingCheckEvent + COMMA +
                                   timCheckLimit +
                                   Optional(COMMA + expr + COMMA +
                                            notifyRegister) + RPAR + SEMI)
        systemTimingCheck5 = Group("$skew" + LPAR + timCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck6 = Group("$recovery" + LPAR +
                                   controlledTimingCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck7 = Group("$setuphold" + LPAR + timCheckEvent +
                                   COMMA + timCheckEvent + COMMA +
                                   timCheckLimit + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck = (
            FollowedBy('$') +
            (systemTimingCheck1 | systemTimingCheck2 | systemTimingCheck3
             | systemTimingCheck4 | systemTimingCheck5 | systemTimingCheck6
             | systemTimingCheck7)).setName("systemTimingCheck")
        sdpd = if_ + Group(LPAR + expr + RPAR) + \
            ( pathDescr1 | pathDescr2 ) + EQ + pathDelayValue + SEMI

        specifyItem = ~Keyword("endspecify") + (
            specparamDecl | pathDecl | levelSensitivePathDecl
            | edgeSensitivePathDecl | systemTimingCheck | sdpd)
        """
        x::= <specparam_declaration>
        x||= <path_declaration>
        x||= <level_sensitive_path_declaration>
        x||= <edge_sensitive_path_declaration>
        x||= <system_timing_check>
        x||= <sdpd>
        """
        specifyBlock = Group("specify" + ZeroOrMore(specifyItem) +
                             "endspecify").setName("specifyBlock")

        moduleItem = ~Keyword("endmodule") + (
            parameterDecl | inputDecl | outputDecl | inoutDecl | regDecl |
            netDecl3 | netDecl1 | netDecl2 | timeDecl | integerDecl | realDecl
            | eventDecl | gateDecl | parameterOverride | continuousAssign
            | specifyBlock | initialStmt | alwaysStmt | task | functionDecl |
            # these have to be at the end - they start with identifiers
            moduleInstantiation | udpInstantiation)
        """  All possible moduleItems, from Verilog grammar spec
        x::= <parameter_declaration>
        x||= <input_declaration>
        x||= <output_declaration>
        x||= <inout_declaration>
        ?||= <net_declaration>  (spec does not seem consistent for this item)
        x||= <reg_declaration>
        x||= <time_declaration>
        x||= <integer_declaration>
        x||= <real_declaration>
        x||= <event_declaration>
        x||= <gate_declaration>
        x||= <UDP_instantiation>
        x||= <module_instantiation>
        x||= <parameter_override>
        x||= <continuous_assign>
        x||= <specify_block>
        x||= <initial_statement>
        x||= <always_statement>
        x||= <task>
        x||= <function>
        """
        portRef = subscrIdentifier
        portExpr = portRef | Group(LBRACE + delimitedList(portRef) + RBRACE)
        port = portExpr | Group((DOT + identifier + LPAR + portExpr + RPAR))

        moduleHdr = Group(
            oneOf("module macromodule") + identifier + Optional(LPAR + Group(
                Optional(
                    delimitedList(
                        Group(
                            oneOf("input output") +
                            (netDecl1Arg | netDecl2Arg | netDecl3Arg))
                        | port))) + RPAR) + SEMI).setName("moduleHdr")

        module = Group(moduleHdr + Group(ZeroOrMore(moduleItem)) +
                       "endmodule").setName("module")  #.setDebug()

        udpDecl = outputDecl | inputDecl | regDecl
        #~ udpInitVal = oneOf("1'b0 1'b1 1'bx 1'bX 1'B0 1'B1 1'Bx 1'BX 1 0 x X")
        udpInitVal = (Regex("1'[bB][01xX]")
                      | Regex("[01xX]")).setName("udpInitVal")
        udpInitialStmt = Group("initial" + identifier + EQ + udpInitVal +
                               SEMI).setName("udpInitialStmt")

        levelSymbol = oneOf("0   1   x   X   ?   b   B")
        levelInputList = Group(OneOrMore(levelSymbol).setName("levelInpList"))
        outputSymbol = oneOf("0   1   x   X")
        combEntry = Group(levelInputList + COLON + outputSymbol + SEMI)
        edgeSymbol = oneOf("r   R   f   F   p   P   n   N   *")
        edge = Group( LPAR + levelSymbol + levelSymbol + RPAR ) | \
               Group( edgeSymbol )
        edgeInputList = Group(
            ZeroOrMore(levelSymbol) + edge + ZeroOrMore(levelSymbol))
        inputList = levelInputList | edgeInputList
        seqEntry = Group(inputList + COLON + levelSymbol + COLON +
                         (outputSymbol | "-") + SEMI).setName("seqEntry")
        udpTableDefn = Group("table" + OneOrMore(combEntry | seqEntry) +
                             "endtable").setName("table")
        """
        <UDP>
        ::= primitive <name_of_UDP> ( <name_of_variable> <,<name_of_variable>>* ) ;
                <UDP_declaration>+
                <UDP_initial_statement>?
                <table_definition>
                endprimitive
        """
        udp = Group("primitive" + identifier + LPAR +
                    Group(delimitedList(identifier)) + RPAR + SEMI +
                    OneOrMore(udpDecl) + Optional(udpInitialStmt) +
                    udpTableDefn + "endprimitive")

        verilogbnf = OneOrMore(module | udp) + StringEnd()

        verilogbnf.ignore(cppStyleComment)
        verilogbnf.ignore(compilerDirective)

    return verilogbnf
Beispiel #26
0
    exprStack.append(toks[0])


def _assignVar(str, loc, toks):
    global targetvar
    targetvar = toks[0]


# -----------------------------------------------------------------------------
# The following statements define the grammar for the parser.

point = Literal(".")
e = CaselessLiteral("E")
plusorminus = Literal("+") | Literal("-")
number = Word(nums)
integer = Combine(Optional(plusorminus) + number)
floatnumber = Combine(integer + Optional(point + Optional(number)) +
                      Optional(e + integer))

lbracket = Literal("[")
rbracket = Literal("]")
ident = Forward()
## The definition below treats array accesses as identifiers. This means your expressions
## can include references to array elements, rows and columns, e.g., a = b[i] + 5.
## Expressions within []'s are not presently supported, so a = b[i+1] will raise
## a ParseException.
ident = Combine(
    Word(alphas + "-", alphanums + "_") +
    ZeroOrMore(lbracket +
               (Word(alphas + "-", alphanums + "_") | integer) + rbracket))
Beispiel #27
0
from distutils.version import StrictVersion
from pyparsing import (ParserElement, Forward, Combine, Optional, Word,
                       Literal, CaselessKeyword, CaselessLiteral, Group,
                       FollowedBy, LineEnd, OneOrMore, ZeroOrMore, nums,
                       alphas, alphanums, printables, delimitedList,
                       quotedString, __version__)

ParserElement.enablePackrat()
grammar = Forward()

expression = Forward()

# Literals
intNumber = Combine(Optional('-') + Word(nums))('integer')

floatNumber = Combine(Optional('-') + Word(nums) + Literal('.') +
                      Word(nums))('float')

sciNumber = Combine((floatNumber | intNumber) + CaselessLiteral('e') +
                    intNumber)('scientific')

aString = quotedString('string')

# Use lookahead to match only numbers in a list (can't remember why this
# is necessary)
afterNumber = FollowedBy(",") ^ FollowedBy(")") ^ FollowedBy(LineEnd())
number = Group((sciNumber + afterNumber) | (floatNumber + afterNumber)
               | (intNumber + afterNumber))('number')

boolean = Group(CaselessKeyword("true") | CaselessKeyword("false"))('boolean')
Beispiel #28
0
def _create_grammar():
    """Create the DBC grammar.

    """

    word = Word(printables.replace(';', '').replace(':', ''))
    integer = Group(Optional('-') + Word(nums))
    positive_integer = Word(nums).setName('positive integer')
    number = Word(nums + '.Ee-+')
    colon = Suppress(Literal(':'))
    scolon = Suppress(Literal(';'))
    pipe = Suppress(Literal('|'))
    at = Suppress(Literal('@'))
    sign = Literal('+') | Literal('-')
    lp = Suppress(Literal('('))
    rp = Suppress(Literal(')'))
    lb = Suppress(Literal('['))
    rb = Suppress(Literal(']'))
    comma = Suppress(Literal(','))
    node = Word(alphas + nums + '_-').setWhitespaceChars(' ')
    frame_id = Word(nums).setName('frame id')

    version = Group(Keyword('VERSION') - QuotedString('"', multiline=True))
    version.setName(VERSION)

    symbol = Word(alphas + '_') + Suppress(LineEnd())

    symbols = Group(Keyword('NS_') - colon - Group(ZeroOrMore(symbol)))
    symbols.setName('NS_')

    discard = Suppress(Keyword('BS_') - colon).setName('BS_')

    nodes = Group(Keyword('BU_') - colon - Group(ZeroOrMore(node)))
    nodes.setName('BU_')

    signal = Group(
        Keyword(SIGNAL) - Group(word + Optional(word)) - colon -
        Group(positive_integer - pipe - positive_integer - at -
              positive_integer - sign) -
        Group(lp - number - comma - number - rp) -
        Group(lb - number - pipe - number - rb) -
        QuotedString('"', multiline=True) - Group(delimitedList(node)))
    signal.setName(SIGNAL)

    message = Group(
        Keyword(MESSAGE) - frame_id - word - colon - positive_integer - word -
        Group(ZeroOrMore(signal)))
    message.setName(MESSAGE)

    event = Suppress(
        Keyword(EVENT) - word - colon - positive_integer - lb - number - pipe -
        number - rb - QuotedString('"', multiline=True) - number - number -
        word - node - scolon)
    event.setName(EVENT)

    comment = Group(
        Keyword(COMMENT) -
        ((Keyword(MESSAGE) - frame_id - QuotedString('"', multiline=True) -
          scolon).setName(MESSAGE)
         | (Keyword(SIGNAL) - frame_id - word -
            QuotedString('"', multiline=True) - scolon).setName(SIGNAL)
         | (Keyword(NODES) - word - QuotedString('"', multiline=True) -
            scolon).setName(NODES)
         | (Keyword(EVENT) - word - QuotedString('"', multiline=True) -
            scolon).setName(EVENT)
         |
         (QuotedString('"', multiline=True) - scolon).setName('QuotedString')))
    comment.setName(COMMENT)

    attribute_definition = Group(
        Keyword(ATTRIBUTE_DEFINITION) -
        ((QuotedString('"', multiline=True))
         | (Keyword(SIGNAL)
            | Keyword(MESSAGE)
            | Keyword(EVENT)
            | Keyword(NODES)) + QuotedString('"', multiline=True)) - word -
        (scolon
         | (Group(
             ZeroOrMore(
                 Group((comma | Empty()) +
                       QuotedString('"', multiline=True)))) + scolon)
         | (Group(ZeroOrMore(number)) + scolon)))
    attribute_definition.setName(ATTRIBUTE_DEFINITION)

    attribute_definition_default = Group(
        Keyword(ATTRIBUTE_DEFINITION_DEFAULT) -
        QuotedString('"', multiline=True) -
        (number | QuotedString('"', multiline=True)) - scolon)
    attribute_definition_default.setName(ATTRIBUTE_DEFINITION_DEFAULT)

    attribute = Group(
        Keyword(ATTRIBUTE) - QuotedString('"', multiline=True) - Group(
            Optional((Keyword(MESSAGE) + frame_id)
                     | (Keyword(SIGNAL) + frame_id + word)
                     | (Keyword(NODES) + word))) -
        (QuotedString('"', multiline=True) | number) - scolon)
    attribute.setName(ATTRIBUTE)

    choice = Group(
        Keyword(CHOICE) - Group(Optional(frame_id)) - word -
        Group(OneOrMore(Group(integer + QuotedString('"', multiline=True)))) -
        scolon)
    choice.setName(CHOICE)

    value_table = Group(
        Keyword(VALUE_TABLE) - word -
        Group(OneOrMore(Group(integer + QuotedString('"', multiline=True)))) -
        scolon)
    value_table.setName(VALUE_TABLE)

    signal_type = Group(
        Keyword(SIGNAL_TYPE) - frame_id - word - colon - positive_integer -
        scolon)
    signal_type.setName(SIGNAL_TYPE)

    signal_multiplexer_values = Group(
        Keyword(SIGNAL_MULTIPLEXER_VALUES) - frame_id - word - word - Group(
            delimitedList(positive_integer - Suppress('-') -
                          Suppress(positive_integer))) - scolon)
    signal_multiplexer_values.setName(SIGNAL_MULTIPLEXER_VALUES)

    message_add_sender = Group(
        Keyword(MESSAGE_TX_NODE) - frame_id - colon -
        Group(delimitedList(node)) - scolon)
    message_add_sender.setName(MESSAGE_TX_NODE)

    attribute_definition_rel = Group(
        Keyword(ATTRIBUTE_DEFINITION_REL) -
        (QuotedString('"', multiline=True)
         | (Keyword(NODES_REL) + QuotedString('"', multiline=True))) - word -
        (scolon
         | (Group(
             ZeroOrMore(
                 Group((comma | Empty()) +
                       QuotedString('"', multiline=True)))) + scolon)
         | (Group(ZeroOrMore(number)) + scolon)))
    attribute_definition_rel.setName(ATTRIBUTE_DEFINITION_REL)

    attribute_definition_default_rel = Group(
        Keyword(ATTRIBUTE_DEFINITION_DEFAULT_REL) -
        QuotedString('"', multiline=True) -
        (number | QuotedString('"', multiline=True)) - scolon)
    attribute_definition_default_rel.setName(ATTRIBUTE_DEFINITION_DEFAULT_REL)

    attribute_rel = Group(
        Keyword(ATTRIBUTE_REL) - QuotedString('"', multiline=True) -
        Keyword(NODES_REL) - word - Keyword(SIGNAL) - frame_id - word -
        (positive_integer | QuotedString('"')) - scolon)
    attribute_rel.setName(ATTRIBUTE_REL)

    signal_group = Group(
        Keyword(SIGNAL_GROUP) - frame_id - word - integer - colon -
        OneOrMore(word) - scolon)
    signal_group.setName(SIGNAL_GROUP)

    entry = (version
             | symbols
             | discard
             | nodes
             | message
             | comment
             | attribute_definition
             | attribute_definition_default
             | attribute
             | choice
             | value_table
             | signal_type
             | signal_multiplexer_values
             | message_add_sender
             | attribute_definition_rel
             | attribute_definition_default_rel
             | attribute_rel
             | signal_group
             | event)

    frame_id.setParseAction(lambda _s, _l, t: int(t[0]))

    return OneOrMore(entry) + StringEnd()
Beispiel #29
0
import functools
import requests
import platform
import urllib3
import urllib
import click
import pyke
import copy
import json
import sys
import re
import os

ESC = Literal('\x1b')
integer = Word(nums)
escapeSeq = Combine(ESC + '[' + Optional(delimitedList(integer, ';')) +
                    oneOf(list(alphas)))

uncolor = lambda s: Suppress(escapeSeq).transformString(s)

verify_tls = os.environ.get('LUMA_CLI_VERIFY_TLS', None)
if verify_tls == 'false' or verify_tls == 'False':
    verify_tls = False
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
else:
    verify_tls = None


def list_options(f):
    options = [
        click.option('--filter', default=''),
# IMPORTANT NOTE: it seems preferable NOT to use pyparsing's LineEnd()/lineEnd,
#                 but line_terminator instead (defined below)

# --- VBA Physical/Logical Lines ---------------------------------------------

# 3.2.1 Physical Line Grammar
# module-body-physical-structure = *source-line [non-terminated-line]
# source-line = *non-line-termination-character line-terminator
# non-terminated-line = *non-line-termination-character
# line-terminator = (%x000D %x000A) / %x000D / %x000A / %x2028 / %x2029
# non-line-termination-character = <any character other than %x000D / %x000A / %x2028 / %x2029>
non_line_termination_character = CharsNotIn(
    '\x0D\x0A', exact=1)  # exactly one non_line_termination_character
line_terminator = Literal('\x0D\x0A') | Literal('\x0D') | Literal('\x0A')
non_terminated_line = Optional(
    CharsNotIn('\x0D\x0A'))  # any number of non_line_termination_character
source_line = Optional(CharsNotIn('\x0D\x0A')) + line_terminator
module_body_physical_structure = ZeroOrMore(source_line) + Optional(
    non_terminated_line)

# 3.2.2 Logical Line Grammar
# module-body-logical-structure = *extended-line
# extended-line = *(line-continuation / non-line-termination-character) line-terminator
# line-continuation = *WSC underscore *WSC line-terminator
# module-body-lines = *logical-line
# logical-line = LINE-START *extended-line LINE-END

# NOTE: according to tests with MS Office 2007, and contrary to MS-VBAL, the line continuation pattern requires at
#      least one whitespace before the underscore, but not after.
# line_continuation = (White(min=1) + '_' + White(min=0) + line_terminator).leaveWhitespace()
whitespaces = Word(' \t\x19').leaveWhitespace()