def test_squareBrackets(self): ast = Parser(Lexer('A = [ B ] ;')).grammar() self.assertEqual(len(ast), 1) self.assertEqual(ast[0].rule, Token('A', Token.RULE)) self.assertEqual(ast[0].stmt.__class__.__name__, "Optional") self.assertEqual(ast[0].stmt.token, Token('B', Token.RULE))
def test_curlyBrackets(self): ast = Parser(Lexer('A = { B } ;')).grammar() self.assertEqual(len(ast), 1) self.assertEqual(ast[0].rule, Token('A', Token.RULE)) self.assertEqual(ast[0].stmt.__class__.__name__, "Repetition") self.assertEqual(ast[0].stmt.token, Token('B', Token.RULE))
def test_brackets(self): ast = Parser(Lexer('A = ( B ) ;')).grammar() self.assertEqual(len(ast), 1) self.assertEqual(ast[0].rule, Token('A', Token.RULE)) self.assertEqual(ast[0].stmt.__class__.__name__, "Grouping") self.assertEqual(ast[0].stmt.token, Token('B', Token.RULE))
def test_id(self): self.assertEqual( Lexer("A = B ;").scanAll(), [ Token('A', Token.RULE), Token('=', Token.SYMBOL), Token('B', Token.RULE), Token(';', Token.SYMBOL) ])
def test_singleQuote(self): self.assertEqual( Lexer("letter = 'A';").scanAll(), [ Token("letter", Token.RULE), Token('=', Token.SYMBOL), Token('A', Token.LITERAL), Token(';', Token.SYMBOL) ])
def test_idAndId(self): ast = Parser(Lexer('A = B , C ;')).grammar() self.assertEqual(len(ast), 1) self.assertEqual(ast[0].rule, Token('A', Token.RULE)) self.assertEqual(ast[0].stmt.__class__.__name__, "Concatenation") self.assertEqual(ast[0].stmt.lhs, Token('B', Token.RULE)) self.assertEqual(ast[0].stmt.rhs, Token('C', Token.RULE))
def test_doubleQuote(self): self.assertEqual( Lexer('letter = "A" ;').scanAll(), [ Token("letter", Token.RULE), Token('=', Token.SYMBOL), Token('A', Token.LITERAL), Token(';', Token.SYMBOL) ])
def test_backslash(self): self.assertEqual( Lexer("A = '\\'' | \"\\\"\" ;").scanAll(), [ Token('A', Token.RULE), Token('=', Token.SYMBOL), Token("'", Token.LITERAL), Token('|', Token.SYMBOL), Token('"', Token.LITERAL), Token(';', Token.SYMBOL) ])
def test_curlyBrackets(self): self.assertEqual( Lexer("A = { B } ;").scanAll(), [ Token('A', Token.RULE), Token('=', Token.SYMBOL), Token('{', Token.SYMBOL), Token('B', Token.RULE), Token('}', Token.SYMBOL), Token(';', Token.SYMBOL) ])
def test_squareBrackets(self): self.assertEqual( Lexer("A = [ B ] ;").scanAll(), [ Token('A', Token.RULE), Token('=', Token.SYMBOL), Token('[', Token.SYMBOL), Token('B', Token.RULE), Token(']', Token.SYMBOL), Token(';', Token.SYMBOL) ])
def test_brackets(self): self.assertEqual( Lexer("A = ( B ) ;").scanAll(), [ Token('A', Token.RULE), Token('=', Token.SYMBOL), Token('(', Token.SYMBOL), Token('B', Token.RULE), Token(')', Token.SYMBOL), Token(';', Token.SYMBOL) ])
def test_multiline(self): ast = Parser(Lexer('A = ( B );\nB = "C" ;')).grammar() self.assertEqual(len(ast), 2) self.assertEqual(ast[0].rule, Token('A', Token.RULE)) self.assertEqual(ast[0].stmt.__class__.__name__, "Grouping") self.assertEqual(ast[0].stmt.token, Token('B', Token.RULE)) self.assertEqual(ast[1].rule, Token('B', Token.RULE)) self.assertEqual(ast[1].stmt, Token('C', Token.LITERAL))
def test_whitespace(self): self.assertEqual( Lexer("A = ' ' | '\\t' | '\\r' | '\\n' ;").scanAll(), [ Token('A', Token.RULE), Token('=', Token.SYMBOL), Token(' ', Token.LITERAL), Token('|', Token.SYMBOL), Token('\t', Token.LITERAL), Token('|', Token.SYMBOL), Token('\r', Token.LITERAL), Token('|', Token.SYMBOL), Token('\n', Token.LITERAL), Token(';', Token.SYMBOL) ])
def test_multiline(self): self.assertEqual( Lexer('A = ( B );\nB = "C" ;').scanAll(), [ Token('A', Token.RULE), Token('=', Token.SYMBOL), Token('(', Token.SYMBOL), Token('B', Token.RULE), Token(')', Token.SYMBOL), Token(';', Token.SYMBOL), Token('B', Token.RULE), Token('=', Token.SYMBOL), Token('C', Token.LITERAL), Token(';', Token.SYMBOL) ])
def test_id(self): ast = Parser(Lexer('A = B ;')).grammar() self.assertEqual(len(ast), 1) self.assertEqual(ast[0].rule, Token('A', Token.RULE)) self.assertEqual(ast[0].stmt, Token('B', Token.RULE))
def test_invalidRule1(self): with self.assertRaises(SyntaxException): Lexer('A = "C').scanAll()
def test_Parse(self): ast = Parser(Lexer('letter = "A" ;')).grammar() self.assertEqual(len(ast), 1) self.assertEqual(ast[0].rule, Token('letter', Token.RULE)) self.assertEqual(ast[0].stmt, Token('A', Token.LITERAL))
def test_invalidRule2(self): with self.assertRaises(SyntaxException): Lexer("A = 'C").scanAll()