Пример #1
0
 def setUp(self):
     
     self._parser = Parser(TestGrammar())
     self._parser.enableBlockComments()
     self._parser.enableFullBacktracking()
     
     Position.setTabSize(4)
Пример #2
0
class ParserTest(unittest.TestCase):

    def setUp(self):
        
        self._parser = Parser(TestGrammar())
        self._parser.enableBlockComments()
        self._parser.enableFullBacktracking()
        
        Position.setTabSize(4)
        
    def tearDown(self):
        
        self._parser = None
        
    def testPositionInfo(self):
        
        filePath = os.path.abspath(os.path.dirname(__file__)) + os.sep + "testcode"
        
        root = self._parser.parseFile(filePath, TreeCatg.PARSE_TREE)
        
        print(root.toXml())
        
        children = root.getChildren()
        for1 = children[0]
        for2 = children[1]
        
        self._checkNode(for1.getChildren()[0], 1, 1, 1, 8)
        self._checkNode(for1.getChildren()[1], 1, 9, 1, 14)
        self._checkNode(for2.getChildren()[1], 6, 13, 6, 18)
        
    def testTokenInfo(self):
        
        filePath = os.path.abspath(os.path.dirname(__file__)) + os.sep + "testcode"
        
        tokenInfo = self._parser.getTokenInfoFromFile(filePath)
        for tokenType, token in tokenInfo:
            startLine, startCol = token.getStartPosition()
            endLine, endCol = token.getEndPosition()
            text = token.getText()
            if not isinstance(tokenType, Keyword):
                print("(%d,%d) - (%d,%d): '%s'" % (startLine, startCol, endLine, endCol, text))
            else:
                print("(%d,%d) - (%d,%d): '%s' (keyword)" % (startLine, startCol, endLine, endCol, text))
        
    def _checkNode(self, node, expStartLine, expStartCol, expEndLine, expEndCol):

        line, col = node.getToken().getStartPosition()
        self.assertEqual(line, expStartLine)
        self.assertEqual(col, expStartCol)
        
        line, col = node.getToken().getEndPosition()
        self.assertEqual(line, expEndLine)
        self.assertEqual(col, expEndCol)