Exemple #1
0
 def terminal(self, text):
   """terminal = '"' . (printable - '"') + . '"'
               | "'" . (printable - "'") + . "'" ;"""
   self._attempting(text)
   return alternation([
     concatenation([
       '"',
       one_or_more(
         exclusion(
           self.printable,
           '"'
         ),
         ignore_whitespace=False
       ),
       '"',
     ], ignore_whitespace=False),
     concatenation([
       "'",
       one_or_more(
         exclusion(
           self.printable,
           "'"
         ),
         ignore_whitespace=False
       ),
       "'",
     ], ignore_whitespace=False),
   ])(text).compressed(TokenType.terminal)
Exemple #2
0
 def variable(self, text):
   """variable = identifier - reserved_words ;"""
   self._attempting(text)
   return exclusion(
     self.identifier,
     self.reserved_words
   )(text).compressed(TokenType.variable)
Exemple #3
0
 def number(self, text):
   """number = ["-"] . ("0" | digit - "0" . {digit}) . ["." . digit +] ;"""
   self._attempting(text)
   return concatenation([
     option(
       "-"
     ),
     alternation([
       "0",
       concatenation([
         exclusion(
           self.digit,
           "0"
         ),
         zero_or_more(
           self.digit,
           ignore_whitespace=False
         ),
       ], ignore_whitespace=False),
     ]),
     option(
       concatenation([
         ".",
         one_or_more(
           self.digit,
           ignore_whitespace=False
         ),
       ], ignore_whitespace=False)
     ),
   ], ignore_whitespace=False)(text).compressed(TokenType.number)
Exemple #4
0
  def test_exclusion(self):
    c0 = P.alternation(["a", "b"])
    c1 = P.exclusion(c0, "b")

    n0 = c1("a")
    self.assertEqual(n0.node_type, P.ParseNodeType.terminal)
    self.assertEqual(n0.value, "a")

    self.assertEqual(c0("b").value, "b")

    with self.assertRaises(P.DeadEnd):
      c1("ba")
Exemple #5
0
 def eol(self, text):
   """eol = { whitespace - "\n" } . "\n" ;"""
   self._attempting(text)
   return concatenation([
     zero_or_more(
       exclusion(
         self.whitespace,
         "\n"
       ),
       ignore_whitespace=False
     ),
     "\n",
   ], ignore_whitespace=False)(text)
Exemple #6
0
 def single_string_char(self, text):
   """single_string_char = "\\" . all_characters - "'"
                         | "\" . "'"
                         | all_characters - "'" ;"""
   self._attempting(text)
   return alternation([
     concatenation([
       "\\\\",
       exclusion(
         self.all_characters,
         "'"
       ),
     ], ignore_whitespace=False),
     concatenation([
       "\\",
       "'",
     ], ignore_whitespace=False),
     exclusion(
       self.all_characters,
       "'"
     ),
   ])(text).retyped(TokenType.single_string_char)
Exemple #7
0
 def double_string_char(self, text):
   """double_string_char = "\\" . all_characters - '"'
                         | "\" . '"'
                         | all_characters - '"' ;"""
   self._attempting(text)
   return alternation([
     concatenation([
       "\\\\",
       exclusion(
         self.all_characters,
         '"'
       ),
     ], ignore_whitespace=False),
     concatenation([
       "\\",
       '"',
     ], ignore_whitespace=False),
     exclusion(
       self.all_characters,
       '"'
     ),
   ])(text).retyped(TokenType.double_string_char)
Exemple #8
0
 def number(self, text):
   """number = digit - "0" . {digit} ;"""
   self._attempting(text)
   return concatenation([
     exclusion(
       self.digit,
       "0"
     ),
     zero_or_more(
       self.digit,
       ignore_whitespace=False
     ),
   ], ignore_whitespace=False)(text).compressed(TokenType.number)
Exemple #9
0
 def comment(self, text):
   """comment = "/*" . {all_characters - "*" | "*" . all_characters - "/"} . "*/" ;"""
   self._attempting(text)
   return concatenation([
     "/*",
     zero_or_more(
       alternation([
         exclusion(
           self.all_characters,
           "*"
         ),
         concatenation([
           "*",
           exclusion(
             self.all_characters,
             "/"
           ),
         ], ignore_whitespace=False),
       ]),
       ignore_whitespace=False
     ),
     "*/",
   ], ignore_whitespace=False)(text).compressed(TokenType.comment)
Exemple #10
0
 def directive(self, text):
   """directive = "#" . { all_characters - "\n" } . eol ;"""
   self._attempting(text)
   return concatenation([
     "#",
     zero_or_more(
       exclusion(
         self.all_characters,
         "\n"
       ),
       ignore_whitespace=False
     ),
     self.eol,
   ], ignore_whitespace=False)(text).compressed(TokenType.directive)
Exemple #11
0
 def complex_identifier(self, text):
   """complex_identifier = "[" . (all_characters - "]") + . "]" ;"""
   self._attempting(text)
   return concatenation([
     "[",
     one_or_more(
       exclusion(
         self.all_characters,
         "]"
       ),
       ignore_whitespace=False
     ),
     "]",
   ], ignore_whitespace=False)(text).retyped(TokenType.complex_identifier)
Exemple #12
0
 def comment(self, text):
   """comment = "(*" . {printable - "*" | "*" . printable - ")"} . "*)" ;"""
   self._attempting(text)
   return concatenation([
     "(*",
     zero_or_more(
       alternation([
         exclusion(
           self.printable,
           "*"
         ),
         concatenation([
           "*",
           exclusion(
             self.printable,
             ")"
           ),
         ], ignore_whitespace=False),
       ]),
       ignore_whitespace=False
     ),
     "*)",
   ], ignore_whitespace=False)(text).compressed(TokenType.comment)