From cc28df8088c693aee3200cf4f5fc45324ad40a24 Mon Sep 17 00:00:00 2001 From: Hannes Schulz Date: Mon, 20 Jul 2015 16:48:40 +0200 Subject: [PATCH 1/7] correctly parse nested braces --- latexrun | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/latexrun b/latexrun index 557a93c..954585e 100755 --- a/latexrun +++ b/latexrun @@ -230,6 +230,18 @@ def mkdir_p(path): pass else: raise +def nested_parenthesis_end(string, opening, closing): + """Return index where closing character corresponds to opening character""" + stack = [] + for i, c in enumerate(string): + if c == opening: + stack.append(i) + elif c == closing and stack: + start = stack.pop() + if not stack: + return i + return -1 + class DB: """A latexrun control database.""" @@ -1276,12 +1288,11 @@ class LaTeXFilter: elif ch == '{': # TeX uses this for various things we want to ignore, like # file names and print_mark. Consume up to the '}' - epos = self.__data.find('}', self.__pos) - if epos != -1: - self.__pos = epos + 1 - else: + epos = nested_parenthesis_end(self.__data[self.__pos-1:], '{', '}') + if epos == -1: self.__message('warning', None, "unbalanced `{' in log; file names may be wrong") + self.__pos += epos + 1 elif ch == '}': self.__message('warning', None, "extra `}' in log; file names may be wrong") From dd0629fc215b1abd50b224e3f952aa1948721bde Mon Sep 17 00:00:00 2001 From: Hannes Schulz Date: Mon, 28 Sep 2015 12:26:24 +0200 Subject: [PATCH 2/7] remove trailing space for testing --- latexrun | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/latexrun b/latexrun index 954585e..ce6bab0 100755 --- a/latexrun +++ b/latexrun @@ -1284,7 +1284,7 @@ class LaTeXFilter: self.__file_stack.pop() else: self.__message('warning', None, - "extra `)' in log; file names may be wrong ") + "extra `)' in log; file names may be wrong") elif ch == '{': # TeX uses this for various things we want to ignore, like # file names and print_mark. Consume up to the '}' From 0ca9874bde84097715f77d3c809b574da426ad17 Mon Sep 17 00:00:00 2001 From: Hannes Schulz Date: Mon, 28 Sep 2015 12:27:27 +0200 Subject: [PATCH 3/7] optionally look for mismatched [{()}] --- latexrun | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/latexrun b/latexrun index ce6bab0..1fb1d0d 100755 --- a/latexrun +++ b/latexrun @@ -79,7 +79,7 @@ def main(): action=ArgParserWarnAction, dest='nowarns', default=set(['underfull']), help='Enable/disable warning from CLASS, which can be any package name, ' 'LaTeX warning class (e.g., font), bad box type ' - '(underfull, overfull, loose, tight), or "all"') + '(underfull, overfull, loose, tight), strict parsing (strict-parse), or "all"') arg_parser.add_argument( '-O', metavar='DIR', dest='obj_dir', default='latex.out', help='Directory for intermediate files and control database ' @@ -230,14 +230,17 @@ def mkdir_p(path): pass else: raise -def nested_parenthesis_end(string, opening, closing): +def nested_parenthesis_end(string, opening, closing, lax_checking=False): """Return index where closing character corresponds to opening character""" stack = [] for i, c in enumerate(string): - if c == opening: - stack.append(i) - elif c == closing and stack: - start = stack.pop() + if c in opening: + stack.append(c) + elif c in closing and stack: + start_ch = stack.pop() + if not lax_checking and opening.index(start_ch) != closing.index(c): + # Mismatch, e.g. expected ')', found '}' + return -1 if not stack: return i return -1 @@ -1288,7 +1291,9 @@ class LaTeXFilter: elif ch == '{': # TeX uses this for various things we want to ignore, like # file names and print_mark. Consume up to the '}' - epos = nested_parenthesis_end(self.__data[self.__pos-1:], '{', '}') + lax_checking = "strict-parse" in self.__suppress + epos = nested_parenthesis_end(self.__data[self.__pos-1:], '{[(', '}])', + lax_checking=lax_checking) if epos == -1: self.__message('warning', None, "unbalanced `{' in log; file names may be wrong") From cf52fd753ed8146b2e6e25b70b5dc806463f9bd1 Mon Sep 17 00:00:00 2001 From: Hannes Schulz Date: Mon, 28 Sep 2015 12:27:38 +0200 Subject: [PATCH 4/7] tests for mismatched braces --- test/T-mismatched-brace.tex | 16 ++++++++++++++++ test/T-mismatched-brace2.tex | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/T-mismatched-brace.tex create mode 100644 test/T-mismatched-brace2.tex diff --git a/test/T-mismatched-brace.tex b/test/T-mismatched-brace.tex new file mode 100644 index 0000000..e80cbec --- /dev/null +++ b/test/T-mismatched-brace.tex @@ -0,0 +1,16 @@ +% Test mismatched braces parsing warning. + +\documentclass{article} + +\begin{document} + + \typeout{Error \{ foo bar } + + baz. + +\end{document} + +%% status: 0 + +%% output: +%% T-mismatched-brace.tex: warning: unbalanced `{' in log; file names may be wrong diff --git a/test/T-mismatched-brace2.tex b/test/T-mismatched-brace2.tex new file mode 100644 index 0000000..859d62d --- /dev/null +++ b/test/T-mismatched-brace2.tex @@ -0,0 +1,19 @@ +% Test mismatched braces parsing warning. + +\documentclass{article} + +\begin{document} + + \typeout{Error \{ foo bar ) \} } + + baz. + +\end{document} + +%% status: 0 + +%% output: +%% T-mismatched-brace2.tex: warning: unbalanced `{' in log; file names may be wrong +%% T-mismatched-brace2.tex: warning: extra `}' in log; file names may be wrong +%% T-mismatched-brace2.tex: warning: extra `)' in log; file names may be wrong + From 690eb5bf3695c1054fbc5f436237d45a1d926c5d Mon Sep 17 00:00:00 2001 From: Hannes Schulz Date: Mon, 28 Sep 2015 12:28:02 +0200 Subject: [PATCH 5/7] T-nooutput used bash features, call w/ bash --- test/run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/run b/test/run index 3562da1..b1323dd 100755 --- a/test/run +++ b/test/run @@ -94,7 +94,7 @@ def test(latexrun_path, latexrun_args, input_path): if input_path.endswith('.tex'): cmd = latexrun_cmd + [os.path.basename(input_path)] elif input_path.endswith('.sh'): - cmd = ['sh', os.path.basename(input_path)] + latexrun_cmd + cmd = ['bash', os.path.basename(input_path)] + latexrun_cmd p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=input_dir or None) From f80c5f280bdbb720864ffc6fcfcac7b4f2421c8d Mon Sep 17 00:00:00 2001 From: Hannes Schulz Date: Mon, 28 Sep 2015 15:12:20 +0200 Subject: [PATCH 6/7] fix consuming of overfull hbox offending text text containing parentheses could break file stack. --- latexrun | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/latexrun b/latexrun index 1fb1d0d..5c493eb 100755 --- a/latexrun +++ b/latexrun @@ -1439,14 +1439,15 @@ class LaTeXFilter: return # Back up to the end of the known message text self.__pos = origpos + m.end() - if self.__lookingat('\n'): + if self.__lookingatre(r'\s*\n'): # We have a newline, so consume it and look for the # offending text. self.__pos += 1 # If there is offending text, it will start with a font # name, which will start with a \. - if 'hbox' in msg and self.__lookingat('\\'): - self.__consume_line(unwrap=True) + if 'hbox' in msg and self.__lookingatre(r'(\[\]\s?)*\s*\\'): + consumed = self.__consume_line(unwrap=True) + if self.TRACE: print('consuming `<{}>\''.format(consumed)) msg = self.__simplify_message(msg) + ' (page {})'.format(self.__pageno) cls = msg.split(None, 1)[0].lower() self.__message('warning', lineno, msg, cls=cls) From 2a321b6006273947f178926d35e05b10b93afb90 Mon Sep 17 00:00:00 2001 From: Hannes Schulz Date: Sun, 14 Oct 2018 10:41:02 +0200 Subject: [PATCH 7/7] fix off-by-one error --- latexrun | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/latexrun b/latexrun index 5c493eb..bcb73b2 100755 --- a/latexrun +++ b/latexrun @@ -1297,7 +1297,8 @@ class LaTeXFilter: if epos == -1: self.__message('warning', None, "unbalanced `{' in log; file names may be wrong") - self.__pos += epos + 1 + else: + self.__pos += epos elif ch == '}': self.__message('warning', None, "extra `}' in log; file names may be wrong")