Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# published under GPLv2 license
from amoco.logger import Log
logger = Log(__name__)
logger.debug('loading module')
from .expressions import bot,top,reg,ext
# expression parser:
#-------------------
import pyparsing as pp
#terminals:
p_bottop = pp.oneOf('⊥ T')
p_symbol = pp.Word(pp.alphas)
p_extern = pp.Suppress('@')+p_symbol
p_cst = pp.Suppress('0x')+pp.Combine(pp.Optional('-')+pp.Regex('[0-9a-f]+'))
p_int = pp.Word(pp.nums).setParseAction(lambda r:int(r[0]))
p_slc = '['+p_int.setResultsName('start')+':'+p_int.setResultsName('stop')+']'
p_op1 = pp.oneOf('~ -')
p_op2 = pp.oneOf('+ - / // * & | ^ << >> < > == <= >= != ? :')
p_term = p_bottop|p_symbol|p_extern|p_cst
#nested expressions:
p_expr = pp.Forward()
p_csl = pp.Suppress('|')+p_slc+pp.Suppress('->')
p_comp = pp.Group(pp.Suppress('{')+pp.ZeroOrMore(p_expr)+pp.Suppress('| }'))
p_mem = 'M'+p_int+pp.Optional(p_symbol)
operators = [(p_op1,1,pp.opAssoc.RIGHT),
def fromString(inputText, verbose=False):
if verbose: print(('Verbose:', verbose))
(TRANSITIONS, INITIAL_STATE, END_STATE, STATES, PARALLEL) = list(map(CaselessKeyword, """
transitions initial_state end_state states parallel""".split()))
semicolon = Suppress(Word(";"))
op = Suppress(Word("{"))
cl = Suppress(Word("}"))
to = Suppress(CaselessLiteral("=>"))
identifier = Word(alphas + "_", alphanums + "_")
list_identifiers = delimitedList(identifier)
# parse States
stateslist = Group(Suppress(STATES) + list_identifiers + semicolon).setResultsName('states')
# parse Transitions
transition = identifier.setResultsName('src') + to + list_identifiers.setResultsName('dests') + semicolon
transitions_list = Group(OneOrMore(Group(transition))).setResultsName("transitions")
transitions = Suppress(TRANSITIONS) + op + transitions_list + cl + semicolon
# parse initialstate and finalstate
initialstate = Suppress(INITIAL_STATE) + identifier.setResultsName('initialstate') + semicolon
finalstate = Suppress(END_STATE) + identifier.setResultsName('finalstate') + semicolon
# parse machine
import pyparsing as p
import pprint
opt_modif = p.Group(p.Optional(p.Word("+-") + p.Word(p.nums)))
simple_cmd = p.Word("weqnN") | p.Literal("ls")
nrng = p.Word(p.nums) + opt_modif
hrng = p.Word(".$") + opt_modif
fs = "/" + p.Word(p.alphas) + "/" + opt_modif
bs = "?" + p.Word(p.alphas) + "?" + opt_modif
the_range = p.delimitedList(p.Group(hrng) | p.Group(fs) | p.Group(bs) | p.Group(nrng))
cmd = p.Optional(p.Literal("-")) + p.Literal("V") + "/" + p.CharsNotIn("/") + "/" + p.Group(p.Optional(p.Word("i")))
cmd2 = p.Literal("rep") + "/" + p.CharsNotIn("/") + "/" + p.CharsNotIn("/") + "/" + p.Group(p.Optional(p.Word("i")))
grammar = simple_cmd.setResultsName("cmd") | p.Group(p.Optional(the_range)).setResultsName("range") + p.Group(p.delimitedList(p.Group(p.Optional(cmd | cmd2)), delim=";")).setResultsName("cmds")
def parse(a):
return grammar.parseString(a)
if __name__ == "__main__":
import sys
print grammar.parseString(sys.argv[1])
# x = grammar.parseString(".-10,$-5")
ss.AND = pp.CaselessLiteral("and")
ss.OR = pp.CaselessLiteral("or")
ss.NOT = pp.CaselessLiteral("not")
ss.caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ss.capsUnderline = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
ss.lowers = ss.caps.lower()
ss.integer = pp.Word(pp.nums)
ss.timeint = pp.Combine(pp.Optional(ss.plus | ss.minuse) + ss.integer).setParseAction(self.parseNumber) | ss.question
ss.stringPhrase = pp.quotedString.setParseAction(self.parseQuotedString)
ss.number = pp.Combine(pp.Optional(ss.plus | ss.minuse) + ss.integer + pp.Optional( ss.dot + ss.integer )).setParseAction(self.parseNumber)
ss.comment = pp.javaStyleComment.setParseAction(self.parseComment)
ss.typeName = pp.Combine(pp.Word(ss.caps,exact=1) + pp.Word(pp.alphas))#("TypeName")#TODO action
ss.objectName = pp.Combine(pp.Word(ss.lowers,exact=1) + pp.Optional(pp.Word(pp.alphas)))#("ObjectName")#TODO action
ss.objectid = pp.Combine(pp.Literal("`")+ ss.integer + pp.Literal("`"))#.setParseAction(self.parseObjectId)#("ObjectId")
ss.var = pp.Combine(pp.Optional(ss.dollar)+pp.Word(pp.alphas+ "_",exact=1) + pp.Optional(pp.Word(pp.alphas + pp.nums + "_"))) | ss.objectid#("VariableName")#TODO action
ss.constValue = ss.number | ss.stringPhrase | ss.NONE | pp.Group(pp.Suppress(ss.dollar) + ss.var + pp.Suppress(ss.dot) + ss.var).setParseAction(self.parseVarProp) | pp.Group(ss.var).setParseAction(self.parseVarToId) #pp.Word(pp.alphas) |
ss.nameValue = pp.Group(pp.Word(pp.alphas) + pp.Suppress(pp.Literal(":")) + ss.constValue)
ss.dTimeValue = ss.number +\
(pp.CaselessLiteral("millisecond") | pp.CaselessLiteral("ms") |\
pp.CaselessLiteral("second") | pp.CaselessLiteral("s") |\
pp.CaselessLiteral("minute") | pp.Literal("m") |\
pp.CaselessLiteral("hour") | pp.CaselessLiteral("h") |\
pp.CaselessLiteral("day") | pp.CaselessLiteral("d") |\
pp.CaselessLiteral("week") | pp.CaselessLiteral("w") |\
pp.CaselessLiteral("month") | pp.Literal("M") |\
pp.CaselessLiteral("year") | pp.CaselessLiteral("y"))
ss.timeValue = (pp.CaselessLiteral("Jalali") | pp.CaselessLiteral("J") |\
field_def.setParseAction(field_act)
field_list_def = delimitedList(field_def)
def field_list_act(toks):
return " | ".join(toks)
field_list_def.setParseAction(field_list_act)
create_table_def = (
Literal("CREATE")
+ "TABLE"
+ Word(alphas, alphanums + "_").setResultsName("tablename")
+ "("
+ field_list_def.setResultsName("columns")
+ ")"
+ ";"
)
def create_table_act(toks):
return (
""""%(tablename)s" [\n\t label="<%(tablename)s> %(tablename)s | %(columns)s"\n\t shape="record"\n];"""
% toks
)
create_table_def.setParseAction(create_table_act)
infixNotation,
opAssoc,
dblQuotedString,
delimitedList,
Combine,
Literal,
QuotedString,
ParserElement,
pyparsing_common as ppc,
)
ParserElement.enablePackrat()
EQ, LPAR, RPAR, COLON, COMMA = map(Suppress, "=():,")
EXCL, DOLLAR = map(Literal, "!$")
sheetRef = Word(alphas, alphanums) | QuotedString("'", escQuote="''")
colRef = Optional(DOLLAR) + Word(alphas, max=2)
rowRef = Optional(DOLLAR) + Word(nums)
cellRef = Combine(
Group(Optional(sheetRef + EXCL)("sheet") + colRef("col") + rowRef("row"))
)
cellRange = (
Group(cellRef("start") + COLON + cellRef("end"))("range")
| cellRef
| Word(alphas, alphanums)
)
expr = Forward()
COMPARISON_OP = oneOf("< = > >= <= != <>")
condExpr = expr + COMPARISON_OP + expr
def grammer(cls):
_id = Word(nums)
_type = Word(alphas)
_hash = Word(alphanums)
_filepath = Word(alphanums+"._-+ ~<>*/")
return _id + _type + _hash + _filepath
and_keyword = 'and'
open_brace = '{'
close_brace = '}'
# build setting Word definition
setting_body = pyparsing.alphanums + '_'
setting_start = pyparsing.alphas
build_setting_name = pyparsing.Word(setting_start, setting_body)
# build configuration Word definition
configutation_body = pyparsing.alphanums + '_'
configuration_start = pyparsing.alphas
build_configuration_name = pyparsing.Word(configuration_start, configutation_body) ^ pyparsing.Word("*")
# parsing conditional statement expressions
conditional_expr = pyparsing.Group(pyparsing.Word(pyparsing.alphas) + pyparsing.Suppress('=') + pyparsing.Word(pyparsing.alphanums+'*\"\'_-'))
conditional_name = pyparsing.Group(pyparsing.delimitedList(conditional_expr, and_keyword))
def processFile(should_lint=True, pyconfig_contents="", output_file=None, scheme_name=None):
# by default we want to suppress keywords, except when linting a file, then we want to make sure they are correctly placed
ParseMethod = pyparsing.Suppress
if should_lint == True:
ParseMethod = pyparsing.Keyword
# Parser Definitions
# include "other.xcconfig" # with optional trailing comment
include_parser = ParseMethod(include_keyword) + pyparsing.dblQuotedString + pyparsing.Suppress(pyparsing.ZeroOrMore(pyparsing.pythonStyleComment))
# group( comma, separated, values, to be used as assignment, for build configurations )
bc_value_parser = pyparsing.Group(pyparsing.Optional(pyparsing.commaSeparatedList.ignore(pyparsing.pythonStyleComment)))
def generate_vim_cmd():
# Vim commands like: :e, :ls, etc.
vim_cmd_name = p.Word(p.alphas)
vim_cmd_arg = p.Word(p.alphas)
arg = p.Optional(vim_cmd_arg, default='')
arg.setParseAction(lambda x: x[0])
vim_cmd = vim_cmd_name + arg
return vim_cmd
integer_number = pyparsing.Word(pyparsing.nums)
decimal_number = (pyparsing.Optional(MINUS) + integer_number +
pyparsing.Optional("." + integer_number))
decimal_number.setParseAction(lambda tokens: float("".join(tokens)))
# Initialize non-ascii unicode code points in the Basic Multilingual Plane.
unicode_printables = u''.join(
six.unichr(c) for c in range(128, 65536) if not six.unichr(c).isspace())
# Does not like comma. No Literals from above allowed.
valid_identifier_chars = (
(unicode_printables + pyparsing.alphanums + ".-_#$%&'*+/:;?@[\\]^`|"))
metric_name = (
pyparsing.Word(pyparsing.alphas, valid_identifier_chars, min=1, max=255)("metric_name"))
dimension_name = pyparsing.Word(valid_identifier_chars + ' ', min=1, max=255)
dimension_value = pyparsing.Word(valid_identifier_chars + ' ', min=1, max=255)
dim_comparison_op = pyparsing.oneOf("=")
dimension = dimension_name + dim_comparison_op + dimension_value
dimension.setParseAction(query_structures.Dimension)
dimension_list = pyparsing.Group((LBRACE + pyparsing.Optional(
pyparsing.delimitedList(dimension)) + RBRACE))
metric = (metric_name + pyparsing.Optional(dimension_list) |
pyparsing.Optional(metric_name) + dimension_list)
metric.addParseAction(query_structures.MetricSelector)
source = pyparsing.Keyword("source")