예제 #1
0
    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))
예제 #2
0
    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))
예제 #3
0
    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))
예제 #4
0
 def test_id(self):
     self.assertEqual(
         Lexer("A = B ;").scanAll(), [
             Token('A', Token.RULE),
             Token('=', Token.SYMBOL),
             Token('B', Token.RULE),
             Token(';', Token.SYMBOL)
         ])
예제 #5
0
 def test_singleQuote(self):
     self.assertEqual(
         Lexer("letter = 'A';").scanAll(), [
             Token("letter", Token.RULE),
             Token('=', Token.SYMBOL),
             Token('A', Token.LITERAL),
             Token(';', Token.SYMBOL)
         ])
예제 #6
0
    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))
예제 #7
0
 def test_doubleQuote(self):
     self.assertEqual(
         Lexer('letter = "A" ;').scanAll(), [
             Token("letter", Token.RULE),
             Token('=', Token.SYMBOL),
             Token('A', Token.LITERAL),
             Token(';', Token.SYMBOL)
         ])
예제 #8
0
 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)
         ])
예제 #9
0
 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)
         ])
예제 #10
0
 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)
         ])
예제 #11
0
 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)
         ])
예제 #12
0
    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))
예제 #13
0
 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)
         ])
예제 #14
0
 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)
         ])
예제 #15
0
    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))
예제 #16
0
 def test_invalidRule1(self):
     with self.assertRaises(SyntaxException):
         Lexer('A = "C').scanAll()
예제 #17
0
    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))
예제 #18
0
 def test_invalidRule2(self):
     with self.assertRaises(SyntaxException):
         Lexer("A = 'C").scanAll()