diff --git a/pykat/finesse.py b/pykat/finesse.py index 4c89389b472bd087999ee0de81630e545532e1b6..313af7fe3876b48b65d1c3f8cd957147e00afe9d 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -32,6 +32,7 @@ import datetime import pickle import pykat import warnings +import re from pykat.exceptions import * @@ -116,14 +117,22 @@ class kat(object): def noxaxis(self,value): self.__noxaxis = bool(value) def loadKatFile(self, katfile): - with open(katfile) as f: - self.parseCommands(f.readlines()) + commands=open(katfile).read() + self.parseCommands(commands) + def parseKatCode(self, code): + #commands = code.split("\n") + self.parseCommands(commands) + def parseCommands(self, commands): blockComment = False self.__currentTag= "" - + + commands=self.remove_comments(commands) + for line in commands.split("\n"): + print line + #for line in commands: if len(line.strip()) >= 2: line = line.strip() @@ -523,3 +532,17 @@ class kat(object): def __get_component(self, name): return getattr(self, '__comp_' + name) + + def remove_comments(self, string): + pattern = r"(\".*?\"|\'.*?\'|%{3}[^\r\n]*$)|(/\*.*?\*/|%[^\r\n]*$|#[^\r\n]*$|//[^\r\n]*$)" + # first group captures quoted strings (double or single) + # second group captures comments (//single-line or /* multi-line */) + regex = re.compile(pattern, re.MULTILINE|re.DOTALL) + def _replacer(match): + # if the 2nd group (capturing comments) is not None, + # it means we have captured a non-quoted (real) comment string. + if match.group(2) is not None: + return "" # so we will return empty to remove the comment + else: # otherwise, we will return the 1st group + return match.group(1) # captured quoted-string + return regex.sub(_replacer, string)