def __init__(self, st, locn, tokString): self.token_string = tokString self.loc = locn self.before_line = line(locn - 1, st) self.source_line = line(locn, st) self.line_num = lineno(locn, st) self.col = col(locn, st)
def check_type(self, arg, argt): print "You hit a bug! Yipeee!" sys.exit(1) if (isinstance(arg, tuple) or isinstance(arg, list)): argt=re.sub('_ARRAY', '', argt) for i in arg: self.check_type(i, argt) if argt == 'INT': if not ival.match(arg): print 'Invalid type on line %d: Not an int: \n -> %s' % ( lineno(self.loc,self.strg), line(self.loc, self.strg).strip()) sys.exit(1) elif argt == 'DBL': if not dval.match(arg): print 'Invalid type on line %d: Not a float: \n -> %s' % ( lineno(self.loc,self.strg), line(self.loc, self.strg).strip()) sys.exit(1) elif argt == 'BOOL': if not lval.match(arg): print 'Invalid type on line %d: Not a bool: \n -> %s' % ( lineno(self.loc,self.strg), line(self.loc, self.strg).strip()) sys.exit(1) elif argt != 'STR': print 'Invalid type on line %d: Not a %s: \n -> %s' % ( lineno(self.loc,self.strg), argt, line(self.loc, self.strg).strip()) sys.exit(1) return argt
def check_vectype(self, arg, argt): if argt == 'INT_ARRAY': for i in arg: if not ival.match(i): print('Invalid type on line %d: Not an int: \n -> %s' % (lineno(self.loc, self.strg), line(self.loc, self.strg).strip())) sys.exit(1) elif argt == 'DBL_ARRAY': for i in arg: if not dval.match(i): print('Invalid type on line %d: Not a float: \n -> %s' % (lineno(self.loc, self.strg), line(self.loc, self.strg).strip())) sys.exit(1) elif argt == 'BOOL_ARRAY': for i in arg: if not lval.match(i): print('Invalid type on line %d: Not a bool: \n -> %s' % (lineno(self.loc, self.strg), line(self.loc, self.strg.strip()))) sys.exit(1) elif argt != 'STR': print('Invalid type on line %d: Not a %s: \n -> %s' % (lineno( self.loc, self.strg), argt, line(self.loc, self.strg).strip())) sys.exit(1) return argt
def check_type(self, arg, argt): if argt == 'INT': if not ival.match(arg): print 'Invalid type on line %d: Not an int: \n -> %s' % ( lineno(self.loc,self.strg), line(self.loc, self.strg).strip()) sys.exit(1) elif argt == 'DBL': if not dval.match(arg): print 'Invalid type on line %d: Not a float: \n -> %s' % ( lineno(self.loc,self.strg), line(self.loc, self.strg).strip()) sys.exit(1) elif argt == 'BOOL': if not lval.match(arg): print 'Invalid type on line %d: Not a bool: \n -> %s' % ( lineno(self.loc,self.strg), line(self.loc, self.strg).strip()) sys.exit(1) elif argt != 'STR': print 'Invalid type on line %d: Not a %s: \n -> %s' % ( lineno(self.loc,self.strg), argt, line(self.loc, self.strg).strip()) sys.exit(1) return argt
def rExp_condition_action(self, text, loc, arg): logging.getLogger(__name__).debug( "rExp_condition_action {0} {1}".format(lineno(loc, text), arg)) try: code = [ "# line {0} rExp_condition {1}".format(lineno(loc, text), line(loc, text)) ] condition_code = arg.condition.rExp['code'] if isinstance(condition_code, str): if 'not_' in arg['condition']: code += [" CMPEQUAL NULL"] else: code += [" CMPNOTEQUAL NULL"] arg['code'] = code else: if 'not_' in arg['condition']: arg['code'] = { False: condition_code[True], True: condition_code[False] } else: arg['code'] = condition_code except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def while_action(self, text, loc, arg): logging.getLogger(__name__).debug("while_action {0} {1}".format( lineno(loc, text), arg)) try: block0 = [ "# line {0} while_statement {1}".format( lineno(loc, text), line(loc, text)) ] if 'code' in arg.condition: if isinstance(arg.condition.code, list): block1 = arg.condition.code JMPCMD = arg.condition.get('jmpcmd', {False: "JMPNCMP"})[False] else: JMPCMD = arg.condition.code[True] block1 = [] elif 'rExp' in arg.condition and 'code' in arg.condition.rExp: if isinstance(arg.condition.rExp.code, list): block1 = arg.condition.rExp.code JMPCMD = arg.condition.rExp.get('jmpcmd', "JMPNCMP") else: JMPCMD = arg.condition.rExp.code[True] block1 = [] block2 = arg.statementBlock.statementBlock['code'] arg['code'] = [ WhileGenerator(self.symbols, JMPCMD, block0, block1, block2) ] logging.getLogger(__name__).debug("while_action generated code ") except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def _setLineAndLineNumberParseAction( self, s: str, loc: int, toks: pyparsing.ParseResults) -> pyparsing.ParseResults: ''' helper that is meant to be used with `<ParserElement>.setParseAction`, which means this function gets called whenever a match for each line of the Telegram TL file gets hit. Here we add stuff to the ParseResults that it passes in we add the source line and source line number to each match @param s is the original parse string @param loc is the location in the string where matching started @param toks is the list of the matched tokens, packaged as a ParseResults object @return a ParseResults if you are modifying it, else None ''' t = toks orig_line = pyparsing.line(loc, s) line_number = pyparsing.lineno(loc, s) column = pyparsing.col(loc, s) logger.debug( "_setLineAndLineNumberParseAction: line_number: `%s`, column: `%s`, line: `%s`", line_number, column, orig_line) t[constants.RESULT_NAME_SOURCE_LINE] = orig_line t[constants.RESULT_NAME_SOURCE_LINE_NUMBER] = line_number return t
def getMessage(pstr, pos, filepath=''): line = pyparsing.line(pos, pstr); lineno = pyparsing.lineno(pos, pstr); col = pyparsing.col(pos, pstr) arrow = ( '-' * (col-1) + '^'); ls = os.linesep + ' '; return ' in file {filepath} (line: {lineno}, col: {col}){ls}{line}{ls}{arrow}'.format(**locals());
def addassignement_action(self, text, loc, arg): logging.getLogger(__name__).debug( "addassignement_action {0} {1}".format(lineno(loc, text), arg)) try: code = [ "# line {0}: add_assignment: {1}".format( lineno(loc, text), line(loc, text)) ] if arg.rval[0] == '1' and arg.op in ['+=', '-=']: self.symbols.getVar(arg.lval) if arg.op == "+=": code.append(" INC {0}".format(arg.lval)) else: code.append(" DEC {0}".format(arg.lval)) else: if 'code' in arg.rval: code += arg.rval.code self.symbols.getVar(arg.lval) if arg.op == "-=": raise CompileException( "-= with expression needs to be fixed in the compiler" ) code.append(" {0} {1}".format(opassignmentLookup[arg.op], arg.lval)) elif 'identifier' in arg.rval: self.symbols.getVar(arg.rval.identifier) code.append(" LDWR {0}".format(arg.lval)) self.symbols.getVar(arg.lval) code.append(" {0} {1}".format(opassignmentLookup[arg.op], arg.rval.identifier)) code.append(" STWR {0}".format(arg.lval)) arg['code'] = code except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def if_action(self, text, loc, arg): logging.getLogger(__name__).debug("if_action {0} {1}".format( lineno(loc, text), arg)) try: block0 = [ "# line {0} if statement {1}".format(lineno(loc, text), line(loc, text)) ] if isinstance(arg.condition.code, list): block0 += arg.condition.code JMPCMD = arg.condition.get('jmpcmd', {False: "JMPNCMP"})[False] else: JMPCMD = arg.condition.code[True] if 'elseblock' in arg: block1 = arg.ifblock.ifblock.code block2 = arg.elseblock.elseblock[ 'code'] if 'elseblock' in arg.elseblock else arg.elseblock[ 'code'] else: block1 = arg.ifblock.ifblock['code'] block2 = None arg['code'] = [ IfGenerator(self.symbols, JMPCMD, block0, block1, block2) ] except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def assignment_action(self, text, loc, arg): logging.getLogger(__name__).debug("assignment_action {0} {1}".format( lineno(loc, text), arg)) try: code = [ "# line {0} assignment {1}".format(lineno(loc, text), line(loc, text)) ] rval_code = find_and_get(arg.rval, 'code') if rval_code is not None: code += arg.rval.code elif arg.rval == "*P": code.append(" LDWI") elif 'identifier' in arg: self.symbols.getVar(arg.identifier) code.append(" LDWR {0}".format(arg.identifier)) if arg.lval == "*P": code.append(" STWI") elif arg.lval != "W": symbol = self.symbols.getVar(arg.lval) code.append(" STWR {0}".format(symbol.name)) if 'code' in arg: arg['code'].extend(code) else: arg['code'] = code except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def read_include_contents(st, locn, toks): """ This parser hook routine (as noted by (st, locn, toks) arguments) will open another file as pointed to by the toks argument :param st: the original string being parsed (see note below) :param locn: the location of the matching substring :param toks: a list of the matched tokens, packaged as a ParseResults object :return: Another long string containing content of include file """ global g_include_depth # If include file is an absolute path, make it relative to g_root_dir if toks.include_filepath[0:1] == '/': include_file_ref = g_root_dir + toks.include_filepath else: # If include file is a relative, then it is relative to g_root_dir include_file_ref = g_root_dir + '/' + toks.include_filepath if args.v: print('Found:', include_file_ref) g_include_depth = g_include_depth + 1 # Add a a_comment line into expanded master include file # for later post-error analysis # Do not wrap C-style a_comment ourselves of this same line because # original line may too have this "/* ... */" # Only way to avoid already-used but inlined C-style a_comment is to # create a separate line # TODO: Strip C-style a_comment from line # TODO: Better cyclical detection of file include recursion include_echo = "#{}# {}\n".format(g_progname, pp.line(locn, st).strip()) include_begin_echo = "# Begin of {} file.\n# Nested include-file depth: {}".format( include_file_ref, # pp.line(locn, st).strip(), g_include_depth) include_end_echo = "# Nested include-file depth: {}\n# End of {} file.".format( g_include_depth, # pp.line(locn, st).strip(), include_file_ref) # Check if file exist, gracefully raise exception next_include_file = '' try: if g_verbosity: print('Opening', include_file_ref) next_include_file = Path(include_file_ref).read_text() except FileNotFoundError as _ric_err: print('read_include_contents: err:', _ric_err) raise FileNotFoundError # guard against recursive includes (doesn't work for reuse of include files) result_include_line = include_echo \ + include_begin_echo \ + '\n' \ + g_include_directive.transformString(next_include_file) \ + include_end_echo g_include_depth = g_include_depth - 1 return result_include_line
def push_sect(self,k): self.stack.append(k) self.cur=self.stack[-1] if self.templ is not None: x=self.path[-1].findsect(k.name) if x is None: print "Invalid section on line %d: \n%s" % ( lineno(self.loc,self.strg), line(self.loc,self.strg)) sys.exit(1) self.path.append(x)
def push_sect(self, k): self.stack.append(k) self.cur = self.stack[-1] if self.templ is not None: x = self.path[-1].fetch_sect(k.name) if x is None: print("Invalid section on line {:d}: \n{}".format( lineno(self.loc, self.strg), line(self.loc, self.strg))) sys.exit(1) self.path.append(x)
def __init__(self, message, print_location=True): super(SemanticException, self).__init__() self._message = message self.location = exshared.location self.print_location = print_location if exshared.location is not None: self.line = lineno(exshared.location, exshared.text) self.col = col(exshared.location, exshared.text) self.text = line(exshared.location, exshared.text) else: self.line = self.col = self.text = None
def in_string(location, code): """Determines if the given location is in a string inside of code. Does not detect triple-quoted multi-line strings.""" str_char = None for c in line(location, code)[:col(location, code) - 1]: if c == str_char: str_char = None elif c in "\"'": str_char = c return str_char is not None
def read_include_contents(s, l, t): include_file_ref = t.include_file_name include_echo = "/* {} */".format(pp.line(l, s).strip()) # guard against recursive includes if include_file_ref not in seen: seen.add(include_file_ref) included_file_contents = Path(include_file_ref).read_text() return (include_echo + '\n' + include_directive.transformString(included_file_contents)) else: lead = ' ' * (pp.col(l, s) - 1) return "/* recursive include! */\n{}{}".format(lead, include_echo)
def opexpression_action(self, text, loc, arg): try: logging.getLogger(__name__).debug( "opexpression_action {0} {1}".format(lineno(loc, text), arg)) code = [ "# line {0}: shiftexpression {1}".format( lineno(loc, text), line(loc, text)), " LDWR {0}".format(arg.operand), " {0} {1}".format(shiftLookup[arg.op], arg.argument.identifier) ] arg['code'] = code logging.getLogger(__name__).debug( "shiftexpression generated code {0}".format(code)) except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def condition_action(self, text, loc, arg): logging.getLogger(__name__).debug("condition_action {0} {1}".format( lineno(loc, text), arg)) try: code = [ "# line {0} condition {1}".format(lineno(loc, text), line(loc, text)) ] if arg.leftidentifier != "W": self.symbols.getVar(arg.leftidentifier) code.append(' LDWR {0}'.format(arg.leftidentifier)) if arg.identifier == 'NULL' and arg.comparison in jmpNullCommands: arg['jmpcmd'] = jmpNullCommands[arg.comparison] else: code.append(' {0} {1}'.format( comparisonCommands[arg.comparison], arg.identifier)) arg["code"] = code except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def procedurecall_action(self, text, loc, arg): try: logging.getLogger(__name__).debug( "procedurecall_action {0} {1}".format(lineno(loc, text), arg)) procedure = self.symbols.getProcedure(arg[0]) code = [ "# line {0}: procedurecall {1}".format(lineno(loc, text), line(loc, text)) ] opcode = procedure.codegen(self.symbols, arg=arg.asList(), kwarg=arg.asDict()) if isinstance(opcode, list): code += opcode else: code = opcode arg['code'] = code logging.getLogger(__name__).debug( "procedurecall generated code {0}".format(code)) except Exception as e: raise CompileException(text, loc, str(e), self) return arg
def process_int_literal(s, loc, toks): try: return Integer(int(toks[0][1:])) except ValueError: raise BibTeXError('%i:%i invalid integer literal\n%s' % (lineno(loc, s), col(loc, s), line(loc, s)))
def parse_error(s, t, d, err): print("Parse error, line {:d}: {}".format( lineno(err.loc, err.pstr), line(err.loc, err.pstr))) sys.exit(1)
def __init__(self, s, loc, tokens): self.described_at = ParseLocation(self, s, loc) self.text = line(loc, s)
def line(self): return line( self.loc, self.pstr )
def __init__(self, s, loc, toks): self.col = pyp.col(loc, s) self.lineno = pyp.lineno(loc, s) self.line = pyp.line(loc, s) self.value = self._fromtoken(toks[0])
def line(self): return p.line(self.start_offset, self.string)
def parse_statement(self, statement, orig_contents): """Parse a statement, possibly called recursively. Args: statement (int, ParseResult): The pyparsing parse result that contains one statement prepended with the match location orig_contents (str): The original contents of the file that we're parsing in case we need to convert an index into a line, column pair. Returns: SensorGraphStatement: The parsed statement. """ children = [] is_block = False name = statement.getName() # Recursively parse all children statements in a block # before parsing the block itself. # If this is a non-block statement, parse it using the statement # parser to figure out what specific statement it is before # processing it further. # This two step process produces better syntax error messsages if name == 'block': children_statements = statement[1] for child in children_statements: parsed = self.parse_statement(child, orig_contents=orig_contents) children.append(parsed) locn = statement[0]['location'] statement = statement[0][1] name = statement.getName() is_block = True else: stmt_language = get_statement() locn = statement['location'] statement = statement['match'] statement_string = str(u"".join(statement.asList())) # Try to parse this generic statement into an actual statement. # Do this here in a separate step so we have good error messages when there # is a problem parsing a step. try: statement = stmt_language.parseString(statement_string)[0] except (pyparsing.ParseException, pyparsing.ParseSyntaxException) as exc: raise SensorGraphSyntaxError( "Error parsing statement in sensor graph file", message=exc.msg, line=pyparsing.line(locn, orig_contents).strip(), line_number=pyparsing.lineno(locn, orig_contents), column=pyparsing.col(locn, orig_contents)) except SensorGraphSemanticError as exc: # Reraise semantic errors with line information raise SensorGraphSemanticError( exc.msg, line=pyparsing.line(locn, orig_contents).strip(), line_number=pyparsing.lineno(locn, orig_contents), **exc.params) name = statement.getName() if name not in statement_map: raise ArgumentError("Unknown statement in sensor graph file", parsed_statement=statement, name=name) # Save off our location information so we can give good error and warning information line = pyparsing.line(locn, orig_contents).strip() line_number = pyparsing.lineno(locn, orig_contents) column = pyparsing.col(locn, orig_contents) location_info = LocationInfo(line, line_number, column) if is_block: return statement_map[name](statement, children=children, location=location_info) return statement_map[name](statement, location_info)
def __init__(self, text, loc, msg): self.line = line(loc, text) self.col = col(loc, text) self.lineno = lineno(loc, text) self.msg = msg
def line(self): return line(self.loc, self.pstr)
def handle_error_and_exit(source, error): print('Error parsing {}: {}'.format(source, error)) print(pyparsing.line(error.loc, string)) print((error.col - 1) * ' ' + '^') sys.exit(1)
def parse_error(s,t,d,err): print "Parse error, line %d: %s" % ( lineno(err.loc,err.pstr), line(err.loc,err.pstr)) sys.exit(1)
def line(self): return p.line(self.offset, self.string)
def line(self): return pp.line(self.loc, self.ps)