Skip to content

Commit

Permalink
Update expr_parser code for new location
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-slac committed Jan 4, 2019
1 parent 2022112 commit f917f3a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 61 deletions.
3 changes: 3 additions & 0 deletions python/lsst/daf/butler/expr_parser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .exprTree import *
from .parserLex import *
from .parserYacc import *
2 changes: 1 addition & 1 deletion python/lsst/daf/butler/expr_parser/exprTree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is part of pipe_supertask.
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/daf/butler/expr_parser/parserLex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is part of pipe_supertask.
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
Expand All @@ -24,7 +24,7 @@

from __future__ import absolute_import, division, print_function

__all__ = []
__all__ = ["ParserLex", "ParserLexError"]

# -------------------------------
# Imports of standard modules --
Expand Down
4 changes: 3 additions & 1 deletion python/lsst/daf/butler/expr_parser/parserYacc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is part of pipe_supertask.
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
Expand All @@ -22,6 +22,8 @@
"""Syntax definition for user expression parser.
"""

__all__ = ["ParserYacc", "ParseError", "ParserYaccError", "ParserEOFError"]

# -------------------------------
# Imports of standard modules --
# -------------------------------
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
max-line-length = 110
ignore = E133, E226, E228, N802, N803, N806, W504
exclude = __init__.py
yacc.py
lex.py

[tool:pytest]
addopts = --flake8
Expand Down
57 changes: 28 additions & 29 deletions tests/test_exprParserLex.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# This file is part of daf_butler.
#
# LSST Data Management System
# Copyright 2018 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -15,18 +16,16 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""Simple unit test for expr_parser/parserLex module.
"""

import re
import unittest

from lsst.pipe.supertask.expr_parser import parserLex
from lsst.daf.butler.expr_parser import ParserLex, ParserLexError
import lsst.utils.tests


Expand Down Expand Up @@ -54,15 +53,15 @@ def testInstantiate(self):
"""

default_reflags = re.IGNORECASE | re.VERBOSE
lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()
self.assertEqual(lexer.lexreflags, default_reflags)

lexer = parserLex.ParserLex.make_lexer(reflags=re.DOTALL)
lexer = ParserLex.make_lexer(reflags=re.DOTALL)
self.assertEqual(lexer.lexreflags, re.DOTALL | default_reflags)

def testSimpleTokens(self):
"""Test for simple tokens"""
lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()

lexer.input("=!= <<= >>= +-*/()")
self._assertToken(lexer.token(), 'EQ', '=')
Expand All @@ -81,7 +80,7 @@ def testSimpleTokens(self):

def testReservedTokens(self):
"""Test for reserved words"""
lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()

# tokens = "IS NOT IN NULL OR XOR AND BETWEEN LIKE ESCAPE REGEXP"
tokens = "NOT IN OR XOR AND"
Expand All @@ -105,7 +104,7 @@ def testReservedTokens(self):

def testStringLiteral(self):
"""Test for string literals"""
lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()

lexer.input("''")
self._assertToken(lexer.token(), "STRING_LITERAL", "")
Expand All @@ -123,16 +122,16 @@ def testStringLiteral(self):

# odd newline inside string
lexer.input("'string\nstring'")
with self.assertRaises(parserLex.ParserLexError):
with self.assertRaises(ParserLexError):
lexer.token()

lexer.input("'string")
with self.assertRaises(parserLex.ParserLexError):
with self.assertRaises(ParserLexError):
lexer.token()

def testNumericLiteral(self):
"""Test for numeric literals"""
lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()

lexer.input("0 100 999. 100.1 1e10 1e-10 1.e+20 .2E5")
self._assertToken(lexer.token(), "NUMERIC_LITERAL", "0")
Expand All @@ -147,7 +146,7 @@ def testNumericLiteral(self):

def testIdentifier(self):
"""Test for numeric literals"""
lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()

lexer.input("ID id _012 a_b_C")
self._assertToken(lexer.token(), "IDENTIFIER", "ID")
Expand All @@ -162,22 +161,22 @@ def testIdentifier(self):
self.assertIsNone(lexer.token())

lexer.input(".id")
with self.assertRaises(parserLex.ParserLexError):
with self.assertRaises(ParserLexError):
lexer.token()

lexer.input("id.")
self._assertToken(lexer.token(), "IDENTIFIER", "id")
with self.assertRaises(parserLex.ParserLexError):
with self.assertRaises(ParserLexError):
lexer.token()

lexer.input("id.id.id")
self._assertToken(lexer.token(), "IDENTIFIER", "id.id")
with self.assertRaises(parserLex.ParserLexError):
with self.assertRaises(ParserLexError):
lexer.token()

def testExpression(self):
"""Test for more or less complete expression"""
lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()

