예제 #1
0
def ply_parse(text):

    def set_book_key(tokens):
        nonlocal current_key
        current_key = tokens.book_id[0]

    def add_book_attr(tokens):
        key, value = tokens[0], tokens[1]
        print(key, value)
        parsed_books[current_key][key.strip()] = value.strip()

    parsed_books = collections.defaultdict(dict)
    current_key = ''

    l_curly, r_curly, equals, comma = map(Suppress, '{}=,')
    book_id = (Suppress('@Book{') + Regex(r'[^\s,]+') + comma)('book_id')
    book_id.addParseAction(set_book_key)
    key_value = Word(alphanums) + equals + (QuotedString('"') | Word(nums))
    key_value.addParseAction(add_book_attr)
    books = OneOrMore(book_id + delimitedList(key_value) + r_curly)

    text = re.sub('\s+', ' ', text)
    try:
        books.parseString(text)
        return parsed_books
    except ParseException as parse_err:
        print('Error: {}'.format(parse_err))
        return {}
예제 #2
0
 def _define_grammer(self):
     """Define the grammar to be used, and add the various actions"""
     self._define_actions()
     node_name = Word(alphas, alphanums)
     lb = Literal("(").suppress()
     clb = Literal("(").suppress()
     rb = Literal(")").suppress()
     crb = Literal(")").suppress()
     node = Forward()
     children = clb + Group(ZeroOrMore(node)) + crb
     node << lb + node_name + Optional(children) + rb
     empty_tree = lb + rb
     tree = node | empty_tree
     self._grammar = tree
     node_name.addParseAction(self._name_action)
     clb.addParseAction(self._clb_action)
     crb.addParseAction(self._crb_action)
예제 #3
0
 def _define_grammer(self):
     '''Define the grammar to be used, and add the various actions'''
     self._define_actions()
     node_name = Word(alphas, alphanums)
     lb = Literal('(').suppress()
     clb = Literal('(').suppress()
     rb = Literal(')').suppress()
     crb = Literal(')').suppress()
     node = Forward()
     children = clb + Group(ZeroOrMore(node)) + crb
     node << lb + node_name + Optional(children) + rb
     empty_tree = lb + rb
     tree = node | empty_tree
     self._grammar = tree
     node_name.addParseAction(self._name_action)
     clb.addParseAction(self._clb_action)
     crb.addParseAction(self._crb_action)
예제 #4
0
def pyparse_pls(file, lowercase_keys=False):
    def accumulate(tokens):
        key, value = tokens
        key = key.lower() if lowercase_keys else key
        keys_values[key] = value

    keys_values = {}
    left_bracket, right_bracket, equal_sign = map(Suppress, '[]=')
    ini_line = left_bracket + CharsNotIn(']') + right_bracket
    key_value = Word(alphanums) + equal_sign + restOfLine
    key_value.addParseAction(accumulate)
    comment = '#' + restOfLine
    parser = Optional(ini_line) + OneOrMore(key_value)
    parser.ignore(comment)

    try:
        parser.parseFile(file)
    except ParseException as parse_err:
        print('Parsing error: {}', parse_err)
        return {}
    return keys_values
예제 #5
0
import pyparsing
from pyparsing import (Keyword, Word, Optional, Literal, White, CharsNotIn,
                       Group, dblQuotedString, sglQuotedString, OneOrMore,
                       ZeroOrMore)

import pykicad.pcbnew_obj as pcbnew_obj

# Main identifier and keyword types
IdentifierTok = pyparsing.Word(pyparsing.alphas + '_')

IntegerTok = Optional(Literal('-')) + Word(pyparsing.nums)
IntegerTok.addParseAction(lambda toks: int("".join(toks)))

UnsignedIntTok = Word(pyparsing.nums)
UnsignedIntTok.addParseAction(lambda toks: int(toks[0]))
FloatTok = Optional(Literal('-')) + Word(
    pyparsing.nums) + Optional(Literal('.') + Optional(Word(pyparsing.nums)))
FloatTok.addParseAction(lambda toks: float("".join(toks)))
HexStringTok = Word(pyparsing.hexnums)
HexStringTok.addParseAction(lambda toks: int(toks[0], base=16))

