From c237f700adcd8436b5db01a4bb84f3e72f34f9f0 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 28 Sep 2015 14:02:20 +0700 Subject: [PATCH] Fix bug in tokenize algorithm The algorithm was failing to add intersections after right parentheses when appropriate. --- openmc/region.py | 13 +++++++++---- src/string.F90 | 8 ++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/openmc/region.py b/openmc/region.py index 025babba8..a89658c03 100644 --- a/openmc/region.py +++ b/openmc/region.py @@ -27,6 +27,9 @@ class Region(object): """ + # Strip leading and trailing whitespace + expression = expression.strip() + # Convert the string expression into a list of tokens, i.e., operators # and surface half-spaces, representing the expression in infix # notation. @@ -49,13 +52,15 @@ class Region(object): # to the list of tokens tokens.append(expression[i]) else: - # For spaces, we need to check the context further. If it - # doesn't appear before a right parentheses or union, it is - # interpreted to be as an intersection operator + # Find next non-space character while expression[i+1] == ' ': i += 1 - if i_start >= 0 and expression[i+1] not in ')^': + # If previous token is a halfspace or right parenthesis and next token + # is not a left parenthese or union operator, that implies that the + # whitespace is to be interpreted as an intersection operator + if (i_start >= 0 or tokens[-1] == ')') and \ + expression[i+1] not in ')^': tokens.append(' ') i_start = -1 diff --git a/src/string.F90 b/src/string.F90 index 213627892..b505aa869 100644 --- a/src/string.F90 +++ b/src/string.F90 @@ -108,10 +108,10 @@ contains i = i + 1 end do - ! If previous token is not an operator and next token is not a left - ! parenthese or union operator, that implies that the whitespace is to - ! be interpreted as an intersection operator - if (i_start > 0) then + ! If previous token is a halfspace or right parenthesis and next token + ! is not a left parenthese or union operator, that implies that the + ! whitespace is to be interpreted as an intersection operator + if (i_start > 0 .or. tokens%data(tokens%size()) == OP_RIGHT_PAREN) then if (index(')^', string_(i+1:i+1)) == 0) then call tokens%push_back(OP_INTERSECTION) end if