expr = ("((instrument='HSC' AND detector != 9) OR instrument='CFHT') "
"AND tract=8766 AND patch.cell_x > 5 AND "
Expand Down Expand Up @@ -228,29 +227,29 @@ def _assertExc(exc, expr, remain, pos, lineno):
self.assertEqual(exc.pos, pos)
self.assertEqual(exc.lineno, lineno)

lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()
expr = "a.b.c"
lexer.input(expr)
self._assertToken(lexer.token(), "IDENTIFIER", "a.b")
with self.assertRaises(parserLex.ParserLexError) as catcher:
with self.assertRaises(ParserLexError) as catcher:
lexer.token()
_assertExc(catcher.exception, expr, ".c", 3, 1)

lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()
expr = "a \n& b"
lexer.input(expr)
self._assertToken(lexer.token(), "IDENTIFIER", "a")
with self.assertRaises(parserLex.ParserLexError) as catcher:
with self.assertRaises(ParserLexError) as catcher:
lexer.token()
_assertExc(catcher.exception, expr, "& b", 3, 2)

lexer = parserLex.ParserLex.make_lexer()
lexer = ParserLex.make_lexer()
expr = "a\n=\n1e5.e2"
lexer.input(expr)
self._assertToken(lexer.token(), "IDENTIFIER", "a")
self._assertToken(lexer.token(), "EQ", "=")
self._assertToken(lexer.token(), "NUMERIC_LITERAL", "1e5")
with self.assertRaises(parserLex.ParserLexError) as catcher:
with self.assertRaises(ParserLexError) as catcher:
lexer.token()
_assertExc(catcher.exception, expr, ".e2", 7, 3)

Expand Down
54 changes: 26 additions & 28 deletions tests/test_exprParserYacc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# This file is part of daf_butler.
#
# LSST Data Management System
# Copyright 2018 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -15,18 +16,15 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""Simple unit test for expr_parser/parserLex module.
"""Simple unit test for expr_parser/parserYacc module.
"""

import unittest

from lsst.pipe.supertask.expr_parser import exprTree
from lsst.pipe.supertask.expr_parser import parserYacc
from lsst.daf.butler.expr_parser import exprTree, ParserYacc, ParseError
import lsst.utils.tests


Expand All @@ -43,12 +41,12 @@ def tearDown(self):
def testInstantiate(self):
"""Tests for making ParserLex instances
"""
parser = parserYacc.ParserYacc() # noqa: F841
parser = ParserYacc() # noqa: F841

def testEmpty(self):
"""Tests for empty expression
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

# empty expression is allowed, returns None
tree = parser.parse("")
Expand All @@ -57,7 +55,7 @@ def testEmpty(self):
def testParseLiteral(self):
"""Tests for literals (strings/numbers)
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse('1')
self.assertIsInstance(tree, exprTree.NumericLiteral)
Expand All @@ -74,7 +72,7 @@ def testParseLiteral(self):
def testParseIdentifiers(self):
"""Tests for identifiers
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse('a')
self.assertIsInstance(tree, exprTree.Identifier)
Expand All @@ -87,7 +85,7 @@ def testParseIdentifiers(self):
def testParseParens(self):
"""Tests for identifiers
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse('(a)')
self.assertIsInstance(tree, exprTree.Parens)
Expand All @@ -97,7 +95,7 @@ def testParseParens(self):
def testUnaryOps(self):
"""Tests for unary plus and minus
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse('+a')
self.assertIsInstance(tree, exprTree.UnaryOp)
Expand All @@ -114,7 +112,7 @@ def testUnaryOps(self):
def testBinaryOps(self):
"""Tests for binary operators
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse('a + b')
self.assertIsInstance(tree, exprTree.BinaryOp)
Expand Down Expand Up @@ -159,7 +157,7 @@ def testBinaryOps(self):
def testIsIn(self):
"""Tests for IN
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse("a in (1,2,'X')")
self.assertIsInstance(tree, exprTree.IsIn)
Expand Down Expand Up @@ -188,7 +186,7 @@ def testIsIn(self):
def testCompareOps(self):
"""Tests for comparison operators
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

for op in ('=', '!=', '<', '<=', '>', '>='):
tree = parser.parse('a {} 10'.format(op))
Expand All @@ -202,7 +200,7 @@ def testCompareOps(self):
def testBoolOps(self):
"""Tests for boolean operators
"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

for op in ('OR', 'XOR', 'AND'):
tree = parser.parse('a {} b'.format(op))
Expand All @@ -221,7 +219,7 @@ def testBoolOps(self):

def testExpression(self):
"""Test for more or less complete expression"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

expression = ("((instrument='HSC' AND detector != 9) OR instrument='CFHT') "
"AND tract=8766 AND patch.cell_x > 5 AND "
Expand Down Expand Up @@ -251,21 +249,21 @@ def _assertExc(exc, expr, token, pos, lineno, posInLine):
self.assertEqual(exc.lineno, lineno)
self.assertEqual(exc.posInLine, posInLine)

parser = parserYacc.ParserYacc()
parser = ParserYacc()

expression = "(1, 2, 3)"
with self.assertRaises(parserYacc.ParseError) as catcher:
with self.assertRaises(ParseError) as catcher:
parser.parse(expression)
_assertExc(catcher.exception, expression, ",", 2, 1, 2)

expression = "\n(1\n,\n 2, 3)"
with self.assertRaises(parserYacc.ParseError) as catcher:
with self.assertRaises(ParseError) as catcher:
parser.parse(expression)
_assertExc(catcher.exception, expression, ",", 4, 3, 0)

def testStr(self):
"""Test for formatting"""
parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse("(a+b)")
self.assertEqual(str(tree), '(a + b)')
Expand All @@ -285,7 +283,7 @@ def testVisit(self):
def _visitor(node, nodes):
nodes.append(node)

parser = parserYacc.ParserYacc()
parser = ParserYacc()

tree = parser.parse("(a+b)")

Expand Down

0 comments on commit f917f3a

Please sign in to comment.