def parse_grd_file(self): global __line try: f = open(self.grd_file, 'r') except: raise for j in range(0, 6): __line = f.next() (self.nx, self.ny) = map(int, __line.split()) print "Grid file %s (%d, %d)" % (self.grd_file, self.nx, self.ny) f.close() floatNumber = Regex(r'-?\d+(\.\d*)?([eE][\+-]\d+)?').setParseAction(lambda s, l, t: [float(t[0])]) integer = Word(nums).setParseAction(lambda s, l, t: [long(t[0])]) grdline = Suppress('ETA=') + Suppress(integer) + OneOrMore(floatNumber) for a in grdline.searchString(file(self.grd_file).read()): if len(self.x) < self.ny: self.x.append(a.asList()) else: self.y.append(a.asList())
def init_parser(self): INTEGER = Word(nums) INTEGER.setParseAction(lambda x: int(x[0])) header = INTEGER("species_count") + INTEGER("sequence_length") +\ Suppress(restOfLine) header.setParseAction(self.set_header) sequence_name = Word( alphas + nums + "!#$%&\'*+-./;<=>?@[\\]^_`{|}~", max=100) # Take a copy and disallow line breaks in the bases bases = self.BASES.copy() bases.setWhitespaceChars(" \t") seq_start = sequence_name("species") + bases( "sequence") + Suppress(LineEnd()) seq_start.setParseAction(self.set_seq_start) seq_start_block = OneOrMore(seq_start) seq_start_block.setParseAction(self.set_start_block) seq_continue = bases("sequence") + Suppress(LineEnd()) seq_continue.setParseAction(self.set_seq_continue) seq_continue_block = Suppress(LineEnd()) + OneOrMore(seq_continue) seq_continue_block.setParseAction(self.set_continue_block) return header + seq_start_block + ZeroOrMore(seq_continue_block)
def parse(self, header): comment = self._comment() quoted = quotedString.copy().setParseAction(removeQuotes) string = quoted | Word(printables, excludeChars='{},%') enum_value = quotedString | Word(printables, excludeChars='{},%') relation = (Suppress(CaselessLiteral("@relation")) + Optional(restOfLine, default='default_name')('rel_name').setParseAction(lambda t: t.rel_name.strip())) relation_part = ZeroOrMore(comment) + relation + ZeroOrMore(comment) nominal = (Empty().copy().setParseAction(lambda t: self.ENUM) + Suppress(Literal("{")) + Group(delimitedList(enum_value, delim=self._separator))("next_arg").setParseAction(self.get_values) + Suppress(Literal("}"))) date = CaselessLiteral("date") + Optional(CharsNotIn("{},\n"))("next_arg").setParseAction(self._adapt_date_format) attributes_part = Forward() relational = CaselessLiteral("relational") + attributes_part + Suppress(CaselessLiteral("@end")) + string attr_type = (CaselessLiteral("numeric") | CaselessLiteral("string") | nominal | date | relational)("attr_type") attribute = Suppress(CaselessLiteral("@attribute")) + (string.copy())("attr_name") + attr_type attribute_line = comment | attribute attributes_part << (Group(OneOrMore(attribute_line)))("children") data_part = (CaselessLiteral("@data"))("data_start").setParseAction(lambda s, p, k: (lineno(p, s))) arff_header = relation_part + attributes_part + data_part attribute.setParseAction(self._create_attribute) try: result = arff_header.parseString(header, parseAll=True) except ParseException as e: raise HeaderError(FileType.ARFF, e.lineno, e.col, e.line, e) self._relation_name = result.rel_name self._find_relational(result.children) self._linearize_attrs(result.children) self._data_start = result.data_start self._index = 0
def parse(date_string): # Parser for individual dates days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'] suffixes = Literal('nd') | Literal('rd') | Literal('st') | Literal('th') day_of_month = Group(Word(nums) + Suppress(Optional(suffixes))).setResultsName('day') single_date = Optional(DateParser._build_literal(days_of_week)).setResultsName('dow') + day_of_month + \ Optional(DateParser._build_literal(LONG_MONTHS + SHORT_MONTHS)).setResultsName('month') + \ Optional(Word(nums)).setResultsName('year') single_date.setParseAction(SingleDate) # Parser for date ranges date_range_separators = DateParser._build_literal(['-', 'until', 'to']) date_range = Suppress(Optional('From')) + single_date.setResultsName('start_date') + \ Suppress(date_range_separators) + single_date.setResultsName('end_date') date_range.setParseAction(DateRange) date_parser = (date_range | single_date) + stringEnd result = date_parser.parseString(date_string) return result
def main(): filename = sys.argv[1] source = open(filename).read() commentFilter = Suppress( javaStyleComment ).ignore( dblQuotedString ) source = commentFilter.transformString(source) source = source.replace('\r\n', '\n') source = source.replace('\r', '') for m in re.finditer('#pragma[ \t][ \t]*pskel.*\n', source): pragma = m.group(0) begin = m.start(0) end = m.end(0)+1 while pragma.strip()[-1]=='\\': pragma = pragma.strip()[:-1].strip() tmp = '' while source[end]!='\n': tmp += source[end] end += 1 pragma += ' '+tmp.strip() end += 1 pragma = pragma.strip() print begin, end print pragma #print source[begin:end] pinfo = parsePragma(pragma) print pinfo fcall = readFuncCall(source,end) print fcall kfunc = findFunctionDef(source,fcall[0]) print source[fcall[2]:fcall[3]] mainf = readOutterFunction(source, begin) #print mainf fcall_name = '_pskelcc_stencil_'+str(fcall[2])+'_'+str(fcall[3]) tmainf = mainf['src'].replace(source[begin:end],'\n').replace(source[fcall[2]:fcall[3]],fcall_name+'();') print tmainf parser = c_parser.CParser() ast = parser.parse(tmainf) pgen = PSkelGenerator(pinfo,fcall_name) new_mainf = pgen.visit(ast) parser = c_parser.CParser() ast = parser.parse(kfunc['src']) pgen = PSkelKernelGenerator(pinfo) new_kfunc = pgen.visit(ast) f = open(sys.argv[2],'w') f.write('#define PSKEL_OMP\n') if pinfo['device']=='gpu': f.write('#define PSKEL_CUDA\n') f.write(''' #include "include/PSkel.h" using namespace PSkel;\n\n''') f.write(source[:kfunc['begin']]+new_kfunc+source[kfunc['end']:mainf['begin']]+new_mainf+source[mainf['end']:]) f.close()
def getEbnfParser(symbols): """ Returns an EBNF parser for the command language. """ identifier = Word(alphas + '_', alphanums + '_') string = quotedString.setParseAction( lambda t: symbols.append((t[0][1:-1], TokenType.StrLit)) ) integer = Word(nums).setParseAction( lambda t: symbols.append((int(t[0]), TokenType.NumLit)) ) var = Suppress("$") + identifier var.setParseAction( lambda t: symbols.append((t[0], TokenType.Var)) ) literal = var | string | integer fnid = Suppress(Optional(".")) + identifier fnid.setParseAction( lambda t: symbols.append((t[0], TokenType.Call)) ) call = Forward() callb = fnid + ZeroOrMore(call | literal) call << ((Suppress("(") + callb + Suppress(")")) | callb) fndef_head = Suppress("let") + identifier fndef_head.setParseAction( lambda t: symbols.append((t[0], TokenType.Def)) ) definition = fndef_head + ZeroOrMore(var) + Suppress("=") + call cmd = OneOrMore((definition | call) + Word(";").setParseAction( lambda t: symbols.append((t[0], TokenType.End)) )) msg = OneOrMore(cmd) return msg
def setup(self): # some expressions that will be reused units = [] for unit in time_units: units.append(Keyword(unit)) units = get_match_first(units) units = units.setResultsName("unit") units.setParseAction(lambda s, l, tok: time_units[tok[0]]) multiplier = Word(nums) multiplier = multiplier.setResultsName("multiply") multiplier.setParseAction(self.parseMulti) adder = [] for add in add_modifiers: adder.append(CL(add)) adder = get_match_first(adder) adder = adder.setResultsName("add") adder.setParseAction(self.parseAdd) modifier = (multiplier | adder) # + FollowedBy(units) # ago # # e.g 5 days ago ago = Optional(modifier) + units + Suppress(Word("ago")) ago.setParseAction(self.parseAgo) # time range # # e.g in the lat 10 days time_range = Suppress(Optional( CL("in the"))) + \ Suppress(Word("last") | Word("past")) + \ Optional(modifier) + \ units time_range.setParseAction(self.parseRange) # special keyword handling # # e.g yesterday # only handles yesterday right now, maybe need to be modified to do # more special_expr = [] for expr in special: special_expr.append( Keyword(expr).setParseAction( lambda s, l, tok: special[tok[0]])) special_expr = get_match_first(special_expr) special_expr = special_expr.setResultsName("unit") special_expr.setParseAction(self.parseAgo) parser = (special_expr | ago | time_range) return parser
def parse_table(attribute, string): Line = OneOrMore(Float)('data') + Literal(';') + Optional(Comments, default='')('name') Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=') + Keyword('[') + Optional(Comments)) + OneOrMore(Group(Line)) + Suppress(Keyword(']') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() _list = list() for r in result: _list.append([int_else_float_except_string(s) for s in r['data'].asList()]) return _list
def parse_puppetfile_forge_mods(cls, file_contents): mod_kwargs = {} quotedString = QuotedString('"') ^ QuotedString('\'') forge_url_grammar = Suppress('forge') + quotedString for url, in forge_url_grammar.searchString(file_contents): mod_kwargs['forge_url'] = url mod_grammar = Suppress('mod') + quotedString + Suppress(',') + quotedString mods = mod_grammar.searchString(file_contents) for mod, version in mods: user, mod_name = mod.split('/') yield cls(user, mod_name, version, **mod_kwargs)
def __init__(self): self.ALPHA_LABEL = Regex(r'alpha\[\d+\]:') self.LNL_LABEL = Literal('Final GAMMA-based Score of best tree') self.FRQ_LABEL = Regex(r'Base frequencies: (?=\d+)') ^ Regex(r'ML estimate base freqs\[\d+\]:') self.NAMES_LABEL = Regex(r'Partition: \d+ with name:\s+') self.RATES_LABEL = Regex(r'rates\[\d+\].+?:') self.MODEL_LABEL = Literal('Substitution Matrix:') self.alpha = OneOrMore(Suppress(SkipTo(self.ALPHA_LABEL)) + Suppress(self.ALPHA_LABEL) + FLOAT) self.lnl = Suppress(SkipTo(self.LNL_LABEL)) + Suppress(self.LNL_LABEL) + FLOAT self.frq = OneOrMore(Group(Suppress(SkipTo(self.FRQ_LABEL)) + Suppress(self.FRQ_LABEL) + OneOrMore(FLOAT))) self.names = OneOrMore(Suppress(SkipTo(self.NAMES_LABEL)) + Suppress(self.NAMES_LABEL) + CharsNotIn('\n') + Suppress(LineEnd())) self.rates = OneOrMore(Group(Suppress(SkipTo(self.RATES_LABEL)) + Suppress(self.RATES_LABEL) + OneOrMore(FLOAT))) self.model = Suppress(SkipTo(self.MODEL_LABEL)) + Suppress(self.MODEL_LABEL) + WORD MODEL_LABEL = Literal('Substitution Matrix:') SCORE_LABEL = Literal('Final GAMMA likelihood:') DESC_LABEL = Literal('Model Parameters of Partition') NAME_LEADIN = Literal(', Name:') DATATYPE_LEADIN = Literal(', Type of Data:') ALPHA_LEADIN = Literal('alpha:') TREELENGTH_LEADIN = Literal('Tree-Length:') RATES_LABEL = Regex(r'rate \w <-> \w:') FREQS_LABEL = Regex(r'freq pi\(\w\):') BEST_LEADIN = Literal('Starting final GAMMA-based thorough Optimization on tree ') PARTITION_LEADIN = Literal('Partition:') INFERENCE_LEADIN = Literal('Inference[') model = Suppress(SkipTo(MODEL_LABEL)) + Suppress(MODEL_LABEL) + WORD likelihood = Suppress(SkipTo(SCORE_LABEL)) + Suppress(SCORE_LABEL) + FLOAT description = (Suppress(SkipTo(DESC_LABEL)) + Suppress(DESC_LABEL) + INT + Suppress(NAME_LEADIN) + SPACEDWORD + Suppress(DATATYPE_LEADIN) + WORD) alpha = Suppress(ALPHA_LEADIN) + FLOAT rates = Suppress(RATES_LABEL) + FLOAT freqs = Suppress(FREQS_LABEL) + FLOAT self.partition = OneOrMore(Suppress(SkipTo(PARTITION_LEADIN)) + Suppress(PARTITION_LEADIN) + INT) self.inference = OneOrMore(Suppress(SkipTo(INFERENCE_LEADIN)) + Suppress(INFERENCE_LEADIN) + INT) self.best = Suppress(SkipTo(BEST_LEADIN)) + Suppress(BEST_LEADIN) + INT self._dash_f_e_parser = (Group(OneOrMore(model)) + likelihood + Group(OneOrMore(Group(description + alpha + Suppress(TREELENGTH_LEADIN) + Suppress(FLOAT) + Group(OneOrMore(rates)) + Group(OneOrMore(freqs)) ))))
def parser(): global _parser if _parser is None: ParserElement.setDefaultWhitespaceChars("") lbrack = Literal("[") rbrack = Literal("]") lbrace = Literal("{") rbrace = Literal("}") lparen = Literal("(") rparen = Literal(")") reMacro = Suppress("\\") + oneOf(list("dwsZ")) escapedChar = ~reMacro + Combine("\\" + oneOf(list(printables))) reLiteralChar = "".join(c for c in string.printable if c not in r"\[]{}().*?+|") reRange = Combine(lbrack.suppress() + SkipTo(rbrack,ignore=escapedChar) + rbrack.suppress()) reLiteral = ( escapedChar | oneOf(list(reLiteralChar)) ) reDot = Literal(".") repetition = ( ( lbrace + Word(nums).setResultsName("count") + rbrace ) | ( lbrace + Word(nums).setResultsName("minCount")+","+ Word(nums).setResultsName("maxCount") + rbrace ) | oneOf(list("*+?")) ) reExpr = Forward() reGroup = (lparen.suppress() + Optional(Literal("?").suppress() + oneOf(list(":P"))).setResultsName("option") + reExpr.setResultsName("expr") + rparen.suppress()) reTerm = ( reLiteral | reRange | reMacro | reDot | reGroup ) reExpr << operatorPrecedence( reTerm, [ (repetition, 1, opAssoc.LEFT, create(Repetition)), (None, 2, opAssoc.LEFT, create(Sequence)), (Suppress('|'), 2, opAssoc.LEFT, create(Alternation)), ] ) reGroup.setParseAction(create(Group)) reRange.setParseAction(create(Range)) reLiteral.setParseAction(create(Character)) reMacro.setParseAction(create(Macro)) reDot.setParseAction(create(Dot)) _parser = reExpr return _parser
def songs_pyparsing(fh): r""" >>> import os >>> filename = os.path.dirname(__file__) >>> filename = os.path.join(filename, "data/Various-Pop.m3u") >>> with open(filename, "rt", encoding="utf8") as fh: ... songs = songs_pyparsing(fh) >>> songs[0].title, songs[0].seconds, songs[0].filename ('Various - Two Tribes', 236, 'Various\\Frankie Goes To Hollywood\\02-Two Tribes.ogg') >>> songs[-1].title, songs[-1].seconds, songs[-1].filename ('The Police - Walking On The Moon', 303, 'Various\\Sting & The Police 1997\\06-Walking On The Moon.ogg') >>> lines = [] >>> lines.append("#EXTM3U") >>> lines.append("#EXTINF:140,The Beatles - Love Me Do") >>> lines.append("Beatles\\Greatest Hits\\01-Love Me Do.ogg") >>> lines.append("#EXTINF:-1,The Beatles - From Me To You") >>> lines.append("Beatles\\Greatest Hits\\02-From Me To You.ogg") >>> import io >>> data = io.StringIO("\n".join(lines)) >>> songs = songs_ply(data) >>> len(songs) == 2 True >>> songs[0].title, songs[0].seconds ('The Beatles - Love Me Do', 140) >>> songs[1].title, songs[1].seconds ('The Beatles - From Me To You', -1) """ def add_song(tokens): songs.append(Song(tokens.title, tokens.seconds, tokens.filename)) #songs.append(Song(**tokens.asDict())) songs = [] title = restOfLine("title") filename = restOfLine("filename") seconds = Combine(Optional("-") + Word(nums)).setParseAction( lambda tokens: int(tokens[0]))("seconds") info = Suppress("#EXTINF:") + seconds + Suppress(",") + title entry = info + LineEnd() + filename + LineEnd() entry.setParseAction(add_song) parser = Suppress("#EXTM3U") + OneOrMore(entry) try: parser.parseFile(fh) except ParseException as err: print("parse error: {0}".format(err)) return [] return songs
def parse_issue(issue): """Given an issue, return data that needs saving""" from pyparsing import Word, Suppress, Literal, OneOrMore, Optional, nums, printables sprint = (Literal('Sprint') | Literal('sprint')) + Word(nums) points = Suppress(Literal('(')) + Word(nums) + Suppress(Literal(')')) deadline = Suppress(Literal('<')) + Word(nums) name = OneOrMore(Word(printables)) title = Optional(sprint).setResultsName("sprint") + \ Optional(name).setResultsName("name") + \ Optional(deadline).setResultsName("deadline") + \ points.setResultsName("points") try: _, r = title.parseString(issue['title']) print r except: pass
def __init__(self): self.ALPHA_LABEL = Regex(r"alpha\[\d+\]:") self.LNL_LABEL = Literal("Final GAMMA-based Score of best tree") self.FRQ_LABEL = Regex(r"Base frequencies: (?=\d+)") ^ Regex(r"ML estimate base freqs\[\d+\]:") self.NAMES_LABEL = Regex(r"Partition: \d+ with name:\s+") self.RATES_LABEL = Regex(r"rates\[\d+\].+?:") self.MODEL_LABEL = Literal("Substitution Matrix:") self.alpha = OneOrMore(Suppress(SkipTo(self.ALPHA_LABEL)) + Suppress(self.ALPHA_LABEL) + FLOAT) self.lnl = Suppress(SkipTo(self.LNL_LABEL)) + Suppress(self.LNL_LABEL) + FLOAT self.frq = OneOrMore(Group(Suppress(SkipTo(self.FRQ_LABEL)) + Suppress(self.FRQ_LABEL) + OneOrMore(FLOAT))) self.names = OneOrMore( Suppress(SkipTo(self.NAMES_LABEL)) + Suppress(self.NAMES_LABEL) + CharsNotIn("\n") + Suppress(LineEnd()) ) self.rates = OneOrMore( Group(Suppress(SkipTo(self.RATES_LABEL)) + Suppress(self.RATES_LABEL) + OneOrMore(FLOAT)) ) self.model = Suppress(SkipTo(self.MODEL_LABEL)) + Suppress(self.MODEL_LABEL) + WORD MODEL_LABEL = Literal("Substitution Matrix:") SCORE_LABEL = Literal("Final GAMMA likelihood:") DESC_LABEL = Literal("Model Parameters of Partition") NAME_LEADIN = Literal(", Name:") DATATYPE_LEADIN = Literal(", Type of Data:") ALPHA_LEADIN = Literal("alpha:") TREELENGTH_LEADIN = Literal("Tree-Length:") RATES_LABEL = Regex(r"rate \w <-> \w:") FREQS_LABEL = Regex(r"freq pi\(\w\):") model = Suppress(SkipTo(MODEL_LABEL)) + Suppress(MODEL_LABEL) + WORD likelihood = Suppress(SkipTo(SCORE_LABEL)) + Suppress(SCORE_LABEL) + FLOAT description = ( Suppress(SkipTo(DESC_LABEL)) + Suppress(DESC_LABEL) + INT + Suppress(NAME_LEADIN) + SPACEDWORD + Suppress(DATATYPE_LEADIN) + WORD ) alpha = Suppress(ALPHA_LEADIN) + FLOAT rates = Suppress(RATES_LABEL) + FLOAT freqs = Suppress(FREQS_LABEL) + FLOAT self._dash_f_e_parser = ( Group(OneOrMore(model)) + likelihood + Group( OneOrMore( Group( description + alpha + Suppress(TREELENGTH_LEADIN) + Suppress(FLOAT) + Group(OneOrMore(rates)) + Group(OneOrMore(freqs)) ) ) ) )
def fromString(inputText, verbose=False): if verbose: print 'Verbose:', verbose text = nestedExpr("/*", "*/").suppress().transformString(inputText) semicolon = Suppress(Word(";")) quote = Suppress(Word("\"")) op = Suppress(Word("{")) cl = Suppress(Word("}")) opp = Suppress(Word("(")) clp = Suppress(Word(")")) lt = Suppress(Word("<")) gt = Suppress(Word(">")) identifier = Word(alphas+"_",alphanums+"_") typeIdentifier = Word(alphas+"_",alphanums+"_:") ## Imports idslImport = Suppress(Word("import")) + quote + CharsNotIn("\";").setResultsName('path') + quote + semicolon idslImports = ZeroOrMore(idslImport) dictionaryDef = Word("dictionary") + lt + CharsNotIn("<>;") + gt + identifier.setResultsName('name') + semicolon sequenceDef = Word("sequence") + lt + CharsNotIn("<>;") + gt + identifier.setResultsName('name') + semicolon enumDef = Word("enum") + identifier.setResultsName('name') + op + CharsNotIn("{}") + cl + semicolon structDef = Word("struct") + identifier.setResultsName('name') + op + CharsNotIn("{}") + cl + semicolon exceptionDef = Word("exception") + identifier.setResultsName('name') + op + CharsNotIn("{}") + cl + semicolon raiseDef = Suppress(Word("throws")) + typeIdentifier + ZeroOrMore( Literal(',') + typeIdentifier ) decoratorDef = Literal('idempotent') | Literal('out') retValDef = typeIdentifier.setResultsName('ret') firstParam = Group( Optional(decoratorDef.setResultsName('decorator')) + typeIdentifier.setResultsName('type') + identifier.setResultsName('name')) nextParam = Suppress(Word(',')) + firstParam params = firstParam + ZeroOrMore(nextParam) remoteMethodDef = Group(Optional(decoratorDef) + retValDef + typeIdentifier.setResultsName('name') + opp + Optional( params).setResultsName('params') + clp + Optional(raiseDef) + semicolon ) interfaceDef = Word("interface") + typeIdentifier.setResultsName('name') + op + Group(ZeroOrMore(remoteMethodDef)) + cl + semicolon moduleContent = Group(structDef | enumDef | exceptionDef | dictionaryDef | sequenceDef | interfaceDef) module = Suppress(Word("module")) + identifier.setResultsName("name") + op + ZeroOrMore(moduleContent).setResultsName("contents") + cl + semicolon IDSL = idslImports.setResultsName("imports") + module.setResultsName("module") IDSL.ignore( cppStyleComment ) tree = IDSL.parseString(text) return IDSLParsing.module(tree)
def detect_token(jade): doctype = LineStart() + oneOf('!!! doctype') + Optional(oneOf('5 html xml' \ + ' default transitional strict frameset 1.1 basic mobile', True)) doctype.setParseAction(parse_doctype) element_id = Suppress('#') + Word(alphanums + '_' + '-') element_class = Suppress('.') + Word(alphanums + '_' + '-') selectors = (element_id.setResultsName('element_id') \ + ZeroOrMore(element_class).setResultsName('element_class')) \ | (OneOrMore(element_class).setResultsName('element_class') \ + Optional(element_id).setResultsName('element_id')) selectors.setParseAction(parse_selectors) element = selectors.setResultsName('selectors') \ | (Word(alphas).setResultsName('element_name') \ + Optional(selectors).setResultsName('selectors')) element.setParseAction(parse_element) attribute = CharsNotIn('('+')') attributes = nestedExpr(content=attribute) tag = element.setResultsName('element') \ + Optional(attributes).setResultsName('attributes') tag.setParseAction(parse_tag) # TODO: block-comment and conditional-comment unbuffered_comment = Suppress(Suppress('//-') + restOfLine) buffered_comment = Suppress('//') + restOfLine buffered_comment.setParseAction(parse_buffered_comment) # Order matters here, as buffered will pick up # unbuffered comments if set first comment = unbuffered_comment | buffered_comment source = doctype | tag | comment parsed = source.parseString(jade) return ' '.join(parsed) '''
def get_network_name(self): """ Retruns the name of the network Example --------------- >>> from pgmpy.readwrite import BIFReader >>> reader = BIF.BifReader("bif_test.bif") >>> reader.network_name() 'Dog-Problem' """ start = self.network.find('network') end = self.network.find('}\n', start) # Creating a network attribute network_attribute = Suppress('network') + Word(alphanums + '_' + '-') + '{' network_name = network_attribute.searchString(self.network[start:end])[0][0] return network_name
def parseReactions(reaction): name = Word(alphanums + '_-') + ':' species = (Word(alphanums + "_:#-") + Suppress('()')) + ZeroOrMore(Suppress('+') + Word(alphanums + "_:#-") + Suppress("()")) rate = Word(alphanums + "()") grammar = Suppress(Optional(name)) + ((Group(species) | '0') + Suppress(Optional("<") + "->") + (Group(species) | '0') + Suppress(rate)) \ ^ (species + Suppress(Optional("<") + "->") + Suppress(rate)) result = grammar.parseString(reaction).asList() if len(result) < 2: result = [result, []] if '<->' in reaction and len(result[0]) == 1 and len(result[1]) == 2: result2 = [result[1], result[0]] result = result2 return result
def get_fragment_grammar(): # Match header [mapping] header = Suppress("[") + Suppress("mapping") + Suppress("]") # There are three possible patterns for mapping entries: # obj:symbol (scheme) # obj (scheme) # * (scheme) obj = Fragment.ENTITY.setResultsName("object") symbol = Suppress(":") + Fragment.IDENTIFIER.setResultsName("symbol") scheme = Suppress("(") + Fragment.IDENTIFIER.setResultsName("scheme") + Suppress(")") pattern1 = Group(obj + symbol + scheme) pattern2 = Group(obj + scheme) pattern3 = Group(Literal(Mapping.MAPPING_ALL_OBJECTS).setResultsName("object") + scheme) mapping_entry = pattern1 | pattern2 | pattern3 # To simplify parsing, classify groups of condition-mapping entry into two types: normal and default # A normal grouping is one with a non-default condition. The default grouping is one which contains the # default condition mapping_entries = Group(ZeroOrMore(mapping_entry)).setResultsName("mappings") normal_condition = Suppress(":") + originalTextFor(SDKConfig.get_expression_grammar()) default_condition = Optional(Suppress(":") + Literal(Mapping.DEFAULT_CONDITION)) normal_group = Group(normal_condition.setResultsName("condition") + mapping_entries) default_group = Group(default_condition + mapping_entries).setResultsName("default_group") normal_groups = Group(ZeroOrMore(normal_group)).setResultsName("normal_groups") # Any mapping fragment definition can have zero or more normal group and only one default group as a last entry. archive = Suppress("archive") + Suppress(":") + Fragment.ENTITY.setResultsName("archive") entries = Suppress("entries") + Suppress(":") + (normal_groups + default_group).setResultsName("entries") mapping = Group(header + archive + entries) mapping.setParseAction(lambda t: Mapping(t[0].archive, t[0].entries)) mapping.ignore("#" + restOfLine) return mapping
class Compiler: def __init__(self): self._pythonVar = None self.varNames = [] Preprocess.setSocialiteModule(getModuleVar()) def pythonVar(self): if not self._pythonVar: from pyparsing import (ParserElement, Word, alphas, alphanums, Literal, Suppress, FollowedBy) _ws = ' \t' ParserElement.setDefaultWhitespaceChars(_ws) ident = Word(alphas+"_", alphanums+"_") lparen = Literal("(") dot = Literal(".") dollar = Literal("$") self._pythonVar = Suppress(dollar) + ident + ~FollowedBy((dot+ident) | lparen) self._pythonVar.setParseAction(self.onPythonVar) return self._pythonVar def compile(self, src): gen=Preprocess.run(src) return gen def processPythonVars(self, query): query = '('+query+')' tmp = query if tmp.find("$") >= 0: tmp = self.pythonVar().transformString(query) if self.varNames: query = ''.join([tmp, "%"+getPassVarsFunc()+"(", ','.join(self.varNames), ")"]) else: query = tmp for i in xrange(len(self.varNames)): self.varNames.pop() return query def onPythonVar(self, inputStr, loc, tokens): varName = ''.join(tokens) self.varNames.append(varName) return "%s"
def __init__(self, cfg): self.cfg = cfg if cfg.datatype == "protein": letters = _protein_letters elif cfg.datatype == "DNA": letters = _dna_letters elif cfg.datatype == "morphology": letters = "0123456789" else: log.error("Unknown datatype '%s', please check" % self.cfg.datatype) raise util.PartitionFinderError self.rate_indexes = self.cfg.data_layout.rate_indexes self.freq_indexes = self.cfg.data_layout.letter_indexes FLOAT = Word(nums + '.-').setParseAction(lambda x: float(x[0])) L = Word(letters, exact=1) COLON = Suppress(":") LNL_LABEL = Regex("Final GAMMA.+:") | Literal("Likelihood:") TIME_LABEL = Regex("Overall Time.+:") | Regex("Overall Time.+tion ") ALPHA_LABEL = Literal("alpha:") TREE_SIZE_LABEL = Literal("Tree-Length:") def labeled_float(label): return Suppress(SkipTo(label)) + Suppress(label) + FLOAT lnl = labeled_float(LNL_LABEL) lnl.setParseAction(self.set_lnl) seconds = labeled_float(TIME_LABEL) seconds.setParseAction(self.set_seconds) alpha = labeled_float(ALPHA_LABEL) alpha.setParseAction(self.set_alpha) tree_size = labeled_float(TREE_SIZE_LABEL) tree_size.setParseAction(self.set_tree_size) LG4X_LINE = "LG4X" + restOfLine lg4x = Optional(LG4X_LINE + LG4X_LINE) rate = Suppress("rate") + L + Suppress("<->") + L + COLON + FLOAT rate.setParseAction(self.set_rate) rates = OneOrMore(rate) freq = Suppress("freq pi(") + L + Suppress("):") + FLOAT freq.setParseAction(self.set_freq) freqs = OneOrMore(freq) LGM_LINE = "LGM" + restOfLine rate_block = Optional(LGM_LINE) + rates + freqs rate_block.setParseAction(self.rate_block) # Just look for these things self.root_parser = seconds + lnl + alpha + tree_size +\ lg4x + OneOrMore(rate_block)
def fromString(inputText, verbose=False): if verbose: print 'Verbose:', verbose text = nestedExpr("/*", "*/").suppress().transformString(inputText) semicolon = Suppress(Word(";")) quote = Suppress(Word("\"")) op = Suppress(Word("{")) cl = Suppress(Word("}")) opp = Suppress(Word("(")) clp = Suppress(Word(")")) identifier = Word( alphas+"_", alphanums+"_" ) commIdentifier = Group(identifier.setResultsName('identifier') + Optional(opp + (CaselessLiteral("ice")|CaselessLiteral("ros")).setResultsName("type") + clp)) # Imports idslImport = Suppress(CaselessLiteral("import")) + quote + CharsNotIn("\";").setResultsName('path') + quote + semicolon idslImports = ZeroOrMore(idslImport) # Communications implementsList = Group(CaselessLiteral('implements') + identifier + ZeroOrMore(Suppress(Word(',')) + identifier) + semicolon) requiresList = Group(CaselessLiteral('requires') + identifier + ZeroOrMore(Suppress(Word(',')) + identifier) + semicolon) subscribesList = Group(CaselessLiteral('subscribesTo') + commIdentifier + ZeroOrMore(Suppress(Word(',')) + commIdentifier) + semicolon) publishesList = Group(CaselessLiteral('publishes') + identifier + ZeroOrMore(Suppress(Word(',')) + identifier) + semicolon) communicationList = implementsList | requiresList | subscribesList | publishesList communications = Group( Suppress(CaselessLiteral("communications")) + op + ZeroOrMore(communicationList) + cl + semicolon) # Language language = Suppress(CaselessLiteral("language")) + (CaselessLiteral("cpp")|CaselessLiteral("python")) + semicolon # GUI gui = Group(Optional(Suppress(CaselessLiteral("gui")) + CaselessLiteral("Qt") + opp + identifier + clp + semicolon )) # additional options options = Group(Optional(Suppress(CaselessLiteral("options")) + identifier + ZeroOrMore(Suppress(Word(',')) + identifier) + semicolon)) componentContents = communications.setResultsName('communications') & language.setResultsName('language') & gui.setResultsName('gui') & options.setResultsName('options') component = Suppress(CaselessLiteral("component")) + identifier.setResultsName("name") + op + componentContents.setResultsName("properties") + cl + semicolon CDSL = idslImports.setResultsName("imports") + component.setResultsName("component") CDSL.ignore( cppStyleComment ) tree = CDSL.parseString(text) return CDSLParsing.component(tree)
def parse_ttable(f): ttable = {} # com = Suppress('[') + ZeroOrMore(CharsNotIn(']')) + Suppress(']') com = Suppress('[' + ZeroOrMore(CharsNotIn(']') + ']')) while True: s = next(f).strip() if not s: continue s = com.transformString(s).strip() if s.lower() == ";": break b = False if s[-1] in ",;": if s[-1] == ';': b = True s = s[:-1] # print(s) k, v = s.split() ttable[k] = v if b: break return ttable
def pythonVar(self): if not self._pythonVar: from pyparsing import (ParserElement, Word, alphas, alphanums, Literal, Suppress, FollowedBy) _ws = ' \t' ParserElement.setDefaultWhitespaceChars(_ws) ident = Word(alphas+"_", alphanums+"_") lparen = Literal("(") dot = Literal(".") dollar = Literal("$") self._pythonVar = Suppress(dollar) + ident + ~FollowedBy((dot+ident) | lparen) self._pythonVar.setParseAction(self.onPythonVar) return self._pythonVar
def __init__(self, datatype): if datatype == "protein": letters = "ARNDCQEGHILKMFPSTWYV" elif datatype == "DNA": letters = "ATCG" else: log.error("Unknown datatype '%s', please check" % datatype) raise RaxmlError FLOAT = Word(nums + '.-').setParseAction(lambda x: float(x[0])) L = Word(letters, exact=1) COLON = Suppress(":") LNL_LABEL = Regex("Final GAMMA.+:") | Literal("Likelihood:") TIME_LABEL = Regex("Overall Time.+:") | Regex("Overall Time.+tion ") ALPHA_LABEL = Literal("alpha:") TREE_SIZE_LABEL = Literal("Tree-Length:") def labeled_float(label): return Suppress(SkipTo(label)) + Suppress(label) + FLOAT lnl = labeled_float(LNL_LABEL) lnl.setParseAction(self.set_lnl) seconds = labeled_float(TIME_LABEL) seconds.setParseAction(self.set_seconds) alpha = labeled_float(ALPHA_LABEL) alpha.setParseAction(self.set_alpha) tree_size = labeled_float(TREE_SIZE_LABEL) tree_size.setParseAction(self.set_tree_size) rate = Suppress("rate") + L + Suppress("<->") + L + COLON + FLOAT rate.setParseAction(self.set_rate) rates = OneOrMore(rate) freq = Suppress("freq pi(") + L + Suppress("):") + FLOAT freq.setParseAction(self.set_freq) freqs = OneOrMore(freq) # Just look for these things self.root_parser = seconds + lnl + alpha + tree_size + rates + freqs
def ListParser(): """ A parser for list columns, where each list is composed of pairs of values. """ value = Regex(r'[-+]?[0-9]+(?:\.[0-9]*)?(?:e[-+]?[0-9]+)?', IGNORECASE) value.setParseAction(lambda toks: float(toks[0])) item = Suppress('(') + value + Suppress(',') + value + Suppress(')') item.setParseAction(tuple) lst = Suppress('[') + delimitedList(item) + Suppress(']') lst.setParseAction(list) def parse(s): try: return lst.parseString(s).asList() except ParseBaseException as e: raise ValueError(e) return parse
def mark_transposed(tokens): tokens[0].setTransposed(True) return tokens # What follows is a Pyparsing description of the grammar symbol = Word(alphanums, bodyChars=alphanums + "_", min=1) sign = Optional(oneOf("+ -")) integer = Combine(sign + Word(nums)).setParseAction(lambda t: int(t[0])) number = Combine( Word("+-" + nums, nums) + Optional("." + Optional(Word(nums))) + Optional(oneOf("e E") + Word("+-" + nums, nums))).setParseAction(lambda t: float(t[0])) LPAREN = Suppress("(") RPAREN = Suppress(")") LBRACE = Suppress("{") RBRACE = Suppress("}") LBRACKET = Suppress("[") RBRACKET = Suppress("]") END = Suppress(";") PLUS = Literal("+") MINUS = Literal("-") single = number ^ symbol | QuotedString('"') | QuotedString("'") tuple_ = Group(LPAREN + delimitedList(single) + RPAREN) subscript_domain = (LBRACE + Group(delimitedList(symbol)).setResultsName("subscripts") + RBRACE)
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) def safe_convert_number(tokens): n = tokens[0] try: return int(n, 10) except ValueError: try: return float(n) except ValueError: return n def convert_period(tokens): period_value = int(tokens.value) period_identifier = tokens.unit period_unit = next((single_unit for single_unit, values in cls.get_supported_period_type_map().items() if period_identifier in values)) return period(period_value, period_unit) # ${path} or ${?path} for optional substitution SUBSTITUTION_PATTERN = r"\$\{(?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()) @contextlib.contextmanager def set_default_white_spaces(): default = ParserElement.DEFAULT_WHITE_CHARS ParserElement.setDefaultWhitespaceChars(' \t') yield ParserElement.setDefaultWhitespaceChars(default) with set_default_white_spaces(): 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 + '._- /') regexp_numbers = r'[+-]?(\d*\.\d+|\d+(\.\d+)?)([eE][+\-]?\d+)?(?=$|[ \t]*([\$\}\],#\n\r]|//))' key = QuotedString('"', escChar='\\', unquoteResults=False) | \ Regex(regexp_numbers, re.DOTALL).setParseAction(safe_convert_number) | \ 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(regexp_numbers, re.DOTALL).setParseAction(convert_number) period_types = itertools.chain.from_iterable( cls.get_supported_period_type_map().values()) period_expr = Regex(r'(?P<value>\d+)\s*(?P<unit>' + '|'.join(period_types) + ')$').setParseAction(convert_period) # 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( r'"(?:[^"\\\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( r'(?:[^^`+?!@*&"\[\{\s\]\}#,=\$\\]|\\.)+[ \t]*', re.UNICODE).setParseAction(unescape_string) substitution_expr = Regex(r'[ \t]*\$\{[^\}]+\}[ \t]*' ).setParseAction(create_substitution) string_expr = multiline_string | quoted_string | unquoted_string value_expr = period_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
def parse_algebra(self): """ Parse an algebraic expression into a tree. Store a `pyparsing.ParseResult` in `self.tree` with proper groupings to reflect parenthesis and order of operations. Leave all operators in the tree and do not parse any strings of numbers into their float versions. Adding the groups and result names makes the `repr()` of the result really gross. For debugging, use something like print OBJ.tree.asXML() """ # 0.33 or 7 or .34 or 16. number_part = Word(nums) inner_number = (number_part + Optional("." + Optional(number_part))) | ("." + number_part) # pyparsing allows spaces between tokens--`Combine` prevents that. inner_number = Combine(inner_number) # SI suffixes and percent. number_suffix = MatchFirst(Literal(k) for k in SUFFIXES.keys()) # 0.33k or 17 plus_minus = Literal('+') | Literal('-') number = Group( Optional(plus_minus) + inner_number + Optional( CaselessLiteral("E") + Optional(plus_minus) + number_part) + Optional(number_suffix)) number = number("number") # Predefine recursive variables. expr = Forward() # Handle variables passed in. They must start with a letter # and may contain numbers and underscores afterward. inner_varname = Combine( Word(alphas, alphanums + "_") + ZeroOrMore("'")) # Alternative variable name in tensor format # Tensor name must start with a letter, continue with alphanums # Indices may be alphanumeric # e.g., U_{ijk}^{123} upper_indices = Literal("^{") + Word(alphanums) + Literal("}") lower_indices = Literal("_{") + Word(alphanums) + Literal("}") tensor_lower = Combine( Word(alphas, alphanums) + lower_indices + ZeroOrMore("'")) tensor_mixed = Combine( Word(alphas, alphanums) + Optional(lower_indices) + upper_indices + ZeroOrMore("'")) # Test for mixed tensor first, then lower tensor alone, then generic variable name varname = Group(tensor_mixed | tensor_lower | inner_varname)("variable") varname.setParseAction(self.variable_parse_action) # Same thing for functions. function = Group(inner_varname + Suppress("(") + expr + Suppress(")"))("function") function.setParseAction(self.function_parse_action) atom = number | function | varname | "(" + expr + ")" atom = Group(atom)("atom") # Do the following in the correct order to preserve order of operation. pow_term = atom + ZeroOrMore("^" + atom) pow_term = Group(pow_term)("power") par_term = pow_term + ZeroOrMore('||' + pow_term) # 5k || 4k par_term = Group(par_term)("parallel") prod_term = par_term + ZeroOrMore( (Literal('*') | Literal('/')) + par_term) # 7 * 5 / 4 prod_term = Group(prod_term)("product") sum_term = Optional(plus_minus) + prod_term + ZeroOrMore( plus_minus + prod_term) # -5 + 4 - 3 sum_term = Group(sum_term)("sum") # Finish the recursion. expr << sum_term # pylint: disable=pointless-statement self.tree = (expr + stringEnd).parseString(self.math_expr)[0]
def define_parser(self): """ Defines xdot grammar. @see: http://graphviz.org/doc/info/output.html#d:xdot """ # Common constructs. point = Group( integer.setResultsName("x") + integer.setResultsName("y")) n_points = (integer.setResultsName("n") + OneOrMore(point).setResultsName("points")) n_bytes = Suppress(integer) + Suppress(minus) + \ Word(printables).setResultsName("b") justify = ToInteger(Literal("-1") | Literal("0") | Literal("1")).setResultsName("j") # Attributes ---------------------------------------------------------- # Set fill color. The color value consists of the n bytes following # the '-'. fill = (Literal("C").suppress() + Suppress(integer) + Suppress(minus) + colour.setResultsName("color")).setResultsName("fill") # Set pen color. The color value consists of the n bytes following '-'. stroke = (Literal("c").suppress() + Suppress(integer) + Suppress(minus) + colour.setResultsName("color")).setResultsName("stroke") # Set font. The font size is s points. The font name consists of the # n bytes following '-'. font = (Literal("F").suppress() + real.setResultsName("s") + n_bytes).setResultsName("font") # Set style attribute. The style value consists of the n bytes # following '-'. The syntax of the value is the same as specified for # a styleItem in style. style = (Literal("S").suppress() + n_bytes).setResultsName("style") # Shapes -------------------------------------------------------------- # Filled ellipse ((x-x0)/w)^2 + ((y-y0)/h)^2 = 1 filled_ellipse = ( Literal("E").suppress() + integer.setResultsName("x0") + integer.setResultsName("y0") + integer.setResultsName("w") + integer.setResultsName("h")).setResultsName("filled_ellipse") # Unfilled ellipse ((x-x0)/w)^2 + ((y-y0)/h)^2 = 1 ellipse = (Literal("e").suppress() + integer.setResultsName("x0") + integer.setResultsName("y0") + integer.setResultsName("w") + integer.setResultsName("h")).setResultsName("ellipse") # Filled polygon using the given n points. filled_polygon = (Literal("P").suppress() + n_points).setResultsName("filled_polygon") # Unfilled polygon using the given n points. polygon = (Literal("p").suppress() + n_points).setResultsName("polygon") # Polyline using the given n points. polyline = (Literal("L").suppress() + n_points).setResultsName("polyline") # B-spline using the given n control points. bspline = (Literal("B").suppress() + n_points).setResultsName("bspline") # Filled B-spline using the given n control points. filled_bspline = (Literal("b").suppress() + n_points).setResultsName("filled_bspline") # Text drawn using the baseline point (x,y). The text consists of the # n bytes following '-'. The text should be left-aligned (centered, # right-aligned) on the point if j is -1 (0, 1), respectively. The # value w gives the width of the text as computed by the library. text = (Literal("T").suppress() + integer.setResultsName("x") + integer.setResultsName("y") + justify + integer.setResultsName("w") + n_bytes).setResultsName("text") # Externally-specified image drawn in the box with lower left corner # (x,y) and upper right corner (x+w,y+h). The name of the image # consists of the n bytes following '-'. This is usually a bitmap # image. Note that the image size, even when converted from pixels to # points, might be different from the required size (w,h). It is # assumed the renderer will perform the necessary scaling. image = (Literal("I").suppress() + integer.setResultsName("x") + integer.setResultsName("y") + integer.setResultsName("w") + integer.setResultsName("h") + n_bytes).setResultsName("image") # The value of the drawing attributes consists of the concatenation of # some (multi-)set of the 13 rendering or attribute operations. value = (Optional(quote).suppress() + OneOrMore(filled_ellipse | ellipse | filled_polygon | polygon | polyline | bspline | filled_bspline | text | fill | stroke | font | style | image) + Optional(quote).suppress()).setResultsName("value") # Drawing operation. # draw_ = Literal("_draw_") + Suppress(equals) + value # # Label drawing. # ldraw_ = Literal("_ldraw_") + Suppress(equals) + value # # Edge head arrowhead drawing. # hdraw_ = Literal("_hdraw_") + Suppress(equals) + value # # Edge tail arrowhead drawing. # tdraw_ = Literal("_tdraw_") + Suppress(equals) + value # # Edge head label drawing. # hldraw_ = Literal("_hldraw_") + Suppress(equals) + value # # Edge tail label drawing. # tldraw_ = Literal("_tldraw_") + Suppress(equals) + value # Parse actions. # n_points.setParseAction(self.proc_points) # Attribute parse actions. fill.setParseAction(self.proc_fill_color) stroke.setParseAction(self.proc_stroke_color) font.setParseAction(self.proc_font) style.setParseAction(self.proc_style) # Shape parse actions. filled_ellipse.setParseAction(self.proc_filled_ellipse) ellipse.setParseAction(self.proc_unfilled_ellipse) filled_polygon.setParseAction(self.proc_filled_polygon) polygon.setParseAction(self.proc_unfilled_polygon) polyline.setParseAction(self.proc_polyline) bspline.setParseAction(self.proc_unfilled_bspline) filled_bspline.setParseAction(self.proc_filled_bspline) text.setParseAction(self.proc_text) image.setParseAction(self.proc_image) return value
from pyparsing import Word, nums, oneOf, Suppress, restOfLine, alphas, alphanums, Group, ZeroOrMore, Optional, Keyword # Parses Spring log lines (logs are taken from ENBD project) log_sample = ''' 2016-08-01 11:27:21.047 WARN 22458 --- [http-bio-8080-exec-24] c.m.enbd.core.AbstractEnbdController : POST /goal/daily: User haven't defined a goal yet 2016-08-01 17:54:47.908 INFO 22458 --- [http-bio-8080-exec-27] com.monitise.enbd.core.RequestFilter : Handling request, /version, POST 2016-08-01 17:54:47.909 INFO 22458 --- [http-bio-8080-exec-30] com.monitise.enbd.core.RequestFilter : Handling request, /features, POST 2016-08-01 17:54:47.914 DEBUG 22458 --- [http-bio-8080-exec-30] c.m.enbd.core.AbstractEnbdController : getting IOS features 2016-08-01 17:54:47.924 INFO 22458 --- [http-bio-8080-exec-27] c.m.enbd.core.AbstractEnbdController : checking version for device code: 1 and device OS: 10.0 and version: 1.0.3''' date = Word(nums + '-') time = Word(nums + ':.') level = Keyword('INFO') | Keyword('WARN') | Keyword('DEBUG') # oneOf(['INFO', 'WARN', 'DEBUG']) pid = Word(nums) thread = Suppress('[') + Word(alphanums + '-') + Suppress(']') source = Word(alphas + '.') message = restOfLine log_line = Group(date + time + level + pid + Suppress('---') + thread + source + Suppress(':') + message) logs = ZeroOrMore(log_line) print(logs.parseString(log_sample)) # So, if you design log strings that can be parsed, you can actually parse them
def get_fragment_grammar(sdkconfig, fragment_file): # Match header [mapping] header = Suppress('[') + Suppress('mapping') + Suppress(']') # There are three possible patterns for mapping entries: # obj:symbol (scheme) # obj (scheme) # * (scheme) obj = Fragment.ENTITY.setResultsName('object') symbol = Suppress(':') + Fragment.IDENTIFIER.setResultsName('symbol') scheme = Suppress('(') + Fragment.IDENTIFIER.setResultsName( 'scheme') + Suppress(')') pattern1 = Group(obj + symbol + scheme) pattern2 = Group(obj + scheme) pattern3 = Group(Literal(Entity.ALL).setResultsName('object') + scheme) mapping_entry = pattern1 | pattern2 | pattern3 # To simplify parsing, classify groups of condition-mapping entry into two types: normal and default # A normal grouping is one with a non-default condition. The default grouping is one which contains the # default condition mapping_entries = Group( ZeroOrMore(mapping_entry)).setResultsName('mappings') normal_condition = Suppress(':') + originalTextFor( SDKConfig.get_expression_grammar()) default_condition = Optional( Suppress(':') + Literal(DeprecatedMapping.DEFAULT_CONDITION)) normal_group = Group( normal_condition.setResultsName('condition') + mapping_entries) default_group = Group(default_condition + mapping_entries).setResultsName('default_group') normal_groups = Group( ZeroOrMore(normal_group)).setResultsName('normal_groups') # Any mapping fragment definition can have zero or more normal group and only one default group as a last entry. archive = Suppress('archive') + Suppress( ':') + Fragment.ENTITY.setResultsName('archive') entries = Suppress('entries') + Suppress(':') + ( normal_groups + default_group).setResultsName('entries') mapping = Group(header + archive + entries) mapping.ignore('#' + restOfLine) def parsed_deprecated_mapping(pstr, loc, toks): fragment = Mapping() fragment.archive = toks[0].archive fragment.name = re.sub(r'[^0-9a-zA-Z]+', '_', fragment.archive) fragment.deprecated = True fragment.entries = set() condition_true = False for entries in toks[0].entries[0]: condition = next(iter(entries.condition.asList())).strip() condition_val = sdkconfig.evaluate_expression(condition) if condition_val: for entry in entries[1]: fragment.entries.add( (entry.object, None if entry.symbol == '' else entry.symbol, entry.scheme)) condition_true = True break if not fragment.entries and not condition_true: try: entries = toks[0].entries[1][1] except IndexError: entries = toks[0].entries[1][0] for entry in entries: fragment.entries.add( (entry.object, None if entry.symbol == '' else entry.symbol, entry.scheme)) if not fragment.entries: fragment.entries.add(('*', None, 'default')) dep_warning = str( ParseFatalException( pstr, loc, 'Warning: Deprecated old-style mapping fragment parsed in file %s.' % fragment_file)) print(dep_warning) return fragment mapping.setParseAction(parsed_deprecated_mapping) return mapping
headerList[headerName] = structt.asDict() def addToAnnotationDict(annotateName, annotate): s = annotate[1] # first remove the quotes if s.startswith("'''"): s = s[3:-3] if s.startswith('"'): s = s[1:-1] annotateDict[annotate['name']] = s comment = '## ' + restOfLine #comment = '#' + restOfLine CMNT = Optional(cStyleComment("comment")) CMNT2 = Optional( (Suppress('//') + restOfLine("comment2"))) #Optional(cppStyleComment("comment2")) STRQ3 = QuotedString("'''", multiline=True) ANNOTSTR = (QuotedString("'''", multiline=True) | quotedString) #IDENTIFIER = Regex(r'[a-zA-Z_][a-zA-Z_0-9]*') #INTEGER = Regex(r'([+-]?(([1-9][0-9]*)|0+))') #IDENTIFIER = Word(alphas+"_", alphas+nums+"_" ) INT_DECI = Regex('([+-]?(([1-9][0-9]*)|0+))') INT_OCT = Regex('(0[0-7]*)') INT_HEX = Regex('(0[xX][0-9a-fA-F]*)') INT = INT_HEX | INT_OCT | INT_DECI FLOAT = Regex( '[+-]?(((\d+\.\d*)|(\d*\.\d+))([eE][-+]?\d+)?)|(\d*[eE][+-]?\d+)') SIZE = INT #VARNAME = IDENTIFIER ##ident = Word(alphas+"_",alphanums+"_").setName("identifier")
DOUBLE = Regex(r"[0-9]+\.[0-9]*%(e)s|\.([0-9])+%(e)s|[0-9]+%(e)s" % {"e": EXPONENT_re}) # DOUBLE.setResultsName('double') DOUBLE.setParseAction(lambda x: rdflib.Literal(x[0], datatype=rdflib.XSD.double)) # [149] INTEGER_POSITIVE ::= '+' INTEGER INTEGER_POSITIVE = Suppress("+") + INTEGER.copy().leaveWhitespace() # [150] DECIMAL_POSITIVE ::= '+' DECIMAL DECIMAL_POSITIVE = Suppress("+") + DECIMAL.copy().leaveWhitespace() # [151] DOUBLE_POSITIVE ::= '+' DOUBLE DOUBLE_POSITIVE = Suppress("+") + DOUBLE.copy().leaveWhitespace() # [152] INTEGER_NEGATIVE ::= '-' INTEGER INTEGER_NEGATIVE = Suppress("-") + INTEGER.copy().leaveWhitespace() INTEGER_NEGATIVE.setParseAction(lambda x: neg(x[0])) # [153] DECIMAL_NEGATIVE ::= '-' DECIMAL DECIMAL_NEGATIVE = Suppress("-") + DECIMAL.copy().leaveWhitespace() DECIMAL_NEGATIVE.setParseAction(lambda x: neg(x[0])) # [154] DOUBLE_NEGATIVE ::= '-' DOUBLE DOUBLE_NEGATIVE = Suppress("-") + DOUBLE.copy().leaveWhitespace() DOUBLE_NEGATIVE.setParseAction(lambda x: neg(x[0])) # [160] ECHAR ::= '\' [tbnrf\"'] # ECHAR = Regex('\\\\[tbnrf"\']') # [158] STRING_LITERAL_LONG1 ::= "'''" ( ( "'" | "''" )? ( [^'\] | ECHAR ) )* "'''"
def read_dot(path): """Generates an alias.ArgumentationFramework from a DOT graph description (.dot) file. Parameters ---------- path : file or string File, directory or filename to be read. Returns ------- framework : alias ArgumentationFramework Examples -------- References ---------- """ try: from pyparsing import Word, Literal, alphas, alphanums, nums, OneOrMore from pyparsing import Forward, Optional, Keyword, Group, Suppress, ParseException except ImportError: raise ImportError("read_dot requires pyparsing") if not isinstance(path, str): return # Define DOT grammar # Punctuation and keywords LBR, RBR, SCOL, COMMA, EQ, LSQ, RSQ, COL = map(Literal, "{};,=[]:") strict, graph, digraph, node, edge, subgraph = map( Keyword, "strict graph digraph node edge subgraph".split()) # Recursive rules stmt_list = Forward() a_list = Forward() edgeRHS = Forward() attr_list = Forward() comp = (Literal("n") | Literal("ne") | Literal("e") | Literal("se") | Literal("s") | Literal("sw") | Literal("w") | Literal("nw") | Literal("c") | Literal("_")).setName("comp") ID = Word(alphas, alphanums).setName("ID") edgeop = Suppress(Literal("--") | Literal("->")) port = (COL + ID + Optional((COL + comp))) | (COL + comp) node_id = ID + Optional(port) subg = (subgraph + Optional(ID)) + LBR + stmt_list + RBR edgeRHS << OneOrMore(edgeop + (node_id | subg)) # Statements & attributes node_stmt = node_id("arg*") + Optional(attr_list) edge_stmt = ((node_id | subg) + edgeRHS)("att*") + Optional(attr_list) attr_stmt = (graph | node | edge) + attr_list a_list << OneOrMore(ID + EQ + ID + Optional(SCOL | COMMA)) attr_list << OneOrMore((LSQ + Optional(a_list) + RSQ)) stmt = (edge_stmt | node_stmt | attr_stmt | (ID + EQ + ID) | subg) stmt_list << OneOrMore(stmt + Optional(SCOL)) dot = OneOrMore( Optional(strict) + (digraph | graph)("gtype") + Optional(ID)("gname") + LBR + stmt_list + RBR) f = open(path, 'r') f = f.read() try: parsed = dot.parseString(f) except ParseException as e: raise al.ParsingException(e) if parsed['gtype'] == 'graph': raise al.ParsingException('Graph must be a directed graph (digraph).') if 'gname' in parsed.keys(): framework = al.ArgumentationFramework(parsed['gname']) else: head, tail = ntpath.split(path) framework = al.ArgumentationFramework(tail) if 'arg' in parsed.keys(): for arg in parsed['arg']: framework.add_argument(arg) if 'att' in parsed.keys(): for edge in parsed['att']: prev = -1 for curr in edge: if prev >= 0: framework.add_attack((edge[prev], curr)) prev = prev + 1 return framework
def initialize(self): ParserElement.setDefaultWhitespaceChars(' \t\r') integer = Regex(r"[+-]?\d+") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) number = Regex(r"[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?") \ .setParseAction(lambda s,l,t: [ float(t[0]) ]) color = Regex(r"#([0-9a-fA-F]{6})") angle = "'" + Regex(r"(360|3[0-5][0-9]|[12][0-9]{2}|[0-9]{1,2})") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) alpha = "'" + Regex(r"(360|3[0-5][0-9]|[12][0-9]{2}|[0-9]{1,2})") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) variable = Word(alphas, exact=1).setParseAction(self.addVar) colon = Literal(":").suppress() comma = Literal(",") lBrace = Literal("(") rBrace = Literal(")") lBracket = Literal("[") rBracket = Literal("]") lAngle = Literal("<") rAngle = Literal(">") plus = Literal("+") minus = Literal("-") FTerm = Literal("F") fTerm = Literal("f") ZTerm = Literal("Z") zTerm = Literal("z") xTerm = Literal("x") cTerm = Literal("c") eol = OneOrMore(LineEnd()).suppress() param = ( angle | color | "!" + number | "|" + number ) self.pList = lBrace + param + ZeroOrMore(comma + param) + rBrace literal = ((lBracket + ( variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList) ) + rBracket) | (variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList))) terminal = (ZTerm | zTerm | FTerm | fTerm | xTerm | cTerm | plus | minus | lBracket | rBracket) lprod = ( (OneOrMore(terminal) + lAngle + variable + rAngle + OneOrMore(terminal)) | (OneOrMore(terminal) + lAngle + variable) | (variable + rAngle + OneOrMore(terminal)) | variable ) rProd = OneOrMore(literal | terminal) comment = Suppress((LineStart() + "#" + SkipTo(eol, include=True))) rules = ( (lprod + Literal("=") + rProd + eol).setParseAction(self.addRule) \ | comment ) defaults = ( ( ("Dimensions" + colon + integer + comma + integer) | ("Position" + colon + integer + comma + integer) | ("Iterations" + colon + integer) | ("Angle" + colon + angle) | ("Linelength" + colon + number) | ("Linewidth" + colon + number) | ("Linecolor" + colon + color) | ("Background" + colon + color) | ("Axiom" + colon + rProd) ) + eol ).setParseAction(self.setAttribute) header = ( defaults | comment ) self.grammar = Suppress(ZeroOrMore(LineEnd())) \ + ZeroOrMore(header) \ + OneOrMore(rules) try: L = self.grammar.parseString( self.stream ) except ParseException, err: print err.line print " "*(err.column-1) + "^" print err
def convertDict(d): return dict(d[0]) def convertTuple(t): return tuple(t[0]) omcRecord = Forward() omcValue = Forward() TRUE = Keyword("true").setParseAction(replaceWith(True)) FALSE = Keyword("false").setParseAction(replaceWith(False)) NONE = (Keyword("NONE") + Suppress("(") + Suppress(")")).setParseAction( replaceWith(None)) SOME = (Suppress(Keyword("SOME")) + Suppress("(") + omcValue + Suppress(")")) omcString = QuotedString(quoteChar='"', escChar='\\', multiline=True).setParseAction(convertString) omcNumber = Combine( Optional('-') + ('0' | Word('123456789', nums)) + Optional('.' + Word(nums)) + Optional(Word('eE', exact=1) + Word(nums + '+-', nums))) ident = Word(alphas + "_", alphanums + "_") | Combine( "'" + Word(alphanums + "!#$%&()*+,-./:;<>=?@[]^{}|~ ") + "'") fqident = Forward() fqident << ((ident + "." + fqident) | ident) omcValues = delimitedList(omcValue)
def _create_parser(self): semicolon = Suppress(Word(";")) quote = Suppress(Word("\"")) op = Suppress(Word("{")) cl = Suppress(Word("}")) opp = Suppress(Word("(")) clp = Suppress(Word(")")) lt = Suppress(Word("<")) gt = Suppress(Word(">")) eq = Suppress(Word("=")) identifier = Word(alphas + "_", alphanums + "_") typeIdentifier = Word(alphas + "_", alphanums + "_:") structIdentifer = Group( typeIdentifier.setResultsName('type') + identifier.setResultsName('identifier') + Optional(eq) + Optional( CharsNotIn(";").setResultsName('defaultValue')) + semicolon) structIdentifers = Group(OneOrMore(structIdentifer)) ## Imports idslImport = Suppress(Word("import")) + quote + CharsNotIn("\";").setResultsName('path') + quote + semicolon idslImports = ZeroOrMore(idslImport) structDef = Word("struct").setResultsName('type') + identifier.setResultsName( 'name') + op + structIdentifers.setResultsName("structIdentifiers") + cl + semicolon dictionaryDef = Word("dictionary").setResultsName('type') + lt + CharsNotIn("<>").setResultsName( 'content') + gt + identifier.setResultsName('name') + semicolon sequenceDef = Word("sequence").setResultsName('type') + lt + typeIdentifier.setResultsName( 'typeSequence') + gt + identifier.setResultsName('name') + semicolon enumDef = Word("enum").setResultsName('type') + identifier.setResultsName('name') + op + CharsNotIn( "{}").setResultsName('content') + cl + semicolon exceptionDef = Word("exception").setResultsName('type') + identifier.setResultsName('name') + op + CharsNotIn( "{}").setResultsName('content') + cl + semicolon raiseDef = Suppress(Word("throws")) + typeIdentifier + ZeroOrMore(Literal(',') + typeIdentifier) decoratorDef = Literal('idempotent') | Literal('out') retValDef = typeIdentifier.setResultsName('ret') firstParam = Group(Optional(decoratorDef.setResultsName('decorator')) + typeIdentifier.setResultsName( 'type') + identifier.setResultsName('name')) nextParam = Suppress(Word(',')) + firstParam params = firstParam + ZeroOrMore(nextParam) remoteMethodDef = Group(Optional(decoratorDef.setResultsName('decorator')) + retValDef.setResultsName( 'ret') + typeIdentifier.setResultsName('name') + opp + Optional(params).setResultsName( 'params') + clp + Optional(raiseDef.setResultsName('raise')) + semicolon) interfaceDef = Word('interface').setResultsName('type') + typeIdentifier.setResultsName('name') + op + Group( ZeroOrMore(remoteMethodDef)).setResultsName('methods') + cl + semicolon moduleContent = Group(structDef | enumDef | exceptionDef | dictionaryDef | sequenceDef | interfaceDef) module = Suppress(Word("module")) + identifier.setResultsName("name") + op + ZeroOrMore( moduleContent).setResultsName("contents") + cl + semicolon IDSL = idslImports.setResultsName("imports") + module.setResultsName("module") IDSL.ignore(cppStyleComment) return IDSL
def parse_line(attribute, string): Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=')) + String('data') + Suppress(Literal(';') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() return [int_else_float_except_string(s) for s in result['data'].asList()]
def _sql_comment(expr): return lineStart + Suppress(Literal("-- ")) + expr
from __future__ import print_function, absolute_import import os from builtins import open import logging import numpy as np from pyparsing import Word, nums, alphanums, LineEnd, Suppress, Literal, restOfLine, OneOrMore, Optional, Keyword, Group, printables from ...utils import int_else_float_except_string logger = logging.getLogger(__file__) Float = Word(nums + '.' + '-' + '+' + 'e') Name = Word(alphanums) String = Optional(Suppress("'")) + Word(printables, alphanums) + Optional(Suppress("'")) NL = LineEnd() Comments = Suppress(Literal('%')) + restOfLine def parse_file(attribute, string): if attribute in ['gen', 'gencost', 'bus', 'branch'] and attribute in string: return parse_table(attribute, string) elif attribute in ['version', 'baseMVA'] and attribute in string: return parse_line(attribute, string) else: logger.debug("Unable to parse mpc.%s. Please check the input file or contact the developer.", attribute) return None def parse_line(attribute, string):
from pyparsing import (CharsNotIn, Optional, Suppress, Word, Regex, ParseException, alphas, nums) from angela2.utils.caching import cached VARIABLE = Word(alphas + '_', alphas + nums + '_').setResultsName('variable') OP = Regex(r'(\+|\-|\*|/|//|%|\*\*|>>|<<|&|\^|\|)?=').setResultsName( 'operation') EXPR = CharsNotIn('#').setResultsName('expression') COMMENT = CharsNotIn('#').setResultsName('comment') STATEMENT = VARIABLE + OP + EXPR + Optional(Suppress('#') + COMMENT) @cached def parse_statement(code): ''' parse_statement(code) Parses a single line of code into "var op expr". Parameters ---------- code : str A string containing a single statement of the form ``var op expr # comment``, where the ``# comment`` part is optional. Returns ------- var, op, expr, comment : str, str, str, str The four parts of the statement.
# # MS-GRAMMAR: lifecycle-handler-name = "Class_Initialize" / "Class_Terminate" lifecycle_handler_name = CaselessKeyword("Class_Initialize") | CaselessKeyword( "Class_Terminate") implemented_name = identifier # duplicate, not used event_handler_name = identifier # duplicate, not used prefixed_name = identifier | lifecycle_handler_name # simplified subroutine_name = identifier | lifecycle_handler_name # simplified # MS-GRAMMAR: typed_name = identifier + optional type_suffix => simplified # MS-GRAMMAR: function_name = typed_name | subroutine_name function_name = Combine( identifier + Suppress(Optional(type_suffix))) | lifecycle_handler_name # 5.3.1 Procedure Declarations # # MS-GRAMMAR: end-label = statement-label-definition #end_label = statement_label_definition # MS-GRAMMAR: procedure-tail = [WS] LINE-END / single-quote comment-body / ":" rem-statement procedure_tail = FollowedBy( line_terminator) | comment_single_quote | Literal(":") + rem_statement # NOTE: rem statement does NOT include the line terminator => BUG? # TODO: here i assume that procedure tail does NOT include the line terminator
['%s:%s' % (k, self.attrs[k]) for k in sorted(self.attrs)]) return 'attr(%s)' % sattrs def __repr__(self): rattrs = ",".join( ['%r:%r' % (k, self.attrs[k]) for k in sorted(self.attrs)]) return 'Attr({%s})' % rattrs @staticmethod def parse_action(s, loc, tokens): where = W(s, loc) attrs = tokens.get('attrs', None) # Python 3 only # attrs = {k: v for k, v in attrs.items()} attrs = dict([(k, v) for k, v in attrs.items()]) return Attr(attrs, where=where) attr_spec = Dict( delimitedList(Group( Word(alphanums) + Suppress(Literal(':')) + contract_expression('value')), delim=';'))('attrs') attrs_spec = ('(' - attr_spec - ')') attr_contract = Keyword('attr') - attrs_spec attr_contract.setParseAction(Attr.parse_action) add_contract(attr_contract) add_keyword('attr')
def __init__(self, fragment_file, sdkconfig): try: fragment_file = open(fragment_file, 'r') except TypeError: pass path = os.path.realpath(fragment_file.name) indent_stack = [1] class parse_ctx: fragment = None # current fragment key = '' # current key keys = list() # list of keys parsed key_grammar = None # current key grammar @staticmethod def reset(): parse_ctx.fragment_instance = None parse_ctx.key = '' parse_ctx.keys = list() parse_ctx.key_grammar = None def fragment_type_parse_action(toks): parse_ctx.reset() parse_ctx.fragment = FRAGMENT_TYPES[ toks[0]]() # create instance of the fragment return None def expand_conditionals(toks, stmts): try: stmt = toks['value'] stmts.append(stmt) except KeyError: try: conditions = toks['conditional'] for condition in conditions: try: _toks = condition[1] _cond = condition[0] if sdkconfig.evaluate_expression(_cond): expand_conditionals(_toks, stmts) break except IndexError: expand_conditionals(condition[0], stmts) except KeyError: for tok in toks: expand_conditionals(tok, stmts) def key_body_parsed(pstr, loc, toks): stmts = list() expand_conditionals(toks, stmts) if parse_ctx.key_grammar.min and len( stmts) < parse_ctx.key_grammar.min: raise ParseFatalException( pstr, loc, "fragment requires at least %d values for key '%s'" % (parse_ctx.key_grammar.min, parse_ctx.key)) if parse_ctx.key_grammar.max and len( stmts) > parse_ctx.key_grammar.max: raise ParseFatalException( pstr, loc, "fragment requires at most %d values for key '%s'" % (parse_ctx.key_grammar.max, parse_ctx.key)) try: parse_ctx.fragment.set_key_value(parse_ctx.key, stmts) except Exception as e: raise ParseFatalException( pstr, loc, "unable to add key '%s'; %s" % (parse_ctx.key, str(e))) return None key = Word(alphanums + '_') + Suppress(':') key_stmt = Forward() condition_block = indentedBlock(key_stmt, indent_stack) key_stmts = OneOrMore(condition_block) key_body = Suppress(key) + key_stmts key_body.setParseAction(key_body_parsed) condition = originalTextFor( SDKConfig.get_expression_grammar()).setResultsName('condition') if_condition = Group( Suppress('if') + condition + Suppress(':') + condition_block) elif_condition = Group( Suppress('elif') + condition + Suppress(':') + condition_block) else_condition = Group( Suppress('else') + Suppress(':') + condition_block) conditional = (if_condition + Optional(OneOrMore(elif_condition)) + Optional(else_condition)).setResultsName('conditional') def key_parse_action(pstr, loc, toks): key = toks[0] if key in parse_ctx.keys: raise ParseFatalException( pstr, loc, "duplicate key '%s' value definition" % parse_ctx.key) parse_ctx.key = key parse_ctx.keys.append(key) try: parse_ctx.key_grammar = parse_ctx.fragment.get_key_grammars( )[key] key_grammar = parse_ctx.key_grammar.grammar except KeyError: raise ParseFatalException( pstr, loc, "key '%s' is not supported by fragment" % key) except Exception as e: raise ParseFatalException( pstr, loc, "unable to parse key '%s'; %s" % (key, str(e))) key_stmt << (conditional | Group(key_grammar).setResultsName('value')) return None def name_parse_action(pstr, loc, toks): parse_ctx.fragment.name = toks[0] key.setParseAction(key_parse_action) ftype = Word(alphas).setParseAction(fragment_type_parse_action) fid = Suppress(':') + Word(alphanums + '_.').setResultsName('name') fid.setParseAction(name_parse_action) header = Suppress('[') + ftype + fid + Suppress(']') def fragment_parse_action(pstr, loc, toks): key_grammars = parse_ctx.fragment.get_key_grammars() required_keys = set( [k for (k, v) in key_grammars.items() if v.required]) present_keys = required_keys.intersection(set(parse_ctx.keys)) if present_keys != required_keys: raise ParseFatalException( pstr, loc, 'required keys %s for fragment not found' % list(required_keys - present_keys)) return parse_ctx.fragment fragment_stmt = Forward() fragment_block = indentedBlock(fragment_stmt, indent_stack) fragment_if_condition = Group( Suppress('if') + condition + Suppress(':') + fragment_block) fragment_elif_condition = Group( Suppress('elif') + condition + Suppress(':') + fragment_block) fragment_else_condition = Group( Suppress('else') + Suppress(':') + fragment_block) fragment_conditional = ( fragment_if_condition + Optional(OneOrMore(fragment_elif_condition)) + Optional(fragment_else_condition)).setResultsName('conditional') fragment = (header + OneOrMore(indentedBlock(key_body, indent_stack, False))).setResultsName('value') fragment.setParseAction(fragment_parse_action) fragment.ignore('#' + restOfLine) deprecated_mapping = DeprecatedMapping.get_fragment_grammar( sdkconfig, fragment_file.name).setResultsName('value') fragment_stmt << (Group(deprecated_mapping) | Group(fragment) | Group(fragment_conditional)) def fragment_stmt_parsed(pstr, loc, toks): stmts = list() expand_conditionals(toks, stmts) return stmts parser = ZeroOrMore(fragment_stmt) parser.setParseAction(fragment_stmt_parsed) self.fragments = parser.parseFile(fragment_file, parseAll=True) for fragment in self.fragments: fragment.path = path
class Lindenmayer(object): def __init__(self, stream): # Set the default image dimensions ... self.width = 500 self.height = 500 # ... and the number of iterations. self.iterations = 5 # Set the default rotation angle in degrees. self.alpha = 90 # Initialize the branch stack, ... self.stack = [] # ... the constants, the rules, the variables and the axiom ... self.const = {'+':'+', '-':'-', '[':'[', ']':']'} self.rules = {} self.vars = [] self.axiom = None # ... and drawing settings. self.bgcolor = (1.0, 1.0, 1.0) self.lineLength = 20 self.lineWidth = 5 self.lineColor = (0, 0, 0) # Calculate the starting position. self.offset = (0, -self.height*0.5) print 'Offset :', self.offset # Finally store the stream ... self.stream = stream # ... and initialize the parser. self.initialize() def initialize(self): ParserElement.setDefaultWhitespaceChars(' \t\r') integer = Regex(r"[+-]?\d+") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) number = Regex(r"[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?") \ .setParseAction(lambda s,l,t: [ float(t[0]) ]) color = Regex(r"#([0-9a-fA-F]{6})") angle = "'" + Regex(r"(360|3[0-5][0-9]|[12][0-9]{2}|[0-9]{1,2})") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) variable = Word(alphas, exact=1).setParseAction(self.addVar) colon = Literal(":").suppress() comma = Literal(",") lBrace = Literal("(") rBrace = Literal(")") lBracket = Literal("[") rBracket = Literal("]") lAngle = Literal("<") rAngle = Literal(">") plus = Literal("+") minus = Literal("-") FTerm = Literal("F") fTerm = Literal("f") ZTerm = Literal("Z") zTerm = Literal("z") xTerm = Literal("x") cTerm = Literal("c") eol = OneOrMore(LineEnd()).suppress() param = ( angle | color | "!" + number | "|" + number ) self.pList = lBrace + param + ZeroOrMore(comma + param) + rBrace literal = ((lBracket + ( variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList) ) + rBracket) | (variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList))) terminal = (ZTerm | zTerm | FTerm | fTerm | xTerm | cTerm | plus | minus | lBracket | rBracket) lprod = ( (OneOrMore(terminal) + lAngle + variable + rAngle + OneOrMore(terminal)) | (OneOrMore(terminal) + lAngle + variable) | (variable + rAngle + OneOrMore(terminal)) | variable ) rProd = OneOrMore(literal | terminal) comment = Suppress((LineStart() + "#" + SkipTo(eol, include=True))) rules = ( (lprod + Literal("=") + rProd + eol).setParseAction(self.addRule) \ | comment ) defaults = ( ( ("Dimensions" + colon + integer + comma + integer) | ("Position" + colon + integer + comma + integer) | ("Iterations" + colon + integer) | ("Angle" + colon + angle) | ("Linelength" + colon + number) | ("Linewidth" + colon + number) | ("Linecolor" + colon + color) | ("Background" + colon + color) | ("Axiom" + colon + rProd) ) + eol ).setParseAction(self.setAttribute) header = ( defaults | comment ) self.grammar = Suppress(ZeroOrMore(LineEnd())) \ + ZeroOrMore(header) \ + OneOrMore(rules) try: L = self.grammar.parseString( self.stream ) except ParseException, err: print err.line print " "*(err.column-1) + "^" print err print 'Rules:', self.rules
def _select_hint(expr): return Suppress(Literal("-- >")) + expr
def initialize(self): ParserElement.setDefaultWhitespaceChars(' \t\r') integer = Regex(r"[+-]?\d+") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) number = Regex(r"[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?") \ .setParseAction(lambda s,l,t: [ float(t[0]) ]) color = Regex(r"#([0-9a-fA-F]{6})") angle = "'" + Regex(r"(360|3[0-5][0-9]|[12][0-9]{2}|[0-9]{1,2})") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) variable = Word(alphas, exact=1).setParseAction(self.addVar) colon = Literal(":").suppress() comma = Literal(",") lBrace = Literal("(") rBrace = Literal(")") lBracket = Literal("[") rBracket = Literal("]") lAngle = Literal("<") rAngle = Literal(">") plus = Literal("+") minus = Literal("-") FTerm = Literal("F") fTerm = Literal("f") ZTerm = Literal("Z") zTerm = Literal("z") xTerm = Literal("x") cTerm = Literal("c") eol = OneOrMore(LineEnd()).suppress() param = ( angle | color | "!" + number | "|" + number ) self.pList = lBrace + param + ZeroOrMore(comma + param) + rBrace literal = ((lBracket + ( variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList) ) + rBracket) | (variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList))) terminal = (ZTerm | zTerm | FTerm | fTerm | xTerm | cTerm | plus | minus | lBracket | rBracket) lprod = ( (OneOrMore(terminal) + lAngle + variable + rAngle + OneOrMore(terminal)) | (OneOrMore(terminal) + lAngle + variable) | (variable + rAngle + OneOrMore(terminal)) | variable ) rProd = OneOrMore(literal | terminal) comment = Suppress((LineStart() + "#" + SkipTo(eol, include=True))) rules = ( (lprod + Literal("=") + rProd + eol).setParseAction(self.addRule) \ | comment ) defaults = ( ( ("Dimensions" + colon + integer + comma + integer) | ("Position" + colon + integer + comma + integer) | ("Iterations" + colon + integer) | ("Angle" + colon + angle) | ("Linelength" + colon + number) | ("Linewidth" + colon + number) | ("Linecolor" + colon + color) | ("Background" + colon + color) | ("Axiom" + colon + rProd) ) + eol ).setParseAction(self.setAttribute) header = ( defaults | comment ) self.grammar = Suppress(ZeroOrMore(LineEnd())) \ + ZeroOrMore(header) \ + OneOrMore(rules) try: L = self.grammar.parseString( self.stream ) except ParseException, err: print err.line print " "*(err.column-1) + "^" print err
def _macros(expr): return lineStart + Suppress(Literal("#")) + expr
class Atmega328(): def calc_addr(self, pc, offs, rng): return pc + 2 * (offs if offs < (rng // 2) else offs - rng) symtable = None imm_4 = lambda d, s: "{K[0]:d}".format(**d) bit = lambda d, s: "{s[0]:d}".format(**d) reg = lambda d, s: "r{d[0]:d}".format(**d) reg_imm = lambda d, s: "r{d[0]:d}, 0x{K[0]:x}".format(**d) reg8_imm = lambda d, s: "r{:d}, 0x{:x}".format(d["d"][0] + 16, d["K"][0]) reg_imm16 = lambda d, s: "r{d[0]:d}, 0x{k[0]:x}".format(**d) imm7_reg = lambda d, s: "0x{k[0]:x}, r{d[0]:d}".format(**d) imm16_reg = lambda d, s: "0x{k[0]:x}, r{d[0]:d}".format(**d) reg_bit = lambda d, s: "r{d[0]:d}, {b[0]:x}".format(**d) bit_reg = lambda d, s: "{b[0]:x}, r{d[0]:d}".format(**d) reg_reg = lambda d, s: "r{d[0]:d}, r{r[0]:d}".format(**d) reg3_reg3 = lambda d, s: "r{:d}, r{:d}".format(d["d"][0] + 16, d["r"][0] + 16) reg4_reg4 = lambda d, s: "r{:d}, r{:d}".format(d["d"][0] + 16, d["r"][0] + 16) reg4 = lambda d, s: "r{:d}".format(d["d"][0] + 16) reg_x = lambda d, s: "r{d[0]:d}, X".format(**d) reg_x = lambda d, s: "r{d[0]:d}, X".format(**d) reg_mx = lambda d, s: "r{d[0]:d}, -X".format(**d) reg_xp = lambda d, s: "r{d[0]:d}, X+".format(**d) reg_y = lambda d, s: "r{d[0]:d}, Y".format(**d) reg_yp = lambda d, s: "r{d[0]:d}, Y+".format(**d) reg_my = lambda d, s: "r{d[0]:d}, -Y".format(**d) reg_yo = lambda d, s: "r{d[0]:d}, Y+{q[0]:d}".format(**d) reg_z = lambda d, s: "r{d[0]:d}, Z".format(**d) reg_zp = lambda d, s: "r{d[0]:d}, Z+".format(**d) reg_mz = lambda d, s: "r{d[0]:d}, -Z".format(**d) reg_zo = lambda d, s: "r{d[0]:d}, Z+{q[0]:d}".format(**d) reg_io = lambda d, s: "r{d[0]:d}, 0x{A[0]:02x}".format(**d) x_reg = lambda d, s: "X, r{r[0]:d}".format(**d) xp_reg = lambda d, s: "X+, r{r[0]:d}".format(**d) mx_reg = lambda d, s: "-X, r{r[0]:d}".format(**d) y_reg = lambda d, s: "Y, r{r[0]:d}".format(**d) yp_reg = lambda d, s: "Y+, r{r[0]:d}".format(**d) my_reg = lambda d, s: "-Y, r{r[0]:d}".format(**d) yo_reg = lambda d, s: "Y+{q[0]:d}, r{r[0]:d}".format(**d) z_reg = lambda d, s: "Z, r{d[0]:d}".format(**d) zp_reg = lambda d, s: "Z+, r{d[0]:d}".format(**d) mz_reg = lambda d, s: "-Z, r{d[0]:d}".format(**d) zo_reg = lambda d, s: "Z+{q[0]:d}, r{d[0]:d}".format(**d) dreg_imm = lambda d, s: "r{:d}, 0x{:x}".format(d["d"][0] * 2 + 24, d["K"][ 0]) dreg_dreg = lambda d, s: "r{:d}, r{:x}".format(d["d"][0] * 2, d["r"][0] * 2 ) rel_add = lambda d, s: "0x{:04x}".format( s.calc_addr(d["pc"], d["k"][0], 128)) rel_add12 = lambda d, s: "0x{:04x}".format( s.calc_addr(d["pc"], d["k"][0], 4096)) add17 = lambda d, s: "0x{k[0]:04x}".format(**d) bit_rel = lambda d, s: "{s[0]:d}, 0x{k[0]:04x}".format(**d) no_opd = lambda d, s: "" io_bit = lambda d, s: "0x{A[0]:02x}, {b[0]:d}".format(**d) io_reg = lambda d, s: "0x{A[0]:02x}, r{r[0]:d}".format(**d) reg8_reg8 = lambda d, s: "r{:d}, r{:d}".format(d["d"][0] + 16, d["r"][0] + 16) just_zp = lambda d, s: "Z+" #ENUM I = 7 T = 6 H = 5 S = 4 V = 3 N = 2 Z = 1 C = 0 #Tokens def convert_hex_1(x): x[1] = int(x[1], 16) return x def convert_hex_0(x): x[0] = int(x[0], 16) return x def convert_reg24_30(x): x[0] = (int(x[0]) - 24) / 2 return x def convert_reg16_31(x): x[0] = int(x[0]) - 16 return x def convert_jmp_0(x): x[0] = Atmega328.symtable[x[0]] return x etiqueta = Word(alphas, alphas + nums) + Suppress(":") comentario = Optional(Suppress(";" + Word(alphanums))) d0_31 = Suppress(Word(alphas) + "r") + Word(nums) d0_31.addCondition(lambda t: int(t[0]) <= 31) d0_31_r0_31 = Suppress(Word(alphas) + "r") + Word(nums) + Suppress("," + ZeroOrMore(" ") + "r") + Word(nums) d0_31_r0_31.addCondition(lambda t: int(t[0]) <= 31 and int(t[1]) <= 31) d24_30_k0_63 = Suppress(Word(alphas) + "r") + Word(nums) + Suppress( "," + ZeroOrMore(" ") + "0x") + Word(hexnums) d24_30_k0_63.addCondition(lambda t: int(t[0]) <= 30 and int(t[0]) >= 24 and int(t[0]) % 2 == 0 and int(t[1], 16) <= 63) d24_30_k0_63.addParseAction(convert_hex_1) d24_30_k0_63.addParseAction(convert_reg24_30) d16_31_k0_255 = Suppress(Word(alphas) + "r") + Word(nums) + Suppress( "," + ZeroOrMore(" ") + "0x") + Word(hexnums) d16_31_k0_255.addCondition( lambda t: int(t[0]) <= 31 and int(t[0]) >= 16 and int(t[1], 16) <= 255) d16_31_k0_255.addParseAction(convert_hex_1) d16_31_k0_255.addParseAction(convert_reg16_31) d0_31_b0_7 = Suppress(Word(alphas) + "r") + Word(nums) + Suppress( "," + ZeroOrMore(" ")) + Word(hexnums) d0_31_b0_7.addCondition(lambda t: int(t[0]) <= 31 and int(t[1]) <= 7) jmp = Suppress(Word(alphas)) + (Word(nums) | Word(alphas)) jmp.setParseAction(convert_jmp_0) #GRUPO N def f_imm_4(self, opcstr, opd_dict): pass def f_bit(self, opcstr, opd_dict): pass #GRUPO N def f_reg(self, opcstr, opd_dict): d = opd_dict["d"][0] #TST if opcstr == "tst": #calculo del resultado res = self.ram[d] & self.ram[d] #calculo de v self.set_flag(self.V, 0) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #GRUPO N def f_reg_imm(self, opcstr, opd_dict): pass #GRUPO N def f_reg8_imm(self, opcstr, opd_dict): d = opd_dict["d"][0] + 16 k = opd_dict["K"][0] #ANDI if opcstr == "andi": #calculo del resultado res = self.ram[d] & k #calculo de v self.set_flag(self.V, 0) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #guardado del resultado en el registro self.ram[d] = res & 0xff #CPI elif opcstr == "cpi": #calculo del resultado res = self.ram[d] - k #calculo de h d3 = self.get_bit(self.ram[d], 3) k3 = self.get_bit(k, 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, (~d3) & k3 | k3 & res3 | res3 & (~d3)) #calculo de v d7 = self.get_bit(self.ram[d], 7) k7 = self.get_bit(k, 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & (~k7) & (~res7) | (~d7) & k7 & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #calculo de c if k > self.ram[d]: self.set_flag(self.C, 1) else: self.set_flag(self.C, 0) #ORI elif opcstr == "ori": #calculo del resultado res = self.ram[d] | k #calculo de v self.set_flag(self.V, 0) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #guardado del resultado en el registro self.ram[d] = res & 0xff #SBCI elif opcstr == "sbci": #calculo del resultado res = self.ram[d] - k - self.get_flag(7) #calculo de h d3 = self.get_bit(self.ram[d], 3) k3 = self.get_bit(k, 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, (~d3) & k3 | k3 & res3 | res3 & (~d3)) #calculo de v d7 = self.get_bit(self.ram[d], 7) k7 = self.get_bit(k, 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & (~k7) & (~res7) | (~d7) & k7 & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff != 0: self.set_flag(self.Z, 0) #calculo de c d7 = self.get_bit(self.ram[d], 7) k7 = self.get_bit(k, 7) res7 = self.get_bit(res, 7) self.set_flag(self.C, (~d7) & k7 | k7 & res7 | res7 & (~d7)) #guardado del resultado en el registro self.ram[d] = res & 0xff #GRUPO N def f_reg_imm16(self, opcstr, opd_dict): pass #GRUPO N def f_imm7_reg(self, opcstr, opd_dict): pass #GRUPO N def f_imm16_reg(self, opcstr, opd_dict): pass #GRUPO 4 def f_reg_bit(self, opcstr, opd_dict): pass #GRUPO N def f_bit_reg(self, opcstr, opd_dict): pass #GRUPO N def f_reg_reg(self, opcstr, opd_dict): d = opd_dict["d"][0] r = opd_dict["r"][0] #ADC if opcstr == "adc": #calculo del resultado res = self.ram[r] + self.ram[d] + self.get_flag(self.C) #calculo de h d3 = self.get_bit(self.ram[d], 3) r3 = self.get_bit(self.ram[r], 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, d3 & r3 | r3 & (~res3) | (~res3) & d3) #calculo de v d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & r7 & (~res7) | (~d7) & (~r7) & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) print(self.flags) #calculo de c crr = self.get_bit(res, 8) self.set_flag(self.C, crr) #guardado del resultado en el registro self.ram[d] = res & 0xff #ADD elif opcstr == "add": #calculo del resultado res = self.ram[r] + self.ram[d] #calculo de h d3 = self.get_bit(self.ram[d], 3) r3 = self.get_bit(self.ram[r], 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, d3 & r3 | r3 & (~res3) | (~res3) & d3) #calculo de v d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & r7 & (~res7) | (~d7) & (~r7) & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #calculo de c c = self.get_bit(res, 8) self.set_flag(self.C, c) #guardado del resultado en el registro self.ram[d] = res & 0xff #AND elif opcstr == "and": #calculo del resultado res = self.ram[d] & self.ram[r] #calculo de v self.set_flag(self.V, 0) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #guardado del resultado en el registro self.ram[d] = res & 0xff #CP elif opcstr == "cp": #calculo del resultado res = self.ram[d] - self.ram[r] #calculo de h d3 = self.get_bit(self.ram[d], 3) r3 = self.get_bit(self.ram[r], 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, (~d3) & r3 | r3 & res3 | res3 & (~d3)) #calculo de v d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & (~r7) & (~res7) | (~d7) & r7 & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #calculo de c d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.C, (~d7) & r7 | r7 & res7 | res7 & (~d7)) #CPC elif opcstr == "cpc": #calculo del resultado res = self.ram[d] - self.ram[r] - self.get_flag(7) #calculo de h d3 = self.get_bit(self.ram[d], 3) r3 = self.get_bit(self.ram[r], 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, (~d3) & r3 | r3 & res3 | res3 & (~d3)) #calculo de v d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & (~r7) & (~res7) | (~d7) & r7 & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #calculo de c d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.C, (~d7) & r7 | r7 & res7 | res7 & (~d7)) #CPSE elif opcstr == "cpse": #calculo del resultado if self.ram[d] == self.ram[r]: opc = self.flash.get_word(self.pc) entry = self.find_opcode(opc) if entry.opcstr[0] == "!": self.pc += 4 #Saltar una instruccion else: self.pc += 2 #EOR elif opcstr == "eor": #calculo del resultado res = self.ram[r] ^ self.ram[d] #calculo de v self.set_flag(self.V, 0) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #guardado del resultado en el registro self.ram[d] = res & 0xff #MOV elif opcstr == "mov": #calculo del resultado self.ram[d] = self.ram[r] #OR elif opcstr == "or": #calculo del resultado res = self.ram[r] | self.ram[d] #calculo de v self.set_flag(self.V, 0) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #guardado del resultado en el registro self.ram[d] = res & 0xff #SBC elif opcstr == "sbc": #calculo del resultado res = self.ram[d] - self.ram[r] - self.get_flag(7) #calculo de h d3 = self.get_bit(self.ram[d], 3) r3 = self.get_bit(self.ram[r], 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, (~d3) & r3 | r3 & res3 | res3 & (~d3)) #calculo de v d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & (~r7) & (~res7) | (~d7) & r7 & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff != 0: self.set_flag(self.Z, 0) #calculo de c d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.C, (~d7) & r7 | r7 & res7 | res7 & (~d7)) #guardado del resultado en el registro self.ram[d] = res & 0xff #SUB elif opcstr == "sub": #calculo del resultado res = self.ram[d] - self.ram[r] #calculo de h d3 = self.get_bit(self.ram[d], 3) r3 = self.get_bit(self.ram[r], 3) res3 = self.get_bit(res, 3) self.set_flag(self.H, (~d3) & r3 | r3 & res3 | res3 & (~d3)) #calculo de v d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.V, d7 & (~r7) & (~res7) | (~d7) & r7 & res7) #calculo de n self.set_flag(self.N, self.get_bit(res, 7)) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #calculo de c d7 = self.get_bit(self.ram[d], 7) r7 = self.get_bit(self.ram[r], 7) res7 = self.get_bit(res, 7) self.set_flag(self.C, (~d7) & r7 | r7 & res7 | res7 & (~d7)) #guardado del resultado en el registro self.ram[d] = res & 0xff #GRUPO N def f_reg3_reg3(self, opcstr, opd_dict): pass #GRUPO N def f_reg4_reg4(self, opcstr, opd_dict): pass #GRUPO N def f_reg4(self, opcstr, opd_dict): pass #GRUPO N def f_reg_x(self, opcstr, opd_dict): pass #GRUPO N def f_reg_mx(self, opcstr, opd_dict): pass #GRUPO N def f_reg_xp(self, opcstr, opd_dict): pass #GRUPO N def f_reg_y(self, opcstr, opd_dict): pass #GRUPO N def f_reg_yp(self, opcstr, opd_dict): pass #GRUPO N def f_reg_my(self, opcstr, opd_dict): pass #GRUPO N def f_reg_yo(self, opcstr, opd_dict): pass #GRUPO N def f_reg_z(self, opcstr, opd_dict): pass #GRUPO N def f_reg_zp(self, opcstr, opd_dict): pass #GRUPO N def f_reg_mz(self, opcstr, opd_dict): pass #GRUPO N def f_reg_zo(self, opcstr, opd_dict): pass #GRUPO N def f_reg_io(self, opcstr, opd_dict): pass #GRUPO N def f_x_reg(self, opcstr, opd_dict): pass #GRUPO N def f_xp_reg(self, opcstr, opd_dict): pass #GRUPO N def f_mx_reg(self, opcstr, opd_dict): pass #GRUPO N def f_y_reg(self, opcstr, opd_dict): pass #GRUPO N def f_yp_reg(self, opcstr, opd_dict): pass #GRUPO N def f_my_reg(self, opcstr, opd_dict): pass #GRUPO N def f_yo_reg(self, opcstr, opd_dict): pass #GRUPO N def f_z_reg(self, opcstr, opd_dict): pass #GRUPO N def f_zp_reg(self, opcstr, opd_dict): pass #GRUPO N def f_mz_reg(self, opcstr, opd_dict): pass #GRUPO N def f_zo_reg(self, opcstr, opd_dict): pass #GRUPO N def f_dreg_imm(self, opcstr, opd_dict): d = opd_dict["d"][0] * 2 + 24 k = opd_dict["K"][0] #ADIW if opcstr == "adiw": #calculo del resultado res = self.ram[d] + k res1 = self.ram[d + 1] + k #calculo de v dh7 = self.get_bit(self.ram[d + 1], 7) r15 = self.get_bit(res1, 7) self.set_flag(self.V, ~dh7 & r15) #calculo de n self.set_flag(self.N, r15) #calculo de s self.set_flag(self.S, self.get_flag(self.N) ^ self.get_flag(self.V)) #calculo de z if res & 0xff == 0 and res1 & 0xff == 0: self.set_flag(self.Z, 1) else: self.set_flag(self.Z, 0) #calculo de c self.set_flag(self.C, ~r15 & dh7) #guardado del resultado self.ram[d] = res & 0xff self.ram[d + 1] = res1 & 0xff #GRUPO N def f_dreg_dreg(self, opcstr, opd_dict): d = opd_dict["d"][0] * 2 r = opd_dict["r"][0] * 2 #MOVW if opcstr == "movw": #calculo del resultado self.ram[d] = self.ram[r] self.ram[d + 1] = self.ram[r + 1] #GRUPO N def f_rel_add(self, opcstr, opd_dict): pass #GRUPO N def f_rel_add12(self, opcstr, opd_dict): pass #GRUPO N def f_add17(self, opcstr, opd_dict): pass #GRUPO N def f_bit_rel(self, opcstr, opd_dict): pass #GRUPO N def f_no_opd(self, opcstr, opd_dict): pass #GRUPO N def f_io_bit(self, opcstr, opd_dict): pass #GRUPO N def f_io_reg(self, opcstr, opd_dict): pass #GRUPO N def f_reg8_reg8(self, opcstr, opd_dict): pass #GRUPO N def f_just_zp(self, opcstr, opd_dict): pass #Grupos: # 1: Gomez-Pereyra # 2: Garijo-Perez # 3: Gueler- # 4: opcodes = ( #A OPC(0xfc00, 0x1c00, "adc", "r4d5r1", reg_reg, f_reg_reg, "b4a5b1", d0_31_r0_31), # 1 OPC(0xfc00, 0x0c00, "add", "r4d5r1", reg_reg, f_reg_reg, "b4a5b1", d0_31_r0_31), # 1 OPC(0xff00, 0x9600, "adiw", "K4d2K2", dreg_imm, f_dreg_imm, "b4a2b2", d24_30_k0_63), # 1 OPC(0xfc00, 0x2000, "and", "r4d5r1", reg_reg, f_reg_reg, "b4a5b1", d0_31_r0_31), # 1 OPC(0xf000, 0x7000, "andi", "K4d4K4", reg8_imm, f_reg8_imm, "b4a4b4", d16_31_k0_255), # 1 OPC(0xfe0f, 0x9405, "asr", "-4d5", reg, f_reg, "-4a5", d0_31), # 3 #B @12 #OPC(0xff8f, 0x9488, "bclr", "-4s3", bit, f_bit), OPC(0xfe08, 0xf800, "bld", "b3-1d5", reg_bit, f_reg_bit, "b3-1a5", d0_31_b0_7), # 4 #OPC(0xfc00, 0xf400, "brbc", "s3k7", bit_rel, f_bit_rel), #OPC(0xfc00, 0xf000, "brbs", "s3k7", bit_rel, f_bit_rel), OPC(0xfc07, 0xf400, "brcc", "-3k7", rel_add, f_rel_add, "-3a7", jmp), # 4 OPC(0xfc07, 0xf000, "brcs", "-3k7", rel_add, f_rel_add, "-3a7", jmp), # 4 OPC(0xffff, 0x9598, "break", "", no_opd, f_no_opd, "", None), # 2 OPC(0xfc07, 0xf001, "breq", "-3k7", rel_add, f_rel_add, "-3k7", jmp), # 4 OPC(0xfc07, 0xf404, "brge", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf405, "brhc", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf005, "brhs", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf407, "brid", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf007, "brie", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf000, "brlo", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf004, "brlt", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf002, "brmi", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf401, "brne", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf402, "brpl", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf400, "brsh", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf406, "brtc", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf006, "brts", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf403, "brvc", "-3k7", rel_add, f_rel_add, "", None), # 4 OPC(0xfc07, 0xf003, "brvs", "-3k7", rel_add, f_rel_add, "", None), # 4 #OPC(0xff8f, 0x9408, "bset", "-4s3", bit, f_bit), OPC(0xfe08, 0xfa00, "bst", "b3-1d5", reg_bit, f_reg_bit, "", None), # 4 #C OPC(0xfe0e, 0x940e, "call", "!k17-3k5", add17, f_add17, "", None), # 2 OPC(0xff00, 0x9800, "cbi", "b3A5", io_bit, f_io_bit, "", None), # 4 OPC(0xffff, 0x9488, "clc", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x94d8, "clh", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x94f8, "cli", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x94a8, "cln", "", no_opd, f_no_opd, "", None), # 4 OPC(0xfc00, 0x2400, "clr", "r4d5r1", reg, f_reg, "", None), # 2 OPC(0xffff, 0x94c8, "cls", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x94e8, "clt", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x94b8, "clv", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9498, "clz", "", no_opd, f_no_opd, "", None), # 4 OPC(0xfe0f, 0x9400, "com", "-4d5", reg, f_reg, "", None), OPC(0xfe00, 0x1400, "cp", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 OPC(0xfc00, 0x0400, "cpc", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 OPC(0xf000, 0x3000, "cpi", "K4d4K4", reg8_imm, f_reg8_imm, "", None), # 1 OPC(0xfc00, 0x1000, "cpse", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 #D OPC(0xfe0f, 0x940a, "dec", "-4d5", reg, f_reg, "", None), # 3 OPC(0xff0f, 0x940b, "des", "-4K4", imm_4, f_imm_4, "", None), # 3 #E OPC(0xffff, 0x9519, "eicall", "", no_opd, f_no_opd, "", None), # 2 OPC(0xffff, 0x9419, "eijmp", "", no_opd, f_no_opd, "", None), # 2 OPC(0xffff, 0x95d8, "elpm", "", no_opd, f_no_opd, "", None), # 2 OPC(0xfe0f, 0x9006, "elpm", "-4d5", reg_z, f_reg_z, "", None), # 2 OPC(0xfe0f, 0x9007, "elpm", "-4d5", reg_zp, f_reg_zp, "", None), # 2 OPC(0xfc00, 0x2400, "eor", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 #F OPC(0xff88, 0x0308, "fmul", "r3-1d3", reg8_reg8, f_reg8_reg8, "", None), # 3 OPC(0xff88, 0x0380, "fmuls", "r3-1d3", reg8_reg8, f_reg8_reg8, "", None), # 3 OPC(0xff88, 0x0388, "fmulsu", "r3-1d3", reg8_reg8, f_reg8_reg8, "", None), # 3 #G - No hay #H - No hay #I OPC(0xffff, 0x9509, "icall", "", no_opd, f_no_opd, "", None), # 2 OPC(0xffff, 0x9409, "ijmp", "", no_opd, f_no_opd, "", None), # 2 OPC(0xf800, 0xb000, "in", "A4d5A2", reg_io, f_reg_io, "", None), # 3 OPC(0xfe0f, 0x9403, "inc", "-4d5", reg, f_reg, "", None), # 3 #J OPC(0xfe0e, 0x940c, "jmp", "!k17-3k5", add17, f_add17, "", None), # 2 #K - No hay #L OPC(0xfe0f, 0x9206, "lac", "-4r5", z_reg, f_z_reg, "", None), OPC(0xfe0f, 0x9205, "las", "-4r5", z_reg, f_z_reg, "", None), OPC(0xfe0f, 0x9207, "lat", "-4r5", z_reg, f_z_reg, "", None), OPC(0xfe0f, 0x900c, "ld", "-4d5", reg_x, f_reg_x, "", None), OPC(0xfe0f, 0x900d, "ld", "-4d5", reg_xp, f_reg_xp, "", None), OPC(0xfe0f, 0x900e, "ld", "-4d5", reg_mx, f_reg_mx, "", None), OPC(0xfe0f, 0x900c, "ld", "-4d5", reg_x, f_reg_x, "", None), OPC(0xfe0f, 0x900d, "ld", "-4d5", reg_xp, f_reg_xp, "", None), OPC(0xfe0f, 0x900e, "ld", "-4d5", reg_mx, f_reg_mx, "", None), OPC(0xfe0f, 0x8008, "ld", "-4d5", reg_y, f_reg_y, "", None), OPC(0xfe0f, 0x9009, "ld", "-4d5", reg_yp, f_reg_yp, "", None), OPC(0xfe0f, 0x900a, "ld", "-4d5", reg_my, f_reg_my, "", None), OPC(0xd208, 0x8008, "ldd", "q3-1d5-1q2-1q1", reg_yo, f_reg_yo, "", None), OPC(0xfe0f, 0x8000, "ld", "-4d5", reg_z, f_reg_z, "", None), OPC(0xfe0f, 0x9001, "ld", "-4d5", reg_zp, f_reg_zp, "", None), OPC(0xfe0f, 0x9002, "ld", "-4d5", reg_mz, f_reg_mz, "", None), OPC(0xd208, 0x8000, "ldd", "q3-1d5-1q2-1q1", reg_zo, f_reg_zo, "", None), OPC(0xf000, 0xe000, "ldi", "K4d4K4", reg8_imm, f_reg8_imm, "", None), OPC(0xfe0f, 0x9000, "lds", "!k16-4d5", reg_imm16, f_reg_imm16, "", None), OPC(0xffff, 0x95c8, "lpm", "", no_opd, f_no_opd, "", None), OPC(0xfe0f, 0x9004, "lpm", "-4d5", reg_z, f_reg_z, "", None), OPC(0xfe0f, 0x9005, "lpm", "-4d5", reg_zp, f_reg_zp, "", None), OPC(0xfc00, 0x0c00, "lsl", "r4d5r1", reg, f_reg, "", None), # 3 OPC(0xfe0f, 0x9406, "lsr", "-4d5", reg, f_reg, "", None), # 3 #M OPC(0xfc00, 0x2c00, "mov", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 OPC(0xff00, 0x0100, "movw", "r4d4", dreg_dreg, f_dreg_dreg, "", None), # 1 OPC(0xfc00, 0x9c00, "mul", "r4d5r1", reg_reg, f_reg_reg, "", None), # 3 OPC(0xff00, 0x0200, "muls", "r4d4", reg4_reg4, f_reg4_reg4, "", None), # 3 OPC(0xff88, 0x0300, "mulsu", "r3-1d3", reg3_reg3, f_reg3_reg3, "", None), # 3 #N OPC(0xfe0f, 0x9401, "neg", "-4d5", reg, f_reg, "", None), # 3 OPC(0xffff, 0x0000, "nop", "", no_opd, f_no_opd, "", None), # 2 #O OPC(0xfc00, 0x2800, "or", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 OPC(0xf000, 0x6000, "ori", "K4d4K4", reg8_imm, f_reg8_imm, "", None), # 1 OPC(0xf800, 0xb800, "out", "A4r5A2", io_reg, f_io_reg, "", None), # 3 #P OPC(0xfe0f, 0x900f, "pop", "-4d5", reg, f_reg, "", None), # 2 OPC(0xfe0f, 0x920f, "push", "-4d5", reg, f_reg, "", None), # 2 #Q - No hay #R OPC(0xf000, 0xd000, "rcall", "k12", rel_add12, f_rel_add12, "", None), # 2 OPC(0xffff, 0x9508, "ret", "", no_opd, f_no_opd, "", None), # 2 OPC(0xffff, 0x9518, "reti", "", no_opd, f_no_opd, "", None), # 2 OPC(0xf000, 0xc000, "rjmp", "k12", rel_add12, f_rel_add12, "", None), # 2 OPC(0xfc00, 0x1c00, "rol", "r4d5r1", reg, f_reg, "", None), # 3 OPC(0xfe0f, 0x9407, "ror", "-4d5", reg, f_reg, "", None), # 3 #S - en proceso OPC(0xfc00, 0x0800, "sbc", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 OPC(0xf000, 0x4000, "sbci", "K4d4K4", reg8_imm, f_reg8_imm, "", None), # 1 OPC(0xff00, 0x9a00, "sbi", "b3A5", io_bit, f_io_bit, "", None), # 4 OPC(0xff00, 0x9900, "sbic", "b3A5", io_bit, f_io_bit, "", None), # 4 OPC(0xff00, 0x9b00, "sbis", "b3A5", io_bit, f_io_bit, "", None), # 4 OPC(0xff00, 0x9700, "sbiw", "K4d2K2", dreg_imm, f_dreg_imm, "", None), # 4 OPC(0xf000, 0x6000, "sbr", "K4d4K4", reg_imm, f_reg_imm, "", None), # 4 OPC(0xfe00, 0xfc00, "sbrc", "b3-1d5", reg_bit, f_reg_bit, "", None), # 4 OPC(0xfe08, 0xfe00, "sbrs", "b3-1d5", reg_bit, f_reg_bit, "", None), # 4 OPC(0xffff, 0x9408, "sec", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9458, "seh", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9478, "sei", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9428, "sen", "", no_opd, f_no_opd, "", None), # 4 OPC(0xff0f, 0xef0f, "ser", "-4d4", reg4, f_reg4, "", None), OPC(0xffff, 0x9448, "ses", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9468, "set", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9438, "sev", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9418, "sez", "", no_opd, f_no_opd, "", None), # 4 OPC(0xffff, 0x9588, "sleep", "", no_opd, f_no_opd, "", None), # 2 OPC(0xffff, 0x95e8, "spm", "", no_opd, f_no_opd, "", None), OPC(0xffff, 0x95f8, "spm", "", just_zp, f_just_zp, "", None), OPC(0xfe0f, 0x920c, "st", "-4r5", x_reg, f_x_reg, "", None), OPC(0xfe0f, 0x920d, "st", "-4r5", xp_reg, f_xp_reg, "", None), OPC(0xfe0f, 0x920e, "st", "-4r5", mx_reg, f_mx_reg, "", None), OPC(0xfe0f, 0x8208, "st", "-4r5", y_reg, f_y_reg, "", None), OPC(0xfe0f, 0x9209, "st", "-4r5", yp_reg, f_yp_reg, "", None), OPC(0xfe0f, 0x920a, "st", "-4r5", my_reg, f_my_reg, "", None), OPC(0xd208, 0x8208, "std", "q3-1r5-1q2-1q1", yo_reg, f_yo_reg, "", None), OPC(0xfe0f, 0x8200, "st", "-4d5", z_reg, f_z_reg, "", None), OPC(0xfe0f, 0x9201, "st", "-4d5", zp_reg, f_zp_reg, "", None), OPC(0xfe0f, 0x9202, "st", "-4d5", mz_reg, f_mz_reg, "", None), OPC(0xd208, 0x8200, "std", "q3-1d5-1q2-1q1", zo_reg, f_zo_reg, "", None), OPC(0xfe0f, 0x9200, "sts", "!k16-4d5", imm16_reg, f_imm16_reg, "", None), OPC(0xf800, 0xa800, "sts", "k4d4k3", imm7_reg, f_imm7_reg, "", None), OPC(0xfc00, 0x1800, "sub", "r4d5r1", reg_reg, f_reg_reg, "", None), # 1 OPC(0xf000, 0x5000, "subi", "K4d4K4", reg8_imm, f_reg8_imm, "", None), # 3 OPC(0xfe0f, 0x9402, "swap", "-4d5", reg, f_reg, "", None), # 2 #T OPC(0xfc00, 0x2000, "tst", "r4d5r1", reg, f_reg, "", None), # 1 #U - No hay #V - No hay #W OPC(0xffff, 0x95a8, "wdr", "", no_opd, f_no_opd, "", None), # 2 #X #~ OPC(0xfe0f, 0x9204, "xch", "-4d5", reg, f_reg) # Not for atmega328 # Y - No hay # Z - No hay ) def __init__(self, symtable): self.flash = Memory(32768, Memory.FLASH) #self.symtable = symtable self.pc = 0 self.sp = 0x7fff self.ram = [0] * 32 self.flags = 0 def get_bit(self, reg, bit): return (reg >> bit) & 0x01 def set_bit(self, reg, bit, value): if value == 1: reg = reg | (1 << bit) else: reg = reg & (~(1 << bit)) return reg def get_flag(self, flag): return self.get_bit(self.flags, flag) def set_flag(self, flag, value): self.flags = self.set_bit(self.flags, flag, value) def reset(self): self.pc = 0 self.sp = 0x7fff self.flags = 0 def load_flash(self, fname): self.flash.load_intel_hex(fname) def find_opcode(self, opc): for entry in self.opcodes: if (opc & entry.mask) == entry.remainder: break else: print("Instruccion no decodificada: {:04x}".format(opc)) return None return entry def find_instruction(self, instruction): for entry in self.opcodes: if instruction == entry.opcstr: break else: print("Instruccion no encontrada:" + instruction) return None return entry def decode_operands(self, entry, pc, opc): opd_dict = {"pc": pc} # para calcular direcciones relativas cmd = entry.opdcmd if (cmd != "") and (cmd[0] == '!'): # Instruccion de 32 bits? opc = (opc << 16) | self.flash.get_word(pc) cmd = cmd[1:] pc += 2 # Ej.: r4d5r1 (para ADC) # decodificar 0x1da5 while cmd != "": # paso 1 paso 2 paso 3 r = re.match("([a-zA-Z-])(\d+)", cmd) # r, 4 d, 5 r, 1 if r == None: print("error en expresion en la table") field_name = r.group(1) # r d r field_len = int(r.group(2)) # 4 5 1 if field_name not in opd_dict: opd_dict[field_name] = [0, 0] step = opd_dict[field_name] temp = opc & (1 << field_len) - 1 # 5 26 0 opc >>= field_len # 0x01da 0x000e 0x0007 step[0] |= temp << step[1] # [r]=5 [d]=26 [r]=5 step[1] += field_len # 4 5 5 cmd = cmd[len(r.group(0)):] # r4d5r1 d5r1 r1 # ->d5r1 -> r1 -> "" return opd_dict def disassemble_one_instruction(self, pc, symtable=None): opc = self.flash.get_word(pc) pc += 2 if opc == None: return None entry = self.find_opcode(opc) if entry == None: return None opd_dict = self.decode_operands(entry, pc, opc) s = "{:8s}{:8s}".format("", entry.opcstr) if entry.fmt in [ Atmega328.add17, Atmega328.rel_add, Atmega328.rel_add12 ]: sym = int(entry.fmt(opd_dict, self), 0) self.symtable.add(self.symtable.create_label(sym), sym) s += "{:s}".format(self.symtable.create_label(sym)) else: s += entry.fmt(opd_dict, self) return (s, pc) #funcion para ensamblar una instruccion def assemble_one_instruction(self, line, mem_pos): instruction = 0 try: l = (self.etiqueta + self.comentario).parseString(line) self.symtable[l[0]] = mem_pos return None except Exception as e: #print(e) pass opc = re.match("([a-zA-Z]+)", line) entry = self.find_instruction(opc.group(1)) instruction = entry.remainder try: opd_list = (entry.token + self.comentario).parseString( line, parseAll=True) if entry.token == Atmega328.jmp: opd_list[0] = opd_list[0] - mem_pos + ( 128 if opd_list[0] < mem_pos else 0) print(opd_list) except Exception as e: print("Instruccion no valida") print(e) return None cmd = entry.code_opd #Parametro para la asignacion de los bits itr = 0 while cmd != "": r = re.match("([a-b-])(\d+)", cmd) if r.group(1) == "-": itr += int(r.group(2)) cmd = cmd[2:] continue #que valor es el correcto opd = 0 if r.group(1) == "a" else 1 #cantidad de bits a desplazar itr_aux = int(r.group(2)) for bit in range(itr_aux): value = int(opd_list[opd]) & 0x01 opd_list[opd] = int(opd_list[opd]) >> 1 instruction = self.set_bit(instruction, itr + bit, value) itr += itr_aux cmd = cmd[2:] return instruction def single_step(self, pc=None, symtable=None): if pc != None: self.pc = pc opc = self.flash.get_word(self.pc) self.pc += 2 if opc == None: return None entry = self.find_opcode(opc) if entry == None: return None opd_dict = self.decode_operands(entry, pc, opc) entry.sim_instr(self, entry.opcstr, opd_dict)
def _sql_identifier(expr): return Suppress(Optional('`')) + expr + Suppress(Optional('`'))
]) # RULES: "Relative to" factor_unit = ( simple_h_name.setResultsName("factor") + Optional(Regex(".*").setResultsName("unparsed_unit"))).setParseAction( lambda _s, l, t: { 'type': 'factor_unit', 'factor': t.factor, 'unparsed_unit': t.unparsed_unit if t.unparsed_unit else "" }) # #### URL Parser ################################################################ url_chars = alphanums + '-_.~%+' fragment = Combine((Suppress('#') + Word(url_chars)))('fragment') scheme = oneOf('http https ftp file data')('scheme') host = Combine(delimitedList(Word(url_chars), '.'))('host') port = Suppress(':') + Word(nums)('port') user_info = (Word(url_chars)('username') + Suppress(':') + Word(url_chars)('password') + Suppress('@')) query_pair = Group(Word(url_chars) + Suppress('=') + Word(url_chars)) query = Group(Suppress('?') + delimitedList(query_pair, '&'))('query') path = Combine(Suppress('/') + OneOrMore(~query + Word(url_chars + '/')))('path') url_parser = (scheme.setResultsName("scheme") + Suppress('://') + Optional(user_info).setResultsName("user_info") + Optional(host).setResultsName("host") +
Optional(_AS + _SQL_ID.setResultsName("alias")) _TABLE_NAME = _SQL_ID.setResultsName("table") _PROCEDURE_NAME = _sql_identifier( Word(alphanums + "_.:").setResultsName("name")).setResultsName('name') _SELECT_COLUMN_LIST = delimitedList(Group(_SELECT_COLUMN), combine=False).setResultsName("columns") _SQL_ARG = Optional(_SQL_DIRECTION, default='IN') + _SQL_ID + _SQL_TYPE _SQL_ARGS = delimitedList(Group(_SQL_ARG), combine=False) _RETURN_TYPE = oneOf("object array", caseless=True).setResultsName("type") # expressions _DEFINE_FUNCTION = _DEFINE + _ID + nestedExpr(content=_ID_LIST, ignoreExpr=None).setResultsName("args") + \ Suppress(White()) + Regex(".*$").setResultsName("body") _DEFINE_VAR = _DEFINE + _ID + SkipTo(lineEnd, include=True).setResultsName("value") _UNDEFINE = _UNDEF + _ID _INCLUDE_FILE = _INCLUDE + quotedString.setResultsName("filename") _EXPAND_VAR = Suppress('$') + _ID _EXPAND_FUNC = Suppress('$') + _ID + nestedExpr( content=_VALUE_LIST, ignoreExpr=None).setResultsName("args") _IF_EXPR = _IF + SkipTo(lineEnd, include=True).setResultsName("condition") _ELSE_EXPR = _ELSE + lineEnd _ENDIF_EXPR = _ENDIF + lineEnd _RETURN_HINT_EXPR = _select_hint( Optional(_ID + Suppress(Literal(":"))) +
def __init__(self): self.rules_parsed = 0 dot = Suppress(".") colon = Word(":") _is = Suppress("is") _and = Suppress("and") then = Suppress("then") when = Suppress("when") override = Suppress("override") every = Suppress("every") at = Suppress("at") word = Word(alphas + nums + "-" + "_") ignoredWord = Suppress(word) verb = word.setResultsName('verb') number = Word(nums) word_or_sentence = (word | quotedString.setParseAction(removeQuotes)) identified_by = Suppress("identified by") def __actions(): state = word_or_sentence.setResultsName("state") receiver = word.setResultsName("receiver") action = Group(verb + receiver + state) actions = ZeroOrMore(action + _and) + action return Optional("always").setResultsName( "always_fire_actions") + Group(actions).setResultsName( "actions") + Optional( override + Optional("off")).setResultsName("override") actions = __actions() def receiver_input_rule(): path = Combine(ZeroOrMore(word + ".") + word) input = path.setResultsName("input") operator = oneOf(operators.keys()).setResultsName("operator") value = path.setResultsName("value") comparison = operator + value is_or_was = Word("is") | Word("was") condition = Group(input + is_or_was.setResultsName("temporal") + comparison) res = ZeroOrMore(condition + _and) + condition conditions = Group(res).setResultsName("conditions") return Optional("always").setResultsName( "always_fire_rule") + when + conditions + then + actions def schedule_rule(): timeIndication = at + Combine(Optional(number) + colon + number).setResultsName("time") recurringPlural = Group( every + number.setResultsName("count") + oneOf("days hours minutes seconds weeks").setResultsName( "unit") + Optional(timeIndication)).setResultsName("pluralSchedule") recurringSingular = Group( every + oneOf("day hour minute second week sunday weekday weekendday" ).setResultsName("unit") + Optional(timeIndication)).setResultsName("singularSchedule") dayOfWeek = oneOf( "monday tuesday wednesday thursday friday saturday sunday") recurringDay = Group( every + Group(ZeroOrMore(dayOfWeek + _and) + dayOfWeek).setResultsName("unit") + Optional(timeIndication)).setResultsName("singularSchedule") return (recurringPlural | recurringSingular | recurringDay) + ( actions | receiver_input_rule().setResultsName("nested-input-rule")) rule_type = (receiver_input_rule().setResultsName("input-rule") | schedule_rule().setResultsName("schedule-rule")) self.rule = rule_type + Optional(identified_by + word.setResultsName("rule-id"))
def split_text(text, tokens): """ Given a body of text that contains tokens, splice the text along those tokens. """ starts = [text.find(t) for t in tokens] slices = zip(starts, starts[1:]) texts = [text[i[0]:i[1]] for i in slices] + [text[starts[-1]:]] return texts _first_markers = [] for idx, level in enumerate(p_levels): marker = (Suppress(Regex(u',|\.|-|—|>')) + Suppress('(') + Literal(level[0]) + Suppress(')')) for inner_idx in range(idx + 1, len(p_levels)): inner_level = p_levels[inner_idx] marker += Optional( Suppress('(') + Literal(inner_level[0]) + Suppress(')')) _first_markers.append(marker) # @profile def get_collapsed_markers(text): """Not all paragraph markers are at the beginning of of the text. This grabs inner markers like (1) and (i) here: (c) cContent —(1) 1Content (i) iContent""" matches = []
# 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')
class Lindenmayer(object): def __init__(self, stream): # Set the default image dimensions ... self.width = N self.height = N # ... and the number of iterations. self.iterations = 5 # Set the default rotation angle in degrees. self.alpha = 2*np.pi/8 self.angle = 0 # Initialize the branch stack, ... self.stack = [] # ... the constants, the rules, the variables and the axiom ... self.const = {'+':'+', '-':'-', '[':'[', ']':']'} self.rules = {} self.vars = [] self.axiom = None # ... and drawing settings. self.bgcolor = (1.0, 1.0, 1.0) self.lineLength = 20 self.lineWidth = 5 self.lineColor = (0, 0, 0) # Calculate the starting position. self.offset = (0, -self.height*0.5) print 'Offset :', self.offset # Finally store the stream ... self.stream = stream self.arr = bak.calc()#np.zeros((N,N))# print "Baking Bread..." # state self.x = int(self.width/2)#int(self.width/2) self.y = int(self.height/2) self.r = 16 self.xparent = self.x self.yparent = self.y # ... and initialize the parser. self.initialize() # calculates new radius based on a poisson distribution (?) def poisson(self): global bubbles, delta #x1 = min(max(self.x-delta,0),N) #y1 = min(max(self.y-delta,0),N) x1 = self.x y1 = self.y suma = 0.0 x0 = max(x1-delta,0) y0 = max(y1-delta,0) x2 = min(x1+delta,N) y2 = min(y1+delta,N) #print x0,y0,x2,y2 #print abs(x0-x2),abs(y0-y2) cant = 0 for i in range(x0,x2): for j in range(y0,y2): #d = np.sqrt((x1-i)*(x1-i) + (y1-j)*(y1-j)).astype(np.float32) # distance #if(d==0): suma+=bubbles[i,j] #else: suma += bubbles[i,j]#*(np.sqrt(2)*delta+1-d)/(np.sqrt(2)*delta) cant += bubbles[i,j]>0 #suma += np.pi*bubbles[i,j]*bubbles[i,j] #if(bubbles[i,j]): print "RADIO:" , bubbles[i,j] factor = delta # (?) #print "Suma:", suma, "Delta: ", delta #print factor*(1/suma) #1/ sum_D (cant*radios*D^2) #D = np.sqrt(np.power((self.x-self.height/2),2)+np.power((self.y-self.width/2),2)) G = 1#+2.5*self.arr[min(self.width,max(self.x-1,0)),min(self.width,max(self.y-1,0))]#D/300 #gelatinization #print "G: ", G x1 = min(max(self.x-delta,0),N) y1 = min(max(self.y-delta,0),N) baking = self.arr[x1,y1]*4 #print baking, ": baking" if(suma > 0): m = 1/suma #if(delta*m < 1): return np.random.randint(0,2) if(cant>10): return 0 return np.random.randint(0,delta*m+2)/baking else: # free return np.random.randint(0,delta/2+delta/2)/baking def rotate(self): #self.x += self.r #self.y += self.r #d = 2*np.pi*random.random() ang = self.angle+random.random()/6 self.x = self.xparent + np.int32(fdist(self.r)*np.cos(ang))+randint(-int(self.r),int(self.r)) self.y = self.yparent + np.int32(fdist(self.r)*np.sin(ang))+randint(-int(self.r),int(self.r)) #self.x = self.xparent + np.int32(fdist(self.r)*np.cos(ang))+randint(-int(self.r),int(self.r)) #self.y = self.yparent + np.int32(fdist(self.r)*np.sin(ang))+randint(-int(self.r),int(self.r)) #pass def moveX(self): self.x += self.r def mmoveX(self): self.x -= self.r def moveY(self): self.y += self.r def mmoveY(self): self.y -= self.r def initialize(self): ParserElement.setDefaultWhitespaceChars(' \t\r') integer = Regex(r"[+-]?\d+") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) number = Regex(r"[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?") \ .setParseAction(lambda s,l,t: [ float(t[0]) ]) color = Regex(r"#([0-9a-fA-F]{6})") angle = "'" + Regex(r"(360|3[0-5][0-9]|[12][0-9]{2}|[0-9]{1,2})") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) alpha = "'" + Regex(r"(360|3[0-5][0-9]|[12][0-9]{2}|[0-9]{1,2})") \ .setParseAction(lambda s,l,t: [ int(t[0]) ]) variable = Word(alphas, exact=1).setParseAction(self.addVar) colon = Literal(":").suppress() comma = Literal(",") lBrace = Literal("(") rBrace = Literal(")") lBracket = Literal("[") rBracket = Literal("]") lAngle = Literal("<") rAngle = Literal(">") plus = Literal("+") minus = Literal("-") FTerm = Literal("F") fTerm = Literal("f") ZTerm = Literal("Z") zTerm = Literal("z") xTerm = Literal("x") cTerm = Literal("c") eol = OneOrMore(LineEnd()).suppress() param = ( angle | color | "!" + number | "|" + number ) self.pList = lBrace + param + ZeroOrMore(comma + param) + rBrace literal = ((lBracket + ( variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList) ) + rBracket) | (variable + Optional(self.pList) | plus + Optional(self.pList) | minus + Optional(self.pList))) terminal = (ZTerm | zTerm | FTerm | fTerm | xTerm | cTerm | plus | minus | lBracket | rBracket) lprod = ( (OneOrMore(terminal) + lAngle + variable + rAngle + OneOrMore(terminal)) | (OneOrMore(terminal) + lAngle + variable) | (variable + rAngle + OneOrMore(terminal)) | variable ) rProd = OneOrMore(literal | terminal) comment = Suppress((LineStart() + "#" + SkipTo(eol, include=True))) rules = ( (lprod + Literal("=") + rProd + eol).setParseAction(self.addRule) \ | comment ) defaults = ( ( ("Dimensions" + colon + integer + comma + integer) | ("Position" + colon + integer + comma + integer) | ("Iterations" + colon + integer) | ("Angle" + colon + angle) | ("Linelength" + colon + number) | ("Linewidth" + colon + number) | ("Linecolor" + colon + color) | ("Background" + colon + color) | ("Axiom" + colon + rProd) ) + eol ).setParseAction(self.setAttribute) header = ( defaults | comment ) self.grammar = Suppress(ZeroOrMore(LineEnd())) \ + ZeroOrMore(header) \ + OneOrMore(rules) try: L = self.grammar.parseString( self.stream ) except ParseException, err: print err.line print " "*(err.column-1) + "^" print err print 'Rules:', self.rules
def parse(content, basedir=None, resolve=True): """parse a HOCON content :param content: HOCON content to parse :type content: basestring :param resolve: If true, resolve substitutions :type resolve: boolean :return: a ConfigTree or a list """ def norm_string(value): for k, v in ConfigParser.REPLACEMENTS.items(): value = value.replace(k, v) return 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) 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>[^"]*)\\1(?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(token): url = None file = None if len(token) == 1: # include "test" value = token[0].value if isinstance( token[0], ConfigQuotedString) else token[0] if value.startswith("http://") or value.startswith( "https://") or value.startswith("file://"): url = value else: file = value elif len(token) == 2: # include url("test") or file("test") value = token[1].value if isinstance( token[1], ConfigQuotedString) else token[1] if token[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) if 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, required=False, resolve=False) 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('".*?"[ \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]*' ).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_expr = (Keyword("include", caseless=True).suppress() + ( quoted_string | ( (Keyword('url') | Keyword('file')) - Literal('(').suppress() - quoted_string - 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: ConfigParser.resolve_substitutions(config) return config
RPC = Keyword('rpc') RETURNS = Keyword('returns') SERVICE = Keyword('service') OPTION = Keyword('option') ENUM = Keyword('enum') ONEOF = Keyword('oneof') REQUIRED = Keyword('required') OPTIONAL = Keyword('optional') REPEATED = Keyword('repeated') TRUE = Keyword('true') FALSE = Keyword('false') message_body = Forward() message_definition= Suppress(MESSAGE) - identifier("message_id") + Suppress(LBRACE) + message_body("message_body") + Suppress(RBRACE) message_definition.setParseAction(message_definition_fn) enum_definition= ENUM - identifier + LBRACE + ZeroOrMore(Group(identifier + EQ + integer + SEMI) ) + RBRACE DOUBLE = Keyword("double") INT32 = Keyword("int32") UINT32 = Keyword("uint32") BOOL = Keyword("bool") STRING = Keyword("string") type_ = (DOUBLE | UINT32 | BOOL | STRING | identifier) type_.setParseAction(type_fn) qualifier = (REQUIRED | OPTIONAL | REPEATED )("qualifier") qualifier.setParseAction(qualifier_fn) field = qualifier - type_("type_") + identifier("identifier") + EQ + integer("field_number") + SEMI field.setParseAction(field_fn)
def compute(self, text, verbose=True): # Literals dollar = Literal('$') amper = Literal('&') at = Literal('@') qm = Literal('?') em = Literal('!') dot = Literal('.') colon = Literal(":") vbar = Literal("|") lbrack = Literal("[") rbrack = Literal("]") lcurly = Literal("{") rcurly = Literal("}") lparen = Literal("(") rparen = Literal(")") lt = Literal("<") gt = Literal(">") eq = Literal("=") deq = Literal("==") eol = LineEnd().suppress() # Reusables spellId = Word(nums, min=2, max=6).addParseAction( tokenMap(int)).setResultsName("spellId") idx = Word(nums, max=1).addParseAction(tokenMap(int)).setResultsName("id") var = Word(alphas).setResultsName("var") # Spell References effectId = Optional( Word(nums, max=2).addParseAction( tokenMap(int)).setResultsName("effectId")) references = (dollar.suppress() + ((at.suppress() + var + Optional(spellId)) | (spellId + var + effectId) | (var + effectId))).addParseAction(self.setReferences) # Conditions brackets = Suppress(lbrack) + SkipTo(rbrack).setResultsName( "statement") + Suppress(rbrack) value = Word(nums, max=5).addParseAction( tokenMap(int)).setResultsName("value") conditionVar = Group( Optional(em).setResultsName("not") + Optional(var) + (spellId | idx) | Optional("-") + value | Word(alphanums, exact=8).setResultsName("hashVariable")) conditions = ((dollar + qm).suppress() + OneOrMore( Group( Optional(Suppress(qm)) + Optional(Suppress(lparen)) + OneOrMore( conditionVar.setResultsName("variables*") + Optional(Combine(em + eq) | amper | vbar | deq | lt | gt).setResultsName("operators*")) + Optional(Suppress(rparen)) + brackets).setResultsName("conditions*")) + brackets).addParseAction(lambda t: self.setConditions( t, verbose=verbose)) + Optional(dot.suppress()) # Call Variable callVariables = (Suppress((lt + dollar) | (dollar + lt)) + SkipTo(gt).setResultsName("name") + Suppress(gt)).addParseAction(self.callVariables) # Expressions expressions = ( Suppress(dollar + lcurly) + SkipTo(rcurly).setResultsName("content") + rcurly + Optional( dot.suppress() + Word(nums, exact=1).addParseAction( tokenMap(int)).setResultsName("mod"), ) ).addParseAction(lambda t: self.setExpressions(t, verbose=verbose)) # Language Choices languageChoices = ( (Literal('$L') | Literal('$l')).suppress() + OneOrMore(Word(alphas) + Optional(Literal(":").suppress()) ).setResultsName("options*") + Literal(';').suppress()).addParseAction(self.setLanguageChoices) # Icons icons = (Literal("|T").suppress() + SkipTo(colon).setResultsName("path") + colon.suppress() + Word(nums, exact=2).addParseAction( tokenMap(int)).setResultsName("size") + Literal("|t").suppress()).addParseAction(self.setIcons) # Parsing layer by layer parsingOrder = [ icons, languageChoices, callVariables, references, expressions, conditions ] steps = [text] for parser in parsingOrder: steps.append(parser.transformString(steps[-1])) result = steps[-1] # Colors endTag = ((vbar + (Literal("r") | Literal("R")) | eol)) parser = (Suppress(vbar + (Literal("c") | Literal("C"))) + Word(hexnums, exact=8).setResultsName("hex") + SkipTo(endTag).setResultsName("content") + Suppress(endTag)).addParseAction(self.colorize) new_result = parser.transformString(result) result = parser.transformString(new_result) while (new_result != result): new_result = result result = parser.transformString(new_result) # Replace each Sha1 Hash placeholder by refering value if verbose: for k, v in self.variables.items(): result = result.replace(k, str(v)) # Display fixes displayFixes = [["*% of", "% of"], ["power)%", "power)"]] for bef, aft in displayFixes: result = result.replace(bef, aft) # Normalize line breakers result = result.replace("\n\n\n", "\n\n").strip("\n") if self.formatType == "html" and verbose: result = result.replace("\r\n", "<br>").replace("\n", "<br>") return result