Пример #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_doubleQuote(self):
     self.assertEqual(
         Lexer('letter = "A" ;').scanAll(), [
             Token("letter", Token.RULE),
             Token('=', Token.SYMBOL),
             Token('A', Token.LITERAL),
             Token(';', Token.SYMBOL)
         ])
Пример #5
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)
         ])
Пример #6
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)
         ])
Пример #7
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))
Пример #8
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))
Пример #9
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)
         ])
Пример #10
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)
         ])
Пример #11
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)
         ])
Пример #12
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)
         ])
Пример #13
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)
         ])
Пример #14
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)
         ])
Пример #15
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))
Пример #16
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))