UnquotedStringTok = ZeroOrMore(
    White()).suppress() + CharsNotIn("()\"\'" + " \r\n")
UnquotedStringTok.addParseAction(lambda toks: "".join(toks).strip())

QuotedStringTok = Group(dblQuotedString() ^ sglQuotedString())
QuotedStringTok.addParseAction(lambda toks: "".join(toks[0]).strip('"'))

AnystringTok = QuotedStringTok ^ UnquotedStringTok
LeftParenTok = Literal('(').suppress()
예제 #6
0
decimalNumber = Word(nums, nums + ",") + Optional("." + OneOrMore(Word(nums)))


def joinTokens(tokens):
    return "".join(tokens)


decimalNumber.setParseAction(joinTokens)


def stripCommas(tokens):
    return tokens[0].replace(",", "")


decimalNumber.addParseAction(stripCommas)


def convertToFloat(tokens):
    return float(tokens[0])


decimalNumber.addParseAction(convertToFloat)


def read_names(name):
    grammar = S(name) + S(':') + OneOrMore(gams_identifier + Ot(S(',')))
    ret = []
    with open("calibration.cal", 'r') as cal_file:
        for line in cal_file:
            try:
    if len(node_stack) > 0:
        node_stack[len(node_stack) - 1].add_child(current_node)


def push_current():
    global current_node, node_stack
    node_stack.append(current_node)
    current_node = None


def pop_current():
    global current_node, node_stack
    current_node = node_stack.pop()

node_name = Word(alphas, alphanums)
node_name.addParseAction(create_node)
lb = Literal('(').suppress()
clb = Literal('(').suppress()
clb.addParseAction(push_current)
rb = Literal(')').suppress()
crb = Literal(')').suppress()
crb.addParseAction(pop_current)
node = Forward()
children = clb + Group(ZeroOrMore(node)) + crb
node << lb + node_name + Optional(children) + rb

tree_str = '(c1 ((c2) (c3 ((c4) (c5))) (c6)))'

results = node.parseString(tree_str)
print(results)
writer = IndentedStringWriter()
        node_stack[len(node_stack) - 1].add_child(current_node)


def push_current():
    global current_node, node_stack
    node_stack.append(current_node)
    current_node = None


def pop_current():
    global current_node, node_stack
    current_node = node_stack.pop()


node_name = Word(alphas, alphanums)
node_name.addParseAction(create_node)
lb = Literal('(').suppress()
clb = Literal('(').suppress()
clb.addParseAction(push_current)
rb = Literal(')').suppress()
crb = Literal(')').suppress()
crb.addParseAction(pop_current)
node = Forward()
children = clb + Group(ZeroOrMore(node)) + crb
node << lb + node_name + Optional(children) + rb

tree_str = '(c1 ((c2) (c3 ((c4) (c5))) (c6)))'

results = node.parseString(tree_str)
print(results)
writer = IndentedStringWriter()
예제 #9
0
IF = Keyword("if", caseless=True)
NOT = Keyword("not", caseless=True)
EXISTS = Keyword("exists", caseless=True)
DELETE = Keyword("delete", caseless=True)
USE = Keyword("use", caseless=True)
CREATE = Keyword("create", caseless=True)
TABLE = Keyword("table", caseless=True)
PRIMARY = Keyword("primary", caseless=True)
KEY = Keyword("key", caseless=True)

# column names
columnName = ident.setName("column").addParseAction(downcaseTokens)
columnNameList = Group(delimitedList(columnName))

# table name
keyspaceName = ident.addParseAction(downcaseTokens).setName("keyspace")
tableName = Group(
    Optional(keyspaceName + ".") +
    ident.addParseAction(downcaseTokens)).setName("table")

# where clause
and_ = Keyword("and", caseless=True)
binop = oneOf("= eq != < > >= <= eq ne lt le gt ge", caseless=True)
Rval = realNum('real') | intNum('int') | quotedString(
    'quoted')  # need to add support for alg expressions
RvalList = Group(delimitedList(Rval))

# where expression
whereExpression = Forward()
whereCondition = Group((columnName() + binop + Rval))
whereExpression <<= whereCondition + ZeroOrMore(and_ + whereExpression)