def apply(self, line, state): result = OperationResult(line, False) if not state.args.cull: return result trimmedLine = line.strip() words = trimmedLine.split() if (len(words) > 0): if (words[0].upper() == "REMSTART" or words[0].upper() == "REMEND"): self.inMultilineComment = not self.inMultilineComment result.discard = True return result if self.inMultilineComment: result.discard = True return result result.line = stripComments(line) stripped = result.line.strip() if stripped == "" or stripped == "\n" or stripped == "\r\n": result.discard = True if state.lastline: result.line = result.line.rstrip("\r\n") return result
def apply(self, line, state): result = OperationResult(line, False) skip = False if not self.stack.isEmpty(): #print(str(state.row) + " : " + str(self.stack.arr)) if self.stack.contains(False): skip = True dirsearch = regex['directive'].search(line) if dirsearch: directive = dirsearch.group(1) identifier = dirsearch.group(2) if directive == "#ifdef" or directive == "#ifndef" and not skip: result.line = commentLine(result.line) if identifier == "": result.error = "Invalid " + directive return result if not identifier in state.macros: self.stack.push(directive == "#ifndef") result.line = handleCulledLine(result.line) return result self.stack.push(directive != "#ifndef") elif directive == "#else": result.line = commentLine(result.line) if self.stack.isEmpty(): result.error = "Unexpected #else" return result if self.stack.top() == True: self.stack.pop() self.stack.push(False) else: self.stack.pop() self.stack.push(True) elif directive == "#endif": result.line = commentLine(result.line) if self.stack.isEmpty(): result.error = "Unexpected #endif" return result self.stack.pop() if skip: result.line = handleCulledLine(result.line) return result
def apply(self, line, state): result = OperationResult(line, False) if not state.args.anticrap: return result #trimmed = stripComments(line) output = line self.strings = scanForStrings(line) slashmatch = regex['backslash'].finditer(line) dotmatch = regex['typedot'].finditer(line) for m in slashmatch: if checkIfInsideString(m.start('backslash'), self.strings): continue substitution = "." output = output[:m.start('backslash')] + substitution + output[ m.end('backslash'):] for m in dotmatch: if checkIfInsideString(m.start('dot'), self.strings): continue substitution = "\\" output = output[:m.start('dot' )] + substitution + output[m.end('dot'):] result.line = output return result
def apply(self, line, state): result = OperationResult(line, False) if not state.args.anticrap: return result #trimmed = stripComments(line) output = line self.strings = scanForStrings(line) slashmatch = regex['backslash'].finditer(line) dotmatch = regex['typedot'].finditer(line) for m in slashmatch: if checkIfInsideString(m.start('backslash'), self.strings): continue substitution = "." output = output[:m.start('backslash')] + substitution + output[m.end('backslash'):] for m in dotmatch: if checkIfInsideString(m.start('dot'), self.strings): continue substitution = "\\" output = output[:m.start('dot')] + substitution + output[m.end('dot'):] result.line = output return result
def apply(self, line, state): result = OperationResult(line, False) if not state.args.dumb_debug: return result trimmed = stripComments(line) addition = ": " if isEmptyLine(trimmed): addition = "" if trimmed is not line: # there's a comment on this line #print("left: " + line[len(trimmed)-1:]) if isEmptyLine(trimmed): trimmed = line[:len(trimmed) - 1] + self.macrostring + addition + line[ len(trimmed) - 1:] else: trimmed = self.macrostring + addition + line else: trimmed = self.macrostring + addition + line result.line = trimmed return result
def apply(self, line, state): result = OperationResult(line, False) # strings = [] # string start and end positions as StringPos instances trimmed = stripComments(line) output = line expanded_line = line # find the directive dirsearch = regex['directive'].search(trimmed) if dirsearch: directive = dirsearch.group(1) identifier = dirsearch.group(2) origline = output output = commentLine(line) if directive == "#define": macro = Macro(dirsearch, trimmed) if macro != None: state.macros[macro.name] = macro elif directive == "#undef": temp_macro = Macro(dirsearch, trimmed) if temp_macro.name in state.macros: del state.macros[temp_macro.name] else: warnings.add( state.row, "Trying to undefine a nonexistent macro " + temp_macro.name) elif directive == "#pragma": if state.args.verbose: print("pragma: " + identifier) state.args = handlePragma(identifier, state.args) elif directive == "#include": if state.args.filedir: self.handleInclude(state, trimmed, state.args.filedir) else: # we'll leave #ifdef, #ifndef and #else to the other operators output = origline else: if state.args.nomacros: return result visited = Stack() #for name in self.macros: output = expandAll(line, state.macros, visited, state) result.line = output return result
def apply(self, line, state): result = OperationResult(line, False) #trimmed = stripComments(line) output = line self.strings = scanForStrings(line) output = self.replaceAll(output, regex['comment_start'], 'REMSTART', 'comment_start') output = self.replaceAll(output, regex['comment_end'], 'REMEND', 'comment_end') result.line = output return result
def apply(self, line, state): result = OperationResult(line, False) # strings = [] # string start and end positions as StringPos instances trimmed = stripComments(line) output = line expanded_line = line # find the directive dirsearch = regex['directive'].search(trimmed) if dirsearch: directive = dirsearch.group(1) identifier = dirsearch.group(2) origline = output output = commentLine(line) if directive == "#define": macro = Macro(dirsearch, trimmed) if macro != None: state.macros[macro.name] = macro elif directive == "#undef": temp_macro = Macro(dirsearch, trimmed) if temp_macro.name in state.macros: del state.macros[temp_macro.name] else: warnings.add(state.row, "Trying to undefine a nonexistent macro " + temp_macro.name) elif directive == "#pragma": if state.args.verbose: print("pragma: " + identifier) state.args = handlePragma(identifier, state.args) elif directive == "#include": if state.args.filedir: self.handleInclude(state, trimmed, state.args.filedir) else: # we'll leave #ifdef, #ifndef and #else to the other operators output = origline else: if state.args.nomacros: return result visited = Stack() #for name in self.macros: output = expandAll(line, state.macros, visited, state) result.line = output return result
def apply(self, line, state): result = OperationResult(line, False) if not state.args.minify: return result l = stripComments(line) strings = scanForStrings(l) commentStart = len(l) stringRegex = r'(("[^"]+")|(|[^"]*?)([^\s]*?))?' comments = r'(?P<comment>(|(\'|//)*$))' def string(s): if not s: return "" return s def replace(m, group): if checkIfInsideString(m.start(group), strings): return string(m.group(0)) return string(m.group(1)) + string(m.group(group)) ops = [] ops.append( Replacement( re.compile(r'' + stringRegex + '\s*(?P<op>[=+\-*/\><,\^]{1,2})\s*'), lambda m: replace(m, "op"))) ops.append( Replacement( re.compile(r'' + stringRegex + r'(?<=\D)(0)(?P<digit>\.\d+)'), lambda m: replace(m, "digit"))) #l = l.lstrip("\t") for o in ops: l = o.regex.sub(o.substitution, l) l = l.rstrip("\r\n") result.line = strInsert(result.line, 0, commentStart - 1, l) return result
def apply(self, line, state): result = OperationResult(line, False) if not state.args.minify: return result l = stripComments(line) strings = scanForStrings(l) commentStart = len(l) stringRegex = r'(("[^"]+")|(|[^"]*?)([^\s]*?))?' comments = r'(?P<comment>(|(\'|//)*$))' def string(s): if not s: return "" return s def replace(m, group): if checkIfInsideString(m.start(group), strings): return string(m.group(0)) return string(m.group(1)) + string(m.group(group)) ops = [] ops.append(Replacement(re.compile(r'' + stringRegex + '\s*(?P<op>[=+\-*/\><,\^]{1,2})\s*'), lambda m: replace(m, "op"))) ops.append(Replacement(re.compile(r'' + stringRegex + r'(?<=\D)(0)(?P<digit>\.\d+)'), lambda m: replace(m, "digit") )) #l = l.lstrip("\t") for o in ops: l = o.regex.sub(o.substitution, l) l = l.rstrip("\r\n") result.line = strInsert(result.line, 0, commentStart-1, l) return result
def apply(self, line, state): result = OperationResult(line, False) if not state.args.dumb_debug: return result trimmed = stripComments(line) addition = ": " if isEmptyLine(trimmed): addition = "" if trimmed is not line: # there's a comment on this line #print("left: " + line[len(trimmed)-1:]) if isEmptyLine(trimmed): trimmed = line[:len(trimmed)-1] + self.macrostring + addition + line[len(trimmed)-1:] else: trimmed = self.macrostring + addition + line else: trimmed = self.macrostring + addition + line result.line = trimmed return result
def apply(self, line, state): result = OperationResult(line, False) trimmed = stripComments(line) output = line expanded_line = line # find the directive dirsearch = regex['directive'].search(trimmed) if dirsearch: directive = dirsearch.group(1) identifier = dirsearch.group(2) output = commentLine(line) if directive == "#macro": if self.macro: warnings.add("Trying to define multiline macro %s inside other macro %s" % (identifier, self.macro.name)) return result self.macro = Macro(dirsearch, trimmed) self.payload = "" elif directive == "#endmacro": #print ("macro %s ends") % (self.macro.name) self.macro.payload = self.macro.payload.rstrip('\n') self.multimacros[self.macro.name] = self.macro self.macro = None # TODO this is basically duplicate of the above functionality, clean it up elif directive == "#lambda": if self.macro: warnings.add("Trying to define multiline lambda %s inside other macro %s" % (identifier, self.macro.name)) return result self.macro = Macro(dirsearch, trimmed) self.macro.oneliner = True self.payload = "" elif directive == "#endlambda": #print ("macro %s ends") % (self.macro.name) self.macro.payload = self.macro.payload.rstrip('\n') self.macro.payload = re.sub("\n+", r':', self.macro.payload) self.multimacros[self.macro.name] = self.macro self.macro = None elif directive == "#undef": temp_macro = Macro(dirsearch, trimmed) if temp_macro.name in self.multimacros: del self.multimacros[temp_macro.name] else: output = output else: if state.args.nomacros: return result # Expand collected macros only if not inside a multiline macro. if self.macro: line_trimmed = line if self.macro.oneliner: line_trimmed = line.lstrip(" ").lstrip("\t") self.macro.payload += line_trimmed output = commentLine(output) else: output = expandAll(line, self.multimacros, Stack(), state) result.line = output return result
def apply(self, line, state): result = OperationResult(line, False) trimmed = stripComments(line) output = line expanded_line = line # find the directive dirsearch = regex['directive'].search(trimmed) if dirsearch: directive = dirsearch.group(1) identifier = dirsearch.group(2) output = commentLine(line) if directive == "#macro": if self.macro: warnings.add( "Trying to define multiline macro %s inside other macro %s" % (identifier, self.macro.name)) return result self.macro = Macro(dirsearch, trimmed) self.payload = "" elif directive == "#endmacro": #print ("macro %s ends") % (self.macro.name) self.macro.payload = self.macro.payload.rstrip('\n') self.multimacros[self.macro.name] = self.macro self.macro = None # TODO this is basically duplicate of the above functionality, clean it up elif directive == "#lambda": if self.macro: warnings.add( "Trying to define multiline lambda %s inside other macro %s" % (identifier, self.macro.name)) return result self.macro = Macro(dirsearch, trimmed) self.macro.oneliner = True self.payload = "" elif directive == "#endlambda": #print ("macro %s ends") % (self.macro.name) self.macro.payload = self.macro.payload.rstrip('\n') self.macro.payload = re.sub("\n+", r':', self.macro.payload) self.multimacros[self.macro.name] = self.macro self.macro = None elif directive == "#undef": temp_macro = Macro(dirsearch, trimmed) if temp_macro.name in self.multimacros: del self.multimacros[temp_macro.name] else: output = output else: if state.args.nomacros: return result # Expand collected macros only if not inside a multiline macro. if self.macro: line_trimmed = line if self.macro.oneliner: line_trimmed = line.lstrip(" ").lstrip("\t") self.macro.payload += line_trimmed output = commentLine(output) else: output = expandAll(line, self.multimacros, Stack(), state) result.line = output return result